In: Computer Science
C++ DO not use arrays to write this program.
Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows:
The output will be:
5, 45, 75
11, 21, 61
20, 40, 100
Please notice that all the numbers are not displayed at the end of the program. Every line is displayed in one iteration and the last line is displayed in the last iteration of your loop.
So, the output would be:
10, 40, 50 are your lucky numbers.
California College
Program:
#include <iostream>
using namespace std;
void sortNumbers(int num1, int num2, int num3) {
if(num1<num2 && num1<num3 && num2>num3 )
{
cout << num1 << " " << num3 << " " << num2;
}
else if(num1<num2 && num1<num3 && num2<num3 )
{
cout << num1 << " " << num2 << " " << num3;
}
else if (num2<num1 && num2<num3 && num3>num1 ){
cout << num2 << " " << num1 << " " << num3;
}
else if (num2<num1 && num2<num3 && num3<num1 ){
cout << num2 << " " << num3 << " " << num1;
}
else if (num3<num1 && num3<num2 && num1>num2){
cout << num3 << " " << num2 << " " << num1;
}
else if (num3<num1 && num3<num2 && num1<num2){
cout << num3 << " " << num1 << " " << num2;
}
}
int main()
{
bool isCheck = true;
while (isCheck) {
// To get random numbers range of 1-100
int num1 = (rand() % 100) + 1;
int num2 = (rand() % 100) + 1;
int num3 = (rand() % 100) + 1;
// Will give the right most digit of the number
int num1Rem = num1%10;
int num2Rem = num2%10;
int num3Rem = num3%10;
// condition to check if the right most digits are 0 for all 3 numbers
if (num1Rem == 0 && num2Rem == 0 && num3Rem == 0) {
isCheck = false;
// Sort the numbers in ascending order
sortNumbers(num1, num2, num3);
cout << " are lucky numbers" << endl;
}
// Condition to check if the right most digits are same for all 3 numbers
else if (num1Rem == num2Rem && num1Rem == num3Rem && num2Rem == num3Rem) {
cout << "Random integers are: ";
sortNumbers(num1, num2, num3);
cout << endl;
}
}
return 0;
}
Output: