Sum of n Natural numbers in C program
- Program Description:
- Pre-Requisites:
- Sum of n Natural Numbers in C Program Algorithm using Loops:
- Sum of n Natural Numbers in C using the while loop:
- Sum of n Natural Numbers in C using the for loop:
- Sum of n Natural Numbers in C using the do while loop:
- Sum of n Natural Numbers in C program using the Formula:
- Prompt until the user enters the valid number using do while loop:
- Sum of N numbers using goto statement on Invalid Input:
- Related Programs:
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.
- While Loop in C Language
- For Loop in C Language with Example Programs
- do while loop in Language with example programs
We are going to write the program using the all loops
- Sum of n Natural Numbers using the while loop
- Sum of first n Natural Numbers using for loop
- Sum of n Natural numbers using the do while loop
- Sum of N natural numbers using the formula
- 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:
- Take the Input from the user and we are going to store it in num variable.
- Check if the num is Positive, If not display the Error Message – ERROR: Please enter valid number and stop the program
- If the given number is Positive, Then we are going to calculate the sum of all numbers from 1 to num.
- 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
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
/*     Program: Sum of Natural Numbers upto given number using while loop */ #include<stdio.h> int main() {      // Take user input     int num;     printf("Enter a Positive Number: ");     scanf("%d", &num);      // sum of natural numbers     // starts with Zero     int sum = 0;      if(num > 0) {         // loop counter         int i = 1;          while(i <= num) {             sum += i;  // sum = sum + i;             i++;         }     }     else     {         // Negative Number, Display error message and return.         printf("ERROR: Please enter valid number\n");         return 0;     }      printf("Sum of Natural Numbers upto %d is : %d \n", num, sum);      return 0; } |
Program Output:
1 2 3 4 5 |
$ gcc sum-of-natural-numbers.c $ ./a.out Enter a Positive Number: 5 Sum of Natural Numbers upto 5 is : 15 $ |
Few more examples:
1 2 3 4 5 6 7 8 9 10 |
$ ./a.out Enter a Positive Number: 10 Sum of Natural Numbers upto 10 is : 55 $ ./a.out Enter a Positive Number: 1000 Sum of Natural Numbers upto 1000 is : 500500 $ ./a.out Enter a Positive Number: 75 Sum of Natural Numbers upto 75 is : 2850 $ |
If the user enters Invalid Input
1 2 3 4 |
$ ./a.out Enter a Positive Number: -7 ERROR: Please enter valid number $ |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
/*     Program: Sum of Natural Numbers upto given number using for loop */ #include<stdio.h> int main() {      // Take user input     int num;     printf("Enter a Positive Number: ");     scanf("%d", &num);      // sum of natural numbers     // starts with Zero     int sum = 0;      if(num > 0) {         // loop counter         int i = 1;          for(; i<=num; i++) {             sum += i;  // sum = sum + i;         }     }     else     {         // Negative Number, Display error message and return.         printf("ERROR: Please enter valid number\n");         return 0;     }      printf("Sum of Natural Numbers upto %d is : %d \n", num, sum);      return 0; } |
Program Outut:
1 2 3 4 5 6 7 8 9 10 11 |
$ gcc sum-of-natural-numbers.c    $ ./a.out Enter a Positive Number: 8 Sum of Natural Numbers upto 8 is : 36 $ ./a.out Enter a Positive Number: 50 Sum of Natural Numbers upto 50 is : 1275 $ ./a.out Enter a Positive Number: 100 Sum of Natural Numbers upto 100 is : 5050 $ |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/*     Program: Sum of Natural Numbers upto given number using Sum of numbers formula */ #include<stdio.h> int main() {      // Take user input     int num;     printf("Enter a Positive Number: ");     scanf("%d", &num);      // sum of natural numbers     // starts with Zero     int sum = 0;      if(num > 0) {         // using formula         // sum = (n(n+1))/2;         sum = (num * (num+1)) / 2;     }     else     {         // Negative Number, Display error message and return.         printf("ERROR: Please enter valid number\n");         return 0;     }      printf("Sum of Natural Numbers upto %d is : %d \n", num, sum);      return 0; } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$ gcc sum-of-natural-numbers.c    $ ./a.out Enter a Positive Number: 8 Sum of Natural Numbers upto 8 is : 36 $ ./a.out Enter a Positive Number: 50 Sum of Natural Numbers upto 50 is : 1275 $ ./a.out Enter a Positive Number: 100 Sum of Natural Numbers upto 100 is : 5050 $ ./a.out Enter a Positive Number: 10 Sum of Natural Numbers upto 10 is : 55 $ |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/*     Program: Sum of Natural Numbers upto given number using Sum of numbers formula */ #include<stdio.h> int main() {      int num;     // sum of natural numbers     // starts with Zero     int sum = 0;      do {         // Take User input, Prompt until user enter valid number.         printf("Enter a Positive Number: ");         scanf("%d", &num);     } while (num <= 0);      // using formula     // sum = (n(n+1))/2;     sum = (num * (num+1)) / 2;      printf("Sum of Natural Numbers upto %d is : %d \n", num, sum);      return 0; } |
Program Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ gcc sum-of-natural-numbers.c    $ ./a.out Enter a Positive Number: -100 Enter a Positive Number: 10 Sum of Natural Numbers upto 10 is : 55 $ ./a.out Enter a Positive Number: 80 Sum of Natural Numbers upto 80 is : 3240 $ ./a.out Enter a Positive Number: -4 Enter a Positive Number: 7 Sum of Natural Numbers upto 7 is : 28 $ |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/*     Program: Sum of Natural Numbers upto given number using Sum of numbers formula */ #include<stdio.h> int main() {      INPUT:  // goto statement label, uses in case of wrong input.     // Take user input     int num;     printf("Enter a Positive Number: ");     scanf("%d", &num);      // sum of natural numbers     // starts with Zero     int sum = 0;      if(num > 0) {         // using formula         // sum = (n(n+1))/2;         sum = (num * (num+1)) / 2;     }     else     {         // Negative Number, Use 'goto' to take back to start of the program.         printf("ERROR: Please enter valid number, Try Again\n");         goto INPUT;     }      printf("Sum of Natural Numbers upto %d is : %d \n", num, sum);      return 0; } |
Program Output:
1 2 3 4 5 6 7 8 9 10 |
$ gcc sum-of-natural-numbers.c    $ ./a.out Enter a Positive Number: 9 Sum of Natural Numbers upto 9 is : 45 $ ./a.out Enter a Positive Number: -80 ERROR: Please enter valid number, Try Again Enter a Positive Number: 20 Sum of Natural Numbers upto 20 is : 210 $ |
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.
3 Responses
[…] C Program to Calculate Sum of First N natural numbers […]
[…] Sum of First N Natural Numbers in C […]
[…] C Program to Calculate Sum of First N natural numbers […]