Sum of n Natural numbers in C program

Spread the love
Sum-of-n-Natural-numbers-in-C-program-using-loops

Program Description:

Write a Program to calculate the Sum of n Natural numbers in C language. We are going to use the loops ( while, for, and do while loops), and also we are going to see how to calculate using the sum of n natural numbers formula.

The program should accept a positive number and provide the sum of the first n natural numbers as the Output. The program should also do the error checks and display an error message if the user enters Invalid Input. Here is an example Input and output of the program

Expected Input and Output:

Input:

Enter a Positive Number: 80

Output:

Sum of Natural Numbers upto 80 is : 3240

Pre-Requisites:

To understand the following programs it is good to know the basics of the C Loops. Please go through the following articles to learn more about the C Loops.

We are going to write the program using the all loops

  1. Sum of n Natural Numbers using the while loop
  2. Sum of first n Natural Numbers using for loop
  3. Sum of n Natural numbers using the do while loop
  4. Sum of N natural numbers using the formula
  5. Sum of N natural numbers prompt the user again on Invalid Input using goto statement.

Sum of n Natural Numbers in C Program Algorithm using Loops:

  1. Take the Input from the user and we are going to store it in num variable.
  2. Check if the num is Positive, If not display the Error Message ERROR: Please enter valid number and stop the program
  3. If the given number is Positive, Then we are going to calculate the sum of all numbers from 1 to num.
  4. To calculate the sum, We are going to use the loop. The loop will iterate from 1 to num . We have a loop control variable i , which starts from and another variable sumthat holds the sum of numbers so far. The initial value of the sum is zero.
    • At each iteration, We are going to add the i to sum
    • and we Increment the value of i by 1.
    • This loop continues until the i <= num
  5. Once the loop is terminated, The variable sum holds the Sum of first n natural numbers, Then display the output onto the console.

Sum of n Natural Numbers in C using the while loop:

Program Output:

Few more examples:

If the user enters Invalid Input

sum-of-n-natural-numbers-in-c-using-while-loop-program

As you can see the program is giving the sum of the first n numbers. Also handling the invalid input cases.

Sum of n Natural Numbers in C using the for loop:

The program logic remains same but in this example, we are going to use the for loop, instead of while loop.

Program Outut:

program-to-calculate-sum-of-first-n-natural-numbers-using-for-loop

Sum of n Natural Numbers in C using the do while loop:

This is the exercise program. Please try the program yourself. The program logic is same as above two loop programs. You have to re-write it using the do while loop.

Sum of n Natural Numbers in C program using the Formula:

The mathematical formula of sum of n natural numbers is (n * ( n + 1) ) / 2

So we can directly use the above formula to calculate the sum of n numbers.

Here is the C program which uses above formula to calculate the sum of first n natural numbers.

In the above program, We have used the (n(n+1))/2 to calculate the sum. i.e sum = (num * (num+1)) / 2;

📢 We do not need to use any loops to calculate the sum of n natural numbers using the maths formula.
The time complexity of formula based program less i.e Time complexity is O(1), Compared to the loops version, which has the time complexity of O(n)

Program Output:

c-program-to-calculate-sum-of-n-natural-numbers-using-formula

As you can see the program and output, We can see our program is giving excepted results.

Prompt until the user enters the valid number using do while loop:

As of now, we are displaying the error message if user enters Invalid Input. We can also just prompt the user again to enter the input again using the do while loop. We are look at this variation of the above sum of natural numbers program.

Program Output:

As you can see from above example, The program presented the prompt again, if user enters the invalid input, When user entered -100 as input, we displayed the Enter a Positive Number: again.

If user enters valid Input, The program will proceed further and calculates the sum of n numbers.

Sum of N numbers using goto statement on Invalid Input:

We can modify above program to ask for the Input again, if the user enters Invalid Input.

Program Output:

sum-of-first-n-natural-numbers-invalid-input-case

As you can see from above output, When user enters Invalid input (i.e -80). The program displayed the Error message and then asked for the user input again. Then user provided the value 20.

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] C Program to Calculate Sum of First N natural numbers […]

  2. […] Sum of First N Natural Numbers in C […]

Leave a Reply