Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to check if a calculation is an integer

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default How to check if a calculation is an integer

    I'm doing the following program with the following instruction: Write a Java program to check whether a given integer is a power of 4 or not.
    My approach is to take a log of the given number on base 4, and if we get an integer then the number is the power of 4.
    Until now I haven't found a way to check if my calculation is an integer? Can you help me with that? Or maybe give me better tips about it?
    I just used the dirty principle of :

    if (x == (int)x)
    {
       ...
    }

    But it seems to not work with my code.

    Here is my code :
        try (Scanner input = new Scanner(System.in)) {
                System.out.print("Enter number : ");
                int n = input.nextInt(); // get user input
     
                // System.out.println(Math.log(n) / Math.log(4)); // this is the calculation of log(n) base 4
     
                if ((Math.log(n) / Math.log(4)) == (int) Math.log(n) / Math.log(4)) { // code never reach this part? 
                    System.out.println(input.nextInt() + " is a power of 4.");
                } else
                    System.out.println(input.nextInt() + " is not a power of 4.");
            }

  2. #2
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: How to check if a calculation is an integer

    It's fine. I finally correct my code with the following :

    try (Scanner input = new Scanner(System.in)) {
                System.out.print("Enter number : ");
                int n = input.nextInt(); // get user input
     
                // System.out.println(Math.log(n) / Math.log(4));
     
                if ((Math.log(n) / Math.log(4)) == (int) (Math.log(n) / Math.log(4))) { // code never reach this part?
                    System.out.println(n + " is a power of 4.");
                } else
                    System.out.println(n + " is not a power of 4.");
            }

    And it seems to work.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to check if a calculation is an integer

    check whether a given integer is a power of 4 or not.
    Look at the modulus operator.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to build an array and use it to check a calculation
    By Reino17 in forum Collections and Generics
    Replies: 1
    Last Post: September 18th, 2018, 07:11 PM
  2. Replies: 1
    Last Post: July 26th, 2014, 04:38 AM
  3. I need calculation
    By Swiss518 in forum Loops & Control Statements
    Replies: 7
    Last Post: January 27th, 2011, 01:26 PM
  4. check if a number is an integer
    By rsala004 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 15th, 2009, 03:51 PM
  5. How to check that console input should be integer only?
    By Konnor in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: February 2nd, 2009, 05:37 AM

Tags for this Thread