Logo 
Search:

C Programming Forum

Ask Question   UnAnswered
Home » Forum » C Programming       RSS Feeds

sum of series of natural numbers

  Asked By: Mrityunjoy    Date: Feb 02    Category: C Programming    Views: 2390
  

write a program in c to display as follows:

enter 1st number =1
enter last number=10

sum is 1+2+3+4+5+6+7+8+9+10 = 55.

note:
i did the program as follows:

#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
int c=0;
clrscr();
while(a<=10)
{
c=c+a;
a++;
}
printf("\n the sum= ",c);
getch();
}

output is:
sum=55.
but i actually want as
sum is 1+2+3+4+5+6+7+8+9+10 = 55.

can anybody help me?
thank you.

Share: 

 

2 Answers Found

 
Answer #1    Answered By: MD.REZA AHMED     Answered On: Feb 04

try this one ...


#include<stdio.h>
int main()
{
int i=1,n,c=0;
printf("\n Enter the no of natural numbers u want to add\n");
scanf("%d",&n);
printf("\n The Sum is ...");
while(i<=n)
{
c+=i;
if(i!=n)
printf("%2d+",i++);
else
printf("%2d",i++);
}
printf("=%2d",c);
return 0;
}

 
Didn't find what you were looking for? Find more on sum of series of natural numbers Or get search suggestion and latest updates.




Tagged: