Hi frnz ,
i need a code for 2^n and suppose user enter n = 4 , so the output will be 16 . Now whatever output we will get i need sum of numbers in that output e.g here we have 16
i.e 1 + 6 = 7 . plz help me with code
Hi frnz ,
i need a code for 2^n and suppose user enter n = 4 , so the output will be 16 . Now whatever output we will get i need sum of numbers in that output e.g here we have 16
i.e 1 + 6 = 7 . plz help me with code
Jump to PostHi ,
can you plz explain it with code :)
thanks for your help
No. We to not write homework programs for people. That's your job as the student. We can help you when you get stuck, but based on your posts, you aren't stuck since …
Jump to PostAnother way to get 2^n is with bit shifting.
(1 << 4) shifts 1 four times. You start out with 0b1 then you get 0b10 then 0b100 then 0b1000 then 0b10000. You started with 1 (decimal) and ended with 0b10000 in binary and 16 in decimal. 2^4 = 16.
…
n=4.
2^4= 2 * 2 * 2 * 2;
2 is multiplied four times.
You can use a loop from 1 to n to multiply 2 to n times.
now % is modulas operator which gives the remainder part i.e,
a % b = R as a=b*d + R
for ex.
21 % 2 = 1 as 21=2*10 + 1
Use this concept to get the digits.
and in C 16/10 !=1.6 its 1 decimal part is not considered.
so use both the concepts and you will get the solution
All the best... :)
Hi frnz ,
i need a code for 2^n and suppose user enter n = 4 , so the output will be 16 . Now whatever output we will get i need sum of numbers in that output e.g here we have 16
i.e 1 + 6 = 7 . plz help me with code
declare an int ans=1 and another n take input from user in this variable declare a for loop from 0 to n and take ans=ans*2 in the loop this will give you 2^n.and u can use %operator to separate all digits of calculated ans by dividing it with power of 10 .
declare an int ans=1 and another n take input from user in this variable declare a for loop from 0 to n and take ans=ans*2 in the loop this will give you 2^n.and u can use %operator to separate all digits of calculated ans by dividing it with power of 10 .
Hi ,
can you plz explain it with code :)
thanks for your help
Hi ,
can you plz explain it with code :)
thanks for your help
No. We to not write homework programs for people. That's your job as the student. We can help you when you get stuck, but based on your posts, you aren't stuck since there's no attempt to solve the problem.
Hi ,
can you plz explain it with code :)
thanks for your help
Post the code you have written than i will obviously help you to correct it .:)
Another way to get 2^n is with bit shifting.
(1 << 4) shifts 1 four times. You start out with 0b1 then you get 0b10 then 0b100 then 0b1000 then 0b10000. You started with 1 (decimal) and ended with 0b10000 in binary and 16 in decimal. 2^4 = 16.
Here's an example of it in use:
int n;
for (n = 0; n <= 10; n++) {
printf("n = %d; 2^n = %d\n", n, (1 << n));
}
Result:
n = 0; 2^n = 1
n = 1; 2^n = 2
n = 2; 2^n = 4
n = 3; 2^n = 8
n = 4; 2^n = 16
n = 5; 2^n = 32
n = 6; 2^n = 64
n = 7; 2^n = 128
n = 8; 2^n = 256
n = 9; 2^n = 512
n = 10; 2^n = 1024
We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.