In: Computer Science
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules:
1
5
31
69
87
43
31
//Kindly note that only one question can be answered according to our guidelines
//But for you i hinted the change in condition in a comment line and you'll get the required output for second usecase
import java.util.Random;
public class RandomG
{
public static void main(String args[])
{
// creating an object of Random class
Random random = new Random();
// Generates random integers 0 to 100
int last=random.nextInt(100);
boolean flag=true;
System.out.println(last);
while(flag){
int c=random.nextInt(100);
if(c>last){
//if(c<last){
//This condition yields the output
for second use case
System.out.println(c);
last=c;
}else{
flag=false;
}
}
}
}
Output:
C++:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
bool flag=true;
int last = (rand() % 100) + 1;
cout<<last<<endl;
while(flag) {
int c= (rand() % 100) + 1;
if(c>last){
//if(c<last){
//This condition yields the output
for second use case
cout<<c<<endl;
last=c;
}else{
flag=false;
}
}
return 0;
}
//Please mask sure about specification because we need to work out more without any detailed specs