Strobogrammatic Number:
A strobogrammatic number is a type of numeral that remains the same even when rotated 180 degrees. In other words, a strobogrammatic number appears unchanged when viewed upside down. This property makes strobogrammatic numbers interesting and is often used as a puzzle or challenge.
The most common strobogrammatic numbers are 0, 1, 8, while 6 and 9 are also considered strobogrammatic because they look the same when rotated 180 degrees:
- 0, when rotated 180 degrees, remains 0.
- 1, when rotated 180 degrees, remains 1.
- 8, when rotated 180 degrees, remains 8.
- 6, when rotated 180 degrees, becomes 9.
- 9, when rotated 180 degrees, becomes 6.
For example, the numbers 101, 69, 88, 818, 9669 are all strobogrammatic numbers because they remain the same when rotated 180 degrees.
Strobogrammatic numbers have various applications in puzzles, mathematics, and even in recreational programming challenges, as we saw in the earlier code where it checked if the input number is a strobogrammatic number.
Program in Java :
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
String num = sc.next();
String s1 = "";
for (int i = 0; i < num.length(); i++) {
char c = num.charAt(i);
if (c == '0') {
s1 = '0' + s1;
} else if (c == '1') {
s1 = '1' + s1;
} else if (c == '6') {
s1 = '9' + s1;
} else if (c == '8') {
s1 = '8' + s1;
} else if (c == '9') {
s1 = '6' + s1;
}
}
if (s1.equals(num)) {
System.out.println("Yeah! It's a strobogrammatic number.");
} else {
System.out.println("No! It's not a strobogrammatic number.");
}
}
}
Explanation :
1. Importing necessary libraries:
import java.util.*;
This line imports the `java.util` package, which includes the `Scanner` class. We use this class later in the code to read user input from the console.
2. Defining the Main class:
public class Main {
This is the main class where the program's execution starts.
3. Declaring the main method
public static void main(String[] args) {
The `main` method is the entry point of the program. It is where the program starts executing.
4. Initializing a Scanner object and reading user input:
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
String num = sc.next();
In these lines, we create a `Scanner` object named `sc`, which allows us to read input from the user. We then print a prompt asking the user to enter a number, and we read the user's input as a string using `sc.next()` and store it in the variable `num`.
5. Initializing an empty string `s1` to store the reversed version of the input number:
String s1 = "";
The variable `s1` is initialized as an empty string. It will be used to build the reversed version of the input number.
6. Reversing the number and checking for strobogrammatic digits:
for (int i = 0; i < num.length(); i++) {
char c = num.charAt(i);
if (c == '0') {
s1 = '0' + s1;
} else if (c == '1') {
s1 = '1' + s1;
} else if (c == '6') {
s1 = '9' + s1;
} else if (c == '8') {
s1 = '8' + s1;
} else if (c == '9') {
s1 = '6' + s1;
}
}
This `for` loop iterates through each character of the input number (`num`). For each character, it checks if it is a strobogrammatic digit ('0', '1', '6', '8', '9') and then builds the reversed string `s1`.
- If the character is '0', we add '0' to the beginning of `s1`, effectively appending it.
- If the character is '1', we add '1' to the beginning of `s1`.
- If the character is '6', we add '9' to the beginning of `s1`.
- If the character is '8', we add '8' to the beginning of `s1`.
- If the character is '9', we add '6' to the beginning of `s1`.
Essentially, this loop reverses the digits while maintaining the strobogrammatic property.
7. Checking if the reversed number is equal to the original number:
if (s1.equals(num)) {
System.out.println("Yeah! It's a strobogrammatic number.");
} else {
System.out.println("No! It's not a strobogrammatic number.");
}
After the loop, the variable `s1` contains the reversed version of the input number. We then compare `s1` with the original number `num` using the `equals` method. If they are equal, it means the input number is strobogrammatic, and we print a corresponding message. Otherwise, we print a different message indicating that the number is not strobogrammatic.
In summary, this code takes a number as input, reverses it while considering strobogrammatic properties (certain digits are mirrored), and then determines whether the original number is a strobogrammatic number or not.
No comments:
Post a Comment