# multiplication in c?

• (2 Pages)
• 1
• 2

## 19 Replies - 2407 Views - Last Post: 13 March 2011 - 12:49 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=221642&amp;s=f3f088ddffa53b3f4f2ee1ff0d9bd8f9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 BenisBen

Reputation: 0
• Posts: 8
• Joined: 11-March 11

# multiplication in c?

Posted 11 March 2011 - 07:55 PM

Hello,

```/*******************************************************************************
Program:4a
Author: Ben M
Date:
*******************************************************************************/
#include <stdio.h>

main()

{
int cmd2 = 0;
double income= 0.0;
char input [80];

printf("Please enter S for single or m for married then income\n");
printf(">>");

fgets(input,80,stdin);

if(strncmp(input,"S",0) ==0)
cmd2 = 1;

if(strncmp(input,"M",0) ==0)
cmd2 - 2;
}

```

I am working on a program that needs multiplication
I need to multiply income by some tax stuff.... I have searched the internet for stuff just can't find help looked in my book

how do you multiply a double in c that is inputed by the user by a number?

Thanks,

ben

Is This A Good Question/Topic? 0

## Replies To: multiplication in c?

### #2 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: multiplication in c?

Posted 11 March 2011 - 08:05 PM

first_operand * second_operand

so if income is a double and already has some value assigned to it, and tax is another double, and you want to multiply income by a tax rate of, say, 0.15 and assign the result to tax, you can write
tax = income * 0.15;

### #3 BenisBen

Reputation: 0
• Posts: 8
• Joined: 11-March 11

## Re: multiplication in c?

Posted 11 March 2011 - 08:23 PM

r.stiltskin, on 11 March 2011 - 08:05 PM, said:

first_operand * second_operand

so if income is a double and already has some value assigned to it, and tax is another double, and you want to multiply income by a tax rate of, say, 0.15 and assign the result to tax, you can write
tax = income * 0.15;

thank you,

### #4 BenisBen

Reputation: 0
• Posts: 8
• Joined: 11-March 11

## Re: multiplication in c?

Posted 11 March 2011 - 08:42 PM

Now I need to do like 7 if staments

do you know how to do that?

google is no help no is my book.... it all different lol =/

thanks

### #5 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: multiplication in c?

Posted 11 March 2011 - 10:00 PM

Here is how to do 7 if() statements
```if(1 == 1){
//something
}
if(2 == 2){
//something
}
if(3 == 3){
//something
}
if(4 == 4){
//something
}
if(5 == 5){
//something
}
if(6 == 6){
//something
}
if(7 == 7){
//something
}

```

I suspect there is some more sense to your question than what you actually asked but you do need to ask whatever your question actually is if you want it answered.

This post has been edited by janotte: 11 March 2011 - 10:02 PM

• Saucy!

Reputation: 6208
• Posts: 23,954
• Joined: 23-August 08

## Re: multiplication in c?

Posted 12 March 2011 - 05:12 AM

```if(strncmp(input,"S",0) ==0)
```

The third argument to strncmp is the maximum number of characters to compare. Does 0 make sense as a proper value for that?

### #7 BenisBen

Reputation: 0
• Posts: 8
• Joined: 11-March 11

## Re: multiplication in c?

Posted 12 March 2011 - 11:26 AM

thanks, I was working on that last night and fixed that...

Ben

• Saucy!

Reputation: 6208
• Posts: 23,954
• Joined: 23-August 08

## Re: multiplication in c?

Posted 12 March 2011 - 11:57 AM

By the way, you can do the same thing without using strncmp by just checking the first character in the array against the desired char:

```if (input[0] == 'A')
{
cmd2 = 1;
}
else if (input[0] == 'B')
{
cmd2 = 2;
}
```

or using a switch block:

```switch(input[0])
{
case 'A':
cmd2 = 1;
break;
case 'B':
cmd2 = 2;
break;
}
```

### #9 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: multiplication in c?

Posted 12 March 2011 - 12:03 PM

Just some other little pedantic points:

it should be int main() and you should really have a return value to the OS:return 0; while I realize your compiler works without them they really should be there as your compiler is cheating for you and letting you get away with something that is non-standard and has undefined behavior.

If your going to program in C you might as well do it right?

### #10 BenisBen

Reputation: 0
• Posts: 8
• Joined: 11-March 11

## Re: multiplication in c?

Posted 12 March 2011 - 12:36 PM

I keep getting a error back in this part

}
if( (income>8525.0) (income<=34650.00)
{
tax= income*0.15;
}

any ideas?

### #11 Salem_c

• void main'ers are DOOMED

Reputation: 1934
• Posts: 3,813
• Joined: 30-May 10

## Re: multiplication in c?

Posted 12 March 2011 - 12:41 PM

Make sure your ( ) balance, and put && between the sub-expressions.

### #12 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: multiplication in c?

Posted 12 March 2011 - 12:53 PM

did you perhaps mean :

```if( (income>8525.0 || income<=34650.00)
{
tax= income*0.15;
}
```

that is if income > 8525.0 OR income <= 34650.00

I guess it makes more sense if you meant:

```if( (income>8525.0 && income<=34650.00)
{
tax= income*0.15;
}
```

### #13 BenisBen

Reputation: 0
• Posts: 8
• Joined: 11-March 11

## Re: multiplication in c?

Posted 12 March 2011 - 02:38 PM

```#include <stdio.h>

main()

{
int cmd2 = 0;
double income= 0.0;
char input [80];
double tax = 0.0;

printf("Please enter S for single or m for married then income\n");
printf(">>");

fgets(input,80,stdin);

if(strncmp(input,"S",1) ==0)
cmd2 = 1;

if(strncmp(input,"M",1) ==0)
cmd2 = 2;
printf("%d\n",cmd2);
fgets(input,80,stdin);

sscanf(&input[0],"%lf",&income);

if( cmd2 == 1)
{
if(income <= 8525.0)
{
tax= income *0.10;
}
if( (income>8525.0)&&(income<=34650.00) )
{
tax= income *0.15;
}
if( (income>34650.00)&&(income<=83900.00) )
{
tax= income *0.25;
}
if( (income>83900.00)&&(income<=194150.00) )
{
tax= income *0.28;
}
if( (income>194150.00)&&(income<=380500.00) )
{
tax= income *0.36;
}
if(income>=380500.00)
{
tax= income *0.396
}
}

if( cmd2 == 2)
{
if(income <= 17050.00)
{
tax= income *0.10;
}
if( (income>17050.00)&&(income<=69300.00) )
{
tax= income *0.15;
}
if( (income>69300.00)&&(income<=139850.00) )
{
tax= income *0.25
}
if( (income>139850.00)&&(income<=2355500.00) )
{
tax= income *0.28;
}
if( (income>235550.00)&&(income<=380500.00) )
{
tax= income *0.36;
}
if( (income>=380500.00)
{
tax= income *0.396;
}
{

printf("%lf\n",tax);
}
```

ok I got this done, what is wrong with it? do you see any error's?
ben

### #14 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: multiplication in c?

Posted 12 March 2011 - 02:52 PM

It's a good idea to use meaningful names for your variables. It makes your code easier for a reader to understand. So instead of cmd2 I would use something like status or category.

### #15 BenisBen

Reputation: 0
• Posts: 8
• Joined: 11-March 11

## Re: multiplication in c?

Posted 12 March 2011 - 02:58 PM

ok

also do you see any errors?