Problem 1 • Branching
Author: Er. Manoj Kumar • Computer Science Fundamentals
Find the Largest of Three Numbers
📋
Problem Statement
Given three distinct numbers A, B, and C, formulate a step-by-step algorithm and draw a flowchart to determine and print the largest number among them using minimal nested comparison branches.
📊
Variable Dictionary / Table
| Variable Name | Data Type | Role & Purpose |
|---|---|---|
| A, B, C | Float / Double | Three input numerical values to compare |
| largest | Float / Double | Stores the maximum identified value |
📝
Step-by-Step Formal Algorithm
Step 1: Start.
Step 2: Read values of A, B, and C.
Step 3: If A >= B:
If A >= C:
largest = A
Else:
largest = C
Step 4: Else (B > A):
If B >= C:
largest = B
Else:
largest = C
Step 5: Print 'The largest number is:', largest.
Step 6: Stop.
🔀
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> Input[/Read A, B, C/]
Input --> Dec1{"Is A >= B ?"}
Dec1 -- Yes --> Dec2{"Is A >= C ?"}
Dec1 -- No --> Dec3{"Is B >= C ?"}
Dec2 -- Yes --> OutA[/"Print 'A is Largest'"/]
Dec2 -- No --> OutC1[/"Print 'C is Largest'"/]
Dec3 -- Yes --> OutB[/"Print 'B is Largest'"/]
Dec3 -- No --> OutC2[/"Print 'C is Largest'"/]
OutA --> Stop([Stop])
OutC1 --> Stop
OutB --> Stop
OutC2 --> Stop
🖥️
Flowchart (ANSI Terminal ASCII Chart)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read A, B, C |
+-----------------+
|
v
/ \
/ A >= B ? \
\ /
YES/ \NO
v v
/ \ / \
/ A >= C?\ / B >= C?\
\ / \ /
Y| |N Y| |N
v v v v
[A] [C] [B] [C]
| | | |
+------+-----+-----+
|
v
+-----------------+
| [ STOP ] |
+-----------------+
🧪
Dry-Run Execution Trace Table
| Execution Step / Case | Variables State 1 | Variables State 2 | Condition Evaluation | Output / Result |
|---|---|---|---|---|
| Test Case 1 | A=25, B=14, C=9 | A >= B (True), A >= C (True) | Branch Dec1 -> Dec2 | Largest = 25 (A) |
| Test Case 2 | A=10, B=80, C=45 | A >= B (False), B >= C (True) | Branch Dec1 -> Dec3 | Largest = 80 (B) |
| Test Case 3 | A=5, B=12, C=99 | A >= B (False), B >= C (False) | Branch Dec1 -> Dec3 -> C | Largest = 99 (C) |
⚡
Interactive C++ Compiler & Runner
Live In-BrowserEdit the code directly with the Edit Code button, provide custom inputs, and press Run Code to compile and execute in the live terminal console below!
📄
problem_1.cpp
GCC 13.2 • C++20
#include <iostream>
using namespace std;
int main() {
double a, b, c;
cout << "Enter three numbers (A B C): ";
if (!(cin >> a >> b >> c)) {
cout << "Invalid input! Please enter valid numeric values.\n";
return 1;
}
double largest;
// Step-by-step nested branching
if (a >= b) {
if (a >= c) {
largest = a;
} else {
largest = c;
}
} else {
if (b >= c) {
largest = b;
} else {
largest = c;
}
}
cout << "\n========================================\n";
cout << "Input values: A = " << a << ", B = " << b << ", C = " << c << "\n";
cout << "Result : Largest Number is " << largest << "\n";
cout << "========================================\n";
return 0;
}
⌨️ Standard Input (cin):
Presets:
💻 Terminal Output
• Ready to run
Input: 25.5 89.2 44.0 Output: ======================================== Input values: A = 25.5, B = 89.2, C = 44 Result : Largest Number is 89.2 ========================================
Developer & Exam Key Takeaway
Nested conditional logic guarantees finding the maximum in exactly 2 comparisons, which is optimal for 3 elements (log2(3!) ≈ 2.58).