LEC03: Decisions and Iterations Q1. Identify and correct the errors in each of the following statements: a) if ( c < 7 ); System.out.println( "c is less than 7" ); b) if ( c => 7 ) System.out.println( "c is equal to or greater than 7" ); Q2. What is the output of following program? System.out.println("+----+"); for (int i = 1; i <= 3; i++) { System.out.println("\\ /"); System.out.println("/ \\"); System.out.println("+----+"); Q3. What is the output of following program? int hightemp = 5; for (int i = -3; i <= hightemp / 2; i++) { System.out.println(i * 1.8 + 32); Q4. What is the output of following program? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 10; j++) { System.out.print("*"); System.out.println(); // to end the line Q5. What is the output of the following nested for loops? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print(i); System.out.println(); Q6. What is the output of the following nested for loops? for (int line = 1; line <= 5; line++) { for (int j = 1; j <= (-1 * line + 5); j++) { System.out.print("."); System.out.println(line);
Q7. What is the output of the following nested for loops? for (int line = 1; line <= 5; line++) { for (int j = 1; j <= (-1 * line + 5); j++) { System.out.print("."); for (int k = 1; k <= line; k++) { System.out.print(line); System.out.println(); Q8. Modify the previous code to produce this output:...1...2...3...4... 5... Q9. Write a method digitsum that accepts an integer parameter and returns the sum of its digits. a. Assume that the number is non-negative. b. Example: digitsum(29107) returns 2+9+1+0+7 or 19 c. Hint: Use the % operator to extract a digit from a number. Q10. Larger.java Write an application that asks the user to enter two integers, obtains the numbers from the user and displays the larger number followed by the words is larger. If the numbers are equal, print the message These numbers are equal. Q11. OddEven.java Write an application that reads an integer and determines and prints whether it is odd or even. [Hint: Use the remainder operator. An even number is a multiple of 2. Any multiple of 2 leaves a remainder of 0 when divided by 2.] Q12. Calculate.java Write a Java application that calculates and prints the sum of the integers from 1 to 10. Use a while statement to loop through the calculation and increment statements. The loop should terminate when the value of x becomes 11 Q13. Gas.java Drivers are concerned with the mileage their automobiles get. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a Java application that will get user input for the miles driven and gallons used (both as integers) for each tankful. The program should calculate and display the miles per gallon obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up to this point. All averaging calculations should produce floating-point results.
Q14. Wages.java Develop a Java application that will determine the gross pay for each of three employees. The company pays straight time for the first 40 hours worked by each employee and pays time and a half for all hours worked in excess of 40 hours. You are given a list of the employees of the company, the number of hours each employee worked last week and the hourly rate of each employee. Your program should input this information for each employee (assume 3)and should determine and display the employee s gross pay. Q15. Largest.java The process of finding the largest value (i.e., the maximum of a group of values) is used frequently in computer applications. For example, a program that determines the winner of a sales contest would input the number of units sold by each salesperson. The salesperson who sells the most units wins the contest. Write a pseudocode program and then a Java application that inputs a series of 10 single-digit numbers as characters and determines and prints the largest of the numbers. Your program should use at least the following three variables: a) counter: A counter to count to 10 (i.e., to keep track of how many numbers have been input and to determine when all 10 numbers have been processed); b) number: The current digit input to the program; c) largest: The largest number found so far.
Q16.Table.java Write a Java application that uses looping to print the following table of values: Q17.EtoX.java Write an application that computes the value of ex by using the formula Q18. Triangles.java Write an application that displays the following patterns separately, one below the othe r. Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form System.out.print( '*' ); which causes the asterisks to print side by side. A statement of the form System.out.println(); can be used to position to the next line. A statement of the form System.out.print( ' ' ); can be used to display a space for the last two patterns. There should be no other output statements in the program. [Hint: The last two patterns require that each line begin with an appropriate number of blank spaces.] Q19. Pi.java Calculate the value of pi from the infinite series Print a table that shows the value of pi approximated by computing one term of this series, by two terms, by three terms, etc. How many terms of this series do you have to use before you first get 3.14? 3.141? 3.1415? 3.14159?
Q20. Power.java Write a method integerpower( base, exponent ) that returns the value of base exponent For example, integerpower( 3, 4 ) calculates 34 (or 3 * 3 * 3 * 3). Assume that exponent is a positive, nonzero integer and that base is an integer. Method integerpower should use a for or while loop to control the calculation. Do not use any math -library methods. Incorporate The main method should reads integer values for base and exponent from user and performs the calculation by calling integerpower method. [Note: your program should have two static methods: main and integerpower. The reason to make integerpower static is that main (which is static) can call only static methods.] Q21.Multiplicity.java Write a method multiple that determines, for a pair of integers, whether the second integer is a multiple of the first. The method should take two integer arguments and return true if the second is a multiple of the first and false otherwise. The main method should get two numbers from user, call this method and print the result.