ADVERTISEMENT
ADVERTISEMENT

C program to calculate sum of first N natural numbers.

This program will read the value of N and calculate sum from 1 to N, first N natural numbers means numbers from 1 to N and N will be read through user.

Sum of first N natural numbers using C program

/*C program to calculate sum of first N natural numbers.*/

#include <stdio.h>

int main()
{
    int n, i;
    int sum;

    printf("Enter the value of N: ");
    scanf("%d", &n);

    sum = 0;

    for (i = 1; i <= n; i++)
        sum += i;

    printf("Sum is: %d\n", sum);

    return 0;
}

Output

RUN 1:
Enter the value of N: 100
Sum is: 5050

RUN 2:
Enter the value of N: 10
Sum is: 55

RUn 3:
Enter the value of N: 3
Sum is: 6

C Looping Programs »


ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.