Problem:
Generate the following series
(a+2pow0 * b), (a+2pow0 * b + 2pow1 * b), ...... (a+2pow0 * b + 2pow1 * b + .... + 2pown-1 * b)
Solution:
package com.myprograms;
import java.util.Scanner;
public class SeriesPrint {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter a value");
int a = s.nextInt();
System.out.println("enter b value");
int b = s.nextInt();
System.out.println("enter n value");
int n = s.nextInt();
int sum = a;
for(int i = 0; i< n; i++){
sum += (int) ( (Math.pow(2, i) * b));
System.out.print(sum + " ");
}
s.close();
}
}
Output:
enter a value
5
enter b value
3
enter n value
5
8 14 26 50 98
enter a value
0
enter b value
2
enter n value
10
2 6 14 30 62 126 254 510 1022 2046
Generate the following series
(a+2pow0 * b), (a+2pow0 * b + 2pow1 * b), ...... (a+2pow0 * b + 2pow1 * b + .... + 2pown-1 * b)
Solution:
package com.myprograms;
import java.util.Scanner;
public class SeriesPrint {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter a value");
int a = s.nextInt();
System.out.println("enter b value");
int b = s.nextInt();
System.out.println("enter n value");
int n = s.nextInt();
int sum = a;
for(int i = 0; i< n; i++){
sum += (int) ( (Math.pow(2, i) * b));
System.out.print(sum + " ");
}
s.close();
}
}
Output:
enter a value
5
enter b value
3
enter n value
5
8 14 26 50 98
enter a value
0
enter b value
2
enter n value
10
2 6 14 30 62 126 254 510 1022 2046
No comments:
Post a Comment