Calculations

C program to calculate the sum of natural numbers

C program to find the sum of natural numbers in  using loops

In this tutorial, we will discuss a concept of C program to find the sum of natural numbers using loops

In this article, we are going to learn  how to  find the sum of natural numbers using loops in the C programming language

C program to calculate the sum

C code to find the sum of natural numbers using loop

C code to find the sum of numbers using for loop

Program 1

This program takes input from the user and stores in variable num. Then, the for loop is used to calculate the sum of natural numbers up to the given numbers.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,i,sum=0;

    printf("Enter a positive integer: ");
    scanf("%d",&num);

    for(i=1; i<=num; i++){
        sum+=i;    //sum=sum+i

    }
    printf("Sum of numbers are: %d",sum);
    getch();
    return 0;
}

When the above code is executed, it produces the following results

Enter a positive integer: 100
Sum of numbers are: 5050

 

C code to find the sum of  numbers using while loop

Program 2

This program takes input from the user and stores in variable num. Then the while loop is used to calculate the sum of natural numbers upto the given number.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,i,sum=0;

    printf("Enter a positive integer: ");
    scanf("%d",&num);

    i=1;
    while(i<=num){
        sum+=i;    //sum=sum+i
 i++;
    }
    printf("Sum of numbers are: %d",sum);
    getch();
    return 0;
}

 

When the above code is executed, it produces the following results

Enter a positive integer: 100
Sum of numbers are: 5050

 

C code to find the sum of  numbers using do-while loop

Program 3

This program takes input from the user and stores in variable num. Then the do-while loop is used to calculate the sum upto the given number.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,i,sum=0;//variable declaration
    printf("Enter a positive integer: ");
    scanf("%d",&num);  //get input from user
    i=1;

do{

    sum+=i;  //sum=sum+i
     i++;
}
    while(i<=num);
          

printf("Sum of numbers are: %d",sum);

    getch();
    return 0;
}

When the above code is executed, it produces the following results

Enter a positive integer: 100
Sum of numbers are: 5050

 

Method:

  1. The following variables i,num and sum=0 are declared.
  2. A positive integer is received from the user.
  3. Then, the loop is used to add a consecutive number from 1 up to the given number.
  4. Finally, the sum is received as output and displayed.

 

Similar post

Java program to find the sum of natural numbers using loops

Python program to Calculate the sum of natural numbers using loops

C++ program to calculate the sum of natural numbers using loops

 

Suggested for you

for loop in C language

while loop in C language

do-while loop in C language

 

Use of C++ program to find sum of two numbers using recursion
Java program to calculate the sum of natural numbers
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

20 hours ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago