Brijmohan lal Sahu - Facebook
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Showing posts with label series. Show all posts
Showing posts with label series. Show all posts

Sunday, December 7, 2014

Program to calculate sum of odd-even number series using while loop in C.


even-odd series sum


Code Box
--------------------------------------------

/*program to calculate sum of the series
1-2+3-4+5-6+7-8+9.......+n
odd-even number series(alternative)
where n is the limit entered by user.
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,evensum,oddsum,sum,num;

clrscr();

evensum=0;
oddsum=0;

printf("\nEnter limit : ");
scanf("%d",&num);

printf("\nOdd Number Series ");

printf("\nSeries= ");

i=1;                     //initialization

while(i<=num)
{

if(i%2==0)          // % modulus operator
{
printf(" - ");
evensum=evensum+i;
}
else
{
printf(" + ");
oddsum=oddsum+i;
}
printf("%d",i);

i++;                 //increment post fix operator

}

sum=oddsum-evensum;

printf("\n\nSum of series= %d",sum);

getch();
}


Program to calculate sum of n numbers using while loop in C.



Sum of series using while loop


Code Box
---------------------------------------------

/*program to calculate sum of the series 
1+2+3+4+.......+n
where n is the limit entered by user
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum,num;

clrscr();

sum=0;

printf("\nEnter limit : ");
scanf("%d",&num);

printf("\nSeries= ");

i=1; //initialization

while(i<=num)
{
printf(" + %d ",i);
sum=sum+i;
i++;             //increment 
}
printf("\n\nSum of series= %d",sum);

getch();
}