# CALCULATOR PROGRAM USING FUNCTIONS!

Page 1 of 1

## 4 Replies - 47905 Views - Last Post: 25 September 2008 - 02:58 AMRate 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=64991&amp;s=1f644a5c3decff70a5ec04f23bedc039&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mariji

Reputation: 0
• Posts: 12
• Joined: 24-September 08

# CALCULATOR PROGRAM USING FUNCTIONS!

Posted 24 September 2008 - 05:11 PM

We are asked to make a calculator program using C that begins with like this:

first sample output:

=================================

Welcome to the calculator program

2- Subtraction
3- Multiplication
4- Division
5- Power
6- Exit

What operation would you like to perform? _

=================================

then.. after choosing the operation, the screen will be cleared.
If the person choose 1 (addition), a note will appear.

second sample output:

=================================

How many numbers do you like to add?_ // assuming that you type 3

_
_
_

The sum is: _

=================================

so here's my sample code for addition and subtraction only using FUNCTIONS but it wont work!!! T_T

It has many errors..! T_T

and i dont know how to use FUNCTIONS!

what's wrong with this code?

```#include<stdio.h>

int sub(no);

main()

{
int op, result;

printf(" 2- subtraction");
printf("\n\n");
printf("What operation would you like to use? "); scanf("%d", op);

switch(op)
{
printf("Sum:%d", result);
break;
case 2: result=sub(no);
printf("Difference:%d", result);
break;
}

{
int no, num, knt, sum=0;

printf("How many integers do you like to use?"); scanf("%d", &no);
printf("\n\n");

for(knt=1; knt<=no; knt++)
{
printf("\t\t");
scanf("%d", &num);
sum=sum+num;
}
}

int sub(no)
{
int no, num, knt, diff=0;

printf("You've choosen SUBTRACTION!");
printf("How many integers do you like to use?"); scanf("%d", &no);
printf("\n\n");

for(knt=1; knt<=no; knt++)
{
printf("\t\t");
scanf("%d", &num);
diff=diff-num;
}
}

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: CALCULATOR PROGRAM USING FUNCTIONS!

### #2 csmanoj

Reputation: 11
• Posts: 150
• Joined: 06-August 07

## Re: CALCULATOR PROGRAM USING FUNCTIONS!

Posted 25 September 2008 - 01:15 AM

Remove 'no' from those brackets. Don't write add(no) just write add() because you don't pass any parameters to the add function. Even if you passed parameters you have to use the parameter type too, in the function declaration. Similarly for subtract() too.

Don't write just 'main()'. Write 'int main()'. You must specify the return type.

And both add() and subtract() must return the results using 'return' statement.

You have put the function definitions of add() and subtract() inside the main() function definition. Move them out of it. (i.e., move the last two lines to the line after the switch block.

Also when you're doing scanf() after the menu, you left the '&' symbols.

The printf() statements that ask you to enter the numbers is incomplete.

And always initialize variables before you use them.

I'd advise you study again about C/C++ functions from a good book.

(That's only about the syntax. The semantics of the subtract function is a bomb.)

### #3 Soura

Reputation: 0
• Posts: 27
• Joined: 24-September 08

## Re: CALCULATOR PROGRAM USING FUNCTIONS!

Posted 25 September 2008 - 02:51 AM

[quote name='mariji' date='24 Sep, 2008 - 05:11 PM' post='423450']
The program is(without power func.)-
```#include<stdio.h>
#include<conio.h>
#include<iostream.h>

main()
{
int a, b, r, n;
printf ("MENU: \n Press '1' For ''Addition'' \n Press '2' For ''Substraction''\n Press '3' For ''Multiplication''\n Press '4' For ''divition'' \n ");
scanf("%d", &n);
if(n==1 || n==2 || n==3 || n==4)
{
printf(" Enter the first no. ");
scanf("%d", &a);
printf(" Enter the second no. ");
scanf("%d", &B)/>;
switch(n)
{
case 1:
r=a+b;
break;
case 2:
r=a-b;
break;
case 3:
r=a*b;
break;
case 4:
r=a/b;
}
printf (" The Result is = %d", r);

}
else
{
printf ("Your CHOICE is WRONG, Please Enter the RIGHT CHOICE.\n Press any key to Exit.");
}

getch();
}
/*end of main*/
```

### #4 gabehabe

• GabehabeSwamp

Reputation: 1398
• Posts: 10,965
• Joined: 06-February 08

## Re: CALCULATOR PROGRAM USING FUNCTIONS!

Posted 25 September 2008 - 02:55 AM

Why doesn't anyone use the code tags nowadays?

### #5 Soura

Reputation: 0
• Posts: 27
• Joined: 24-September 08

## Re: CALCULATOR PROGRAM USING FUNCTIONS!

Posted 25 September 2008 - 02:58 AM

gabehabe, on 25 Sep, 2008 - 02:55 AM, said:

Why doesn't anyone use the code tags nowadays?