# Write a c program to print all natural numbers between 1 to n using functions.

Using recursive function.I haven't got the desired output.

+1 vote
answered May 6, 2021 by (55,660 points)
selected May 18, 2021 by Anitha

Could you please share your code so one can have a look?

The base idea is really simple (see pseudo code below): you keep calling the same function from itself as long as you haven't reached the maximum (n) every time increasing the current element (where we started from).

```function printNaturalNumber(start, end)
if start < end
print(start)
if (start <= end)
printNaturalNumber(start + 1, end)```

and this function would be called like

`printNaturalNumber(1, n)`
answered May 5, 2021 by (110 points)
#include<iostream>

using namespace std;

int main()

{

int  x;

cout << " Enter Value to print Natural Numbers =  "<<endl;

cin >> x;

cout << " Natural Numbers from 1 to " << x << " are\n";

for(int i = 1; i <= x; i++)

{

cout << i <<" ";

}

return 0;

}
–1 vote
answered May 5, 2021 by (120 points)
#include <stdio.h>

void imprime (int i, int last)
{
if (i<=last)
{
if (i==last)
printf("%d ",i);
else
printf("%d, ",i);

imprime (++i,last);
}
}

int main()
{
printf("Hello World\n");
imprime (1,9);

return 0;
}
answered May 6, 2021 by (140 points)
#include<stdio.h>
void num();
int main()
{
num();
return 0;
}
void num()
{
int i=1;
int n;
printf("enter the number\n");
scanf("%d",&n);
do{
printf("%d\n",i);
i++;
}
while(i<=n);
}
–1 vote
answered May 6, 2021 by (160 points)
void printNumber(int n)

{

if(n>0)

{

printNumber(n-1);

printf("%d ",n);

}

}
answered May 19, 2021 by (1,270 points)
#include <stdio.h>
void prime(int n)
{
static int i =1;
if(i<=n)
{
printf("%d ",i++);
prime(n);
}

}
int main()
{
int n;
printf("enter n number");
scanf("%d",&n);
prime(n);

return 0;
}
commented May 19, 2021 by (55,660 points)
This doesn't work.

First of all, I'm not sure why you call your function "prime" when it has nothing to do with prime numbers.

Second, you declare i to be static, that is to outlive the scope of the function prime(). Next time prime() is called, i is not set to 1, instead i keeps its value from the last run.

It's easy to see that this time not all the numbers will be printed between 1 and n.
answered May 31, 2021 by (270 points)
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i;
printf(" Enter any number");
scanf(" %d",&n);

do
{
printf("%d",i);
i++;
}
while(i<n);

return 0;
}
commented Jun 1, 2021 by (55,660 points)
You did not initialize the value of i. The value is indeterminate. (In debug mode, the the compiler *may* be kind to initialize it to 0 for you, but in actual production, such a variable would have indeterminate value -- a.k.a. memory garbage.)

For your code work correctly, it should have been initialized to 1.

Also, you should print some separator between the numbers(space, coma, new line) as this code prints them continuously so one cannot tell where one number starts and where the other ends; the whole thing looks like a huge number with many-many digits.
answered Jun 1, 2021 by (140 points)
#include<stdio.h>

void natural(int n);

int main()

{

printf("ENTER THE RANGE \n");

scanf("%d",&a);

natural(a);

}

void natural(int n)

{

int i;

for(i=1;i<=n;i++)

{

printf("%d",i);

}

}