Answered # sum of digit

Featured Reply cse.avinash -1 YAMNA MIDHAT -5 Discussion Starter virendra_sharma -1 WaltP 2,905 YAMNA MIDHAT -5 Mouche 63 Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...

2

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

0

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 .

0

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

0

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.

0

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

0

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 5 Years Ago by Mouche*: n/a

This question has already been answered. Start a new discussion instead.

Recommended Articles

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

I don’t want at this stage work on a big separate project as I've already got plenty ...