Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
JAVA – Programming Language RHHS – SE3 Problem 2 Write a java program that calculates the area, and the perimeter of a rectangle. The width and the height are integer numbers given by the user. The output of the program should appear as the following form Rectangle ---------------------| | ----------------------Enter the width: 10 Enter the height: 2 Area = 20 Perimeter = 24 Solution inta,p,w,h; System.out.println("Rectangle"); System.out.println("----------"); System.out.println("| |"); System.out.println("----------"); System.out.print("Enter the width : "); w=scan.nextInt(); System.out.print("Enter the height : "); h=scan.nextInt(); a=w*h; p=(w+h)*2; System.out.println("Area = " + a); System.out.println("Perimeter = " + p); Informatics PROGRAMMING -1- RHHS – SE3 JAVA – Programming Language Problem 3 Write a java program that calculates the perimeter of a square. - The side is an integer number entered by the user. - The side should be >=1; if not display an error message. - Perimeter = 4 * Side The output of the program should appear as the following form: Enter the side: 2 Perimeter = 8 Solution ints,p; System.out.print("Enter the side : "); s=scan.nextInt(); if(s>=1) { p=s*4; System.out.println("Perimeter = " + p); } else {System.out.println("Side must be >=1"); } Informatics PROGRAMMING -2- RHHS – SE3 JAVA – Programming Language Problem 4 Write a java program that enters the salary of an employee and then calculates the new salary which is salary + bonus. The Salary is given by the user. The Bonus is calculated depending on the following conditions: If the Salary <= 1000 then the bonus is 100 Salary >=1001 and <=2000) then the bonus is 200 Salary >=2001 and <=3000) then the bonus is 300 Salary >=3001 then the bonus is 400 The output of the program should appear as the following form Enter the salary: 2200 Bonus = 300 New salary = 2500 Solution int s, ns; System.out.print("Enter the salary : "); s=scan.nextInt(); if (s<=1000) {ns=s+100; System.out.println("bonus = 100"); System.out.println("New salary = " + ns); } else if (s>=1001 && s<=2000) {ns=s+200; System.out.println("bonus = 200"); System.out.println("New salary = " + ns); Informatics PROGRAMMING -3- RHHS – SE3 JAVA – Programming Language } else if (s>=2001 && s<=3000) {ns=s+300; System.out.println("bonus = 300"); System.out.println("New salary = " + ns); } else {ns=s+400; System.out.println("bonus = 400"); System.out.println("New salary = " + ns); } Informatics PROGRAMMING -4- JAVA – Programming Language RHHS – SE3 Problem 5 Write a java program that displays the maximum of 3 integer numbers given by the user. The output of the program should appear as the following form Enter a number : 20 Enter a number : 10 Enter a number :5 Maximum = 20 Solution 1 inta,b,c; System.out.print("Enter a number : "); a=scan.nextInt(); System.out.print("Enter a number : "); b=scan.nextInt(); System.out.print("Enter a number : "); c=scan.nextInt(); if(a>b){ if(a>c) System.out.println("Maximum = " + a); elseSystem.out.println("Maximum = " + c); } Informatics PROGRAMMING -5- JAVA – Programming Language RHHS – SE3 else { if(b>c) System.out.println("Maximum = " + b); elseSystem.out.println("Maximum = " + c); } Solution 2 If(a>b && a>c){ System.out.println("Maximum = " + a); } else if (b>a && b>c){ System.out.println("Maximum = " + b); } else { System.out.println("Maximum = " + c); } Informatics PROGRAMMING -6- RHHS – SE3 JAVA – Programming Language Problem 6 Write a java program that determines if a given number by the user is ODD or EVEN. The output of the program should appear as the following form Enter a number:10 10 is even Solution int a; System.out.print("Enter a number : "); a=scan.nextInt(); if( a%2==0) System.out.println(a + " is even "); elseSystem.out.println(a + " is odd "); Informatics PROGRAMMING -7- RHHS – SE3 JAVA – Programming Language Problem 7 Write a java program that calculates the needed operation between two integer numbers given by the user. In case user entered number different from 1 or 2, the program should display "Invalid number" The output of the program should appear as the following form Enter a number: 9 Enter a number: 3 1- Display the maximum number. 2- Display whether the first number is divisible by the second number. Choose from 1 or 2 : 1 Maximum is 50 Solution inta,b,c; System.out.print("Enter a number : "); a=scan.nextInt(); System.out.print("Enter a number : "); b=scan.nextInt(); System.out.println("1-Display the maximum number "); System.out.println("2-Display whether the first number is divisible by the second number "); System.out.print("Choose from 1 or 2 : "); c=scan.nextInt(); switch (c) { case 1: if(a>b) System.out.println("maximum = " + a); Informatics PROGRAMMING -8- RHHS – SE3 JAVA – Programming Language elseSystem.out.println("maximum = " + b); break; case 2: if (a%b==0) System.out.println(a + " is divisible by " + b); elseSystem.out.println(a + " is not divisible by " + b); break; default: System.out.println("Invalid Number "); Informatics PROGRAMMING -9- RHHS – SE3 JAVA – Programming Language Problem 8 (Do – while) Write a java program that enters an integer number by the user, the number must be greater than 20; Solution intnb; do { System.out.print("Enter a number "); nb=scan.nextInt(); } while (nb<=20); Informatics PROGRAMMING - 10 - RHHS – SE3 JAVA – Programming Language Problem 9 (Do – while) Write a java program that enters an integer number by the user, and then counts and displays the number of even numbers. Note: the number must be between 5 and 10. Solution int nb; do { System.out.print("Enter a number "); nb=scan.nextInt(); } while (nb<5 || nb>10); Informatics PROGRAMMING - 11 - RHHS – SE3 JAVA – Programming Language Problem 10 Write a java program that enters 5 integer numbers by the user and then prints the result of the square of each number. The output of the program should be as the following form Enter a number : 10 The square of 10 is 100 Enter a number : 5 The square of 5 is 25 …… Solution inti; intnb,square; for(i=1;i<=5;i=i+1) { System.out.print("Enter a number "); nb=scan.nextInt(); square=nb*nb; System.out.println("The square of " + nb + " is " + square); } Informatics PROGRAMMING - 12 - JAVA – Programming Language RHHS – SE3 Problem 11 Write a java program that prints the following sequence of numbers from 0 to 100. The output of the program should be as the following form 2 4 6 … 100 Solution int I; for(i=2;i<=100;i=i+2) { System.out.print(I); } Informatics PROGRAMMING - 13 - RHHS – SE3 JAVA – Programming Language Problem 12 Write a java program that displays the following sequence of numbers from 100 to 0. The output of the program should be as the following form 100 95 90 … 0 Solution: Int I; for(i=100;i>=0;i=I-5) { System.out.println(I); } Informatics PROGRAMMING - 14 - RHHS – SE3 JAVA – Programming Language Problem 13 Write a java program that enters an integer number by the user and displays the multiplication table of it. The output of the program should be as the following form Enter a number: 5 -------------0 * 5 = 0 1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25 6 * 5 = 30 7 * 5 = 35 8 * 5 = 40 9 * 5 = 45 10 * 5 = 50 Solution Int nb,i; System.out.print("ENTER A NUMBER : "); nb=scan.nextInt(); System.out.println("---------------- "); for(i=0;i<=10;i++) { System.out.println(i + " * " + nb + " = " + (i*nb)); } Informatics PROGRAMMING - 15 - RHHS – SE3 JAVA – Programming Language Problem 14 Write a java program that enters 10 integer numbers by the user, and display the number of occurrence (count) of even numbers. Solution inti,nb,c; c=0; for(i=1;i<=10;i++) { System.out.print("Enter a number: "); nb=scan.nextInt(); if(nb%2==0) c=c+1 ; } System.out.println("Even number = " + c); } Informatics PROGRAMMING - 16 - RHHS – SE3 JAVA – Programming Language Problem 15 Write a java program that enter 5 integer numbers by the user, and then displays the total of them. Example: Enter a number : 5 Enter a number : 8 Enter a number : 2 Enter a number : 10 Enter a number : 15 Total = 40 Solution inti,s,nb; s=0; for(i=1;i<=5;i++) { System.out.print("Enter a number : "); nb=scan.nextInt(); s=s+nb; } System.out.println("Total = " + s); Informatics PROGRAMMING - 17 - RHHS – SE3 JAVA – Programming Language Problem 16 Write a java program that enters 5 integer numbers by the user, and then displays the multiplication of them. Solution inti, nb, p; p=1; for(i=1;i<=5;i++){ System.out.print("Enter a number : "); nb=scan.nextInt(); p=p*nb; } System.out.println("Product = " + p); Informatics PROGRAMMING - 18 - RHHS – SE3 JAVA – Programming Language Problem 17 Write a java program that calculates the total of numbers from 1 to 50 and displays the result. The output of the program should be as the following form The total of numbers from 1 to 50 is 1275 Solution inti, s; s=0; for (i=1;i<=50;i++) s=s+i; System.out.println("The total of numbers from 1 to 50 is = " + s); Informatics PROGRAMMING - 19 - RHHS – SE3 JAVA – Programming Language Problem 21 What is the output of the following program? int i,c; c=0; For (i=2;i<=6;i++) { c=c+i; c--; } System.out.println(c); Solution 15 Informatics PROGRAMMING - 20 -