In: Computer Science
Write a C++ program to generate two random numbers (Rnd1 and Rnd2). These two numbers should be within a range [100, 500]. If Rnd1 greater than or equals to Rnd2, print out the result of (Rnd1-Rnd2). Otherwise, print out the result of (Rnd2-Rnd1). In all cases, print Rnd1, Rnd2, and the results of subtractions in suitable messages.
#include <iostream>
#include <stdlib.h>
#include<time.h>
using namespace std;
int main()
{
srand (time(NULL));//initializing the random seed(means, to change the numbers in every run)
int Rnd1 = rand() % 500 + 100;//generate random number1 in range [100,500]
int Rnd2 = rand() % 500 + 100;//generate random number2 in range [100,500]
cout<<"Random Number 1 : "<<Rnd1<<"\nRandom Number 2 : "<<Rnd2<<"\n";
if(Rnd1>=Rnd2)
{
cout<<Rnd1 <<" is greater and the difference is : "<<Rnd1-Rnd2;
}
else
{
cout<<Rnd2 <<" is greater and the difference is : "<<Rnd2-Rnd1;
}
return 0;
}
output