Hackerrank : Day 5: Loops!
Problem Statement
Welcome to Day 5! Check out the video review of loops here, or just jump right into the problem.
In this problem you will test your knowledge of loops. Given three integers a , b , and N , output the following series:
Input Format
The first line will contain the number of testcases T . Each of the next T lines will have three integers, a , b , and N .
Constraints
Output Format
Print the answer to each test case in a separate line.
Sample Input
2
5 3 5
0 2 10
Sample Output
8 14 26 50 98
2 6 14 30 62 126 254 510 1022 2046
Explanation
There are two test cases.
In the first case:a =5, b =3 ,N =5
1st term =5+(20×3)=8
2nd term =5+(20×3 )+(21×3)=14
3rd term =5+(20×3 )+(21×3 )+(22×3)=26
4th term =5+(20×3 )+(21×3 )+(22×3)+(23×3)=50
5th term =5+(20×3 )+(21×3 )+(22×3)+(23×3)+(24×3)=98
In the first case:
1st term =
2nd term =
3rd term =
4th term =
5th term =
Solution on this:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
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 T=sc.nextInt();
for(int i=0; i<T; i++)
{
int a=sc.nextInt();
int b=sc.nextInt();
int N=sc.nextInt();
double result = a;
for(double j=0; j<N; j++)
{
result = result + (Math.pow(2,j) * b);
System.out.print((int)result);
System.out.print(" ");
}
System.out.println("");
}
}
}
Comments
Post a Comment