AF
Home
Tag
Submit Notes
Ask Anything
Login
Subscribe Us
A
ny
F
orum
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.
Follow @anyforumin
corejava-programming: Program to print the result of the series sum
Write a Java program that would take four integers a, b, c and n from the user and print the result of the series sum:
[a*(a + b)] + [(a+c)*(a+b+c)] + [(a+2c) * (a+b+2c)] + [(a+3c) * (a+b+3c)] + ... n terms Note: 2c is to be understood as 2 multiplied by c
Sample Input-1 1 1 1 4
Expected output 40
Sample Input-2 3 2 1 3
Expected output 74
corejava x 353
programming x 169
Posted On :
2018-01-12 16:46:58.0
Divesh
101
18
0
4
Answers
import java.util.Scanner;
class Series{
public static void main(String [] args){
int a,b,c,n;
Scanner sc=new Scanner(System.in);
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
n=sc.nextInt();
System.out.println(getAns(a,b,c,n));
}
public static int getAns(int a,int b,int c, int n){
int ans=a *(a+b);
if(n >1){
for(int i=1;i<n;i++){
ans=ans+((a+(i*c))*(a+b+(i*c)));
}
}
return ans;
}
}
Posted On :
2018-01-15 16:02:36
Satisfied :
0 Yes
0 No
Divesh
101
18
0
Reply This Thread
5
Post Answer
Please Login First to Post Answer:
Login
Answer:
anyforum.in