Compute integer powers of 2 : While « Language Basics « C# / C Sharp






Compute integer powers of 2

Compute integer powers of 2
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Compute integer powers of 2. 
 
using System; 
 
public class Power {   
  public static void Main() { 
    int e; 
    int result; 
 
    for(int i=0; i < 10; i++) { 
      result = 1; 
      e = i; 
 
      while(e > 0) { 
        result *= 2; 
        e--; 
      } 
 
      Console.WriteLine("2 to the " + i +  
                         " power is " + result);        
    } 
  }   
}

           
       








Related examples in the same category

1.Continue in while
2.Compute the order of magnitude of an integer Compute the order of magnitude of an integer
3.Display the digits of an integer in reverse orderDisplay the digits of an integer in reverse order
4.Using break to exit a do-while loopUsing break to exit a do-while loop
5.While TesterWhile Tester
6.Do While TesterDo While Tester
7.While SignalWhile Signal
8.While loop to display 1 to 5While loop to display 1 to 5
9.while loop to calculate and display the Fibonacci numbers less than 50while loop to calculate and display the Fibonacci numbers less than 50
10.a do...while loopa do...while loop
11.While true testWhile true test
12.Simplest do whileSimplest do while
13.Simplest while