We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Java
- Introduction
- Java Loops II
- Discussions
Java Loops II
Java Loops II
+ 1 comment import java.util.; import java.io.;
public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt();
for(int i=0; i<t; i++) { int r = 0; int a = sc.nextInt(); int b = sc.nextInt(); int n = sc.nextInt(); for (int j=0; j<n; j++) { if(j==0) { r = r + (a + (int)Math.pow(2, j) * b); } else { r = r + ((int)Math.pow(2, j) * b); } System.out.print(r + " "); } System.out.println(); }sc.close(); }
}
+ 3 comments hi guys below is my code i'm not getting the output please correct me
public static void main(String []argh){ Scanner in = new Scanner(System.in);
int a = in.nextInt(); int b = in.nextInt(); int n = in.nextInt(); int size=n-1; int su=a; if(a>=0 && b>=0 && n>0){ for(int i=0;i<=size;i++){ int power= (int)Math.pow(2,i); su=su+power*b; System.out.format("%d ",su); } System.out.println(); } in.close(); }
+ 0 comments My solution
import java.util.*; import java.io.*; import java.*; class Solution{ 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 sum=a; for(int j=0;j<n;j++) { sum=sum+(int)(Math.pow(2,j))*b; System.out.printf("%d ",sum); } System.out.printf("\n"); } in.close(); } }
+ 0 comments My solution, an easy way
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 < t; i++) { int a = in.nextInt(); int b = in.nextInt(); int n = in.nextInt(); double prev = a; for (int j = 0; j < n; j++) { prev += Math.pow(2, j) * b; //System.out.println(a + " + " + 2 + "**" + j + " * " + b); System.out.print((int) prev + " "); } System.out.println(); } in.close(); } }
+ 0 comments import java.util.*; import java.util.stream.*; 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<t;i++){ int a = in.nextInt(); int b = in.nextInt(); int n = in.nextInt(); int number=0; for(int x=0;x<n;x++){ if(x==0){ number=(a+(b*(int)Math.pow(2,x))); }else{ number+=(b*(int)Math.pow(2,x)); } System.out.format("%d ",number); } System.out.println(); } in.close(); } }
Load more conversations
Sort 2921 Discussions, By:
Please Login in order to post a comment