Natural numbers have a fascinating history. The ancient Egyptian and Babylonian used Natural numbers for the purpose of counting, which means these numbers have been around ever since the stone age!
Times have changed, and we can now generate the sum of the first ‘n’ natural numbers using a C program. Let us explore how to write a C program to generate the sum of the first ‘n’ natural numbers. We will look at two different algorithms of the same program.
But first, let’s revisit natural numbers and revise their properties.
What are Natural Numbers?
Natural numbers are used for counting. It is the set of all positive numbers beginning with 1 and extending up to infinity. The set of natural numbers is denoted by the symbol, N. While the smallest element of the set is 1, each subsequent element is 1 more than the previous element.
For example, 2 = 1+1;
3 = 2+1;
4 = 3+1; and so on.
How to Calculate the Sum of the First n Natural Numbers?
There are various methods to calculate the sum of First n Natural numbers. The basic approach would be to start at number 1 and iteratively add the numbers until you reach the number n.
For example, let us say n = 6. Therefore, the sum of the first 6 numbers would be 1+2+3+4+5+6=21.
Alternatively, the Gauss formula may be used to find the sum of the first n natural numbers.
There’s an amazing story behind this formula.
Did you know that Carl Friedrich Gauss discovered this formula in elementary school?
So, the teacher assigned this task where students had to calculate the sum of all numbers from 1 to 100 , assuming that it would take quite a while for them to solve the question. But young Gauss discovered the answer in quite a few seconds of thought!
How did young Gauss find the answer so quickly?
Have a look at this formula-
So, Gauss observed a pattern and added the first and the last number in pairs. Now that was extraordinary for a kid in elementary school!
This was the mathematical way to calculate the sum of first ‘n’ natural numbers.
Now, let’s get to the interesting part.
Yes! The Gauss Formula can be applied to calculate the sum of First ‘n’ numbers through the C programming.
Let’s find out!
C Program to Print the Sum of the First n Natural Numbers-
Are you familiar with certain terms and tags from the above coding? Let’s break them down:
● #include is a directive to the preprocessor to include the contents of the stdio.h file into the source code before the compilation of the program.
● stdio.h is the standard input-output header file that contains functions to read from input devices like the keyboard and print to output devices like the screen.
● main() function is the point from which the C program begins execution. Every C program has a mandatory main() function.
● int is a variable declaration statement. Integer variables are defined using int.
● printf() and scanf() are input-output functions defined in the standard input output header file, stdio.h. While scanf() reads keyboard input, printf() presents output to the screen.
● for loop is a looping construct used to repeat tasks iteratively.
● return 0 from the main function is a way to indicate the operating system that the program ran successfully.
So. these are the two different versions of the C program that are used to find the ‘sum of n
Natural numbers’.
Now, let us use the C program to find the sum of the first 100 natural numbers.
Have a look :
C Program to Print the Sum of the First 100 Natural Numbers
The output displayed on the screen will be as below:
Well, wasn’t that fun? I hope you had a great time finding the sum of natural numbers with C programming. I hope this was a good read.
If you have any queries regarding this topic, you can comment down below. Thank you for spending your time here.