Â
In this, we are going to see do-while loop, the program is based on do-while loop which prints, numbers in reverse order from 'm' to 'n'.
The code given below can be used for TURBO C++ Compiler:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int m, n;
cout << "To print values from m to n in reverse order \n(Note : m must be GREATER than or EQUAL to n)\n";
cout << "Enter value of m :\n";
cin >> m;
cout << "Enter value of n :\n";
cin >> n;
do
{
if (m >= n)
{
cout << m << " ";
m--;
}
else
{
cout << "You have enterd Invalid values for m or n, or both\n(Note : m must be GREATER than or EQUAL to n)";
break;
}
} while (m >= n);
getch();
}
The code given below can be used for g++/gcc Compiler:-
#include<iostream>
using namespace std;
int main()
{
int m, n;
cout << "To print values from m to n in reverse order \n(Note : m must be GREATER than or EQUAL to n)\n";
cout << "Enter value of m :\n";
cin >> m;
cout << "Enter value of n :\n";
cin >> n;
do
{
if (m >= n)
{
cout << m << " ";
m--;
}
else
{
cout << "You have enterd Invalid values for m or n, or both\n(Note : m must be GREATER than or EQUAL to n)";
break;
}
} while (m >= n);
return 0;
}
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP