Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

C program to sum of natural numbers from 1 to n|5 ways

Posted on October 19, 2019September 28, 2020

Table of Contents

  • C program to sum of natural numbers from1 to n  language|5 ways
    • Sum of natural numbers from1 to n-using for loop
    • Sum of natural numbers from1 to n-using while loop
    • Sum of natural numbers from1 to n-using do-while loop
    • Sum of natural numbers from1 to n-using function
    • Sum of natural numbers from1 to n-using recursive function

C program to sum of natural numbers from1 to n  language|5 ways

In this tutorial, we will discuss the concept of C program to the sum of Natural number from 1 to n – (n is entered number)

In this post, we are going to learn how to find the sum of natural numbers in C language in different 5 ways

C program to sum of natural numbers from 1 to n
Addition of Natural number

Sum of natural numbers from1 to n-using for loop

Program 1

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

int main()
{
    int sum=0,num,i;
    printf("Enter a natural number\n");
    scanf("%d",&num);
    for(i=1; i<=num; i++){
  sum+=i;   //sum=sum+i;
}
printf("Sum of natural numbers from 1 to %d: %d",num,sum);
getch();
    return 0;
}

When the above code is executed it produces the following output

Enter a natural number
10
Sum of natural numbers from 1 to 10: 55

This program allows the user to enter a maximum number. and it displays sum of natural numbers from 1 to given number using for loop in C language

Sum of natural numbers from1 to n-using while loop

Program 2

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

int main()
{
    int sum=0,num,i;
    printf("Enter a natural number\n");
    scanf("%d",&num);
    i=1;
    while(i<=num){
  sum+=i;   //sum=sum+i;
   i++;
}
printf("Sum of natural numbers 1 to %d: %d",num,sum);
getch();
    return 0;
}

When the above code is executed it produces the following output

Enter a natural number
12
Sum of natural numbers 1 to 12: 78

This program allows the user to enter a maximum number. and it displays sum of natural numbers from 1 to given number using while loop in C language

Sum of natural numbers from1 to n-using do-while loop

Program 3

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

int main()
{
    int sum=0,num,i;
    printf("Enter a natural number\n");
    scanf("%d",&num);
    i=1;
   do{
  sum+=i;   //sum=sum+i;
   i++;
} while(i<=num);
printf("Sum of natural numbers 1 to %d: %d",num,sum);
getch();
    return 0;
}

When the above code is executed it produces the following output

Enter a natural number
100
Sum of natural numbers 1 to 100: 5050

This program allows the user to enter a maximum number. and it displays sum of natural numbers from 1 to given number using the do-while loop in C language

Sum of natural numbers from1 to n-using function

Program 4

#include <stdio.h>
#include <stdlib.h>
sumNatyralnum(int);//function prototype
int main()
{
    int num,sum=0;
    printf("Enter a natural number\n");
    scanf("%d",&num);
    sum=sumNatyralnum(num);//function call
getch();
    printf("The sum of natural numbers 1 to %d : %d",num,sum);
    return 0;
}
int sumNatyralnum(int n){//function definition
   if(n==0)
   {
       return n;
   }
   else{
    return (n*(n+1)/2);
   }


}

When the above code is executed it produces the following output

Enter a natural number
20
The sum of natural numbers 1 to 20 : 210

This program allows the user to enter a maximum number. and it displays the addition of natural numbers from 1 to given number using the function in C language

Sum of natural numbers from1 to n-using recursive function

Program 5

#include <stdio.h>
#include <stdlib.h>
sumNaturalnum(int);
int main()
{
    int num,sum=0;
    printf("Enter a natural number\n");
    scanf("%d",&num);
    sum=sumNaturalnum(num);
    printf("The sum of natural numbers 1 to %d : %d",num,sum);
    getch();
    return 0;
}
int sumNaturalnum(int n){
   if(n==0)
   {
       return n;
   }
   else{
    return (n+sumNaturalnum(n-1));
   }


}

When the above code is executed it produces the following output

Enter a natural numnber
50
The sum of natural numbers 1 to 50 : 1275

This program allows the user to enter a maximum number. and it displays addition of numbers from 1 to given number using recursive function in C language

Suggested for you

for loop in C language

while loop in C language

do-while loop in C language

function in C language

 

Similar post

Java program to Sum of natural numbers 1 to n |5 ways

C++ program to sum of Natural number from 1 to n

Python program to Sum of natural numbers 1 to n

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes