100% prog to design a simple calculator with user defined function
#include <stdio.h>
float calculate(int, int,int);//function prototyping
void main()
{
int p,q,choice;
float uttar;
printf("\n enter two integers:");
scanf("%d%d",&p,&q);
printf("\n Enter 1 for add, 2 for subtract, 3 for multiply and 4 for division: ");
scanf("%d",&choice);
calculate(p,q,choice);
printf("\n back in main. ");
}
float calculate(a,b,c) //function definition
{
float z;
switch(c)
{
case 1:
z=a+b;
break;
case 2:
z=a-b;
break;
case 3:
z=a*b;
break;
case 4:
float calculate(int, int,int);//function prototyping
void main()
{
int p,q,choice;
float uttar;
printf("\n enter two integers:");
scanf("%d%d",&p,&q);
printf("\n Enter 1 for add, 2 for subtract, 3 for multiply and 4 for division: ");
scanf("%d",&choice);
calculate(p,q,choice);
printf("\n back in main. ");
}
float calculate(a,b,c) //function definition
{
float z;
switch(c)
{
case 1:
z=a+b;
break;
case 2:
z=a-b;
break;
case 3:
z=a*b;
break;
case 4:
if (b!=0)
{
z=(float) a/b;
}
{
z=(float) a/b;
}
else
{
printf("\n avoid dividing with 0");
}
}
printf("\n math operation of given numbers %d & %d =%f as printed in the called function",a,b,z);
return(z);
}
}
printf("\n math operation of given numbers %d & %d =%f as printed in the called function",a,b,z);
return(z);
}
ఔట్పుట్ తెరమీద
enter two integers:4 5
Enter 1 for add, 2 for subtract, 3 for multiply and 4 for division: 4
math operation of given numbers 4 & 5 =0.800000 as printed in the called function
back in main.
ఔట్పుట్ తెరమీద మరోసారి
enter two integers:2 7
Enter 1 for add, 2 for subtract, 3 for multiply and 4 for division: 3
math operation of given numbers 2 & 7 =14.000000 as printed in the called function
back in main.
కామెంట్లు
కామెంట్ను పోస్ట్ చేయండి
దయచేసి మీ సలహాలను సూచనలను స్పష్టంగా పేర్కొనగలరు. plz see that ur comments are 'acceptable' in a value based society.