Java Loops II

Sort by

recency

|

3203 Discussions

|

  • + 0 comments

    import java.util.; import java.io.; public class Solution { public static void main(String[] argh) { Scanner in = new Scanner(System.in); int q =in.nextInt(); for (int i = 0; i < q; i++) { int a = in.nextInt(); int b = in.nextInt(); int n = in.nextInt(); for (int j = 0; j < n; j++) { a=a+b; b=b*2; System.out.print(a+" "); }

    System.out.println(""); } in.close(); } }

  • + 0 comments

    **import java.util.; import java.io.; public class Solution { public static void main(String[] argh) { Scanner in = new Scanner(System.in); int q =in.nextInt(); for (int i = 0; i < q; i++) { int a = in.nextInt(); int b = in.nextInt(); int n = in.nextInt(); for (int j = 0; j < n; j++) { a=a+b; b=b*2; System.out.print(a+" "); }

            System.out.println("");
        }
        in.close();
    }
    

    } **

  • + 0 comments

    List calculatedNumbers = getSeries(a,b,n); calculatedNumbers.stream().forEach(c -> System.out.print(c+" ")); System.out.println();

    public static List<Integer> getSeries(int a, int b, int n) {
        List<Integer> numbers = new ArrayList<>();
        for(int i=0;i<=n-1;i++) {
            int referenceInt = i;
            double x = 0;
            while(referenceInt >=0) {
                x = x + (Math.pow(2, referenceInt)*b);
                referenceInt--;
            }
            x = x + a;
            numbers.add((int)x); 
        }
        return numbers;
    }
    
  • + 0 comments

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

    class Solution{

     static double power(double b, int e) {
    
        // Base Case: pow(b, 0) = 1
        if (e == 0)
            return 1;
    
        if (e < 0)
            return 1 / power(b, -e);
    
        double temp = power(b, e / 2);
    
        if (e % 2 == 0)
            return temp * temp;
        else
            return b * temp * temp;
    }
    
    public static void main(String []argh){
        Scanner in = new Scanner(System.in);
        int t=in.nextInt();
    
        for(int i=0;i<t;i++){
            int a = in.nextInt();
            int b = in.nextInt();
            int n = in.nextInt(); 
            int res = a;
            for (int j = 0; j < n; j++) {
                res += power(2, j) * b;
                System.out.print(res + " ");
            }
            System.out.println();
        }
    
    
        in.close();
    }
    

    }

  • + 0 comments

    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

            int sum = a;//because it is present in all 
            for(int j = 0;j < n;j++){
                sum += b * Math.pow(2, j);//simply used the formula 
                System.out.print(sum + " ");
            }
    
    
                        It is the simple Question but all you need is using applying formula 
            System.out.println();
        }
        in.close();
    }
    

    }