Java Loops II

  • + 0 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();

        // 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(); 
    }
    

    }