Display 1 to n numbers || do while loop || C++
In this, we are going to see do-while loop, the program is based on do-while loop which prints, numbers from 1 to n.

The code given below can be used for TURBO C++ Compiler:-

    #include<iostream.h>
#include<conio.h>

void main()
{
    clrscr();
    int n, i = 1;
    cout << "To print values from 1 to n in reverse order \n(Note : n must be greater than 0)\n";
    cout << "Enter value of n :\n";
    cin >> n;

    do
    {
        cout << i << " ";
        i++;
    } while (i <= n);

    getch();
}
    
    

The code given below can be used for g++/gcc Compiler:-

    #include<iostream>
using namespace std;

int main()
{
    int n, i = 1;
    cout << "To print values from 1 to n in reverse order \n(Note : n must be greater than 0)\n";
    cout << "Enter value of n :\n";
    cin >> n;

    do
    {
        cout << i << " ";
        i++;
    } while (i <= n);

    return 0;
}
    
    

#ENJOY CODING

 

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post