We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
import java.util.Scanner;
import java.lang.Math; // This is no longer needed if we remove Math.pow
class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int q = scn.nextInt();
// The outer 'if' check is not needed.
// A 'for' loop with q=0 will just not run, which is correct.
for(int i = 0; i < q; i++) { // Using i=0; i<q is the more common Java convention
int a, b, n;
a = scn.nextInt();
b = scn.nextInt();
n = scn.nextInt();
// The inner 'if' check is also not needed for HackerRank
int series = a;
for(int j = 0; j < n; j++) {
// Use (1 << j) instead of Math.pow(2, j)
// It's faster and works directly with integers.
series += (1 << j) * b;
System.out.print(series + " ");
}
System.out.println();
}
// Good practice to always close the scanner!
scn.close();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Loops II
You are viewing a single comment's thread. Return to all comments →
import java.util.Scanner; import java.lang.Math; // This is no longer needed if we remove Math.pow
class Test { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int q = scn.nextInt();
}