C Program to Print Sum of Natural Numbers.

In this C programming, we are going to calculate the sum of n natural numbers using different methods. Before moving to the code part, let's first understand natural numbers.


What are Natural Numbers?

Natural numbers are a set of positive whole numbers starting from 1 and extending indefinitely, including 1, 2, 3, 4, and so on. They do not include zero or any negative numbers. 

Example:

Input: n = 10
Output: 55

Explanation: 
1 + 2 + 3+ 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

Below are four different approaches we are going to use to find the sum of natural numbers:

Sum of Natural Numbers using while Loop.

In this approach we are using a while loop, we take input n from the user and calculate the sum of natural numbers from 1 to n.

C Code:
//C program to sum natural numbers using while loop
#include <stdio.h>

int main() {
    int n, sum = 0, i = 1;

    printf("Enter a positive integer: ");
    scanf("%d", &n);

    while (i <= n) {
        sum += i;
        i++;
    }

    printf("Sum of natural numbers from 1 to %d is %d.\n", n, sum);

    return 0;
}
Output:
Enter a positive integer: 8
Sum of natural numbers from 1 to 8 is 36.

Time Complexity: O(n) as the while loop run n times.
Space Complexity: O(1) no extra space is required.

Sum of Natural Numbers using for Loop.

In this approach we are using a for loop, we follow the same process but use a for loop to iterate from 1 to n.

C Code:
//C program to find sum of natural numbers using for loop
#include <stdio.h>

int main() {
    int n, sum = 0;

    printf("Enter a positive integer: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        sum += i;
    }

    printf("Sum of natural numbers from 1 to %d is %d.\n", n, sum);

    return 0;
}
Output:
Enter a positive integer: 11
Sum of natural numbers from 1 to 11 is 66.

Time Complexity: O(n) as the for loop runs n times.
Space Complexity: O(1) no extra space is required.

Sum of Natural Numbers using Recursion.

In this approach we are using recursion, we define a function sumRecursion that takes n as input. The function calculates the sum of natural numbers from 1 to n using recursion. The base case is when n becomes 0, and the function returns 0. Otherwise, it returns n + sumRecursion(n - 1).

C Code:
//C program to find the sum of natural numbers using Recursion
#include <stdio.h>

//recursive function
int sumFunction(int n) {
    int sum = 0;

    for (int i = 1; i <= n; i++) {
        sum += i;
    }
    return sum;
}

int main() {
    int n, sum;

    printf("Enter a positive integer: ");
    scanf("%d", &n);

    sum = sumFunction(n);

    printf("Sum of natural numbers from 1 to %d is %d.\n", n, sum);

    return 0;
}
Output:
Enter a positive integer: 12
Sum of natural numbers from 1 to 12 is 78.

Time Complexity: O(n) as the recursive function is called n+1 times.
Space Complexity: O(n) as we require extra stack space for a recursive function.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS