Write a program to print the sum of first n numbers :-


//Print sum of first n numbers

#include<stdio.h>
void main()
{

int i, n, sum = 0;
    
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=1; i <= n; i++)

{
sum += i;   // sum = sum+i;
}

printf("Sum of first n numbers is = %d",sum);
return 0;
}

Output:


Enter a positive integer: 8
Sum of first n numbers is = 36

			

Previous Next