Question

2. Write an application that inputs a 3-digits number from the user and checks if all digits are prime numbers. If all digits are prime your program should stop. Use do/while for reading the input and any loop format to test if the number is prime. When checking the prime numbers dont use an if to check numbers from 1 - 9; you need to find an algorithm to check if the number is prime or not. Enter a 3-digit number: 124 4 is not a prime number! Enter a 3-digit number: 642 6 is not a prime number! 4 is not a prime number! Enter a 3-digit number: 352 All Prime! Thank You!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class Primes {

public static void main(String[] args) {

// TODO Auto-generated method stub

int n = 0,temp;

int flag=0, primeflag=1;

do

{

flag=0;

System.out.println("Enter a 3 digit number: "); // Enter the 3 digit number

Scanner sc =new Scanner(System.in);

n=sc.nextInt();

// Getting each digit and checking whether its a prime or not

while(n!=0)

{

primeflag=1;

temp=n%10; // Each digit

//Prime Number logic

for(int i=2;i<=temp-1;i++)

{

if(temp%i==0)

{

primeflag=0;

break;

}

}

if(primeflag==0)

{

System.out.println(temp+" is not a prime number"); // Print if a number is not a prime

}

else

{

flag++; // Increase flag if number is not a prime

}

  

n=n/10;

  

}

  

if(flag==3)

System.out.println("All are prime");

  

}while(flag!=3); // repeat until every digit is prime

System.out.println("\nThank You!");

}

}

Output:

Enter a 3 digit number: 146 not a prime 4 is not a prime number Enter a 3 digit number: 134 4 is not a prime number Enter a 3 digit number: 351 All are prime Thank You!

Please upvote if you like the answer

Add a comment
Know the answer?
Add Answer to:
2. Write an application that inputs a 3-digits number from the user and checks if all...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java Write an application that input a number from the user and checks if all digits...

    java Write an application that input a number from the user and checks if all digits are prime numbers using method prime. The number entered can be of any size. If all digits are prime your program should stop. Use do/while for reading the input and any loop format to test if the number is prime. When checking the prime numbers don’t use an if to check numbers from 1 – 9; you need to find an algorithm to check...

  • Write an application (SpaceDigits) that: (in java) a. inputs one number consisting of five digits from...

    Write an application (SpaceDigits) that: (in java) a. inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print “4 2 3 3 9.” (Hint: The number input is five digits long, dividing it by 10000 gives the leftmost digit. digit1 = number / 10000 ) b. determines...

  • build a phone number from digits entered by your user. Your program will loop until 10...

    build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and 9 inclusively. If so, the digit will be stored as the next number in the...

  • Write a Java application that prompts the user for pairs of inputs of a product number...

    Write a Java application that prompts the user for pairs of inputs of a product number (1-5), and then an integer quantity of units sold (this is two separate prompts for input values). You must use a switch statement and a sentinel-controlled loop (i.e. a loop that stops execution when an out of range value, such as -1, is input). All 15 items below are for a single purchase. There are five sets of inputs as follows: Product 1    1...

  • Write a program that inputs a string from the user representing a password, and checks its...

    Write a program that inputs a string from the user representing a password, and checks its validity. A valid password must contain: -At least 1 lowercase letter (between 'a' and 'z') and 1 uppercase letter (between 'A' and 'Z'). -At least 1 digit (between '0' and '9' and not between 0 and 9). At least 1 special character (hint: use an else to count all the characters that are not letters or digits). -A minimum length 6 characters. -No spaces...

  • To use the digits function, enter 1 To use the average function, enter 2 To use the perfect sum f...

    use matlab To use the digits function, enter 1 To use the average function, enter 2 To use the perfect sum function, enter3 To exit the program, enter 4 Please select a number = 6 Please re-select again: 2 please enter the first number 3 please enter the second number: 6 please enter the third number: 3 The average equals to: 4 Write a function, called digits function that is able to calculate the number of digits and the summation...

  • using c# visual studio 3.28 (Digits of an Integer) Write an app that inputs one number...

    using c# visual studio 3.28 (Digits of an Integer) Write an app that inputs one number consisting of five digits from the user, separates the number into its individual digits and displays the digits separated from one another by three spaces each. For example, if the user types 42339, the app should display 4 2 3 3 9 Assume that the user enters the correct number of digits. What happens when you execute the app and type a number with...

  • Write an application in java that reads in a five-digit integer and determines whether it is...

    Write an application in java that reads in a five-digit integer and determines whether it is a palindrome. If the number is not five digits long, display an error message and allow the user to enter a new value. Tips: 1. Determine the number of digits in the value input by the user , i.e. prompt the user to enter the number of digits of the number. Use a while loop to determine whether the user input contains the proper...

  • c++ I am suppose to write a function that gets a number from a user, say...

    c++ I am suppose to write a function that gets a number from a user, say 3.123456789 and then the program gets another number from the user which is used to find out how many decimal digits we want rounded to. So if the user inputs 3 then the number would turn into 3.123 then we are suppose to add two digits next to the rounded number so it would turn into 3.12300. the rounding is suppose to be done...

  • Help pls for assignment 2. Write an algorithm to display up to 10 user inputs 3....

    Help pls for assignment 2. Write an algorithm to display up to 10 user inputs 3. Write an algorithm to display print first 5 items in a collection. 4. Write an algorithm to read in 3 numbers. Display the largest number.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT