This C program calculates the sum of the first N natural numbers using a for loop. It iterates from 1 to N and accumulates the sum by adding each number to the previous sum.
Problem Statement
Given a positive integer N, we need to find the sum of the first N natural numbers.
C Program to Find Sum of Digits of a Number using Recursion
#include <stdio.h>
int sumOfDigits(int number);
int main() {
    int number;
    printf("Enter a positive integer: ");
    scanf("%d", &number);
    int sum = sumOfDigits(number);
    printf("Sum of digits of %d = %d\n", number, sum);
    return 0;
}
int sumOfDigits(int number) {
    if (number == 0) {
        return 0;
    } else {
        return (number % 10) + sumOfDigits(number / 10);
    }
}
How it Works:
- The user is prompted to enter a positive integer N.
- The forloop is used to iterate from 1 to N. The loop variableiis initialized to 1, and the loop continues as long asiis less than or equal to N. After each iteration,iis incremented by 1.
- Inside the loop, the current value of iis added to thesumvariable. This accumulates the sum of the numbers from 1 to N.
- After the loop completes, the final sum is stored in the sumvariable.
- The main function displays the calculated sum on the screen.
Input/Output:

 
															 
								 
								