Write a C, C++ program to find sum of n natural numbers using recursion.
In this program, we take positive input number from user and our program will display sum of numbers up to that number.
For example -
If user enter 8 , then our program will calculate sum upto that number.
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
In my last post, i explained how to calculate of sum of n natural numbers using iterative approach. If you are not familiar with recursive and iterative approach then read this post which explain recursion vs iteration.
Programming questions on strings.
Sorting algorithms and their time complexity.
Output -
Enter any positive number : 8
Sum of 8 natural numbers is : 36
Explanation of Program
Let's suppose user has entered number 8. Our program will execute like this.
8 + sum (7)
8 + 7 + sum (6)
8 + 7 + 6 + sum (5)
8 + 7 + 6 + 5 + sum (4)
8 + 7 + 6 + 5 + 4 + sum (3)
8 + 7 + 6 + 5 + 4 + 3 + sum (2)
8 + 7 + 6 + 5 + 4 + 3 + 2 + sum (1)
8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum (0) // After this statement program will terminate
In this program, we take positive input number from user and our program will display sum of numbers up to that number.
For example -
If user enter 8 , then our program will calculate sum upto that number.
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
In my last post, i explained how to calculate of sum of n natural numbers using iterative approach. If you are not familiar with recursive and iterative approach then read this post which explain recursion vs iteration.
Programming questions on strings.
Sorting algorithms and their time complexity.
Find Sum of N Natural Numbers using Recursion in C++
#include <iostream> using namespace std; int sum (int n) { /* If n is less than equal to zero then return 0 */ if ( n <= 0) { return 0; } else { /* Recursive call */ return n + sum (n-1); } } int main() { int n; cout << "Enter any positive number\n"; cin >> n; /* sum method is called */ cout << "Sum of " << n << " natural numbers is " << sum (n);
return 0; }
Find Sum of N Natural Numbers using Recursion in C
#include <stdio.h> int sum (int n) { /* If n is less than equal to zero then return 0 */ if ( n <= 0) { return 0; } else { /* Recursive call */ return n + sum (n-1); } } int main(void) { int n, num; printf ("Enter any positive number \n"); scanf ("%d", &n); /* Function call */ num = sum (n); printf ("Sum of %d natural numbers is %d ", n, num); return 0; }
Output -
Enter any positive number : 8
Sum of 8 natural numbers is : 36
Explanation of Program
Let's suppose user has entered number 8. Our program will execute like this.
8 + sum (7)
8 + 7 + sum (6)
8 + 7 + 6 + sum (5)
8 + 7 + 6 + 5 + sum (4)
8 + 7 + 6 + 5 + 4 + sum (3)
8 + 7 + 6 + 5 + 4 + 3 + sum (2)
8 + 7 + 6 + 5 + 4 + 3 + 2 + sum (1)
8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum (0) // After this statement program will terminate
No comments:
Post a Comment