Difference between revisions of "Print Powers of Two"
(12 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | + | {{1010PrAD|ProblemName=Print Powers of Two | |
− | {| | + | |Problem= Remember that a while loop will execute indefinitely as long as 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> up to <math>2^9</math>. |
− | | | + | |
− | + | ||
− | + | ||
− | + | ||
− | 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: | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
2 to the power of 0 is 1 | 2 to the power of 0 is 1 | ||
2 to the power of 1 is 2 | 2 to the power of 1 is 2 | ||
Line 21: | Line 15: | ||
2 to the power of 8 is 256 | 2 to the power of 8 is 256 | ||
2 to the power of 9 is 512 | 2 to the power of 9 is 512 | ||
− | + | }} | |
− | + | ||
− | + | |SideSectionTitle=While and For Loops | |
− | + | ||
− | + | |SideSection= | |
− | + | [[Image:Wiki_loops03.jpg|center]]<BR> | |
− | + | ||
− | | | + | |
− | + | |Solution=You will need to declare 2 variables for this problem, one for the exponent and one for the answer. | |
− | + | {{CodeBlock | |
− | + | |Code= | |
− | + | ||
int exp; | int exp; | ||
int ans; | int ans; | ||
− | + | }} | |
− | You know that you need the powers of 2 from <math>2^0</math> <math>2^9</math>, your program will need to loop through 10 times. Since the number of iterations is known, you should use a for loop to calculate each exponent. Note that the variable 'i' is declared inside the loop. This is the counter for the loop. While it is good programming practice to declare all your variables at the beginning of each method, it is common to declare variables this way in a loop statement. | + | You know that you need the powers of 2 from <math>2^0</math> up to <math>2^9</math>, which means that your program will need to loop through 10 times. |
− | + | Since the number of iterations is known, you should use a for loop to calculate each exponent. | |
+ | Note that the variable 'i' is declared inside the loop. This is the counter for the loop. | ||
+ | While it is good programming practice to declare all your variables at the beginning of each method, it is common to declare | ||
+ | variables this way in a loop statement. | ||
+ | {{CodeBlock | ||
+ | |Code= | ||
for (int i=0; i<10; i++) | for (int i=0; i<10; i++) | ||
{ | { | ||
ans=1; | ans=1; | ||
exp=i; | exp=i; | ||
− | + | }} | |
− | + | ||
− | Because <math>2^0 = 1</math>, it is a special case. It doesn't fit nicely into the rest of the powers of 2 pattern since 1 is not evenly divisible by 2. We can | + | Because <math>2^0 = 1</math>, it is a special case. It doesn't fit nicely into the rest of the powers of 2 pattern since 1 |
+ | is not evenly divisible by 2. We can account for this in our while loop by testing to see if the exponent is greater than 0. | ||
+ | Since in the first iteration it is not, the while loop is skipped, resulting in the correct answer being printed out. | ||
+ | {{CodeBlock | ||
+ | |Code= | ||
while(exp>0) | while(exp>0) | ||
{ | { | ||
− | + | }} | |
Here, the answer is multiplied by 2. Notice that 'ans *= 2' is the same as 'ans = ans * 2'. It is just a little shorter to write. This works with all arithmetic operators. | Here, the answer is multiplied by 2. Notice that 'ans *= 2' is the same as 'ans = ans * 2'. It is just a little shorter to write. This works with all arithmetic operators. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
ans *= 2; | ans *= 2; | ||
− | + | }} | |
The while loop is exited and the answer is printed to the console. The loop repeats until 'i' reaches 10, which is the exit condition. | The while loop is exited and the answer is printed to the console. The loop repeats until 'i' reaches 10, which is the exit condition. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
exp --; | exp --; | ||
} | } | ||
Line 66: | Line 70: | ||
} | } | ||
} | } | ||
− | + | }} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | |||
− | |||
− | [ | + | |SolutionCode=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); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | }} |
Latest revision as of 03:14, 6 December 2011
Back to the Program-A-Day homepage
ProblemRemember that a while loop will execute indefinitely as long as 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> up to <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 | |
---|---|---|
SolutionYou will need to declare 2 variables for this problem, one for the exponent and one for the answer. int exp; int ans; You know that you need the powers of 2 from <math>2^0</math> up to <math>2^9</math>, which means that your program will need to loop through 10 times. Since the number of iterations is known, you should use a for loop to calculate each exponent. Note that the variable 'i' is declared inside the loop. This is the counter for the loop. While it is good programming practice to declare all your variables at the beginning of each method, it is common to declare variables this way in a loop statement. for (int i=0; i<10; i++) { ans=1; exp=i;
while(exp>0) { Here, the answer is multiplied by 2. Notice that 'ans *= 2' is the same as 'ans = ans * 2'. It is just a little shorter to write. This works with all arithmetic operators. ans *= 2; The while loop is exited and the answer is printed to the console. The loop repeats until 'i' reaches 10, which is the exit condition. exp --; } System.out.println("2 to the power of " + i + " is " + ans); } } } | ||
Code |