In: Computer Science
write a C++ program that uses function swap (a,b) to enter two real numbers from the keyboard and uses function min_max(a,b) to return a to be the smaller of the two numbers entered always.
Program in C++:
#include <iostream>
using namespace std;
void swap(int *a,int *b){// swap function
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int min_max(int a,int b){
if(a>b){// if a is greater than b swap value of a and b, and return a(Smaller value).
swap(&a,&b);
return a;// return a
}
else{// if a is Smaller then b return a
return a;// return a
}
}
int main()
{
int a,b,min;
cout<<"Enter the value of a"<<endl;
cin>>a;// input a
cout<<"Enter the value of b"<<endl;
cin>>b; // input b
min=min_max(a,b);// call min_max function
cout<<"Smaller value is : "<<min;// print min value
return 0;
}
Output 1:
Output 2:
Program Screenshot:
Summary:
Program print smaller of the two numbers and function min_max(a,b) always return value of a. So when value of a is greater then b then we swap the values of a and b using swap function.