C program to calculate sum of N natural numbers .


Today we are learn how to find sum of ‘n’ natural number using c language. We know that natural number is 1,2,3,4,5………, n.

We take the input from the user (take n=) and then add the number up to that.

It’s obvious we are use loop to calculate all the number, so in this program we use “for” loop  



Our program work like:-

When we enter number suppose: 5 our sum is

1+2+3+4+5=15

If we enter: 10

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

Now we write a program to find addition of ‘n’ natural number  

SOURCE CODE:-


#include<stdio.h>
int main(){
    int n,i,sum=0;
    printf("enter the number n:");
    scanf("%d",&n);
     for(i=0;i<=n;i++){
         sum=sum+i;
         printf("%d  ",i);
     }
     printf("\nsum=%d",sum);
    return 0;
}

OUTPUT:-

enter the number n:12
0  1  2  3  4  5  6  7  8  9  10  11  12  
sum=78

 

0 Comments