Call us at 0700-922-6559 or Click here

Write a function to find the largest of three numbers.

#include <iostream>

using namespace std;

// Function to find the largest of three numbers

int largest(int num1, int num2, int num3) {

 // Assume the first number is the largest initially

 int largest_num = num1;

 // Compare with the second number

 if (num2 > largest_num) {

   largest_num = num2;  // Update if num2 is larger


 // Compare with the third number

 if (num3 > largest_num) {

   largest_num = num3;  // Update if num3 is larger


 // Return the largest number found

 return largest_num;


int main() {

 // Declare variables with Indian names

 int Ram_score = 85, Sita_score = 92, Ramesh_score = 88;

 // Find the largest score using the function

 int highest_score = largest(Ram_score, Sita_score, Ramesh_score);

 cout << “The highest score among Ram, Sita, and Ramesh is: ” << highest_score << endl;

 return 0;



The highest score among Ram, Sita, and Ramesh is: 92

Here’s a step-by-step explanation of the code:

1. Header and Namespace:

  • #include <iostream>: Includes the iostream header for input/output operations.
  • using namespace std;: Brings the std namespace into scope for convenient use of elements like cin, cout, and endl.

2. Largest Function:

  • int largest(int num1, int num2, int num3): Declares a function named largest that takes three integer arguments and returns an integer (the largest number).
  • int largest_num = num1;: Assumes num1 is the largest initially and stores it in largest_num.
  • if (num2 > largest_num) { … }: Compares num2 with largest_num. If num2 is larger, updates largest_num with num2.
  • if (num3 > largest_num) { … }: Similarly, compares num3 with largest_num and updates if num3 is larger.
  • return largest_num;: Returns the final value of largest_num, which holds the largest number found.

3. Main Function:

  • int main(): The main function where program execution begins.
  • int Ram_score = 85, Sita_score = 92, Ramesh_score = 88;: Declares and initializes three integer variables with names.
  • int highest_score = largest(Ram_score, Sita_score, Ramesh_score);: Calls the largest function, passing the three scores as arguments, and stores the returned largest value in highest_score.
  • cout << “The highest score…”;: Prints the highest score to the console.
  • return 0;: Indicates successful program termination.

Leave a Reply

Your email address will not be published. Required fields are marked *