C program to sum first n numbers. It takes n as input and output sum of first n numbers.
Here I have used Code::Blocks IDE.
CODE:
#include<stdio.h>
void main() {
int n, i, sum=0;
printf("Enter n: ");
scanf("%d", &n);
for (i=1; i <= n; ++i) {
sum += i; // equivalent to sum = sum + i;
}
printf("\n\nSum of first %d numbers: %d", n, sum);
}
OUTPUT:
Enter n: 20
Sum of first 20 numbers: 210
Screenshots:
Here I have used Code::Blocks IDE.
CODE:
#include<stdio.h>
void main() {
int n, i, sum=0;
printf("Enter n: ");
scanf("%d", &n);
for (i=1; i <= n; ++i) {
sum += i; // equivalent to sum = sum + i;
}
printf("\n\nSum of first %d numbers: %d", n, sum);
}
OUTPUT:
Enter n: 20
Sum of first 20 numbers: 210
Screenshots:
For any query or suggestion please comment below...