Write a C++ code to find largest among 4 numbers using the concept call by reference.
Question
Write a C++ code to find largest among 4 numbers using the concept call by reference.
Solution
Sure, here is a simple C++ code that uses the concept of call by reference to find the largest among 4 numbers:
#include<iostream>
using namespace std;
void findLargest(int &a, int &b, int &c, int &d, int &largest) {
largest = a;
if(b > largest)
largest = b;
if(c > largest)
largest = c;
if(d > largest)
largest = d;
}
int main() {
int num1, num2, num3, num4, largest;
cout << "Enter four numbers: ";
cin >> num1 >> num2 >> num3 >> num4;
findLargest(num1, num2, num3, num4, largest);
cout << "Largest number is: " << largest << endl;
return 0;
}
In this code, we have a function findLargest
that takes four numbers and a reference to a variable largest
as arguments. It compares each number with largest
and if the number is greater than largest
, it updates largest
. After the function call in main
, the largest number is stored in the largest
variable and is printed out.
Similar Questions
Write a C++ code to find largest among 4 numbers using the concept call by reference.
Develop a Java program to identify the largest among four numbers (32, 29,34, 21) using appropriate control statement and operators.
Write a function int max_of_four(int a, int b, int c, int d) which reads four arguments and returns the greatest of them.
Write a program to display a menu driven facility to find the greatest of two numbers,three numbers and four numbers
Construct a Pseudocode to take three numbers and print largest and smallest among them.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.