html5-img
1 / 12

Computer Programming

Computer Programming. Lab 6. Exercise 1. ( Algebra: solve quadratic equations ) The two roots of a quadratic equation ax 2 + bx + c = 0 can be obtained using the following formula: and

bonner
Download Presentation

Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Programming Lab 6

  2. Exercise 1 (Algebra: solve quadratic equations) The two roots of a quadratic equation ax2 + bx + c = 0 can be obtained using the following formula: and b2 – 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots. Write a program that prompts the user to enter the values for a, b and c and displays the result based on the discriminant. if the discriminant is positive, display two roots. If the discriminant is 0, display one root. Otherwise, display “The equation has no real roots”.

  3. Sample Runs

  4. Source Code Scanner input = new Scanner(System.in); System.out.print("Enter a, b and c: "); double a = input.nextInt(); double b = input.nextInt(); double c = input.nextInt(); double discriminant = ((b*b) - (4*a*c));

  5. Cont if (discriminant > 0){ double root = Math.pow(discriminant, 0.5); double ro1 = ((-b + root)/(2*a)); double ro2 = ((-b - root)/(2*a)); System.out.println("The roots are " + ro1 + " and "+ro2); }else if (discriminant == 0){ double ro = (-b/(2*a)); System.out.println("The root is " + ro); }else System.out.println("The Equation has no real roots.");

  6. Exercise 2 (Find future dates) Write a program that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1, . . ., and Saturday is 6). Also prompt the user to enter the number of days after today for a future day and display the future day of the week.

  7. Source Code Scanner input = new Scanner(System.in); System.out.print("Enter today’s day: "); int today = input.nextInt(); System.out.print("Enter the number of days elapsed since today: "); int elapsed = input.nextInt(); String nameDay;

  8. Cont. else if (today == 3) { nameDay = "Wednesday"; } else if (today == 4) { nameDay = "Thursday"; } else if (today == 5) nameDay = "Friday"; else nameDay = "Saturday"; if (today == 0) { nameDay = "Sunday"; } else if (today == 1) { nameDay = "Monday"; } else if (today == 2) { nameDay = "Tuesday"; }

  9. Cont. else if (futureDay == 3) { nameFuture = "Wednesday"; } else if (futureDay == 4) { nameFuture = "Thursday"; } else if (futureDay == 5) nameFuture = "Friday"; else nameFuture = "Saturday"; System.out.println("Today is " + nameDay + " and the future day is " + nameFuture); intfutureDay = (today + elapsed) % 7; String nameFuture; if (futureDay == 0) { nameFuture = "Sunday"; } else if (futureDay == 1) { nameFuture = "Monday"; } else if (futureDay == 2) { nameFuture = "Tuesday"; }

  10. Exercise 3 (Sort three integers) write a program that sorts three integers. The integers are entered from the user and stored in variables num1, num2 and num3, respectively. The program sorts the number so that .

  11. Source Code Scanner input = new Scanner(System.in); System.out.print("Enter three integers: "); int number1 = input.nextInt(); int number2 = input.nextInt(); int number3 = input.nextInt(); if (number1 > number2) { int temp = number1; number1 = number2; number2 = temp; }

  12. Cont if (number2 > number3) { int temp = number2; number2 = number3; number3 = temp; } if (number1 > number2) { int temp = number1; number1 = number2; number2 = temp; } System.out.println("The sorted numbers are " + number1 + " " + number2 + " " + number3);

More Related