Difference between revisions of "Print Powers of Two"

From CompSciWiki
Jump to: navigation, search
Line 1: Line 1:
[[Program_a_day|Back to the Program-A-Day homepage]]
+
{{1010PrAD|ProblemName=Print Powers of Two
{|style="width: 100%; font-weight: normal" cellpadding="5" cellspacing="0"
+
|Problem= Remember that a while loop will execute infinitely until a specified condition is met.  A for loop is similar except that the number of iterations is specified as an argument for the loop.  Using both for and while loops, create a program that prints the powers of 2 from <math>2^0</math> <math>2^9</math>.
|-
+
!style="font-weight: normal" align="left" width="65%" valign="top"|
+
=Problem=
+
{{{Problem|Remember that a while loop will execute infinitely until a specified condition is met.   
+
A for loop is similar except that the number of iterations is specified as an argument for the loop.   
+
Using both for and while loops, create a program that prints the powers of 2 from <math>2^0</math> <math>2^9</math>.
+
 
+
  
 
Your output should look something like this:
 
Your output should look something like this:
Line 23: Line 16:
 
</pre>
 
</pre>
  
Do not use any of Java's Math class functions. .}}}
+
 
__NOTOC__ __NOEDITSECTION__
+
|SideSectionTitle=While and For Loops
!align="left" width="5%"|&nbsp;
+
 
!align="left" width="30%" style="background:#C4D0DD; font-weight: normal" valign="top"|
+
|SideSection=
={{{SideSectionTitle|While and For Loops}}}=
+
 
[[Image:Wiki_loops03.jpg|center]]<BR>
 
[[Image:Wiki_loops03.jpg|center]]<BR>
|-
+
 
!style="font-weight: normal" align="left" colspan="3" valign="top"|
+
 
=Solution=
+
 
{{{Solution|You will need to declare 2 variables for this problem, one for the exponent and one for the answer.
+
|Solution=The solution...
 +
 
 +
|SolutionCode=You will need to declare 2 variables for this problem, one for the exponent and one for the answer.
 
<pre>
 
<pre>
 
     int exp;
 
     int exp;
Line 73: Line 67:
 
   }
 
   }
 
}
 
}
</pre>}}}
+
</pre>
|-
+
!colspan="3" valign="top" align="left" style="font-weight: normal"|
+
=Code=
+
{{{SolutionCode|<pre>
+
class PowersOfTwo
+
{
+
  public static void main (String[] args)
+
  {
+
    int exp;
+
    int ans;
+
+
    for (int i=0; i<10; i++)
+
    {
+
      ans=1;
+
      exp=i;
+
+
      while(exp>0)
+
      {
+
        ans *= 2;
+
        exp --;
+
      }
+
+
      System.out.println("2 to the power of " + i + " is " + ans);
+
    }
+
  }
+
}
+
</pre>}}}
+
|}
+
 
+
[[Category:COMP 1010]]
+
[[Category:Program-A-Day]]
+
  
[[Program_a_day|Back to the Program-A-Day homepage]]
+
}}

Revision as of 10:27, 8 April 2010

Back to the Program-A-Day homepage

Problem

Remember that a while loop will execute infinitely until a specified condition is met. A for loop is similar except that the number of iterations is specified as an argument for the loop. Using both for and while loops, create a program that prints the powers of 2 from <math>2^0</math> <math>2^9</math>.

Your output should look something like this:

2 to the power of 0 is 1
2 to the power of 1 is 2
2 to the power of 2 is 4
2 to the power of 3 is 8
2 to the power of 4 is 16
2 to the power of 5 is 32
2 to the power of 6 is 64
2 to the power of 7 is 128
2 to the power of 8 is 256
2 to the power of 9 is 512
 

While and For Loops

Wiki loops03.jpg

Solution

The solution...

Code

Solution Code

Back to the Program-A-Day homepage