# LAB6

```Computer Programming
Lab 6
Exercise 1
ax2 + bx + c = 0 can be obtained using the following formula:
1 =
−+ 2 −4
2
and 2 =
−− 2 −4
2
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”.
Sample Runs
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));
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.");
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.
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;
Cont.
if (today == 0) {
else if (today == 3) {
nameDay = "Sunday";
nameDay = "Wednesday";
}
}
else if (today == 1) {
else if (today == 4) {
nameDay = "Monday";
nameDay = "Thursday";
}
}
else if (today == 2) {
else if (today == 5)
nameDay = "Tuesday";
}
nameDay = "Friday";
else
nameDay = "Saturday";
Cont.
int futureDay = (today + elapsed) % 7;
else if (futureDay == 3) {
String nameFuture;
nameFuture = "Wednesday";
}
if (futureDay == 0) {
else if (futureDay == 4) {
nameFuture = "Sunday";
nameFuture = "Thursday";
}
}
else if (futureDay == 1) {
else if (futureDay == 5)
nameFuture = "Monday";
}
nameFuture = "Friday";
else
else if (futureDay == 2) {
nameFuture = "Saturday";
nameFuture = "Tuesday";
}
System.out.println("Today is " + nameDay +
" and the future day is " + nameFuture);
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 1 ≤ 2 ≤ 3.
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;
}
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);
```

– Cards

– Cards

– Cards

– Cards