🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Recursion is a powerful concept in programming where a function calls itself to solve a problem. One of the classic problems solved using recursion is finding the sum of the first 'n' natural numbers. This post will guide you through writing a C program to achieve this using recursion.
2. Program Overview
The C program will:
1. Prompt the user to input a number 'n'.
2. Calculate the sum of the first 'n' natural numbers using recursion.
3. Display the result.
3. Code Program
#include <stdio.h>
// Recursive function to find the sum of the first n natural numbers
int sumOfNaturalNumbers(int n) {
if(n == 0)
return 0; // Base condition
else
return n + sumOfNaturalNumbers(n - 1); // Recursive call
}
int main() {
int num;
// Taking input from the user
printf("Enter a positive integer: ");
scanf("%d", &num);
// Ensuring that the number entered is non-negative
if(num < 0) {
printf("Please enter a non-negative integer.");
return 1;
}
// Displaying the result
printf("Sum of the first %d natural numbers is: %d", num, sumOfNaturalNumbers(num));
return 0;
}
Output:
Enter a positive integer: 5 Sum of the first 5 natural numbers is: 15
4. Step By Step Explanation
1. Recursive Function:
- The function sumOfNaturalNumbers is defined to calculate the sum recursively.
- It takes an integer 'n' as its argument.
- If n is 0, it returns 0 (base case).
- Otherwise, it returns the sum of 'n' and the sum of the first 'n-1' natural numbers. This is achieved by the function calling itself.
2. User Input:
- The user is prompted to input a positive integer.
- A check is performed to ensure the number entered is non-negative.
3. Displaying Result:
- The result, i.e., the sum of the first 'n' natural numbers, is displayed using the printf function.
By using recursion, we have efficiently solved the problem in a logical and concise manner. This example demonstrates the elegance and power of recursive techniques in programming.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment