330 likes | 449 Vues
This code snippet demonstrates how to print the powers of two that are less than or equal to 2^N. The program starts with an integer 'i' at 0 and a value 'v' initialized to 1. It increments 'i' while printing the current value of 'i' and 'v', doubling 'v' each time. For N = 6, it will print powers of two from 2^0 to 2^6, alongside their respective indices. This exercise illustrates the concept of exponential growth in numbers through simple iterative logic.
E N D
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i 0 int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v 0 1 int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true 4 N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true 4 16 N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true 4 16 true N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 4 16 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true 4 16 true N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 4 16 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 4 16 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 4 16 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 4 16 5 32 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 4 16 5 32 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true 6 N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 4 16 5 32 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true 6 64 N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 4 16 5 32 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true 6 64 true N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 4 16 5 32 6 64 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true 6 64 true N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 4 16 5 32 6 64 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true 6 64 true 7 N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 4 16 5 32 6 64 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true 6 64 true 7 128 N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 4 16 5 32 6 64 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true 6 64 true 7 128 false N = 6
Powers of Two: Trace • Ex. Print powers of 2 that are 2N. • Increment i from 0 to N. • Double v each time. i v i <= N 0 1 1 2 2 4 3 8 4 16 5 32 6 64 0 1 true int i =0; int v =1; while(i <= N){ System.out.println(i +" " + v); i = i +1; v =2* v; } 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true 6 64 true 7 128 false N = 6