For Loop

Java Pyramid 2 Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

Add Comment

  • This program will ask the user to enter his choice.
    If the user enters 1 meaning you are going to add the first number to the second number.
    Hint: pls take note that everything you typed in using keyboard is automatically converted to String. Since you can’t add string to another string. You need to convert the first number to integer equivalent e.g.
    [code]
    int firstNumber =0;
    int secondNumber =0;

    String Fnumber = r.readline();
    String Snumber = r.readline();

    firstNumber = Integer.parseInt(Fnumber);
    secondNumber = Integer.parseInt(Snumber);

    int sum = firstNumber + secondNumber;

    sop (“ The sum is : “ + sum);

    [/code]

    (1)Add
    (2)Subject
    (3)Multiply
    (4)Divide
    (5)Quit

    Enter your choice: 2

    Enter First number : 5
    Enter Second number: 4
    The difference is : 1

    (6)Add
    (7)Subject
    (8)Multiply
    (9)Divide
    (10)Quit

    Enter your choice: 1

    Enter First number : 3
    Enter Second number: 4
    The sum is : 7

    (1)Add
    (2)Subject
    (3)Multiply
    (4)Divide
    (5)Quit

    Enter your choice: 3

    Enter First number : 3
    Enter Second number: 4
    The sum is : 12

    Enter your choice: 5

Sponsors

Facebook Fans