25 C Program to find Sum of N Numbers using Recursion
Posted by Superadmin on December 22 2015 13:36:37
C Program to find Sum of N Numbers using Recursion
The following C program using recursion displays the first N natural number on the terminal. The user enters the Nth number as the input, the program then displays all integral number starting from 1 up to the N.
Here is the source code of the C program to display first N numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
 * C Program to find Sum of N Numbers using Recursion
 */
#include <stdio.h>
 
void display(int);
 
int main()
{
    int num, result;
 
    printf("Enter the Nth number: ");
    scanf("%d", &num);
    display(num);
    return 0;
}
 
void display(int num)
{
    static int i = 1;
 
    if (num == i)
    {
        printf("%d   \n", num);
        return;
    }
    else
    {
        printf("%d   ", i);
        i++;
        display(num);
    }
}
$ cc pgm33.c
$ a.out
Enter the Nth number: 10
1   2   3   4   5   6   7   8   9   10