Users Online

· Guests Online: 146

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

25 C Program to find Sum of N Numbers using Recursion

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

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.96 seconds
10,816,464 unique visits