Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

C program to display natural numbers from 1 to n | 5 different ways

Posted on October 5, 2019September 28, 2020

Table of Contents

  • C program to display natural numbers from 1 to n
    • C code to print  natural numbers Using for loop
    • C code to print natural numbers Using while loop
    • C code to display natural numbers Using do-while loop
    • C code to display natural numbers Using function
    • C code to display natural numbers Using recursion

C program to display natural numbers from 1 to n

In this tutorial, we will discuss the C program to display natural numbers from 1 to n through different 5 ways

C program to display natural numbers from 1 to n
Display Natural numbers

In this post, we are going to learn how to print natural number 1 to entered number in different 5 ways

C code to print  natural numbers Using for loop

This program allows the user to enter a maximum number. and then, it displays the natural numbers from 1 to given number using for loop in C language

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,i;
    printf("Enter the Maximum number: \n");
    scanf("%d",&num); //get input from user

    printf("First %d Natural numbers are: \n",num);
    for(i=1; i<=num; i++){
        printf("%d",i);
        printf(" ");
    }
    getch();
    return 0;
}

When the above code is executed it produces the following output

Enter the maximum number for num: 20
First 20 Natural numbers are::
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 17 18 19 20

 

C code to print natural numbers Using while loop

This program allows the user to enter a maximum number. and then, it displays natural numbers from 1 to given number using while loop in C language

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,i;
    printf("Enter the Maximum number: \n");
    scanf("%d",&num); //get input from user

    printf("First %d Natural numbers are: \n",num);

    i=1;
    while(i<=num){
        printf("%d",i);
        printf(" ");
         i++;
    }
    getch();
    return 0;
}

When the above code is executed it produces the following output

Enter the maximum number:
24
First 24 Natural numbers are:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

 

C code to display natural numbers Using do-while loop

This program allows the user to enter a maximum number. and then, it displays natural numbers from 1 to given number using do while loop in C language

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,i;
    printf("Enter the Maximum number: \n");
    scanf("%d",&num); //get input from user

    printf("First %d Natural numbers are: \n",num);

    i=1;
   do{
        printf("%d",i);
        printf(" ");
         i++;
    } while(i<=num);
    getch();
    return 0;
}

When the above code is executed it produces the following output

Enter the maximum number:
15
First 15 natural numbers are:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

 

C code to display natural numbers Using function

Program 4

This program allows the user to enter a maximum number. and then,it displays natural numbers from 1 to given number using the function in C language

#include <stdio.h>
#include <stdlib.h>
int naturalNum(int num);
int main()
{
    int num;
    printf("Enter the Maximum number: \n");
    scanf("%d",&num); //get input from user

    printf("First %d Natural numbers are: \n",num);
    int result=naturalNum(num);
    printf("%d",result);

    getch();
    return 0;
}
int naturalNum(int num){
    int i;
    for(i=1; i<num; i++){
printf("%d",i);
  printf(" ");
    }



}


When the above code is executed it produces the following output

Enter the Maximum number:
12
First 12 Natural numbers are:
1 2 3 4 5 6 7 8 9 10 11 12

 

C code to display natural numbers Using recursion

This program allows the user to enter a maximum number. and it displays natural numbers from 1 to given number using recursion in C language

Program 5

#include <stdio.h>
#include <stdlib.h>
int naturalNum(int lower_Limit, int upper_Limit);
int main()
{
 int lower_Limit=1,upper_Limit;
    printf("Enter the Maximum number: \n");
    scanf("%d",&upper_Limit); //get input from user

    printf("First %d Natural numbers are: \n",upper_Limit);


    naturalNum(lower_Limit,upper_Limit);

    getch();
    return 0;
}
int naturalNum(int lower_Limit,int upper_Limit){
    if(lower_Limit>upper_Limit)
        return;
    printf("%d",lower_Limit);
    printf(" ");
naturalNum(lower_Limit+1,upper_Limit);
    }




When the above code is executed it produces the following output

Enter the naximum number:
12
First 12 Natural numbers are:
1 2 3 4 5  7 8 9 10 11 12

 

Similar post

C++ code to display natural numbers from 1 to n

Java code to display natural numbers from 1 to n

Python  code to display natural numbers from 1 to n

 

Suggested post

for loop in C language

while loop in C language

do-while loop in C language

Function in C language

Recursion in C language

 

 

 

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes