Question

In Java, write a program that uses a stack to print the prime factors of a...

In Java, write a program that uses a stack to print the prime factors of a positive integer in descending order.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;
import java.util.Stack;

public class PrimeFactors {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int n = in.nextInt();
        int num = n;
        Stack<Integer> stack = new Stack<>();
        for (int i = 2; i <= n; i++) {
            while (n % i == 0) {
                stack.push(i);
                n /= i;
            }
        }
        System.out.print("Prime factors of " + num + " are:");
        while (!stack.isEmpty()) {
            System.out.print(" " + stack.pop());
        }
        System.out.println();
    }
}

Add a comment
Know the answer?
Add Answer to:
In Java, write a program that uses a stack to print the prime factors of a...
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
  • Write a Java application that asks for an integer and returns its factorization into prime factors...

    Write a Java application that asks for an integer and returns its factorization into prime factors including their powers. The prime factors must appear in increasing order. For example if 120 (= 2*2*2*3*5) in the input number, then your program should output 120 = 2^3 * 3^1 * 5^1 The factors should appear in increasing order and should be separated from each other by asterisks (*).

  • Write a java program to print all the prime numbers below a certain given number. A...

    Write a java program to print all the prime numbers below a certain given number. A prime number is defined as a number that can only be divided by 1 and itself. Requirements: 1. Accept the upper limit from the user as an integer. 2. You can assume the user input will be positive and smaller than INT MAX 3. Go from 1 to the number. If you happen to find a number that is prime, print it. The input...

  • Write a Java Program to find the sum of all factors of a given positive integer....

    Write a Java Program to find the sum of all factors of a given positive integer. Make sure to satisfy and show different results for any inputs, for example, 0, negative numbers, odd numbers, prime numbers, huge integers, etc. For example, if the integer is 12: Then the factors are: 1, 2, 3, 4, 6, 12 ; And their sum is: 28.

  • Write a program that uses a stack to reverse its inputs. Your stack must be generic...

    Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...

  • In Java Write a program that will ask the user for integers and print if the...

    In Java Write a program that will ask the user for integers and print if the integer is positive, negative or zero. The program should continue asking for integer until the user enters a negative value.

  • MUST BE WRITTEN IN JAVA CODE Write a program that uses a stack to determine whether...

    MUST BE WRITTEN IN JAVA CODE Write a program that uses a stack to determine whether a string is a palindrome (i.e., the string is spelled identically backward and forward). The program should ignore spaces and punctuation.

  • JAVA please! Prime numbers are interesting numbers. A prime number is one that is only divisible...

    JAVA please! Prime numbers are interesting numbers. A prime number is one that is only divisible by 1 and itself. For hundreds of years mathematicians have looked for the largest prime number. In the year 1456 the largest known prime was 8191. By the year 1588 it was a 6-digit number: 131,071. The famous mathematician Leonhard Euler discovered the 10-digit number 2,147,483,647 in 1772. Over the years larger and larger primes have been found. There are contests to find the...

  • Q2. To check and print if entered integer is prime or not. Prompt the user to...

    Q2. To check and print if entered integer is prime or not. Prompt the user to enter a positive integer or 0 to exit. while negative integer entered - prompt for valid values again. If positive integer entered - check if it is prime. Write a separate function that accepts a positive integer as argument and checks and returns 1 if the integer is prime or 0 if it is not prime. Call the function from main. Accept the return...

  • Write a program to print all the prime numbers below a certain given number. A prime...

    Write a program to print all the prime numbers below a certain given number. A prime number is defined as a number that can only be divided by 1 and itself. Requirements: 1. Accept the upper limit from the user as an integer. 2. You can assume the user input will be positive and smaller than INT MAX 3. Go from 1 to the number. If you happen to find a number that is prime, print it. The input and...

  • Create a Java Program that uses a Stack to implement a deck of cards. Import java.util.stack...

    Create a Java Program that uses a Stack to implement a deck of cards. Import java.util.stack and use its classes and methods. Note that java.uitl.stack uses peek instead of top (as we did in class). Look up the documentation for java.uitl.stack to see what methods it contains and how to call them. Create a Stack of cards that will hold strings. Push new items to the stack (Use "Qclubs" for Queen of clubs and so on) After your 'deck' has...

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