Software Development Exam  >  Software Development Notes  >  Basics of C++  >  Code: Find Largest of 3 Numbers

Code: Find Largest of 3 Numbers | Basics of C++ - Software Development PDF Download

Introduction

In this article, we will learn how to write a C++ program to find the largest of three numbers. This program is a basic programming exercise that is commonly asked in beginner-level coding interviews. We will explain each step of the code in an easy-to-understand manner.

Understanding the Problem

Before writing the program, we must first understand the problem. We need to find the largest number among three given numbers. Let's assume that we have three variables - A, B, and C - and we want to find the largest number among them.

Approach

To solve the problem, we can take the following approach:

  • Read the input values of A, B, and C from the user.
  • Compare A and B, and store the greater number in a temporary variable.
  • Compare the temporary variable with C, and store the greater number in another temporary variable.
  • The final temporary variable will contain the largest number among A, B, and C.
  • Print the final temporary variable.

Code Explanation

Now let's write the code to find the largest of three numbers in C++.

#include <iostream>

using namespace std;

int main()

{

    int A, B, C, temp1, temp2, largest;

    cout << "Enter three numbers: ";

    cin >> A >> B >> C;

    temp1 = (A > B) ? A : B;

    temp2 = (temp1 > C) ? temp1 : C;

    largest = temp2;

    cout << "Largest number is " << largest << endl;

    return 0;

}

Let's break down the code step-by-step:

  • We start by including the iostream library, which is needed for taking input and output from the console.
  • We use the "using namespace std" statement, which is a standard C++ statement that allows us to use cout and cin without the need for specifying std:: before them.
  • We declare five variables: A, B, C, temp1, temp2, and largest. A, B, and C will store the input numbers, and temp1, temp2, and largest are temporary variables used for comparison and storing the final result.
  • We ask the user to enter three numbers using cout and then take input from the console using cin.
  • We compare A and B using the ternary operator (?:) and store the greater number in temp1.
  • We compare temp1 and C using the ternary operator and store the greater number in temp2.
  • We assign the value of temp2 to the largest variable.
  • Finally, we print the largest number using cout.

Example:
Let's say we want to find the largest of the following three numbers: 7, 15, and 3. We will run the above code and input the values.

Enter three numbers: 7 15 3

Largest number is 15

As we can see, the output is 15, which is the largest number among 7, 15, and 3.

Conclusion

In this article, we have learned how to write a C++ program to find the largest of three numbers. We have explained each step of the code in detail and provided an example to help you understand the concept better. By following the approach we have provided, you can easily solve similar problems in the future.

The document Code: Find Largest of 3 Numbers | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development
70 videos|45 docs|15 tests

Top Courses for Software Development

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Objective type Questions

,

Important questions

,

Code: Find Largest of 3 Numbers | Basics of C++ - Software Development

,

practice quizzes

,

study material

,

shortcuts and tricks

,

Exam

,

Sample Paper

,

video lectures

,

Viva Questions

,

Code: Find Largest of 3 Numbers | Basics of C++ - Software Development

,

Previous Year Questions with Solutions

,

past year papers

,

ppt

,

mock tests for examination

,

Summary

,

Extra Questions

,

Free

,

Code: Find Largest of 3 Numbers | Basics of C++ - Software Development

,

MCQs

,

Semester Notes

,

pdf

;