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

virendra_sharma
-1
Light Poster

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

yourjob 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 …

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.

…

cse.avinash
-1
Junior Poster

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... :)

YAMNA MIDHAT
-5
Junior Poster in Training

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 .

virendra_sharma
-1
Light Poster

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

WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague

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.

YAMNA MIDHAT
-5
Junior Poster in Training

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 .:)

Mouche

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
```

Edited
by Mouche because:
*
n/a *

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.

Broken Link

**You're trying to visit a URL that doesn't currently exist on the web.**
Most likely, a member posted a link a long time ago to a web page that has since been removed.
It's also possible that there was a typo when posting the URL.
We redirect you to this notice instead of stripping out the link to preserve the integrity of the post.