How can you print all the powers of two up to a given number in Java?

  • Comp Sci
  • Thread starter Hiche
  • Start date
  • Tags
    Java
In summary, a program was written to print all positive integers that are powers of 2 and less than or equal to the given integer M. The program uses a for loop and an if conditional, and the pow method is used to determine if an integer is a power of two. The output may appear as doubles rather than integers due to the use of the pow method. Another method without using pow can also be used, as shown in the conversation. The final program includes logic to prevent negative numbers and unnecessary 0s from appearing in the output.
  • #1
Hiche
84
0

Homework Statement



Write a program that takes a command line argument integer M and prints all the positive integers that are powers of 2 and less than or equal to M.

Homework Equations



for loops; if conditional.

The Attempt at a Solution



Code:
public class PowersOfTwo
{
	public static void main(String[] args)
	{
		int m = Integer.parseInt(args[0]);

		for (int i = 0; i <= m ; i++)
		{
			if (Math.pow(2, i) <= m)
				System.out.println(Math.pow(2, i));
		}
	}
}

Is this true and answers the problem? Why are the values printed in type double rather than integer? Like so, when I input 4 in the command-line:

1.0
2.0
4.0


And is there another way to do that using boolean? How do you check if an integer is a power of two? Do you use the modulus or what?
 
Physics news on Phys.org
  • #2
Because the pow method returns a double, not an int.

You can do this without using the pow method, by instead using nothing more complicated that multiplication.
2 = 21 = 2
4 = 22 = 2*2
8 = 23 = 2*2*2
and so on.
 
  • #3
Code:
public class Power
{
	public static void main(String[] args)
	{
		int n = Integer.parseInt(args[0]);
		int a = 2;

		for (int i = 1; i <= n ; i++)
		{
			a = a * 2;
			if (a <= n)
			{
				System.out.println(a);
			}
		}
	}
}

Is this fine? Whenever I input a number like 99, it works fine for the first couple of values then a negative number appears and a series of 0s appear out of nowhere.
 
  • #4
You should have some logic in your loop so that it exits after a > n.

Try this.
Code:
for (int i = 1; i <= n ; i++)
{
   a = a * 2;
   if (a <= n)
   {
      System.out.println(a);
   }
   else
   {
      break;
   }
}
 
  • #5
Nevermind.

I think I got it.
 
  • #6
Your version worked, but I did this:

Code:
public class Power
{
	public static void main(String[] args)
	{
		int n = Integer.parseInt(args[0]);
		int a = 1;

		for (int i = 1; i <= n * i / a; i++)
		{
			a = a * 2;
			if (a <= n)
			{
				System.out.println(a);
			}
		}
	}
}

..and it worked. I unintentionally did that, though; the i <= n * i /a.
 
  • #7
I like my solution better because it is simpler. A simpler solution is usually better than a clever solution, because it will be easier for some other person reading your code to understand.
 

Related to How can you print all the powers of two up to a given number in Java?

1. What are "Java powers of two"?

"Java powers of two" refers to the concept of using the binary number system to represent numbers in Java programming. In this system, each digit can only have a value of 0 or 1, and powers of two (2, 4, 8, 16, etc.) are used to represent different values. For example, the binary number 1010 would represent the decimal number 10 in Java.

2. How is the concept of "Java powers of two" used in programming?

The concept of "Java powers of two" is used in various aspects of programming, such as in bitwise operations and memory allocation. By understanding how powers of two work in binary numbers, programmers can optimize their code and make it more efficient.

3. Why is it important to understand "Java powers of two" in programming?

Understanding "Java powers of two" is important because it enables programmers to work with binary numbers and perform operations at a lower level. This can lead to more efficient code and better performance in applications.

4. How can I calculate powers of two in Java?

In Java, you can use the Math.pow() method to calculate powers of two. For example, Math.pow(2, 3) would return the value 8, as 2 to the power of 3 is 8.

5. Are powers of two only relevant in Java programming?

No, powers of two are relevant in many other programming languages as well. They are commonly used in computer science and engineering for various purposes, such as data representation and algorithm design.

Similar threads

Replies
7
Views
2K
Replies
7
Views
2K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
3
Views
2K
Back
Top