पृष्ठ

Friday, July 15, 2011

A program for custom function : calculator

See below code i have created a calculator with the use of my own custom functions.

Here you can learn : how to create custom functions in C?



C - Code Example


#include<stdio.h>
#include<conio.h>

void sum(int,int); // Function prototype declaration.
void product(int,int); // Function prototype declaration.
void substraction(int,int); // Function prototype declaration.
void divide(int,int); // Function prototype declaration.

void sum(int x,int y) // Definition function.
{
int z;
z=x+y;
printf("%d + %d = %d\n", x,y,z);
}

void product(int x,int y) // Definition function.
{
int z;
z=x*y;
printf("%d x %d = %d\n", x,y,z);
}

void substraction(int x,int y) // Definition function.
{
int z;

z=x-y;
printf("%d - %d = %d\n", x,y,z);
}

void divide(int x,int y) // Definition function.
{
float z;
z=x/y;
printf("%d / %d = %f\n", x,y,z);
}


void main()
{
int a,b,i;

clrscr();

printf("\n\n***********Calculator : Type number***********\n\n");
printf("Type first number : ");
scanf("%d",&a);
printf("\nType second number : ");
scanf("%d",&b);

clrscr();
printf("\n\n***********Calculator : Choose operation***********\n\n");
printf("Select an option for perform the calculation task for these two numbers %d and %d\n\n",a,b);

printf("Choose option \n\n");
printf("for \n'+' : (1)'\n'x' : (2)\n'-' : (3)\n'/' : (4) : ");
printf("\n\nType here : ");
scanf("%d",&i);

clrscr();
printf("\n\n***********Calculator : Result (%d and %d)***********\n\n",a,b);

printf("Results is : ");


switch(i)
{

case 1 : sum(a,b);break; // Calling function.
case 2 : product(a,b);break; // Calling function.
case 3 : substraction(a,b);break; // Calling function.
case 4 : divide(a,b);break; // Calling function.
default : printf("\nYou put invalid input!!!"); // Calling function.
break;
}

printf("\n\n***********Thanks for using it***********\n\n");
printf("\n\n\n\n\t\t\t\tCreated by : Pawan Mall");
getch();
}
Download file : Calculator

0 comments:

Post a Comment