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 | 5 different ways
    • C++ code to display natural numbers Using for loop
    • C++ code to display natural numbers Using while loop
    • C++ code to display natural numbers Using do-while loop
    • C++ code to print natural numbers Using function
    • C++ code to print natural numbers Using recursion
      • Similar post
    • Related

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

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

Natural numbers

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

Program 1

C++ code to display natural numbers Using for loop

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

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
     int num,i;
     cout << "Enter the Maximum number: " << endl;
    cin>>num; //get input from user

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

    return 0;
}

When the above code is executed it produces the following output

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

 

C++ code to display 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 <iostream>
#include <conio.h>
using namespace std;

int main()
{
     int num,i;
     cout << "Enter the Maximum number for num: " << endl;
    cin>>num; //get input from user

    cout<<"First "<<num<<" Natural numbers are:"<<"\n";
    i=1;
    while( i<=num ){
        cout<<i;
        cout<<" ";
        i++;
    }
    getch();
    return 0;

    return 0;
}

When the above code is executed it produces the following output

Enter the maximum number for num
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 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++

Program 3

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
     int num,i;
     cout << "Enter the Maximum number: " << endl;
    cin>>num; //get input from user

    cout<<"First "<<num<<"Natural numbers are:"<<"\n";
    i=1;
    do{
        cout<<i;
        cout<<" ";
        i++;
    }while( i<=num );
    getch();
    return 0;

    return 0;
}

When the above code is executed it produces the following output

Enter the maximum number:
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 function

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

Program 4

#include <iostream>
#include <conio.h>
using namespace std;

int naturalNum(int num);
int main()
{
    int num;
    cout<<"Enter the Maximum number: "<<"\n";
    cin>>num; //get input from user

    cout<<"First "<<num<<" Natural numbers are: "<<"\n";
    int result=naturalNum(num);
    cout<<result;

    getch();
    return 0;
}
int naturalNum(int num){
    int i;
    for(i=1; i<num; i++){
cout<<i;
  cout<<" ";
    }



}

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 print natural numbers Using recursion

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

Program 5

#include <iostream>
#include <conio.h>
using namespace std;

int naturalNum(int lower_Limit, int upper_Limit);
int main()
{
 int lower_Limit=1,upper_Limit;
    cout<<"Enter the Maximum number:"<<"\n";
    cin>>upper_Limit; //get input from user

    cout<<"First "<<upper_Limit<<" Natural numbers are:"<<endl;


    naturalNum(lower_Limit,upper_Limit);

    getch();
    return 0;
}
int naturalNum(int lower_Limit,int upper_Limit){
    if(lower_Limit>upper_Limit)
        return 0;
    cout<<lower_Limit;
    cout<<" ";
naturalNum(lower_Limit+1,upper_Limit);
    }





When the above code is executed it produces the following output

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

Similar post

C code to print natural numbers from 1 to n

Java code to print natural numbers from 1 to n

Python  code to print natural numbers from 1 to n

 

Suggested post

for loop in C++ language

while loop in C++ language

do-while loop in C++ language

 

 

 

 

 

Related

Recent Posts

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes