Question

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 if the number is prime or not. Your method should return an array of numbers that were not prime. Your method should print all the numbers that are not prime and should return false if a non prime number is found. boolean prime (int x);
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.*;

public class Main
{
//variable declaration
public static int primeArraySize = 0;
public static int intSize = 0;
  
//function to check prime or not
public static boolean prime(int num)
{
//variable declaration and initialization
boolean flag = false;
switch(num)
{
case 2:
case 3:
case 5:
case 7:
flag = true;
break;
  
}
return flag;
}
  
//method to check number is full prime or not
public static int[] checkFullPrime(int num)
{
boolean prime = true;
int notPrime[] = new int[10];
int i=0, rem;
  
while (num!=0)
{
intSize++;
rem = num % 10;
if(!prime(rem))
{
notPrime[i] = rem;
i++;
primeArraySize++;
}
num = num / 10;
}
notPrime[i] = '\n';
return notPrime;
}

   public static void main(String[] args)
   {
   do
   {
   //variable initialization
   primeArraySize = 0;
intSize = 0;
   int num = 0;
   int notPrime[] = new int[10];
   Scanner input = new Scanner(System.in);
       System.out.print("Enter a 3-digit number: ");
       num = input.nextInt();
      
       notPrime = checkFullPrime(num);
       if(primeArraySize==0)
       {
           System.out.println("All Prime! Thank You!");
           return;
       }
       else
       {
       for(int i=0; i<primeArraySize; i++)
       {
       System.out.println(notPrime[i] + " is not a prime number!");
       }
  
   }
   }while(true);
   }
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
java Write an application that input a number from the user and checks if all digits...
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
  • 2. Write an application that inputs a 3-digits number from the user and checks if all...

    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 don't 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...

  • 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...

  • Please do both parts (in Java); thanks! An Armstrong number of three digits is an integer...

    Please do both parts (in Java); thanks! An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3^3 + 7^3 + 1^3 = 371. Draw the flowchart and write a Java code to find ALL Armstrong number in the range of 0 and 999. You should write your code in two ways: (Yes as if you are...

  • 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...

  • (Java Please) Sum of Digits Write a program that will sum the digits of a number...

    (Java Please) Sum of Digits Write a program that will sum the digits of a number input by the user. For example, if the user enters the number 1234, the sum of the digits will be 10 (1 + 2 + 3 + 4 = 10). The user will enter a number with at least 4 digits (greater than 1000). That value will be sent to a method which will return the sum. Sample Output 1: SUM OF DIGITS -------------...

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input:...

    c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input: an int output: boolean function: Return true if the number is a prime number and return false otherwise */ //TO DO ************************************** /* Write a function checkPrime such that input: an array of int and size of array output: nothing function: Display the prime numbers of the array */ //TO DO ************************************** /* Write a function SumofPrimes such that input: an int output: nothing...

  • Banks issue credit cards with 16 digit numbers. If you've never thought about it before you...

    Banks issue credit cards with 16 digit numbers. If you've never thought about it before you may not realize it, but there are specific rules for what those numbers can be. For example, the first few digits of the number tell you what kind of card it is - all Visa cards start with 4, MasterCard numbers start with 51 through 55, American Express starts with 34 or 37, etc. Automated systems can use this number to tell which company...

  • please answer all qustions use this input(6587) Problem Description: Consider afour digit number corresponding to number...

    please answer all qustions use this input(6587) Problem Description: Consider afour digit number corresponding to number of letters in your full name. Input this number in your program to compute the below: a) Count the number of digits in the input number: Suppose if the input number is 3561, output should be 4. b) Compute the sum of digits in the input number: Suppose if the input number is 3561, output should be 3+5+6+1 = 15. C) Reverse the digits...

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