paint-brush
Display a Number's Prime Digitsby@mayankvikash
793 reads
793 reads

Display a Number's Prime Digits

by Mayank VikashSeptember 21st, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

A few days ago, I had a computer test and I got this program which you can read in the title. I don't know how I solved this that time. So, when I returned home I searched on Google for the solution but couldn't find anything. So, I tried solving that program again myself and after removing some logical errors, I finally made it. In this article, I am going to share how I made the program with you. I have to **take a number as input and print the digits which are prime**. A digit of a number is between 0-9.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Display a Number's Prime Digits
Mayank Vikash HackerNoon profile picture


A few days ago, I had a computer test and I got this program which you can read in the title. I don't know how I solved this that time. So, when I returned home I searched on Google for the solution but couldn't find anything.


So, I tried solving that program again myself and after removing some logical errors, I finally made it. In this article, I am going to share how I made the program with you.


Let's try thinking of the logic.


Okay great, let's see how will the program work.


I have to take a number as input and print the digits which are prime.


A number is called prime, when it has only two factors, 1 and the number itself.


A digit of a number is between 0-9. So between that, we get:


  • 0 is not a prime number
  • 1 is also not a prime number
  • 2 is a prime number
  • 3 is also a prime number
  • 4 is not a prime number
  • 5 is a prime number
  • 6 is not a prime number
  • 7 is a prime number
  • 8 is not a prime number
  • 9 is not a prime number


So between 0 and 9, we have four prime numbers, which are 2, 3, 5, and 7.


Well this makes the logic so easy, I just have to extract the digits and check with an if-else if the digits match one of these five numbers, then I have to print it.

Code

First, I have initialized the variables which are going to be used in the program.


 int n, d;


Then, ask the user to enter a number.


System.out.println("Enter a number");


Input


n = in.nextInt();


Starting the while loop


while(condition){
// body
}


Condition in the while loop.


while(n>0)


Extracting the digits


d = n%10;


The modulus operator will divide the number by 10 and store the remainder in the variable d. For example, 77 % of 10 will divide by 77 by 10 and the result will be the remainder, which is 7.

Then, check if the digit is prime or not and display if it is prime.


if ( d==2 || d==3 || d==5 || d ==7)
     System.out.println(d);


After this, dividing the number by 10


n = n/10;


If the number is 89 then, 89/10 will result in 8 which will be now stored in the variable 'n'.

Source Code

import java.utril.*;
public class PrimeDigit{
  public static void main(String args[]){
    Scanner in = new Scanner(System.in);
       int n, d;
            System.out.println("Enter a number");
            n = in.nextInt();
            while (n>0) {
                d = n%10;
                if ( d==2 || d==3 || d==5 || d ==7)
                    System.out.println(d);
                n = n/10;
            }
      }
}

Output

Also Published Here.