# Basic Calculator in C programming

Page 1 of 1

## 7 Replies - 11230 Views - Last Post: 04 February 2011 - 07:56 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=213976&amp;s=95a76cb9078cb3ba9d5ea03418b0695b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mohd zahirin

Reputation: 0
• Posts: 5
• Joined: 04-February 11

# Basic Calculator in C programming

Posted 04 February 2011 - 12:31 AM

My assignment is to do C programming for basic calculator which includes arithmetic operation(+,-,*,/).By using if...else statement.Here is my code that I tried to do but still have mistakes.
int main()
{
int num1,num2;
printf("enter first number");
scanf("%d",&num1);
printf("enter second number");
scanf("%d",&num2);
printf("choose arithmetic operation[+,-,*,/]");
scanf(???);
so, what should I put in scanf..??& also what are the if...else statement that I should do..??

Is This A Good Question/Topic? 0

## Replies To: Basic Calculator in C programming

### #2 fromTheSprawl

• Monomania

Reputation: 514
• Posts: 2,063
• Joined: 28-December 10

## Re: Basic Calculator in C programming

Posted 04 February 2011 - 12:41 AM

What you need to do is declare a char variable and after asking what arithmetic operation is needed, that char variable should hold the symbol. For example:

```char operation;
printf("What arithmetic operation is needed?")
scanf("%c", &operation );

```

The if-else statement on the other hand will check what operation is needed to be done.

```if(operation=='+')
{
}
else if(operation=='-')
{
subtract here
}
...

```

I'm not sure if you can save the operators into a char variable, but if that doesn't work, make your printf say "Press (1) for addition..." then on the if-else statement substitute 1 for +.

That's it.

### #3 mohd zahirin

Reputation: 0
• Posts: 5
• Joined: 04-February 11

## Re: Basic Calculator in C programming

Posted 04 February 2011 - 12:51 AM

fromTheSprawl, on 04 February 2011 - 12:41 AM, said:

What you need to do is declare a char variable and after asking what arithmetic operation is needed, that char variable should hold the symbol. For example:

```char operation;
printf("What arithmetic operation is needed?")
scanf("%c", &operation );

```

The if-else statement on the other hand will check what operation is needed to be done.

```if(operation=='+')
{
}
else if(operation=='-')
{
subtract here
}
...

```

I'm not sure if you can save the operators into a char variable, but if that doesn't work, make your printf say "Press (1) for addition..." then on the if-else statement substitute 1 for +.

That's it.

why should I put { and } brackets at if...else statement before I don't finished my programming..??Usually, I put this { and this } at first & last...

### #4 fromTheSprawl

• Monomania

Reputation: 514
• Posts: 2,063
• Joined: 28-December 10

## Re: Basic Calculator in C programming

Posted 04 February 2011 - 01:04 AM

Delimiters(the brackets) are not just used to know where the program starts and ends. It can be also used with if statements, functions and for loops. It is very helpful in grouping the processes you want to do if a certain condition is met. If I try to write it like this:

```
if(a==B)/>
printf...
else if(a==c)
printf...
printf...
printf...

```

you would be confused on what lines of code will execute on what condition. By using curly braces you are given a sense of organization by knowing what lines of code are grouped.

### #5 fromTheSprawl

• Monomania

Reputation: 514
• Posts: 2,063
• Joined: 28-December 10

## Re: Basic Calculator in C programming

Posted 04 February 2011 - 01:19 AM

Anyway I tried coding it myself, and it turns out that if you use
```scanf("%c", &operation );

```

something goes wrong. Use this instead:
```scanf("%s", &operation );

```

I changed the %c with %s.

Anyway, did it work?

---

Anyone please check out this code, I took up C eons ago and when I run this program, the x value becomes 0.

What gives?
Spoiler

This post has been edited by fromTheSprawl: 04 February 2011 - 01:22 AM

### #6 mohd zahirin

Reputation: 0
• Posts: 5
• Joined: 04-February 11

## Re: Basic Calculator in C programming

Posted 04 February 2011 - 04:25 AM

fromTheSprawl, on 04 February 2011 - 01:19 AM, said:

Anyway I tried coding it myself, and it turns out that if you use
```scanf("%c", &operation );

```

something goes wrong. Use this instead:
```scanf("%s", &operation );

```

I changed the %c with %s.

Anyway, did it work?

---

Anyone please check out this code, I took up C eons ago and when I run this program, the x value becomes 0.

What gives?
Spoiler

sorry, what different actually between %c and %s..??
%c from my lecturer notes means a character..%s refer to..??
this actually my next semester's subject..I take it because it does not has pre-require subject..so, it diffuclt to me to understand this subject because I miss my 1'st class..hope, you will help me to understand it...

### #7 mohd zahirin

Reputation: 0
• Posts: 5
• Joined: 04-February 11

## Re: Basic Calculator in C programming

Posted 04 February 2011 - 11:10 AM

yeah, right..this time the program turn to be zero(0) for num2 right..??

This post has been edited by mohd zahirin: 04 February 2011 - 11:11 AM

### #8 kaye143

Reputation: 2
• Posts: 29
• Joined: 08-January 11

## Re: Basic Calculator in C programming

Posted 04 February 2011 - 07:56 PM

mohd zahirin, on 04 February 2011 - 04:25 AM, said:

fromTheSprawl, on 04 February 2011 - 01:19 AM, said:

Anyway I tried coding it myself, and it turns out that if you use
```scanf("%c", &operation );

```

something goes wrong. Use this instead:
```scanf("%s", &operation );

```

I changed the %c with %s.

Anyway, did it work?

---

Anyone please check out this code, I took up C eons ago and when I run this program, the x value becomes 0.

What gives?
Spoiler

sorry, what different actually between %c and %s..??
%c from my lecturer notes means a character..%s refer to..??
this actually my next semester's subject..I take it because it does not has pre-require subject..so, it diffuclt to me to understand this subject because I miss my 1'st class..hope, you will help me to understand it...

%s is string that accepts variable with letters and numbers