1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
/* Java Pyramid 2 Example This Java Pyramid example shows how to generate pyramid or triangle like given below using for loop. ***** **** *** ** * */ public class JavaPyramid2 { public static void main(String[] args) { for(int i=5; i>0 ;i--){ for(int j=0; j < i; j++){ System.out.print("*"); } //generate a new line System.out.println(""); } } } /* Output of the example would be ***** **** *** ** * */ |
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
Thank you for this very simple program it’s very easy to understand.