C++ program to find greatest of four numbers28 Aug 2024 | 3 min read In this tutorial, we will write a C++ program to find the greatest of four numbers. For example a = 10, b = 50, c = 20, d = 25 The greatest number is b 50 a = 35, b = 50, c = 99, d = 2 The greatest number is c 99 Approach 1 The approach is the traditional way of searching for the greatest among four numbers. The if condition checks whether a is greater and then use if-else to check for b, another if-else to check for c, and the last else to print d as the greatest. Algorithm
C++ Code Output a=10 b=50 c=20 d=25 b is greatest a=35 b=50 c=99 d=2 c is greatest Approach 2 This approach uses the inbuilt max function. Here is the syntax of max function template constexpr const T& max (const T& a, const T& b); Here, a and b are the numbers to be compared. Return: Larger of the two values. For example std :: max(2,5) will return 5 So, to find out the maximum of 4 numbers, we can use chaining of a max function as follows - int x = max(a, max(b, max(c, d))); C++ code Output a=10 b=50 c=20 d=25 b is greatest a=35 b=50 c=99 d=2 c is greatest Next Topic# |
Rethrowing an Exception in C++
An Exception caught can be caught within a try block and handled using one or more Catch Blocks. A few cases exist where the exception is to be caught using a single Catch block and rethrow it, as the Catch block at the top of the...
4 min read
Socket Programming in C/C++
In today's world, computer networks play an important role in the data transfer field. It is a subject that each programmer should know of. Under the computer network, socket programming is one of the most important topics in the programming world. In this topic, we are...
11 min read
Conversion Constructor in C++
Introduction Constructors are unique member functions in C++ that initialize objects belonging to a class. When an object is created, they are automatically called. Conversion constructors, commonly referred to as single-argument constructors or converting constructors, are an effective C++ feature that allow for implicit conversions between various...
3 min read
Kruskal's Algorithm in C++
kruskal's algorithm in C++ Trees are essential in the field of computer science and data structures for effectively organizing and managing data. In real-world applications, trees are hierarchical structures that are used to depict a variety of connections and hierarchies. They are the cornerstone of computer science...
11 min read
Binary Heap in C++
Introduction: Binary heap is a fundamental data structure commonly used in computer science for efficient implementation of priority queues. It is a complete binary tree where each node has a value smaller than or equal to its children if it's a min-heap or greater if it's a...
6 min read
C++ program for run Length Encoding and Decoding
Run-length encoding (RLE) is a straightforward method of data compression that substitutes a single element followed by a count of how many times it repeats for a series of identical elements (such as letters or numbers). There are the following steps: 1. Encoding The input data is scanned...
4 min read
Maximum circular subarray sum in C++
When tackling challenges related to maximum subarray sums, Kadane's Algorithm frequently emerges as the preferred solution. In this blog post, we will delve into an intriguing variation of this problem and determe the maximum circular subarray sum. We will explore the underlying concept, furnish a thorough...
4 min read
Difference between New and Delete operator in C++
Difference between new and delete Operator in C++ In the C++ programming language, the new and delete operators are mainly utilized for dynamic memory allocation and deallocation. They enable us to dynamically allocate and free the memory, which means that we can create objects whose size...
6 min read
Add Two Matrix in C++
In C++, a matrix is a two-dimensional array consisting of rows and columns of elements. It can be created using various methods, such as using nested for loops or by dynamically allocating memory. One way to create a matrix in C++ is to declare a...
4 min read
std::unary_negate() in C++
The std::unary_negate() is a wrapper function object. It returns the opposite of the unary condition it contains. A wrapper function is a procedure in a software library or computer program that is designed to invoke a second subroutine or a system call with little or no...
3 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India