multiplication in c?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2205 Views - Last Post: 13 March 2011 - 12:49 PM Rate Topic: -----

#1 BenisBen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • 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;
Was This Post Helpful? 1
  • +
  • -

#3 BenisBen  Icon User is offline

  • New D.I.C Head

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

Re: multiplication in c?

Posted 11 March 2011 - 08:23 PM

View Postr.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,
Was This Post Helpful? 0
  • +
  • -

#4 BenisBen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • 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

Was This Post Helpful? 4
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • 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?
Was This Post Helpful? 2
  • +
  • -

#7 BenisBen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • 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;
}

Was This Post Helpful? 1
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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?
Was This Post Helpful? 2
  • +
  • -

#10 BenisBen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#11 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1743
  • View blog
  • Posts: 3,360
  • 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.
Was This Post Helpful? 1
  • +
  • -

#12 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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;
}

Was This Post Helpful? 1
  • +
  • -

#13 BenisBen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#14 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#15 BenisBen  Icon User is offline

  • New D.I.C Head

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

Re: multiplication in c?

Posted 12 March 2011 - 02:58 PM

ok 0_0

also do you see any errors?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2