Algorithm and Flowchart for finding the Sum of Natural Number upto given Number

[490 views]


What is a Natural Number?

It is series of the number starting from 1 to infinity. It does not include 0 or negative numbers.

What should be the approach for this program?

As we have to count until the number given it definitely means we have to use loop until the number. So what can we do is we use a variable to store the previous number of the loop and keep adding the new number.

So we will use for The loop will run n times ,so we declare variable sum and store the value of sum and iteration of the loop. print that number.

Flowchart for finding the sum of Natural Number

Flowchart for finding the Sum of Natural Number upto given Number
Remove WaterMark from Above Flowchart

Pseudocode for finding the sum of Natural Number

Declare a variable n, i and sum as integer; Read number n ; for i upto n increment i by 1 and i=1 { sum=sum+i; } Print sum;

Here in this Algorithm we declare 3 variables n for storing the number, i for running the for loop and sum for storing the sum. Read the number n. We use for loop and increment the loop by 1 upto n. Then we add the numbers and store it in sum. Like if we take n as 4.

  • so in the first iteration i=1 and sum = 0 + 1 as sum is initialized 0 at the beginning.
  • In the second iteration i=2 and sum = 1 + 2 as sum was previously stored as 1 in the 1st iteration.
  • In the third iteration i=3 and sum = 3 + 3 as sum was previously stored as 3 in the 2nd iteration.
  • In the last iteration i=4 and sum = 6 + 4 as sum was previously stored as 6 in the 2nd iteration.
and the loop ends and we print the value of sum.

Implementation of the Program in C:

#include<stdio.h> int main() { int n, i, sum; printf("Enter the number to find its sum:"); scanf("%d", & n); for (i = 1; i <= n; i++) { sum = sum + i; } printf("Sum of natural number upto %d is %d: ", n, sum); }

Output of the program:

        

Algorithm Logic Test

Our Quiz prepared by Experts Helps you identify your knowledge in Algorithms. Everyone should atleast attempt this Quiz Once. 



Comments



Recommended Book Deals Ends in

Download 101 Ready to Use Excel Formulas ebook
Download Python All in One for Dummies ebook



Search
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags

    Algorithm to compute the Sum of Natural Number upto given Number

    Flowchart to compute the Sum of Natural Number upto a Number

    C Program to calculate sum of Natural Numbers upto n