Sum of n natural numbers
The concept of summing natural numbers is fundamental in mathematics. A natural number is any positive integer starting from 1. The sum of the first n natural numbers is a well-known formula and plays a vital role in various mathematical and computational applications. This article aims to explain how to calculate the sum of the first n natural numbers using C programming.
Problem Statement
Given a positive integer n, the task is to calculate the sum of the first n natural numbers. The formula for
calculating this sum is: sum = 1 + 2 + 3 + ... + n
.
Example
For instance, if n is 4, then the sum of the first 4 natural numbers would be 1 + 2 + 3 + 4 = 10
. If n
is 10, the sum would be 1 + 2 + 3 + ... + 10 = 55
.
Idea to Solve
The solution involves utilizing the formula for the sum of the first n natural numbers. The formula
sum = n * (n + 1) / 2
efficiently computes this sum. This formula is based on the arithmetic
progression of natural numbers.
Pseudocode
function sum_natural_no(n):
if n <= 0:
return
sum = n * (n + 1) / 2
Print "Sum of", n, "natural numbers is", sum
function main():
sum_natural_no(4)
sum_natural_no(10)
Algorithm Explanation
- The
sum_natural_no
function takes a single parametern
. - It first checks if
n
is less than or equal to 0. If it is, the function returns without performing any calculations. - Using the formula
sum = n * (n + 1) / 2
, it calculates the sum of the firstn
natural numbers. - The function then prints the calculated sum along with the provided value of
n
. - The
main
function tests thesum_natural_no
function with different values ofn
.
Code Solution
// C Program
// Sum of n natural numbers
#include <stdio.h>
//Calculate the sum of n natural numbers
void sum_natural_no(int n)
{
if (n <= 0)
{
return;
}
// Formula
// n*(n+1) / 2
long long int sum = n *(n + 1) / 2;
//Display calculated result
printf(" Sum of %d natural numbers is %lld\n", n, sum);
}
int main()
{
//Test case
sum_natural_no(4);
sum_natural_no(10);
return 0;
}
Output
Sum of 4 natural numbers is 10
Sum of 10 natural numbers is 55
// Java Program
// Sum of n natural numbers
class MyMaths
{
//Calculate the sum of n natural numbers
public void sum_natural_no(int n)
{
if (n <= 0)
{
return;
}
// Formula
// n*(n+1) / 2
long sum = n * (n + 1) / 2;
//Display calculated result
System.out.print(" Sum of " + n + " natural numbers is " + sum + "\n");
}
public static void main(String[] args)
{
MyMaths obj = new MyMaths();
//Test case
obj.sum_natural_no(4);
obj.sum_natural_no(10);
}
}
Output
Sum of 4 natural numbers is 10
Sum of 10 natural numbers is 55
//Include header file
#include <iostream>
using namespace std;
// C++ Program
// Sum of n natural numbers
class MyMaths
{
public:
//Calculate the sum of n natural numbers
void sum_natural_no(int n)
{
if (n <= 0)
{
return;
}
// Formula
// n*(n+1) / 2
long sum = n * (n + 1) / 2;
//Display calculated result
cout << " Sum of " << n << " natural numbers is " << sum << "\n";
}
};
int main()
{
MyMaths obj = MyMaths();
//Test case
obj.sum_natural_no(4);
obj.sum_natural_no(10);
return 0;
}
Output
Sum of 4 natural numbers is 10
Sum of 10 natural numbers is 55
//Include namespace system
using System;
// C# Program
// Sum of n natural numbers
class MyMaths
{
//Calculate the sum of n natural numbers
public void sum_natural_no(int n)
{
if (n <= 0)
{
return;
}
// Formula
// n*(n+1) / 2
long sum = n * (n + 1) / 2;
//Display calculated result
Console.Write(" Sum of " + n + " natural numbers is " + sum + "\n");
}
public static void Main(String[] args)
{
MyMaths obj = new MyMaths();
//Test case
obj.sum_natural_no(4);
obj.sum_natural_no(10);
}
}
Output
Sum of 4 natural numbers is 10
Sum of 10 natural numbers is 55
<?php
// Php Program
// Sum of n natural numbers
class MyMaths
{
//Calculate the sum of n natural numbers
public function sum_natural_no($n)
{
if ($n <= 0)
{
return;
}
// Formula
// n*(n+1) / 2
$sum = intval($n * ($n + 1) / 2);
//Display calculated result
echo " Sum of ". $n ." natural numbers is ". $sum ."\n";
}
}
function main()
{
$obj = new MyMaths();
//Test case
$obj->sum_natural_no(4);
$obj->sum_natural_no(10);
}
main();
Output
Sum of 4 natural numbers is 10
Sum of 10 natural numbers is 55
// Node Js Program
// Sum of n natural numbers
class MyMaths
{
//Calculate the sum of n natural numbers
sum_natural_no(n)
{
if (n <= 0)
{
return;
}
// Formula
// n*(n+1) / 2
var sum = parseInt(n * (n + 1) / 2);
//Display calculated result
process.stdout.write(" Sum of " + n + " natural numbers is " + sum + "\n");
}
}
function main()
{
var obj = new MyMaths();
//Test case
obj.sum_natural_no(4);
obj.sum_natural_no(10);
}
main();
Output
Sum of 4 natural numbers is 10
Sum of 10 natural numbers is 55
# Python 3 Program
# Sum of n natural numbers
class MyMaths :
# Calculate the sum of n natural numbers
def sum_natural_no(self, n) :
if (n <= 0) :
return
# Formula
# n*(n+1) / 2
sum = int(n * (n + 1) / 2)
# Display calculated result
print(" Sum of ", n ," natural numbers is ", sum )
def main() :
obj = MyMaths()
# Test case
obj.sum_natural_no(4)
obj.sum_natural_no(10)
if __name__ == "__main__": main()
Output
Sum of 4 natural numbers is 10
Sum of 10 natural numbers is 55
# Ruby Program
# Sum of n natural numbers
class MyMaths
# Calculate the sum of n natural numbers
def sum_natural_no(n)
if (n <= 0)
return
end
# Formula
# n*(n+1) / 2
sum = n * (n + 1) / 2
# Display calculated result
print(" Sum of ", n ," natural numbers is ", sum ,"\n")
end
end
def main()
obj = MyMaths.new()
# Test case
obj.sum_natural_no(4)
obj.sum_natural_no(10)
end
main()
Output
Sum of 4 natural numbers is 10
Sum of 10 natural numbers is 55
// Scala Program
// Sum of n natural numbers
class MyMaths
{
//Calculate the sum of n natural numbers
def sum_natural_no(n: Int): Unit = {
if (n <= 0)
{
return;
}
// Formula
// n*(n+1) / 2
var sum: Long = (n * (n + 1) / 2).toInt;
//Display calculated result
print(" Sum of " + n + " natural numbers is " + sum + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyMaths = new MyMaths();
//Test case
obj.sum_natural_no(4);
obj.sum_natural_no(10);
}
}
Output
Sum of 4 natural numbers is 10
Sum of 10 natural numbers is 55
// Swift Program
// Sum of n natural numbers
class MyMaths
{
//Calculate the sum of n natural numbers
func sum_natural_no(_ n: Int)
{
if (n <= 0)
{
return;
}
// Formula
// n*(n+1) / 2
let sum: Int = n * (n + 1) / 2;
//Display calculated result
print(" Sum of ", n ," natural numbers is ", sum );
}
}
func main()
{
let obj: MyMaths = MyMaths();
//Test case
obj.sum_natural_no(4);
obj.sum_natural_no(10);
}
main();
Output
Sum of 4 natural numbers is 10
Sum of 10 natural numbers is 55
Time Complexity
The time complexity of this code is constant, O(1), since the calculations performed inside the
sum_natural_no
function involve only basic arithmetic operations. The execution time remains constant
regardless of the input value of n
.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment