How to get the average of 2 Numbers using function

Friday, February 13, 2009 · Posted in

If you have some problem in C++ or either in C adding numbers and getting its average by using a function and then I have some codes here that can really help you a lot.


Output:

Code:





#include<......>

#include<......>
#include<......>
#define p printf

float ave(float x, float y);
int add(int x, int y)
{
int sum=0;

sum=x+y;

return sum;
}
int main()
{
menu:
system("cls");
int a,b;

p("Enter Num1: ");
scanf("%d", &a);
p("Enter Num2: ");
scanf("%d", &b);
p("\n\nThe sum of the two integers is %d.",add(a,b));
p("\n\nThe average of the two integers is %.1f.",ave(a,b));
p("\n\n");
system("pause");
char choice;
p("\nDo you want to repeat the process?[y for yes][n for no]: ");
scanf("%s", &choice);


if(choice=='y'||choice=='Y')

goto menu;

else if(choice=='n'||choice=='N')
exit(0);


getch();

}

float ave(float x, float y)
{
float ave,sum;
sum=x+y;
ave=sum/2;

return ave;

}

Powered by Blogger.