C Program to Find the Sum of N Natural Numbers Using Recursion

In C language, recursion is a process in which a function calls itself repeatedly to solve a problem. A recursive function must have a base condition to stop further recursive calls. In this program, we will calculate the sum of the first N natural numbers using a recursive function.

सी लैंग्वेज में Recursion एक ऐसी प्रक्रिया है जिसमें कोई फंक्शन किसी समस्या को हल करने के लिए स्वयं को बार-बार कॉल करता है। Recursive function में एक Base Condition होना आवश्यक है, जो आगे होने वाली recursive calls को रोकती है। इस प्रोग्राम में हम Recursive Function का उपयोग करके प्रथम N प्राकृतिक संख्याओं का योग ज्ञात करेंगे।

Formula

The formula for calculating the sum of the first N natural numbers is:

प्रथम N प्राकृतिक संख्याओं का योग ज्ञात करने का सूत्र है:

Sum = 1+2+3+....+n or Sum = n*(n+1)/2

For example, if n = 5:

उदाहरण के लिए, यदि n = 5 है:

Sum = 1+2+3+4+5 = 15

C Program Using Recursion

#include <stdio.h>
int sum(int n);
int main(void)
{
int num, result;
printf("Enter a positive integer: ");
if (scanf("%d", &num) != 1)
{
printf("Invalid input!\n");
return 1;
}
if (num < 0)
{
printf("Please enter a non-negative integer.\n");
return 1;
}
result = sum(num);
printf("Sum = %d\n", result);
return 0;
}
int sum(int n)
{
if (n == 0)
return 0;
else
return n + sum(n - 1);
}

Output

Example 1:

Enter a positive integer: 5
Sum = 15

Example 2:

Enter a positive integer: 10
Sum = 55

Example 3:

Enter a positive integer: 1
Sum = 1

Explanation of the Program

  1. int sum(int n); is the function prototype that declares the recursive function.

    int sum(int n); फंक्शन प्रोटोटाइप है, जो Recursive Function को डिक्लेयर करता है।

  2. The user enters a positive integer, which is stored in the variable num.

    यूजर एक धनात्मक पूर्णांक इनपुट करता है, जिसे num वेरिएबल में स्टोर किया जाता है।

  3. The statement result = sum(num); calls the recursive function and stores the returned result.

    result = sum(num); स्टेटमेंट Recursive Function को कॉल करता है और प्राप्त परिणाम को result में स्टोर करता है।

  4. Inside the sum() function, if (n == 0) is the base condition. It returns 0 and stops further recursive calls.

    sum() फंक्शन में if (n == 0) Base Condition है। यह 0 रिटर्न करता है और आगे होने वाली Recursive Calls को रोकता है।

  5. The statement return n + sum(n - 1); adds the current value of n to the result returned by the next recursive call.

    return n + sum(n - 1); वर्तमान n की वैल्यू में अगली Recursive Call से प्राप्त परिणाम को जोड़ता है।

Working of Recursive Function

Suppose the user enters 5. The recursive function calls itself as follows:

मान लीजिए यूजर 5 इनपुट करता है। Recursive Function स्वयं को निम्न प्रकार कॉल करता है:

sum(5)
= 5 + sum(4)
= 5 + 4 + sum(3)
= 5 + 4 + 3 + sum(2)
= 5 + 4 + 3 + 2 + sum(1)
= 5 + 4 + 3 + 2 + 1 + sum(0)
= 5 + 4 + 3 + 2 + 1 + 0
= 15

Therefore, the final result is 15.

इसलिए अंतिम परिणाम 15 प्राप्त होता है।

Important Points

  • Recursion means a function calling itself.

  • A base condition is necessary to terminate recursion.

  • Each recursive call reduces the value of n by 1.

  • The function returns 0 when n becomes 0.

  • Recursion uses additional memory for function calls on the call stack.

  • Recursion का अर्थ है किसी फंक्शन का स्वयं को कॉल करना।

  • Recursion को समाप्त करने के लिए Base Condition आवश्यक है।

  • प्रत्येक Recursive Call में n की वैल्यू 1 कम होती जाती है।

  • जब n की वैल्यू 0 हो जाती है, तो फंक्शन 0 रिटर्न करता है।

  • Recursion में प्रत्येक Function Call के लिए Call Stack में अतिरिक्त मेमोरी का उपयोग होता है।

This program uses int, so very large input values may cause integer overflow. For very large values, a wider integer type and overflow checks are required.

इस प्रोग्राम में int डेटा टाइप का उपयोग किया गया है, इसलिए बहुत बड़ी इनपुट वैल्यू देने पर Integer Overflow हो सकता है। बड़ी वैल्यू के लिए अधिक क्षमता वाले Integer Data Type और Overflow Checks की आवश्यकता होती है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025