Java Loops II

Sort by

recency

|

3175 Discussions

|

  • + 1 comment

    Easiest solution to the question. Juste understand the series and develop the formula i. e. Tn = a+b(2^n -1) . Now solve it easily :)

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

    } `

  • + 0 comments

    public static void main(String[] args) throws IOException { int[][] mint=new int[100][3];

        Scanner scanner1 =new Scanner(System.in);
        int q =scanner1.nextInt();
    
    
        for (int i=0; i<q;i++){
    
            mint[i][0]=scanner1.nextInt();
            mint[i][1]=scanner1.nextInt();
            mint[i][2]=scanner1.nextInt();
    
        }
        scanner1.close();
    
    
        for (int i=0; i<q;i++){
            int sonuc=mint[i][0];
            int us=1;
            for (int j=0; j<mint[i][2];j++){
                int b= us*mint[i][1];
                sonuc=(sonuc+b);
                System.out.print(sonuc+" ");
                us*=2;
            }
            System.out.println();
        }
    }
    
  • + 0 comments

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

    class Solution{ public static void main(String []args){ Scanner in = new Scanner(System.in); int t=in.nextInt(); for(int i=0;iorder(a,b,n); } in.close(); } static void order(int a,int b,int n){ int sum=a+(1*b); System.out.print(sum+" "); int c=2; for(int i=1;i System.out.println(); } }

  • + 0 comments
     for(int i=0;i<t;i++){
                int a = in.nextInt();
                int b = in.nextInt();
                int n = in.nextInt();
                int res = 0;
                
                int v = 1;
                for(int j = 0; j < n;j++){
                    
                    res = (v * b) + res;
                    v = v*2;
                    System.out.print(a+res+" "); 
    
                }
                System.out.println();
            }
            
    
  • + 0 comments

    class Solution{

        public int display(int n,int i){
        int mul;
        if(i == 0) {
            mul = 1;
            return mul;
        }else if(i == 1) {
            mul = 2;
            return mul;
        }else{
            mul = n * 2;
            return mul;
        }
    
    }
    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 total;
            int l = 2;
            Solution x = new Solution();
            for(int j = 0; j < n; j++){
                int m = x.display(l,j);
                total = a + m * b;
                System.out.printf("%d ",total);
                a = total;
                l = m;
            }
            System.out.println("");
        }
    }
    

    }