w3resource

Java: Accepts two integer values between 25 to 75 and return true if there is a common digit in both numbers

Java Basic: Exercise-64 with Solution

Common Digit in Numbers

Write a Java program that accepts two integer values between 25 and 75 and returns true if there is a common digit in both numbers.

Sample Solution:

Java Code:

import java.util.*;

public class Exercise64 {
    public static void main(String[] args) {
        // Create a Scanner object for user input
        Scanner in = new Scanner(System.in);

        // Prompt the user to input the first number
        System.out.print("Input the first number : ");
        int a = in.nextInt();  // Read and store the first number

        // Prompt the user to input the second number
        System.out.print("Input the second number: ");
        int b = in.nextInt();  // Read and store the second number

        // Call the common_digit method with the two numbers and print the result
        System.out.println("Result: " + common_digit(a, b));
    }

    // Define a method to check if there's a common digit between two numbers
    public static boolean common_digit(int p, int q) {
        // Check if p is less than 25 or q is greater than 75
        if (p < 25 || q > 75) {
            return false;
        }

        // Extract the last digit of each number
        int x = p % 10;
        int y = q % 10;

        // Remove the last digit from both numbers
        p /= 10;
        q /= 10;

        // Check if there's a common digit by comparing the remaining digits
        return (p == q || p == y || x == q || x == y);
    }
}

Sample Output:

Input the first number : 35                                            
Input the second number: 45                                            
Result: true

Pictorial Presentation:

Java exercises: Accepts two integer values between 25 to 75 and return true if there is a common digit in both numbers
Java exercises: Accepts two integer values between 25 to 75 and return true if there is a common digit in both numbers

Flowchart:

Flowchart: Java exercises: Accepts two integer values between 25 to 75 and return true if there is a common digit in both numbers

Java Code Editor:

Previous: Write a Java program that accepts two integer values from the user and return the larger values.
Next: Write a Java program to calculate the modules of two numbers without using any inbuilt modulus operator.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/basic/java-basic-exercise-64.php