Find elements

C program to find the sum of natural numbers using recursion

C program to find the sum of natural numbers using recursion

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

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

Find the sum of natural numbers

What are Natural numbers

The positive integer numbers 1,2,3,4….n are known as natural numbers.

Program

This program allows the user to enter a positive integer to calculate sum up to given numbers using the recursive function in C programming language

#include <stdio.h>
#include <stdlib.h>
int addNumbers(int); //function Prototype

int main()
{
    int num;       //variable declaration
    printf("enter a positive integer");
    scanf("%d",&num);
    printf("Sum of two numbers are: %d\n",addNumbers(num));
                     //functiuon call
    getch();
    return 0;
}
int addNumbers(int n)       //recursive function definition
{
    if(n!= 0)
        return n+addNumbers(n-1);
    else
        return n;
}

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

Enter a positive integer: 10
Sum od numbers: 55

 

In the above program , the number entered by the user is passed to the addNumber() function as an argument.

In the above case, when the user enters 10 as an argument, 10 is passed to function. Subsequently, until “if” statement returns the true value, value decreases by one in every step. Finally, when the value becomes 0, “if” statement returns as false and the “else” part is executed.

Therefore, function return with decreasing value to calculate the result e.g 1+2+3+4+5+6+7+8+9+10=55

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

C program to calculate the sum of natural numbers using loops

 

Java program to find the sum of natural numbers using recursion

Python program to Calculate the sum of natural numbers using recursion

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

 

 

Suggested for you

C if-else statements

C function

C recursion

Use of Java program to find sum of two numbers using recursion
Find the product of two numbers in C using recursion
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…

21 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