I need to write a code for a project and part of the code needs to be able to do as follows: Generate one random positive integer from 1 to 100, and then display all of the numbers in octal notation from 1 to the random integer.
Can someone please help me with this? (only the octal notation part)
You can use Random.nextInt() for random number and Integer.toOctalString() for octal conversion.
Try this out.
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random();
int number = random.nextInt(100) + 1;
for(int i = 1; i < number; i++){
System.out.println(Integer.toOctalString(i));
}
}
}
1st generate a random number:
int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1); // in your case min=1 and max=100;
and to convert above number to octal you can use toOctalString method.
String myOctNumber = Integer.toOctalString(randomNum);
System.out.println("myOctNumber = "+myOctNumber);