Thursday, April 2, 2020

C program to Sum of all natural number using for loop

C program to Sum of all Natural Number Using for loop 

The positive numbers are 1,2,3.............n are known as natural numbers. The sum of natural numbers are upto 10.
#include 
int main() 
{
    int n, i, sum = 0; // integer value declaration

    printf("Enter a positive integer: "); // enter user value
    scanf("%d", &n);

    for (i = 1; i <= n; ++i) // loop check natural number's
    {
        sum = sum + i; // 
    }

    printf("Sum = %d", sum); // print natural numbers
    return 0;
}

Output

Ente positive integer = 100
sum = 5050

  • The above program takes input program user and then store the variable in n .  Then, forloop is used to calculate the sum of all natural number's.

0 comments:

Post a Comment

Recent Posts