Java Loops II

Sort by

recency

|

3234 Discussions

|

  • + 0 comments

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

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int q = sc.nextInt();
        for(int i=0;i<q;i++){
            int a = sc.nextInt();
            int b = sc.nextInt();
            int n = sc.nextInt();
    
            int sum = 0;
            int first = a+b;
            int temp = first;
            int mul = b*2;
    
            for(int j=1;j<=n;j++){
                if(j==1){
                System.out.print(first+" ");
                }
            else{
                sum = temp + mul;
                System.out.print(sum+" ");
                temp = sum;
                mul = mul*2;
                }
            }
            System.out.println();
        }
    }
    

    }

  • + 0 comments

    This one stumped me for a bit had to work on it on paper before I could get it to work...I haven't done Java in over 20 years.

  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            int q, a, b, n;
            
            Scanner scan = new Scanner(System.in);
            
            q = scan.nextInt();
            
            // read the buffer \n in input stream and ignore
            scan.nextLine();
            
            for (int i = 0; i < q; i++){
                String line = scan.nextLine();
                String[] tokens = line.split(" ");
                
                a = Integer.parseInt(tokens[0]);
                b = Integer.parseInt(tokens[1]);
                n = Integer.parseInt(tokens[2]);
                
                int ex = 0;
                for (int j = 0; j <= n-1; j++){
                    // caching the previous output to avoid repeated computations
                    ex += (int)(Math.pow(2, j) * b);
                    System.out.print(a + ex);
                    System.out.print(" ");
                }
                
                System.out.println();
            }
            scan.close();
        }
    }
    
  • + 0 comments

    import java.util.*;

    class Solution { public static void main(String[] argh) { Scanner in = new Scanner(System.in);

        int t = in.nextInt(); // Number of test cases
    
        for (int i = 0; i < t; i++) {
            int a = in.nextInt(); // Base value
            int b = in.nextInt(); // Multiplier
            int n = in.nextInt(); // Number of terms
    
            int result = a;
            int power = 1; // Start with 2^0 = 1
    
            for (int j = 0; j < n; j++) {
                result += power * b;
                System.out.print(result + " ");
                power *= 2; // Next power of 2
            }
    
            System.out.println(); // Move to next line after each test case
        }
    
        in.close(); // Close the scanner
    }
    

    }

  • + 0 comments

    Java is such a powerful and versatile programming language—whether you're building web apps. betguru 247.net