Java Loops II

Sort by

recency

|

3277 Discussions

|

  • + 0 comments

    Better method :- use for lop 1.use 1 << i instead of Math.pow(2,i) 2.bit shifft operator is faster

    Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
         for(int j=0;j<t;j++){
            int a = sc.nextInt();
            int b = sc.nextInt();
            int n = sc.nextInt();
    
            int result = a;
            for(int i=0;i<n;i++){
                result += (1 << i) * b;
                System.out.print(result + " ");
            }
    
            System.out.println();
         }
         sc.close();
    }
    
  • + 0 comments

    Java Loops II is a popular practice problem on HackerRank that helps learners strengthen their understanding of loops and mathematical patterns in Java. In this challenge, you’re asked to generate a series where each term builds on the previous one using powers of two, which trains you to work with nested calculations inside a loop. It’s especially useful for beginners who want to get comfortable micrófono de Discord no funciona solución with for loops, input handling, and formatted output, while also improving problem-solving speed for coding interviews and competitive programming.

  • [deleted]
    + 0 comments

    Scanner sc = new Scanner(System.in); int t = sc.nextInt(); //no. of test cases

        /* An outer loop to run the block t times and after each run
            reduce t (This loop will handle test cases) */
    
        while(t-- > 0) {
            // user input a, b, and n
    
            int a = sc.nextInt();
            int b = sc.nextInt();
            int n = sc.nextInt();
    
            /* Declaring and assigning values to the varibale */
    
            int sum = a; //Stores running total
            int power = 1;  //will generate GP like 1 2 4 8 16...
    
            //Because power *= 2;
    
            // Inner loop which gives GP as asked in question
            for(int i = 0; i < n; i++) {    //Runs n times
                sum += power * b;
                System.out.print(sum + " ");
                power *= 2;
            }
            System.out.println();   // each series on a new line
        }
    
        sc.close();
    
  • + 0 comments

    I think I added two loops and complicated:

    import java.util.; import java.io.;

    class Solution{ public static void main(String []argh){ Scanner in = new Scanner(System.in); int t=in.nextInt(); for(int i=0;i } in.close(); } }

  • + 0 comments

    You are given queries in the form of ,a b, and n . For each query, print the series corresponding to the given ,a ,b and n values as a single line of space-separated integers. how to solve the problem