Q:

Print numbers without using loops

0

Print numbers without using loops

 

C program to print first n natural numbers without using any loop (do-while, for, or while). In the first program, we use recursion to achieve the desired output, and in the second, we use goto statement, i.e., without creating any function other than main.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>

void print(int, int);

int main()
{
  int n;

  scanf("%d", &n);

  print(1, n);

  return 0;
}

void print(int s, int n) {
  if (s > n)
    return;

  printf("%d\n", s);

  print(++s, n);
}

output:

10

1

2

3

4

5

6

7

8

9

10

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now