C Program to Find the Sum of First N Natural Numbers

This C Program calculates the sum of first N natural numbers.

Here is source code of the C program to calculate the sum of first N natural numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to find the sum of 'N' natural numbers
  3.  */
  4. #include <stdio.h>
  5.  
  6. void main()
  7. {
  8.     int i, num, sum = 0;
  9.  
  10.     printf("Enter an integer number \n");
  11.     scanf ("%d", &num);
  12.     for (i = 1; i <= num; i++)
  13.     {
  14.         sum = sum + i;
  15.     }
  16.     printf ("Sum of first %d natural numbers = %d\n", num, sum);
  17. }


$ cc pgm3.c
$ a.out
Enter an integer number
1300
Sum of first 1300 natural numbers = 845650
 
$ a.out
Enter an integer number
15
Sum of first 15 natural numbers = 120

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.
Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







Subscribe Newsletter & Posts
Follow Manish & Sanfoundry