Program to generate series of odd numbers and find sum

 

Flowchart

Generate series of odd numbers and their sum flowchart

 

Coding in C

#include<stdio.h>
int main()
{
int num=2,n,sum=0;
printf(“Enter n: “);
scanf(“%d”,&n);
while(num<=n)
{
printf(“%d\n”,num);
sum=sum+num;
num=num+2;
}
printf(“Sum of series = %d”,sum);
return 0;
}

 

Coding in C++

#include<iostream>
using namespace std;
int main()
{
int num=2,n,sum=0;
cout<<“Enter n: “;
cin>>n;
while(num<=n)
{
cout<<num<<“\n”;
sum=sum+num;
num=num+2;
}
cout<<“Sum of series = “<<sum;
return 0;
}

 

Output

Generate series of odd numbers and their sum output

 

Published by

admin

I am a computer teacher, programmer and web developer