[Solved] Nice Number with Java

Nice Number: “The Greatest Furniture Expo” is a biggest fair exhibiting furniture products, services and equipment, interior services, decoration plans, modular kitchen accessories, bedroom furniture, stylish sittings etc in the Furniture industry. It is a 4-day event and on the inaugural day of the event, the Event coordinators have announced for a Lucky lottery contest.

According to the Lucky lottery, the visitors’ entry tickets are collected and the visitors whose ticket numbers are nice numbers are chosen as winners and attractive discount coupons are distributed to the winners. A nice number is a positive integer which has exactly 4 divisors.

The Event coordinators wanted to know if a specific’s entry ticket number is a nice number or not.

Hence create a class named NiceNumber with the following method.

Method NameDescription
int findType(int)                      This method return 1 if the number is nice or return 0 if it is not a nice number.

Create a driver class called Main. In the Main method, obtain input from the user in the console and display “Nice” if the given ticket number is a nice number. Print “Not Nice” otherwise by calling the findType method present in NiceNumber class.

[Note: Strictly adhere to the Object Oriented Specifications given in the problem statement.

All class names, attribute names and method names should be the same as specified in the problem statement. Create separate classes in separate files.]

Input Format:

First line of the input is an integer that corresponds to the entry ticket number of a visitor.

Output format:

Output should display “Nice”(without quotes) if the given ticket number is a nice number. Print “Not Nice”(without quotes) otherwise.

Refer sample input and output for formatting specifications.

Sample Input 1:

6

Sample Output 1:

Nice

Sample Input 2:

4

Sample Output 2:

Not Nice

Solution

import java.util.*;
class Main {
    public int findType(int num) {
        int count = 0;
        for (int i = 1; i <= num; i++) {
            if (num % i == 0) {
                count++;
                //sauravhathi
            }
        }
        if (count == 4) {
            return 1;
        } else {
            return 0;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        Main m = new Main();
        //sauravhathi
        int type = m.findType(num);
        if (type == 1) {
            System.out.println("Nice");
        } else {
            System.out.println("Not Nice");
        }
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *