When a question is give like this , it simply mean that it is a user define program. Here, N will be declare by user. To sum a series of a number there are two ways , this are:
- We can use loops(for/while) with a proper termination condition.
- We can use mathematical formula i.e. n(n+1)/2 .
In this article we are going to discuss about this two type of procedure.
Method 1:
#include<stdio.h>
int main()
{
int i,s=0,n,p;
printf("ENTER THE LIMIT: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
s=s+i;
}
printf("SUM OF %d NUMBERS IS: %d",i,s);
return 0;
}
Method 2:
#include<stdio.h>
int main()
{
int n,sum;
printf("ENTER LIMIT: ");
scanf("%d",&n);
sum=n*(n+1)/2;;
printf("SUM OF THE SERIES IS: %d",sum);
return 0;
}
Output:
And there you have done with your task!
Thank You.
Stay update with us.
You show 1 problem in 2 way. Really awesome.
Great. Carry on ma’am
LikeLike
carry on maam
LikeLike