KnowledgeBoat Logo

Computer Applications

Write a program to calculate the value of the given expression:
        1/a2 + 2/b2 + 3/c2
Take the values of a, b and c as input from the console. Finally, display the result of the expression to its nearest whole number.

Java

Java Math Lib Methods

87 Likes

Answer

import java.util.Scanner;

public class KboatExpression
{
    public static void main(String args[]) {
        
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter the value of a: ");
        int a = in.nextInt();
        
        System.out.print("Enter the value of b: ");
        int b = in.nextInt();
        
        System.out.print("Enter the value of c: ");
        int c = in.nextInt();
        
        double exp = (1 / Math.pow(a, 2)) + (2 / Math.pow(b, 2)) + (3 / Math.pow(c, 2));
        long roundedExp = Math.round(exp);
        
        System.out.println("Result rounded to whole number: " + roundedExp);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program to calculate the value of the given expression: 1/a 2 + 2/b 2 + 3/c 2 Take the values of a, b and c as input from the console. Finally, display the result of the expression to its nearest whole number.

Answered By

37 Likes


Related Questions