Ugly Number in Java

 Ugly Number

Question : Write a program to accept a number and check whether it is Ugly number or not.

Ugly Number : An Ugly number is a positive integer whose prime factors are limited to 2, 3, and 5

First 11 Ugly Numbers : 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15


Example :-

Factors of 12 : 1, 2, 3, 4, 6, 12

Prime Factors of 12 : 2, 3

Prime Factorization : Writing a number as products of its prime factors.

12 : 2 X 2 X 3 = 12 



Program to check whether a number is Ugly Number or not :-
Program :-

import java.io.*;
import java.util.*;
class Ugly
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        int n,k=0,p;
        System.out.println("Enter Number");
        n= in.nextInt();
        p=n;
        while(n!=1)
        {
            if(n%2==0)
            {
                n=n/2;
            }
            else if(n%3==0)
            {
                n=n/3;
            }
            else if(n%5==0)
            {
                n=n/5;
            }
            else
            {
                System.out.println("It is not a Ugly Number "+p);
                k=1;
                break;
            }
        }
        if(k==0)
        {
            System.out.println("It is a Ugly Number : "+p);
        }
    }//end of main method
}//end of class


For Proper Understanding Watch the Video :-




Watch this video : Ugly Number in Java


All the best :)
Keep Learning :)

Comments

Popular posts from this blog

Frequency of each digit of a number in Java

Trimorphic Number in JAVA

PalPrime Number in JAVA