Users Online
· Guests Online: 36
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
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
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.