Question

In: Computer Science

Write a program which prompts the user for a positive integer, and then prints out the...

Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response.

Do not import anything other than the Scanner.

One way you might go about this using nested loops:

  • Start a "factor" variable at 2
  • In a loop:
    • repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor
    • increment the factor

This plan is by no stretch of the imagination efficient, but it does the job.

HINT: You can the modulus operator % to determine if one integer is divisible by another. If a is divisible by b, then a % b (the remainder of division of a by b) has what value?

Solutions

Expert Solution

So based on the given problem and constraints I can think of 2 cases that we need to include in our solution that are when a number is a composite number and one where it is prime itself.

So for prime we can directly print the number out as it is the self prime factor of itself.

For composite number I have first print out all the times it is divisible by '2' by using a while loop and the '%' operator.

Then after the '2' to print out other prime factors of the number I have used another loop which starts with '3' and goes till the square root of the number left. By number left I mean to say that I have updated number itself everytime it is divisible by '2'. So the updated number after its done with all '2' factors will go through this loop and within this there is another loop which will print the other prime factors of the number and at the same time it will also update the number as done by the loop that checks '2' as the factor of the number.

Here is the Code for above Problem in Java:

Code:

import java.util.Scanner;
public class PrimeFactorisation {
public static void pFactors(int num)
{
while(num%2==0)
{
System.out.print(2+" ");
num/=2;
}

for(int i=3;i<=Math.sqrt(num);i+=2)
{
while(num%i==0)
{
System.out.print(i+" ");
num/=i;
}

}

if(num>2) // if number is prime then above steps won't work
System.out.print(num+" ");
}

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter no.: ");
int num=sc.nextInt();

pFactors(num);
sc.close();
}
}

Some of the Outputs tested on the above code are:

For Composite Numbers:

For Prime Numbers:

For any doubt in Code or the approach to the problem do comment in the comments section.


Related Solutions

Write a program which: Prompts the user for a positive integer >= 0 Validates the user...
Write a program which: Prompts the user for a positive integer >= 0 Validates the user input to ensure it is a positive integer >= 0 Allocate (dynamically) an array big enough for the data. Load the array with random numbers ranging in value from1 to 100 Display the elements of the array (unsorted) Display the elements of the array (sorted) Display the average Display the median Display the mode, if none, display appropriate message RESTRICTIONS No global variables No...
Write a program that prompts a user for an integer from 1 to 99 and prints...
Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words. The program will loop in case the user wants to input an additional number. If the user enters -99, the program will exit. Example: Input: 89 Output: Eighty nine Input: 45 Output: Fourty five Input: -99 Output: Have a nice day. <program exits> For this project, you are to: 1) You should validate any data coming from the...
Write a program that prompts a user for an integer from 1 to 99 and prints...
Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words. The program will loop in case the user wants to input an additional number. If the user enters -99, the program will exit. Example: Input: 89 Output: Eighty nine Input: 45 Output: Fourty five Input: -99 Output: Have a nice day. <program exits> c++ project. need help.
in C++ programing language Write a program that prompts the user for an integer, then prints...
in C++ programing language Write a program that prompts the user for an integer, then prints all of the numbers from one to that integer, separated by spaces. Use a loop to print the numbers. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Drop to a new line after printing each 20 numbers. If the user typed...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Write a C++ program which prompts the user to enter an integer value, stores it into...
Write a C++ program which prompts the user to enter an integer value, stores it into a variable called ‘num’, evaluates the following expressions and displays results on screen. num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3)) For performing addition and subtraction, you are allowed to use ONLY the increment and decrement operators (both prefixing and postfixing are allowed). You must remember that using increment/decrement operators changes the original value of a number. Indent your code and include comments for...
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT