Dremendo Tag Line

Calculate expression in Java

Input - Question 15

In this question, we will see how to input the values in the variable a, b in Java programming using the java input and find the result of the expression a2+2ab+b2. To know more about java input click on the java input lesson.

Q15) Write a program in Java to input the values in the variable a, b and find the result of the expression a2+2ab+b2.

Program

import java.util.Scanner;

public class Q15
{
    public static void main(String args[])
    {
        int a,b,c;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter value of a ");
        a=sc.nextInt();
        System.out.print("Enter value of b ");
        b=sc.nextInt();
        c=(a*a)+(2*a*b)+(b*b);
        System.out.print("Result=" + c);
    }
}

Output

Enter value of a 2
Enter value of b 3
Result=25
video-poster