Mathematical Formula
Below is the illustration for calculating the total of n positive integers utilizing the mathematical expression:
Sum of n natural number = n * (n + 1) / 2
Where n defines the natural number .
If we are interested in finding the total of the initial 20 natural numbers, we must incorporate a mathematical expression to determine the sum:
Sum = 20 * (20 + 1) / 2 = 20 * 10.50 = 210
Or
20 * (20 + 1) /2 = 10 * 21 = 210
Pseudo code
- int i, sum = 0, num
- input positive number
- i = 0
- sum = sum + i
- i = i + 1
- iterate the value of i < = num
- display the sum of the first natural number.
Using for Loop
Let's develop a C program that calculates the total of n natural numbers using a for loop.
SumOfNaturalNumber1.c
#include <stdio.h>
#include <conio.h>
void main()
{
int num, i, sum = 0; // declare local variables
printf(" Enter a positive number: ");
scanf("%d", &num); // take any positive number
// executes until the condition remains true.
for (i = 0; i <= num; i++)
{
sum = sum + i; // at each iteration the value of i is added to the sum variable
}
// display the sum of natural number
printf("\n Sum of the first %d number is: %d", num, sum);
getch();
}
Output:
Enter a positive number: 25
Sum of the first 25 number is: 325
Using while Loop
Let's develop a C program that calculates the total of n natural numbers by employing a while loop.
SumOfNaturalNumber2.c
#include <stdio.h>
#include <conio.h>
void main()
{
int num, i, sum = 0; // initialize and declare the local variables
printf("Enter a positive number : ");
scanf("%d", &num); // take a value up to which find the sum of n natural number
i = 0;
while (i <= num) // define the while loop and i should be less than num
{
sum = sum + i; // store the sum of natural number
i++; // increment by 1
}
// print the sum of natural number
printf(" \n Sum of first %d natural number is : %d", num, sum);
getch();
}
Output:
Enter a positive number: 20
Sum of the first 20 natural number is: 210
When a positive integer such as 20 is provided in the previous instance, the while loop iterates persistently through the counter ranging from i = 0 to 20. During each cycle, the value of i gets summed up with the variable sum, and i gets incremented by 1. Upon the while condition turning untrue, the loop terminates and displays the total sum of the initial 20 natural numbers.
Using do-while Loop
Let's explore the given scenario to compute the total of positive integers using a Do-While loop.
SumOfNaturalNumber3.c
#include <stdio.h>
#include <conio.h>
void main()
{
int num, i, sum = 0; // initialize and declare the local variables
printf("Enter a positive number: ");
scanf("%d", &num); // take a value up to which find the sum of natural number
i = 0;
do
{
sum = sum + i; // store the sum of natural number
i++; // increment by 1
} while (i <= num); // define the while loop and i should be less than num
// print the sum of natural number
printf(" \n Sum of first %d natural number is : %d", num, sum);
getch();
}
Output:
Enter a positive number: 30
Sum of the first 30 natural number is: 465
In the scenario described, if a positive integer such as 30 is input, the do loop will repeatedly run through the counter starting from i = 0 up to 30. During each iteration, the value of i will be summed with the variable sum, and i will be incremented by 1. Once the while condition evaluates to false, the loop will terminate, displaying the sum of the initial 30 natural numbers.
Using the Mathematical Formula
Let's develop a program to display the total of n natural numbers by leveraging the mathematical expression.
SumOfNaturalNumber4.c
#include<stdio.h>
int main()
{
int n = 40; // declare & initialize local variable n.
int sum = (n * (n + 1) ) / 2; /* define the mathematical formula to calculate the sum of given number. */
printf("Sum of %d natural number is = %d", n, sum); // print the sum of natural number
return 0;
}
Output:
Sum of 40 natural number is = 840
Using Function
Let's explore the given scenario to determine the total of positive integers utilizing a function in the C programming language.
SumOfNaturalNumber5.c
#include <stdio.h>
#include <conio.h>
void main()
{
int num, total; // local variable
printf("Enter a natural number: ");
scanf("%d", &num); // take a natural number from the user
total = natural_no(num); // call the function
printf(" Sum of the %d natural number is: %d", num, total);
}
int natural_no(num)
{
int i, sum = 0;
// use for loop until the condition becomes false
for (i = 0; i <= num; i++)
{
// adding the counter variable i to the sum value
sum = sum + i;
}
return sum;
}
Output:
Enter a natural number: 100
Sum of the 100 natural number is: 5050
Sum of n natural numbers Between a Given Range
Calculate the total sum of natural numbers starting from any initial number up to a specified final number.
SumOfNaturalNumber6.c
#include <stdio.h>
#include <conio.h>
void main()
{
int num, i, sum = 0; // define the local variables
printf("Enter the first number: ");
scanf("%d", &i); // accept the starting number
printf(" Up to the last natural number: ");
scanf("%d", &num); // accept the last number
// As long as the loop condition is true, it continuously iterates the statement while(i <= num)
{
// adding the counter variable i to the sum variable
sum = sum + i;
i++; // increment by 1
}
printf("Sum of natural number is = %d", sum);
getch();
}
Output:
Enter the first number: 1
Up to the last number natural number: 25
Sum of natural number is = 325
Using Recursion
Let's examine the subsequent instance to compute the total of positive integers through recursion.
SumOfNaturalNumber7.c
#include <stdio.h>
#include <conio.h>
int sum_natural_no(int num); // declare function outside the main function
int
main()
{
int num, sum = 0; // declare local variable
printf("Enter any positive number to calculate the sum of natural no. ");
scanf("%d", &num); // take an input from the user
sum = sum_natural_no(num); // call the function
printf("Sum of the first %d natural number is: %d", num, sum); // print the sum of natural number
return 0;
}
int sum_natural_no(int num)
{
if( num == 0) // define if condition
{
return num;
}
else
{ // return the else condition
return( num + sum_natural_no( num - 1));
}
}
Output:
Enter any positive number to calculate the sum of natural no. 50
Sum of the first 50 natural number is: 1275
Using an Array
SumOfNaturalNumber8.c
#include <stdio.h>
int main()
{
// declare & initialize local variable
int num, sum = 0, i, array[50];
printf(" Enter a positive number as we want to sum the natural number: ");
scanf("%d", &num); // take a positive number
printf("\n Enter the number one by one: \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]); // read value one by one
sum = sum + array[i]; // store number into the sum variable
}
printf("Sum of the given number is = %d\n", sum);
return 0;
}
Output:
Enter a positive number as we want to sum the natural number: 5
Enter the number one by one:
2
4
5
6
7
Sum of the given number is = 24