Thursday, June 18, 2020

Java Program To Check Ugly Number


Write a Java program to check whether a given number is an ugly number. 

What is Ugly Number


Ugly numbers are positive numbers whose only prime factors are 2, 3 or 5. First 10 ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12. By convention, 1 is included.

Test Date:Input an integer number: 235
Output : It is not an ugly number.

import java.util.*;
class Ugly
{
 int i;
 void input(int m)
{
for(i=2;i<m;i++)
{
     if(m%i==0 && prime(i) && check(i))
     break;
    }
    if(i==m)
    System.out.print("\nUgly Number:");
    else
     System.out.print("\nNot Ugly Number:");
}
boolean prime(int n)
{
    int i;
    for(i=2;i<n;i++)
    {
         if(n%i==0)
         break;
    }
    if(i==n)
    return true;
    else
    return false;
}
boolean check(int n)
{
   System.out.println(n);
   if(n!=2 && n!=3 && n!=5)
    return true;
    else
    return false;
}

public static void main(String args[])
{
    int m;
    Scanner sc=new Scanner(System.in);
    Ugly obj=new Ugly();
    System.out.print("\nEnter Number:");
    m=sc.nextInt();
    obj.input(m);
    
}        
}

Other Programs On Numbers: CLICK HERE

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner