C Program to Find the Sum of First N Natural Numbers

Advertisements
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.
Advertisements
Subscribe Newsletter & Posts

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.

Practice Interview Questions & Answers, Quizzes, Objective, Multiple Choice Aptitude Tests for Freshers and Experienced People in these topics - Learn C, C++, Java and C# Programming with coding example on Simple problems as well as tough Algorithms and Data-structures along with runtime output: - C Programs, Data-Structures and Algorithms, C++ Algorithms, Java Algorithms, C# Programs, Android Programs in Java

C Tutorials and Linux Commands Tutorial - C Tutorials, Linux Commands

Career Mentoring with our Founder / CTO - Read More
Advertisements

Follow Manish & Sanfoundry