Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

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

+8 votes
asked May 5, 2021 by Anitha (430 points)
Using recursive function.I haven't got the desired output.

8 Answers

+1 vote
answered May 6, 2021 by Peter Minarik (84,720 points)
selected May 18, 2021 by Anitha
 
Best answer

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)
–2 votes
answered May 5, 2021 by Himangshu Nayak (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 John (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;
}
0 votes
answered May 6, 2021 by Amrit Dahal (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 MANI GARG (160 points)
void printNumber(int n)

{

if(n>0)

{

printNumber(n-1);

printf("%d ",n);

}

}
–2 votes
answered May 19, 2021 by rohan ag (1,310 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 Peter Minarik (84,720 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.
0 votes
answered May 31, 2021 by bikash123-c (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 Peter Minarik (84,720 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.
0 votes
answered Jun 1, 2021 by APOORV KUMAR SRIVASTAVA (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);

}

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...