Thread: WAP for Algebraic Formulas

Forum : Problem Solving   21 Days ago
User Image
Satish15

Newbie

Joined: Jan 5, 2018

Points: 105

Threads: 10

Posts: 5

WAP for Algebraic Formulas

| Quote Date : Nov 24, 2018    Views:130    

Explanation:-
We have formulas like

(a+b)^ 2 = (a^2+b^2+2*a*b) ,
(a+b)^ 3 = (a^3+b^3+3*a^2*b+3*a*b^2),   etc...

Now I want the program to write the Algebra Formulas, If pass "n" value as power
Example:-
If I pass n=2, then formula should be like (a+b)^ 2 = (a^2+b^2+2*a*b)
If I pass n=3, then formula should be like  (a+b)^ 3 = (a^3+b^3+3*a^2*b+3*a*b^2)

Comments

User Image
ekkumar

Newbie

Joined : Dec 5, 2018

Points : 100

Threads: 0

Posts: 2

Re: WAP for Algebraic Formulas

Reply Date : Dec 5, 2018

import java.util.*;
class TC{

public static void main(String[] args){
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int x=1;
int k=0;
int []temp=new int[x];
while(k<=n){
int []temp1=new int[x+1];
for(int i=0;i<temp.length;i++){
if(i==0 || i==temp.length-1){
temp[0]=1;
temp[i]=1;
temp1[0]=1;
temp1[i]=1;
}
else
temp1[i]=temp[i-1]+temp[i];
}
k++;
x++;
temp=temp1;
}
for(int i=n,j=0;j<=n;i--,j++){
if(i==0)
System.out.print("+b^"+j);
else if(j==0)
System.out.print("a^"+i);
else{
if(i==1)
System.out.print("+"+temp[j]+"*a"+"*b^"+j);
else if(j==1)
System.out.print("+"+temp[j]+"*a^"+i+"*b");
else
System.out.print("+"+temp[j]+"*a^"+i+"*b^"+j);
}
}
System.out.println("");

}
}

Load More

No More Comments