Sunday, December 27, 2009

C programming function which add,substract,multiply and make average?

Help me to resolve this questionC programming function which add,substract,multiply and make average?
include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





void calculator(int num1,int num2);


void main()


{


int n1,n2;


clrscr();


printf(';\n enter the first number:-';);


scanf(';%d';,%26amp;n1);


printf(';\n enter the second number:-';);


scanf(';%d';,%26amp;n2);


printf(';\n\n****************************鈥?br>

calculator( n1,n2 );





getch( );


}





void calculator(int num1,int num2)


{


int opt,ans;


printf(';\n\n enter the appropriate code:-';);


printf(';\n press 1 for addition';);


printf(';\n press 2 for subraction';);


printf(';\n press 3 for multiplication';);


printf(';\n press 4 for average';);


printf(';\n press 5 for exit';);





scanf(';%d';,%26amp;opt);





switch(opt)


{


case 1:


ans=num1+num2;


printf(';\n the answer is :- %d';,ans);


break;


case 2:


ans=num1-num2;


printf(';\n the answer is :- %d';,ans);


break;


case 3:


ans=num1*num2;


printf(';\n the answer is :- %d';,ans);


break;


case 4:


ans=(num1+num2) / 2 ;


printf(';\n the answer is :- %d';,ans);


break;


case 5:


printf(';\nthank you';);


break;


default:


printf(';\n you have entered wrong choice';);


break;





}


}C programming function which add,substract,multiply and make average?
Have your main function that calls on other functions that do your other operations:


Here is the pseudo code:





main(){


float a,b,c,d,e,f,g,h;


add(a,b);


sub(a,b);


mult(a,b);


avg(a,b,c,d,e,f,g,h);


}





add(float x, float y){


return x + y;


}





...





avg(...){


float total;


total = a+b+c+d+...+h;


total = total / 8 // number of variables


return total;


}





Hope this helps.
If you be more specific with your requirement, it would be easier to answer your question. From what you have provided so far, it seems like a simple code to do ';a+b';, ';a-b';, ';a*b'; and so on. Why don't you attempt something first and post it here if you have errors?
I think you need to specify more of what you are looking to do, are you wanting to do this with an array or are you just wanting to create a function that allows you to do that with a user's input?
int a,b;


printf(';enter two numbers';);


scanf(';%d %d';,a,b);


printf(';%d';,a+b);








similarly you can replace all arithmetic operations

No comments:

Post a Comment