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 – SE2 Problem 1 What are the outputs of the following program? int a; int b; int c; String name; a=10;b=20; System.out.print("The value of a is " + a); System.out.println(" The value of b is " + b); c=a+b; System.out.println(c); a=a*2; c=a+b; System.out.print(a); System.out.println(c); name="java"; System.out.println(name); name="End"; System.out.println(name); Solution The value of a is 10 The value of b is 20 30 20 40 Java end Informatics PROGRAMMING - 34 - RHHS – SE2 JAVA – Programming Language Problem 2 Write a java program that calculates the area, and the perimeter of a circle. The radius of the circle is equal to 10. The output of the program should appear as the following form Area and Perimeter -----------------Radius = 10 Area = 314 Perimeter = 62.8 Solution double a,p; int r=10; System.out.println("Area and perimeter"); System.out.println("------------------"); System.out.println("Radius = " + r); a=3.14*r*r; System.out.println("Area = " + a); p=2*3.14*r; System.out.println("Perimeter = " + p); Informatics PROGRAMMING - 35 - RHHS – SE2 JAVA – Programming Language Problem 3 What are the outputs of the following program? int a; int b; int c; String K; a=5; b=a+10; c=1; System.out.println("a = " + a); System.out.println("b = " + b); c=a; c=c*3; System.out.print(c); a=a*a; c=1; a=c; System.out.println(a); K="K"; K="Stop"; System.out.println(K); Solution a=5 b = 15 15 1 stop Informatics PROGRAMMING - 36 - JAVA – Programming Language RHHS – SE2 Problem 4 Write a java program that calculates the average mark of the student Cynthia Al-zein. Take into consideration: Exam1 = 80, Exam2=90 The output of the program should appear as the following form: FName: Cynthia Lname: Al-zein Exam1: 80 Exam2: 90 The average of CynthiaAl-zein is 85.0 Use the following variables: String fn, ln Int exam1, exam2 Double avg; String fn,ln; int exam1, exam2; double avg; System.out.print("First name : "); fn=scan.next(); System.out.print("Last name : "); ln=scan.next(); System.out.print("Exam 1 : "); exam1=scan.nextInt(); System.out.print("Exam 2 : "); exam2=scan.nextInt(); avg=(exam1+exam2)/2; System.out.println("The average of " + fn + " " + ln + " is " + avg); Informatics PROGRAMMING - 37 - RHHS – SE2 JAVA – Programming Language Problem 5 Write a java program that calculates the Square of an integer number. The number is given by the user. The output of the program should appear as the following form Enter a number: 10 The square number of 10 is 100 int a,s; System.out.println("Enter a number : "); a=scan.nextInt(); s=a*a; System.out.println("The square number of " + a + " is " + s); Informatics PROGRAMMING - 38 - RHHS – SE2 JAVA – Programming Language Problem 6 Write a java program that calculates the area and the perimeter of a rectangle. The width and the height are given by the user. The output of the program should appear as the following form Area - Perimeter of a Rectangle ------------------------------Enter the width : 10 Enter the Height : 20 Area = 200 Perimeter = 60 Solution : int w,h,a,p; System.out.println("Area - Perimeter of a Rectangle "); 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 - 39 - RHHS – SE2 JAVA – Programming Language Problem 7 Write a java program that calculates the following operations as the following form: First and second numbers are given by the user. The output of the program should appear as the following form Enter the first number: 10 Enter the second number: 4 10 + 4 = 14.0 10 * 4 = 40.0 10 / 4 = 2.5 Solution Int a,b,s,m; double d; System.out.print(“enter the first number:”); a=scan.nextInt(); System.out.print(“enter the second number:”); b=scan.nextInt(); s=a+b; m=a*b; d=a/b; System.out.println(a + “ + ” + b + “ = “ + s); System.out.println(a + “ * ” + b + “ = “ + m); System.out.println(a + “ / ” + b + “ = “ + d); Informatics PROGRAMMING - 40 - RHHS – SE2 JAVA – Programming Language Problem 8 Write a java that calculates the absolute value of a given number. The output of the program should appear as the following form Enter a number: -5 The absolute value of -5 is 5 Solution int a; System.out.print("Enter a number : "); a=scan.nextInt(); if(a>0) { System.out.println("The absolute value of " + a + " is " + a); } else { System.out.println("The absolute value of " + a + " is " + (-a)); } Informatics PROGRAMMING - 41 - RHHS – SE2 JAVA – Programming Language Problem 9 Write a java program that displays the remainder of the division between 2 numbers given by the user The output of the program should appear as the following form Enter number 1 : 10 Enter number 2 : 3 The remainder is: 1 Solution Int a,b,c; System.out.print(“Enter number 1: ”); a=scan.nextInt(); System.out.print(“Enter number 2: ”); b=scan.nextInt(); c=a%b; System.out.println(“The remainder is: ” + c); Informatics PROGRAMMING - 42 - JAVA – Programming Language RHHS – SE2 Problem 10 Write a java program that determines if a given number by the user is ODD or EVEN. The number should be greater than 0. (Use do while statement) 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 "); } else { System.out.println(a + " is odd "); } Informatics PROGRAMMING - 43 - JAVA – Programming Language RHHS – SE2 Problem 10 with do while Write a java program that determines if a given number by the user is ODD or EVEN. The number should be greater than 0. (Use do while statement) The output of the program should appear as the following form Enter a number:10 10 is even Solution int a; do{ System.out.print("Enter a number : "); a=scan.nextInt(); } while (a<0); if(a%2==0) { System.out.println(a + " is even "); } else { System.out.println(a + " is odd "); } Informatics PROGRAMMING - 44 - JAVA – Programming Language RHHS – SE2 Problem 11 Write a java program that displays the Bonus of an employee. The Salary is given by the user. Display the Bonus depending on the following conditions: - If the Salary <= 1000 then display 100 - Salary >=1001 and <=2000) then display 200 - Salary >=2001 and <=3000) then display 300 - Salary >=3001 then display 400 The salary should be greater than 200 and less than 5000. (Use do while statement) The output of the program should appear as the following form Enter the salary: 2200 The bonus is 300 Solution int s; System.out.print("Enter the salary : "); s=scan.nextInt(); if(s<=1000) { System.out.println("The bonus is 100"); } else if(s>=1001 && s<=2000) { System.out.println("The bonus is 200"); } else if(s>=2001 && s<=3000) { System.out.println("The bonus is 300"); } else { System.out.println("The bonus is 400"); } Informatics PROGRAMMING - 45 - JAVA – Programming Language RHHS – SE2 Problem 12 Write a java program that displays the maximum of 2 numbers given by the user. The output of the program should appear as the following form Maximum -------Enter the first number: 20 Enter the second number: 10 The maximum is: 20 Solution Int a,b; System.out.println("Maximum "); System.out.println("----------- "); System.out.print(“Enter the first number ”); a=scan.nextInt(); System.out.print(“Enter the second number ”); b=scan.nextInt(); if(a>b) { System.out.println(“The maximum is : ” + a); } Else { System.out.println(“The maximum is : ” + b); } Informatics PROGRAMMING - 46 - RHHS – SE2 JAVA – Programming Language Problem 13 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, 2 or 3, the program should display “Invalid number” The output of the program should appear as the following form Enter a number: 5 Enter a number: 3 Operations: 1- Addition 2- Multiplication 3- Subtraction Choose from 1 to 3 : 2 The multiplication is 15 Solution int a,b,c; System.out.print("Enter a number : "); a=scan.nextInt(); System.out.print("Enter a number : "); b=scan.nextInt(); System.out.println("Operations : "); System.out.println("1-Addition "); System.out.println("2-Multiplication "); System.out.println("3-Subtraction "); System.out.print("Choose a number from 1 to 3: "); c=scan.nextInt(); Informatics PROGRAMMING - 47 - JAVA – Programming Language RHHS – SE2 switch(c) { case 1: System.out.println("The addition is " + (a+b)); break; case 2: System.out.println("The multiplication is " + (a*b)); break; case 3: System.out.println("The Subtraction is " + (a-b)); break; default: System.out.println("Invalid number "); } Informatics PROGRAMMING - 48 - RHHS – SE2 JAVA – Programming Language Problem 14 (Do – while) Write a java program that enters an integer number by the user, the number must be greater than 20; Solution int a; do{ System.out.print("Enter a number : "); a=scan.nextInt(); } while(a<=20); Problem Extra instead problem 15 (Do – while) Write a java program that enters integer number by the user, and then calculates and displays the summation of them. The program must stop when the summation become over than 1000. Solution int a; int s=0; do{ System.out.print("Enter a number : "); a=scan.nextInt(); s=s+a; System.out.println("Total = " + s); } while(s<=1000); Informatics PROGRAMMING - 49 - RHHS – SE2 JAVA – Programming Language Problem 16 Write a java program that enters 5 integer numbers by the user and 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 int I, nb, s; for(i=1;i<=5;i++) { System.out.print("Enter a number : "); nb=scan.nextInt(); s=nb*nb; System.out.println("The square number of " + nb + " is " + s); } Informatics PROGRAMMING - 50 - JAVA – Programming Language RHHS – SE2 Problem 17 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 0 2 4 6 … 100 Solution int i; for(i=0;i<=100;i=i+2) { System.out.print(i + " "); } Problem 18 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 - 51 - JAVA – Programming Language RHHS – SE2 Problem 19 Write a java program that enters 4 integer numbers by the user and verifies for every number if it is divided with 2 or not. The output of the program should be as the following form Enter a number : 10 10 is divided by 2 Enter a number : 11 11 is not divided by 2 …… Solution Int I, nb; For(i=1;i<=4;i=i+1) { System.out.print(“Enter a number”); Nb=scan.nextInt(); If (nb%2==0) { System.out.println(nb + “is divided by 2”); } Else { System.out.println(nb + “is not divided by 2”); } } Informatics PROGRAMMING - 52 - JAVA – Programming Language RHHS – SE2 Problem 20 Write a java program that enters an integer number by the user and then displays the multiplication table of it. (Note the number should be >=1) 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; do { System.out.print("Enter a number : "); nb=scan.nextInt(); } while(nb<1); System.out.println("----------------"); for(i=0;i<=10;i=i+1) { System.out.println(i + " * " + nb + " = " + (i*nb)); } Informatics PROGRAMMING - 53 - JAVA – Programming Language RHHS – SE2 Problem 21 Write a java program that enters 10 integer numbers by the user, and then counts and displays the number of even numbers. Solution int i, 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("Count = " + c); Informatics PROGRAMMING - 54 - JAVA – Programming Language RHHS – SE2 Problem 22 Write a java program that calculates the total of numbers from 1 to 50 and displays the result. S=1+2+3+4+5+… 50 The output of the program should be as the following form The total of numbers from 1 to 50 is 1275 Solution int i, 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 - 55 - JAVA – Programming Language RHHS – SE2 Problem 23 Write a java program that enters 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 int i, nb, s; 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 - 56 -