Computer Programming

12
Computer Programming Lab 6

description

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 - PowerPoint PPT Presentation

Transcript of Computer Programming

Page 1: Computer Programming

Computer Programming

Lab 6

Page 2: Computer Programming

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”.

Page 3: Computer Programming

Sample Runs

Page 4: Computer Programming

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));

Source Code

Page 5: Computer Programming

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.");

Page 6: Computer Programming

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.

Page 7: Computer Programming

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;

Page 8: Computer Programming

Cont.

if (today == 0) {

nameDay = "Sunday";

}

else if (today == 1) {

nameDay = "Monday";

}

else if (today == 2) {

nameDay = "Tuesday";

}

else if (today == 3) {

nameDay = "Wednesday";

}

else if (today == 4) {

nameDay = "Thursday";

}

else if (today == 5)

nameDay = "Friday";

else

nameDay = "Saturday";

Page 9: Computer Programming

Cont.

int futureDay = (today + elapsed) % 7;

String nameFuture;

if (futureDay == 0) {

nameFuture = "Sunday";

}

else if (futureDay == 1) {

nameFuture = "Monday";

}

else if (futureDay == 2) {

nameFuture = "Tuesday";

}

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);

Page 10: Computer Programming

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 .

Page 11: Computer Programming

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;

}

Source Code

Page 12: Computer Programming

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);

Cont