AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
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 ?.



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 336
programming x 153
Posted On : 2018-01-12 16:46:58.0
profile Divesh - anyforum.in Divesh
8700
up-rate
0
down-rate

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
profile Divesh - anyforum.in Divesh
8700
Reply This Thread
up-rate
0
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in