C Program to print numbers from 1 to N without using loops

  • Write a program in C to print numbers from 1 to N without using any loop.
  • How to print numbers between 1 to 100 using recursion.
  • WAP to print natural numbers from 1 to N using goto statement.

C program to print numbers from 1 to N using recursion.

#include <stdio.h>

void printNumber(int N);

int main(){
    int N;
    printf("Enter a number\n");
    scanf("%d", &N);
    
    printNumber(N);

    return 0;
}

void printNumber(int N){
    if(N >= 1){
        printNumber(N-1);
        printf("%d ", N);
    }
}
Output
Enter a number
10
1 2 3 4 5 6 7 8 9 10

#include <stdio.h>

void printNumber(int i, int N);

int main(){
    int N;
    printf("Enter a number\n");
    scanf("%d", &N);
    
    printNumber(1, N);

    return 0;
}

void printNumber(int i, int N){
    if(i <= N){
         printf("%d ", i);
         printNumber(i + 1, N);
    }
}
Output
Enter a number
12
1 2 3 4 5 6 7 8 9 10 11 12

C program to print numbers from 1 to N using goto statement.

#include <stdio.h>
 
int main() {
  int N, i = 1;
  
  printf("Enter a number\n");
  scanf("%d", &N);  
 
  label:
 
  printf("%d ", i);
  i++;
 
  if(i <= N)
      goto label;
 
  return 0;
}
Output
Enter a number
14
1 2 3 4 5 6 7 8 9 10 11 12 13 14