write a program to Print the sum of first n natural numbers.

// program to Print the sum of first n natural numbers.

#include<stdio.h>


int main()


{

int sum=0, n;

printf("enter any number\n");

scanf("%d", &n);

for(int i=1; i<=n; i++){

sum = sum+i; //it will work like 1+2+3+4+5+6=21

}

printf("sum of first n natural numbers = %d\n", sum);

return 0;

}

Output :

enter any number

6

sum of first n natural numbers = 21


--------------------------------

Process exited after 3.741 seconds with return value 0

Press any key to continue . . .

Comments

Popular posts from this blog

program to (keep taking number as input from user until user enters a number which is multiple of 5.)

write a program to print the numbers from 0 to n, if n is given by user.

Write a program to print the average of 3 numbers.