Java Loops II

Sort by

recency

|

3262 Discussions

|

  • + 0 comments
            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 aux=1;
            int sum=a;
            int compteur =0;
            while(compteur<n){
                sum+=aux*b;
                System.out.printf(sum+" ");
                aux*=2;
                compteur++;  
            }
            System.out.println();
            } in.close();
            
        }
    }
    
    
    
  • + 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(); 
    }
    

    }

  • + 0 comments

    Java Optimal Code (100% beats) #java #coding

    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 scn = new Scanner(System.in);
        int q = scn.nextInt(); // q represent number of series u want to print
    
        for(int i = 1; i <= q; i++) {
            int a, b, n;
    
            a = scn.nextInt(); //First element in the series
            b = scn.nextInt(); //Second element in the series
            n = scn.nextInt(); // number of elements in the series
    
            int series = a; // we initialize with a to avoid multiple addition
                            // of a in the main logic 
    
            // j represent the exponential of base case (b^e)
            for(int j = 0; j < n; j++){
                //main logic
                //(1<<j) is the fast way to get 2^j
                series += (1<<j) * b;
                System.out.print(series + " ");
            }
            System.out.println();
        }
    
    }
    

    }

  • + 0 comments

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

    public class Solution {

    public static void main(String[] args) {
        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();
        if (q>=0 && q<=500) {
            int series = a;
            for (int j = 0; j < n; j++) {
                series = series + (int)Math.pow(2,j) * b;
                System.out.print(series + " ");
            }
            System.out.println();
        }
        }
    }
    

    }

  • + 0 comments

    For Java15 Platform

    I wrote the code from scratch just to get more practice

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