import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); // Validating the input if(n >= 20 && n <= 98) { // Running the countdown while(true) { System.out.print(n + " "); // Displaying the current value of the countdown // If the digits are the same, if(areDigitsIdentical(n)) { break; // Breaking out from the loop } // Decreasing the value of the countdown n--; System.out.println(""); } } else { System.out.println("Input must be 20-98"); // Displaying the appropriate message for InValid Input } } public static boolean areDigitsIdentical(int n) { int d0 = n % 10; // Extracting out the 1st digit int d1 = n / 10; // Extracting out the 2nd digit // If the two digits are the same, if(d0 == d1) return true; // Return true else // Otherwise, return false; // Return false } } The language is Java. The output has to print a space after each number including the last one (The pictures shown is what the code needs to print as well as what the code above prints)
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
// Validating the input
if(n >= 20 && n <= 98) {
// Running the countdown
while(true) {
System.out.print(n + " "); // Displaying the current value of the countdown
// If the digits are the same,
if(areDigitsIdentical(n)) {
break; // Breaking out from the loop
}
// Decreasing the value of the countdown
n--;
System.out.println("");
}
} else {
System.out.println("Input must be 20-98"); // Displaying the appropriate message for InValid Input
}
}
public static boolean areDigitsIdentical(int n) {
int d0 = n % 10; // Extracting out the 1st digit
int d1 = n / 10; // Extracting out the 2nd digit
// If the two digits are the same,
if(d0 == d1)
return true; // Return true
else // Otherwise,
return false; // Return false
}
}
The language is Java. The output has to print a space after each number including the last one (The pictures shown is what the code needs to print as well as what the code above prints)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 10 images