Twitter Feed Facebook Google Plus Youtube

Adsense responsive

Recent Post below header

Thursday 26 March 2020

C PROGRAM: TO SUM FIRST N NUMBERS

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:

Image of Code: Sum of first N numbers


Output Image: Sum of first N numbers


For any query or suggestion please comment below...

No comments:

Post a Comment