In the previous article we have discussed about Java Program to Compute (x+a)(x+b) where Value of x, a and b are Given
In this program we are going to see how to compute x2+y2+z2-xy-yz-zx where value of x, y, and z is given by using Java programming language.
Java Program to Compute x2+y2+z2-xy-yz-zx where Value of x, y, and z is Given
The formula of x2+y2+z2-xy-yz-zx is given below.
x2+y2+z2-xy-yz-zx = (x−y)2+(y-z)2+(z-x)2
Now we will convert this into a valid Java expression.
Let x= x2+y2+z2-xy-yz-zx
= (x−y)2+(y-z)2+(z-x)2
Example:
Suppose x=27, y=32, and z=10, Then = x2+y2+z2-xy-yz-zx = (x−y)2+(y-z)2+(z-x)2 = (27−32)^2+(32-10)^2+(10-27)^2 = 798
Now let’s see different ways to compute x2+y2+z2-xy-yz-zx.
- By Using Static Input Value and Pow() Function
- By Using User Input Value and Pow() Function
- By Using User Defined Method
Method-1: Java Program to Compute x2+y2+z2-xy-yz-zx where Value of x, y, and z is Given By Using Static Input Value and Pow() Function
Approach:
- Declare and initialize three integer variables say
x
,y
, andz
. - By using the formula compute x2+y2+z2-xy-yz-zx.
- Print the result.
Program:
import java.lang.Math; class Main { public static void main(String[] args) { //declare the first integer variable with an integer value int x= 10; System.out.println("x= " +x); //declare the second integer variable with an integer value int y= 12; System.out.println("y= " +y); //declare the third integer variable with an integer value int z= 14; System.out.println("z= " +z); //declare another integer variable and assigned the formulated value to it. int res= (int)(Math.pow(x-y,2)+ Math.pow(y-z,2)+ Math.pow(z-x,2)); System.out.println("x2+y2+z2-xy-yz-zx = "+res); } }
Output: x= 10 y= 12 z= 14 x2+y2+z2-xy-yz-zx = 24
Method-2: Java Program to Compute x2+y2+z2-xy-yz-zx where Value of x, y, and z is Given By Using User Input Value and Pow() Function
Approach:
- Declare and initialize three integer variables say
x
,y
, andz
. - Prompt the user to enter the values to the corresponding variables by using Scanner class.
- Then By using the formula compute x2+y2+z2-xy-yz-zx.
- Print the result.
Program:
import java.lang.Math; import java.util.Scanner; class Main { public static void main(String[] args) { //create object of scanner class. Scanner sc=new Scanner(System.in); System.out.print("x= "); //Declare an integer variable and prompt the user to enter corresponding value to it. int x= sc.nextInt(); System.out.print("y= "); //Declare another integer variable and prompt the user to enter corresponding value to it. int y= sc.nextInt(); System.out.print("z= "); //Declare another integer variable and prompt the user to enter corresponding value to it. int z= sc.nextInt(); //declare another integer variable and assigned the formulated value to it. int res= (int)(Math.pow(x-y,2)+ Math.pow(y-z,2)+ Math.pow(z-x,2)); System.out.println("x2+y2+z2-xy-yz-zx = "+res); } }
Output: x= 3 y= 2 z= 7 x2+y2+z2-xy-yz-zx = 42
Method-3: Java Program to Compute x2+y2+z2-xy-yz-zx where Value of x, y, and z is Given By Using User Defined Method Function
Approach:
- Declare and initialize three integer variables say
x
,y
, andz
. - Prompt the user to enter the values to the corresponding variables by using Scanner class.
- Then call a user defined method say
computeValue()
and passx
,y
, andz
as parameter. - Then inside method by using the formula compute x2+y2+z2-xy-yz-zx.
- Print the result.
Program:
import java.lang.Math; import java.util.Scanner; class Main { public static void main(String[] args) { //create object of scanner class. Scanner sc=new Scanner(System.in); System.out.print("x= "); //Declare an integer variable and prompt the user to enter corresponding value to it. int x= sc.nextInt(); System.out.print("y= "); //Declare another integer variable and prompt the user to enter corresponding value to it. int y= sc.nextInt(); System.out.print("z= "); //Declare another integer variable and prompt the user to enter corresponding value to it. int z= sc.nextInt(); //call the function computeValue(x,y,z); } //define the method public static void computeValue(int x,int y, int z) { //declare another integer variable and assigned the formulated value to it. int res= (int)(Math.pow(x-y,2)+ Math.pow(y-z,2)+ Math.pow(z-x,2)); System.out.println("x2+y2+z2-xy-yz-zx = "+res); } }
Output: x= 20 y= 30 z= 40 x2+y2+z2-xy-yz-zx = 600
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.
Related Java Programs:
- Java Program to Compute (b-c)2+(c-a)2+(a-b)2 where Value of a, b and c are Given
- Java Program to Compute a2(b-c)+b2(c-a)+c2(a-b) where Value of a, b and c are Given
- Java Program to Compute bc(b-c)+ca(c-a)+ab(a-b) where Value of a, b and c are Given
- Java Program to Compute a(b2-c2)+b(c2-a2)+c(a2-b2) where Value of a, b and c are Given