Object

Write a program to print the sum of all odd numbers form 1 to 50.

Algorithm

  1. Declare integer variables sum and i.
  2. Set i = 1 and sum = 0.
  3. DO
    • sum = sum + i.
    • i = i + 2.
    • WHILE i<=50.
    • [End of DO-WHILE loop.]
  4. Write sum.
  5. Exit.

Flowchart

C++ Source Code

// program 63
#include<iostream>
using namespace std;

int main()
{
  int i = 1;
  int sum = 0;

  do
  {
    sum = sum + i;
    i = i + 2;
  } 
  while (i <= 50);
  cout << "The sum of all odd numbers from 1 to 50 is: " << sum;

  return 0;
}

C Source Code

/*program 63*/
#include<stdio.h>

int main()
{
  int i, sum = 0;	
  i = 1;
  do
   {
   sum = sum + i;
   i = i + 2;
   }
  while(i<=50);
  printf("The sum of all odd numbers from 1 to 50 is: %d", sum);
  return 0;
}

Output

The sum of all odd numbers from 1 to 50 is: 625

Design a site like this with WordPress.com
Get started