Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


charles kaifiti
IJVM
1 Answer(s)      3 years and 7 months ago
Posted in : Java Beginners


User should be prompted to enter two non-negative numbers (say, a and b where a >0 and b > 0) line by line and then press the enter key. Your program should calculate and display the following for these numbers (see sample output below):  Sum (a + b)  Difference (a - b or b - a, whichever gives a positive value)  Product (a x b)  Power (ab) OUTPUT Num1: 2 Num2: 10 Sum = 12 Difference = 8 Product = 20 Power = 1024 Num1: a Invalid Number Num1: x End of program


Advertisement
View Answers

May 26, 2011 at 5:55 PM


import java.util.*;

public class Calculate {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Num1: ");
        int num1 = input.nextInt();
        System.out.print("Num2: ");
        int num2 = input.nextInt();
        if (num1 > 0 && num2 > 0) {
            int sum = num1 + num2;
            System.out.println("Sum= " + sum);
            int diff = 0;
            if (num1 > num2) {
                diff = num1 - num2;
            } else {
                diff = num2 - num1;
            }
            System.out.println("Difference= " + diff);
            int prod = num1 * num2;
            System.out.println("Product= " + prod);
            double pow = Math.pow(num1, num2);
            System.out.println("power= " + pow);
        } else {
            System.out.println("Invalid Number");
            System.out.println("End of program");
        }
    }
}


Related Tutorials/Questions & Answers:
IJVM
in IJVM. For example, if two numbers 2 and 3 are on the stack, the power function... value and b being the value on top). Write an IJVM code segment... are allowed to use existing IJVM instructions only. Include appropriate comments
IJVM
in IJVM. For example, if two numbers 2 and 3 are on the stack, the power function... value and b being the value on top). Write an IJVM code segment... are allowed to use existing IJVM instructions only. Include appropriate comments
Advertisements
IJVM
IJVM  using IJVM..can anybody help me in this question User should be prompted to enter two non-negative numbers (say, a and b where a >0 and b > 0) line by line and then press the enter key. Your program should
Advertisements
 

 

 

DMCA.com