In: Computer Science
Write a function, which accept three integer values as arguments find the largest of three and then return the largest value to main program. Write a main program which will call the function by passing three integer values and print the value returned by the function.?
1. First we create a function that takes the value as a parameter.
void findLargestNumber(int, int, int); // Prototype
void findLargestNumber(int num1, int num2, int num3)
{
}
2. After creating the function now we implement the logic to compare given value/integers. The Logic is given below
if (num1 == 0 || num2 == 0 || num3 == 0)
{
cout << "No one must be zero";
}
else if (num1 > num2 && num1 > num3)
{
cout << num1 << " is largest number";
}
else if (num2 > num1 && num2 > num3)
{
cout << num2 << " is largest number";
}
else if (num3 > num1 && num3 > num2)
{
cout << num3 << " is largest number";
}
3. Now we implement this into our function.
void findLargestNumber(int num1, int num2, int num3)
{
if (num1 == 0 || num2 == 0 || num3 == 0)
{
cout << "No one must be zero";
}
else if (num1 > num2 && num1 > num3)
{
cout << num1 << " is largest number";
}
else if (num2 > num1 && num2 > num3)
{
cout << num2 << " is largest number";
}
else if (num3 > num1 && num3 > num2)
{
cout << num3 << " is largest number";
}
}
4. After that now we call this function in our main program and pass the value through the user.
// Main Function
int main()
{
int firstNumber, secondNumber, thirdNumber;
cout << "Enter First Number: ";
cin >> firstNumber;
cout << "Enter Second Number: ";
cin >> secondNumber;
cout << "Enter Third Number: ";
cin >> thirdNumber;
findLargestNumber(firstNumber, secondNumber, thirdNumber);
getch();
}
// Our Logical Function which compare integers
void findLargestNumber(int num1, int num2, int num3)
{
if (num1 == 0 || num2 == 0 || num3 == 0)
{
cout << "No one must be zero";
}
else if (num1 > num2 && num1 > num3)
{
cout << num1 << " is largest number";
}
else if (num2 > num1 && num2 > num3)
{
cout << num2 << " is largest number";
}
else if (num3 > num1 && num3 > num2)
{
cout << num3 << " is largest number";
}
}
5. That's all and the final program looks like this.
#include <iostream>
#include <conio.h>
using namespace std;
void findLargestNumber(int, int, int); // Prototype
// Main Function
int main()
{
int firstNumber, secondNumber, thirdNumber;
cout << "Enter First Number: ";
cin >> firstNumber;
cout << "Enter Second Number: ";
cin >> secondNumber;
cout << "Enter Third Number: ";
cin >> thirdNumber;
findLargestNumber(firstNumber, secondNumber, thirdNumber);
getch();
}
// Our logical function
void findLargestNumber(int num1, int num2, int num3)
{
if (num1 == 0 || num2 == 0 || num3 == 0)
{
cout << "No one must be zero";
}
else if (num1 > num2 && num1 > num3)
{
cout << num1 << " is largest number";
}
else if (num2 > num1 && num2 > num3)
{
cout << num2 << " is largest number";
}
else if (num3 > num1 && num3 > num2)
{
cout << num3 << " is largest number";
}
}
#include <iostream>
#include <conio.h>
using namespace std;
void findLargestNumber(int, int, int);
int main()
{
int firstNumber, secondNumber, thirdNumber;
cout << "Enter First Number: ";
cin >> firstNumber;
cout << "Enter Second Number: ";
cin >> secondNumber;
cout << "Enter Third Number: ";
cin >> thirdNumber;
findLargestNumber(firstNumber, secondNumber, thirdNumber);
getch();
}
void findLargestNumber(int num1, int num2, int num3)
{
if (num1 == 0 || num2 == 0 || num3 == 0)
{
cout << "No one must be zero";
}
else if (num1 > num2 && num1 > num3)
{
cout << num1 << " is largest number";
}
else if (num2 > num1 && num2 > num3)
{
cout << num2 << " is largest number";
}
else if (num3 > num1 && num3 > num2)
{
cout << num3 << " is largest number";
}
}