Home > Java > javaTutorial > body text

How to check if a number is ugly in Java?

WBOY
Release: 2023-08-20 13:25:07
forward
1327 people have browsed it

How to check if a number is ugly in Java?

If the prime factors of a number only include 2, 3 and 5, then it is called ugly number.

Maybe some numbers have prime factors with only one or two factors, but those factors must be one of 2, 3, and 5.

In this article we will see how to check if a number is an Ugly number or not by using Java programming language.

To show you some instances

Instance-1

Input number is 20.

Let’s check this using the logic of ugly numbers.

Prime factors of 20 = 2, 2, 5
Copy after login

So, as you notice here, all prime factors include only one of 2, 3, and 5.

Hence, 20 is an ugly number.

Instance-2

Input number is 30.

Let’s check this using the logic of ugly numbers.

Prime factors of 30 = 2, 3, 5
Copy after login

So, as you notice here, all prime factors include only one of 2, 3, and 5.

Hence, 30 is an ugly number.

Instance-3

Input number is 14.

Let’s check this using the logic of ugly numbers.

Prime factors of 14 = 2, 7
Copy after login

So, as you notice here 7 is present at one of the prime factors above.

Hence, 14 is not an ugly number.

Algorithm

  • Step 1 − First we define a function for checking if the input number is a prime number or not.

  • Step 2 - Collect user input through static methods or user-defined methods.

  • Step 3 − Initialize the loop to find all prime factors of the input number.

  • Step 4 - After finding the prime factors, we run a condition to check if the factors only contain 2, 3, and 5.

  • Step 5 − If the condition is true then we are printing the input number is an ugly number otherwise the input number is not an ugly number.

Multiple Approaches

We provide solutions in different ways.

  • By using static input values

  • By Using User Defined Method

Let us look at the Java program and its output one by one.

Approach-1: By Using Static Input Value

In this approach one nonzero, positive integer value will be initialized in the program and then by using the algorithm we can check whether a number is an ugly number or not .

Example

import java.util.Scanner;
public class Main {
   public static void main(String args[]) {

      //declare a variable with a static value
      int inputNumber = 25;

      //declare a variable with boolean value
      boolean check = true;

      //initialise the loop for the checking of ugly number
      for(int i = 2; i<=inputNumber; i++) {

      // checks whether numbers only include 2,3 and 5
         if(i!=2&&i!=3&&i!=5) {

         // Checks if there are some other prime factors
            if(inputNumber%i==0&&checkPrime(i)) {

            // Sets the flag to false if there are some other prime factors
               check = false;
               break;
            }

         }
      }
      if(check) {
         System.out.println(inputNumber+" is an ugly number");
      } else {
         System.out.println(inputNumber+" is Not an ugly number");
      }
   }

   // Function that checks for prime
   static boolean checkPrime(int number) {
      boolean flag = true;
      for(int i = 2; i<=number/2; i++) {
         if(number%i==0) {
            flag = false;
            break;
         }
      }
      return flag;
   }
}
Copy after login

Output

25 is an ugly number
Copy after login

Approach-2: By Using User Defined Method

In this approach one nonzero, positive integer value will be initialized in the program and then we will call a user defined method by passing this input number as parameter.

In this method, we will use an algorithm to check if a number is an ugly number.

Example

import java.util.Scanner;
public class Main {
   //initialise main method
   public static void main(String[] args) {

      //declare a variable with a static value
      int inp = 56;

      //check whether our condition is true or not in user defined method
      //if true number is ugly otherwise not
      if(checkUgly(inp)) {
         System.out.println(inp+" is an ugly number");
      } else {
         System.out.println(inp+" is Not an ugly number");
      }

   }
   // Function that checks for prime
   static boolean checkPrime(int number) {
      boolean flag = true;
      for(int i = 2; i<=number/2; i++) {
         if(number%i==0) {
            flag = false;
            break;
         }
      }
      return flag;
   }

   //define the user defined method
   //checking for ugly number
   public static boolean checkUgly(int inputNumber) {

      //declare a variable with boolean value
      boolean check = true;

      //initialise the loop for the checking of Ugly number
      for(int i = 2; i<=inputNumber; i++) {

         // checks whether numbers only include 2,3 and 5
         if(i!=2&&i!=3&&i!=5) {

            // Checks if there are some other prime factors
            if(inputNumber%i==0&&checkPrime(i)) {

               // Sets the flag to false if there are some other prime factors
               return false;
            }

         }
      }
      return true;
   }
}
Copy after login

Output

56 is Not an ugly number
Copy after login

In this article, we explored how to check a number whether it is an ugly number or not in Java by using different approaches.

The above is the detailed content of How to check if a number is ugly in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template