Computer Science, asked by hitanshi888, 1 year ago

write a programme in java to accept a number from user and display its factor

Answers

Answered by QGP
6
import java.util.*;
public class Factorisation
{
    static Scanner in = new Scanner(System.in);
    static Factorisation ob = new Factorisation();
    public static void main(String args[])
    {  
        String cont="";     //A String for accepting a random input. To be used at end of each program run.
        mainLoop:while(true)       //mainLoop repeatedly shows MENU after each completion of program.
        {
            System.out.println("------------------------------------------------------------");
            System.out.println("1. Print all prime factors once");
            System.out.println("2. Print all factors once");
            System.out.println("3. Completely factorise into powers of prime factors");
            System.out.println("4. Exit");
            System.out.println("------------------------------------------------------------");
            System.out.print("\nEnter your choice [1-4]: ");
            int ch = in.nextInt();  
            System.out.println();
            switch(ch)
            {
                case 1:
                {
                    long n = ob.input();
                    System.out.println("Prime Factors are:\n");
                    for(long i=2L;i<=n;i++)
                    {
                        if(ob.isPrime(i))
                        {
                            if(n%i==0)
                                System.out.println(i);
                        }
                    }
                    break;
                }

                case 2:
                {
                    long n = ob.input();
                    System.out.println("Factors are:\n");
                    for(long i=1L;i<=n;i++)
                    {
                        if(n%i==0)
                            System.out.println(i);
                    }
                    break;
                }

                case 3:
                {
                    long n = ob.input();

                    int fact[] = new int[(int)(Math.sqrt(n))];
                    int pow[] = new int[(int)(Math.sqrt(n))];
                   
                    long num = n;
                    int len=0;
 
                    int i,ind;
                   
                    for(i=2,ind=0;;i++)
                    {
                        int cnt = 0;
                        while(num%i==0)
                            {
                                num = num/i;
                                cnt++;
                            }
                       
                        if(cnt!=0)
                        {
                            fact[ind] = (int)i;
                            pow[ind] = cnt;
                            ind++;
                            len++;
                        }
                       
                        if(num==1)
                            break;
                    }
                   
                    System.out.println("The prime factorisation of the number is: \n");
                    System.out.print(n+" = ");
                    for(int j=0;j<len;j++)
                    {
                        System.out.print(fact[j]+ob.sup(pow[j])+((j==len-1)? "":" \u2A09 "));
                    }
                    System.out.println();
                    break;
                }

                case 4:
                {
                    break mainLoop;
                }

                default:
                {
                    System.out.println("Invalid Choice.");
                }
            }
           
            System.out.print("\nEnter any character to continue: ");
            cont = in.next();
            System.out.println();
        }
        System.out.println("Program Ends.");
    }

    long input()        //This function takes input from user
    {
        System.out.print("Enter number: ");
        long n = in.nextLong();
        return n;
    }

    boolean isPrime(long n)     //Function to check whether a given number is Prime
    {
        if(n==2)        //Checking if the number is 2
            return true;
        if(n%2==0)          //Checking if the number is divisible by 2
            return false;
        for(long i=3L;i<=(long)(Math.sqrt(n));i+=2)     //Loop for checking divisibility by odd numbers
        {
            if(n%i==0)
                return false;
        }
        return true;
    }
   
    /* INFO: For Mathematical Cross Operator, Unicode: U+2A09 */
    String sup(int n) //This function converts a given string of numbers into a superscript string of numbers
    {
        String digits[] = new String[10];
        String ans="";
       
        digits[0] = "\u2070";   //These are Unicode IDs for the Superscript characters
        digits[1] = "\u00B9";
        digits[2] = "\u00B2";
        digits[3] = "\u00B3";
        digits[4] = "\u2074";
        digits[5] = "\u2075";
        digits[6] = "\u2076";
        digits[7] = "\u2077";
        digits[8] = "\u2078";
        digits[9] = "\u2079";
       
        do
        {
            int a = n%10;
            ans = digits[a] + ans;
            n = n/10;
        }
        while(n!=0);
           
        return ans;
    }
}



QGP: This program does everything. It does Simple Factorisation and even Prime Factorisation.
QGP: The only way to exit the program is to Choose Choice No. 4 : Exit
QGP: Hope it helps
ChrisSmith: Brilliant answer ^_^
QGP: Thanks Chris :)
Ankit02: Wonderful
aadi93: this is great bro
aadi93: what an answer
Ankit02: He just want an example but u gave him full book bro. ::)))
QGP: Thanks :))
Similar questions