It’s simple, just do the power calculation and then add 1.
As there are more details below. The code calculates all the powers of 2 and the powers of 2 added with 1.
Scanner input = new Scanner(System.in);
System.out.print("Digite um número: ");
int numeroEscolhido = input.nextInt(); //número digitado pelo usuário
for(int i = 0; i <= numeroEscolhido; i++){
int potenciaDe2 = (int)Math.pow(2, i);
System.out.println(String.format("(2^%s): %s | (2^%s)+1: %s", i, potenciaDe2, i, potenciaDe2 + 1));
}
Entering with the number 5, the exit will be:
(2^0): 1 | (2^0)+1: 2
(2^1): 2 | (2^1)+1: 3
(2^2): 4 | (2^2)+1: 5
(2^3): 8 | (2^3)+1: 9
(2^4): 16 | (2^4)+1: 17
(2^5): 32 | (2^5)+1: 33
See working on repl.it.
You have to do up to what power?
– Jéf Bueno
@jbueno the power is up to what you want. I am basically making a method in which the user enters the value of the maximum power that wants the tests. For example, if the user puts 5, the tests will be done up to (2 5)+1, ie will do tests at 1,2,3,4,5,8,9,16,17,32,33
– Dantex
@jbueno was just that, thanks
– Dantex