This comprehensive algorithm suite, interactive flowcharts, and C++ compiler environment are password-protected. Enter your authorized master password to unlock access.
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.
💡 Answer hidden • Solve on your own before checking!
Variable Table
Variable
Data Type
Purpose / Role
A
Numeric
First input number
B
Numeric
Second input number
C
Numeric
Third input number
Step-by-Step Algorithm
Step 1: Start.
Step 2: Read values of A, B, and C.
Step 3: Compare A and B:
If A > B:
Check if A > C: If True, Print 'A is Largest'; Else Print 'C is Largest'.
Else (B >= A):
Check if B > C: If True, Print 'B is Largest'; Else Print 'C is Largest'.
Step 4: 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 (Visual ASCII Diagram)
+------------------+
| [ START ] |
+------------------+
|
v
+------------------+
| Read A, B, C |
+------------------+
|
v
/ \
/ A > B? \
/ \
YES NO
/ \
v v
/ \ / \
/ A > C? \ / B > C? \
/ \ / \
YES NO YES NO
| | | |
v v v v
[Print A] [Print C] [Print B] [Print C]
\ | | /
\-----------+--------+-----------/
|
v
+------------------+
| [ STOP ] |
+------------------+
Dry-Run Trace Table
Execution Step
Var State 1
Var State 2
Evaluation
Output / Result
Run 1
45
12
45 > 12 (True)
Print 'C is Largest' (89)
Run 2
10
95
10 > 95 (False)
Print 'B is Largest' (95)
💡
Developer Key Takeaway
Always use nested decision trees rather than multiple independent if-statements to minimize redundant comparisons from 3 comparisons to at most 2 comparisons.
C++ 17 / 20
C++ Program Implementation
#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;
}
Sample Terminal Output:
Input:
25.5 89.2 44.0
Output:
========================================
Input values: A = 25.5, B = 89.2, C = 44
Result : Largest Number is 89.2
========================================
Given a positive integer N, design an algorithm and flowchart to determine whether N is a Prime number (divisible only by 1 and itself) or a Composite number.
💡 Answer hidden • Solve on your own before checking!
Variable Table
Variable
Data Type
Purpose / Role
N
Integer
The input number to test
i
Integer
Loop divisor counter starting from 2 up to sqrt(N)
isPrime
Boolean Flag
1 if prime, 0 if composite
Step-by-Step Algorithm
Step 1: Start.
Step 2: Read positive integer N.
Step 3: If N <= 1, Print 'Neither Prime nor Composite' and go to Step 8.
Step 4: If N == 2, Print '2 is a Prime Number' and go to Step 8.
Step 5: Initialize divisor counter i = 2.
Step 6: While (i * i <= N):
If (N % i == 0):
Print 'N is Composite (Divisible by i)'.
Go to Step 8 (Early Exit).
Else:
Increment i = i + 1.
Step 7: If loop finishes without finding divisor, Print 'N is a Prime Number'.
Step 8: Stop.
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> In[/Read integer N/]
In --> Chk1{N <= 1 ?}
Chk1 -- Yes --> Out1[/Print 'Neither'/] --> Stop([Stop])
Chk1 -- No --> Chk2{N == 2 ?}
Chk2 -- Yes --> Out2[/Print '2 is Prime'/] --> Stop
Chk2 -- No --> Init[Set i = 2]
Init --> LoopCond{i * i <= N ?}
LoopCond -- Yes --> DivTest{N % i == 0 ?}
DivTest -- Yes --> OutComp[/Print 'Composite'/] --> Stop
DivTest -- No --> Inc[i = i + 1] --> LoopCond
LoopCond -- No --> OutPrime[/Print 'Prime'/] --> Stop
Flowchart (Visual ASCII Diagram)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read N |
+-----------------+
|
v
/ \
/ N <= 1? \ ---YES--> [Print Neither] --> [STOP]
\ /
| NO
v
/ \
/ N == 2? \ ---YES--> [Print Prime] --> [STOP]
\ /
| NO
v
+-----------------+
| i = 2 |
+-----------------+
|
+------->|
| v
| / \
| / i*i<=N? \ ---NO---> [Print Prime] --> [STOP]
| \ /
| | YES
| v
| / \
| / N%i==0? \ ---YES--> [Print Composite]-> [STOP]
| \ /
| | NO
| v
| +---------+
+---| i = i+1 |
+---------+
Dry-Run Trace Table
Execution Step
Var State 1
Var State 2
Evaluation
Output / Result
N = 29
i=2
29%2 != 0
i=4, 29%4!=0
Loop exits at i=6 (6*6=36>29) -> '29 is Prime'
N = 49
i=2..6
No divisor
49%7 == 0 (True)
Print '49 is Composite'
💡
Developer Key Takeaway
Checking up to sqrt(N) reduces loop operations from O(N) to O(sqrt(N)), turning millions of operations into a few hundred.
C++ 17 / 20
C++ Program Implementation
#include <iostream>
using namespace std;
int main() {
long long n;
cout << "Enter a positive integer: ";
if (!(cin >> n)) {
cout << "Invalid input! Please enter a valid integer.\n";
return 1;
}
if (n <= 1) {
cout << n << " is NEITHER prime nor composite (by definition).\n";
return 0;
}
if (n == 2 || n == 3) {
cout << n << " is a PRIME number.\n";
return 0;
}
if (n % 2 == 0) {
cout << n << " is a COMPOSITE number (divisible by 2).\n";
return 0;
}
bool isPrime = true;
for (long long i = 3; i * i <= n; i += 2) {
if (n % i == 0) {
isPrime = false;
cout << n << " is a COMPOSITE number (divisible by " << i << ").\n";
break;
}
}
if (isPrime) {
cout << n << " is a PRIME number.\n";
}
return 0;
}
Formulate an algorithm and flowchart to calculate the factorial of a given non-negative integer N, defined as N! = 1 * 2 * 3 * ... * N, with 0! = 1.
💡 Answer hidden • Solve on your own before checking!
Variable Table
Variable
Data Type
Purpose / Role
N
Integer
Non-negative input number
Fact
Integer
Accumulator product, initialized to 1
i
Integer
Loop multiplier counter, initialized to 1
Step-by-Step Algorithm
Step 1: Start.
Step 2: Read integer N.
Step 3: If N < 0, Print 'Factorial undefined for negative numbers' and go to Step 8.
Step 4: Initialize Fact = 1 and loop counter i = 1.
Step 5: While (i <= N):
Fact = Fact * i.
i = i + 1.
Step 6: Print 'Factorial of N is Fact'.
Step 7: Stop.
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> In[/Read N/]
In --> Valid{N < 0 ?}
Valid -- Yes --> Err[/Print 'Invalid'/] --> Stop([Stop])
Valid -- No --> Init[Fact = 1, i = 1]
Init --> Cond{i <= N ?}
Cond -- Yes --> Calc[Fact = Fact * i i = i + 1] --> Cond
Cond -- No --> Out[/Print Fact/] --> Stop
Flowchart (Visual ASCII Diagram)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read N |
+-----------------+
|
v
/ \
/ N < 0? \ ---YES--> [Print Error] --> [STOP]
\ /
| NO
v
+-----------------+
| Fact=1, i=1 |
+-----------------+
|
+------->|
| v
| / \
| / i <= N? \ ---NO---> [Print Fact] --> [STOP]
| \ /
| | YES
| v
| +---------------+
| | Fact=Fact * i |
| | i = i + 1 |
| +---------------+
+--------+
Dry-Run Trace Table
Execution Step
Var State 1
Var State 2
Evaluation
Output / Result
Init
N = 5
Fact = 1
1 <= 5 (True)
i becomes 2
Loop 2
N = 5
Fact = 1
2 <= 5 (True)
i becomes 3
Loop 3
N = 5
Fact = 2
3 <= 5 (True)
i becomes 4
Loop 4
N = 5
Fact = 6
4 <= 5 (True)
i becomes 5
Loop 5
N = 5
Fact = 24
5 <= 5 (True)
i becomes 6 -> Exits -> Fact=120
💡
Developer Key Takeaway
Factorials grow at an astronomical rate O(N^N). In practical engineering, use 64-bit BigInt for N > 20 to prevent numeric overflow.
C++ 17 / 20
C++ Program Implementation
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a non-negative integer (0 to 20): ";
if (!(cin >> n)) {
cout << "Invalid input! Please enter an integer.\n";
return 1;
}
if (n < 0) {
cout << "Error: Factorial is undefined for negative integers!\n";
return 1;
}
unsigned long long fact = 1;
for (int i = 1; i <= n; ++i) {
fact *= i;
}
cout << n << "! = " << fact << "\n";
return 0;
}
Sample Terminal Output:
Input:
6
Output:
6! = 720
Problem 4 • Loops
Generate the First N Terms of the Fibonacci Series
Design an algorithm and flowchart to generate and print the first N terms of the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
💡 Answer hidden • Solve on your own before checking!
Variable Table
Variable
Data Type
Purpose / Role
N
Integer
Total count of terms to print
T1
Integer
First term, initialized to 0
T2
Integer
Second term, initialized to 1
Next
Integer
Sum of prior two terms (T1 + T2)
count
Integer
Iteration counter from 3 to N
Step-by-Step Algorithm
Step 1: Start.
Step 2: Read the number of terms N.
Step 3: If N <= 0, Print 'N must be positive' and go to Step 10.
Step 4: If N == 1, Print '0' and go to Step 10.
Step 5: Initialize T1 = 0, T2 = 1.
Step 6: Print T1, T2 (first two terms).
Step 7: Initialize loop counter count = 3.
Step 8: While (count <= N):
Next = T1 + T2.
Print Next.
T1 = T2.
T2 = Next.
count = count + 1.
Step 9: Stop.
Design an algorithm and flowchart that reverses the digits of an integer N and checks whether N is a Palindrome (reads the same backward as forward, like 12321).
💡 Answer hidden • Solve on your own before checking!
Variable Table
Variable
Data Type
Purpose / Role
N
Integer
Original input number
Orig
Integer
Copy of original number for comparison
Rev
Integer
Reversed integer, initialized to 0
Rem
Integer
Extracted rightmost remainder digit (N % 10)
Step-by-Step Algorithm
Step 1: Start.
Step 2: Read positive integer N.
Step 3: Orig = N, Rev = 0.
Step 4: While (N > 0):
Rem = N % 10.
Rev = (Rev * 10) + Rem.
N = floor(N / 10).
Step 5: Print 'Reversed Number is Rev'.
Step 6: If (Rev == Orig):
Print 'Orig is a Palindrome Number'.
Else:
Print 'Orig is NOT a Palindrome Number'.
Step 7: Stop.
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> In[/Read N/]
In --> Set[Orig = N Rev = 0]
Set --> Cond{N > 0 ?}
Cond -- Yes --> Calc[Rem = N % 10 Rev = Rev * 10 + Rem N = N // 10] --> Cond
Cond -- No --> PrintRev[/Print Rev/]
PrintRev --> Chk{Rev == Orig ?}
Chk -- Yes --> OutP[/Print 'Palindrome'/] --> Stop([Stop])
Chk -- No --> OutNP[/Print 'Not Palindrome'/] --> Stop
Flowchart (Visual ASCII Diagram)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read N |
| Orig = N, Rev =0|
+-----------------+
|
+------->|
| v
| / \
| / N > 0? \ ---NO---> [Print Rev]
| \ / |
| | YES v
| v / \
| +-------------------+/Rev==Orig?\
| | Rem = N % 10 |\ /
| | Rev = Rev*10 + Rem| |YES |NO
| | N = N // 10 | v v
| +-------------------+ [Palin][Not]
+--------+ \ /
v v
[ STOP ]
Dry-Run Trace Table
Execution Step
Var State 1
Var State 2
Evaluation
Output / Result
Init
N = 121
Orig = 121
N > 0 (True)
Rev = 0*10+1 = 1 | N = 12
Iter 2
N = 12
Orig = 121
N > 0 (True)
Rev = 1*10+2 = 12 | N = 1
Iter 3
N = 1
Orig = 121
N > 0 (True)
Rev = 12*10+1 = 121 | N = 0
Exit
N = 0
Orig = 121
Rev == Orig (121 == 121)
Print '121 is a Palindrome'
💡
Developer Key Takeaway
The place-value shifting formula Rev = Rev * 10 + Rem eliminates the overhead of string conversions.
C++ 17 / 20
C++ Program Implementation
#include <iostream>
using namespace std;
int main() {
long long n;
cout << "Enter an integer: ";
if (!(cin >> n)) {
cout << "Invalid input! Please enter an integer.\n";
return 1;
}
long long original = n;
long long temp = (n < 0) ? -n : n;
long long reversed = 0;
while (temp > 0) {
long long rem = temp % 10;
reversed = (reversed * 10) + rem;
temp /= 10;
}
cout << "\nOriginal Value : " << original << "\n";
cout << "Reversed Value : " << reversed << "\n";
if (n >= 0 && original == reversed) {
cout << "Result : " << original << " is a PALINDROME number.\n";
} else {
cout << "Result : " << original << " is NOT a palindrome number.\n";
}
return 0;
}
Sample Terminal Output:
Input:
12321
Output:
Original Value : 12321
Reversed Value : 12321
Result : 12321 is a PALINDROME number.
Calculate Simple Interest (SI) and Compound Interest (CI) for Principal P, Rate R, and Time T years, applying a +2% bonus interest rate if the investment period T > 5 years.
💡 Answer hidden • Solve on your own before checking!
Variable Table
Variable
Data Type
Purpose / Role
P
Float
Principal investment amount
R
Float
Base annual interest rate (e.g., 7.5)
T
Float
Investment duration in years
SI
Float
Calculated simple interest
CI
Float
Calculated compound interest
Step-by-Step Algorithm
Step 1: Start.
Step 2: Read Principal P, Rate R, and Time T.
Step 3: If P <= 0 or R <= 0 or T <= 0, Print 'Inputs must be positive' and Stop.
Step 4: Check if T > 5:
If True: EffectiveRate = R + 2.0 (Bonus Tier).
Else: EffectiveRate = R.
Step 5: Compute SI = (P * EffectiveRate * T) / 100.
Step 6: Compute CI = P * ((1 + EffectiveRate / 100) ^ T) - P.
Step 7: Print EffectiveRate, SI, CI.
Step 8: Stop.
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> In[/Read P, R, T/]
In --> Valid{"Is P > 0, R > 0, and T > 0 ?"}
Valid -- No --> Err[/Print 'Invalid Inputs'/] --> Stop([Stop])
Valid -- Yes --> Bonus{"Is T > 5 ?"}
Bonus -- Yes --> Add["EffectiveRate = R + 2"]
Bonus -- No --> Base["EffectiveRate = R"]
Add --> Calc[SI = P * EffectiveRate * T / 100 CI = P * (1 + EffectiveRate/100)^T - P]
Base --> Calc
Calc --> Out[/Print SI, CI, Rate/] --> Stop
Flowchart (Visual ASCII Diagram)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read P, R, T |
+-----------------+
|
v
/ \
/ T > 5? \
\ /
YES / \ NO
v v
+-------+ +-------+
|Eff=R+2| |Eff=R |
+-------+ +-------+
\ /
v v
+-----------------------------+
| SI = P * Eff * T / 100 |
| CI = P * (1+Eff/100)^T - P |
+-----------------------------+
|
v
+-----------------+
| Print SI & CI |
+-----------------+
|
v
+-----------------+
| [ STOP ] |
+-----------------+
Dry-Run Trace Table
Execution Step
Var State 1
Var State 2
Evaluation
Output / Result
Case 1
P = 10,000
R = 8%
T > 5 (False) -> Rate = 8%
CI = 10000*(1.08)^3 - 10000 = 2,597.12
Case 2
P = 10,000
R = 8%
T > 5 (True) -> Rate = 10%
CI = 10000*(1.10)^6 - 10000 = 7,715.61
💡
Developer Key Takeaway
Applying conditional modifiers prior to calculation pipelines centralizes business rules and prevents code duplication.
C++ 17 / 20
C++ Program Implementation
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
double P, R, T;
cout << "Enter Principal (P), Base Rate % (R), Time in years (T): ";
if (!(cin >> P >> R >> T) || P <= 0 || R <= 0 || T <= 0) {
cout << "Invalid inputs! P, R, and T must be strictly positive.\n";
return 1;
}
double effectiveRate = (T > 5.0) ? (R + 2.0) : R;
double SI = (P * effectiveRate * T) / 100.0;
double CI = P * pow(1.0 + (effectiveRate / 100.0), T) - P;
cout << fixed << setprecision(2);
cout << "\n========================================\n";
cout << " FINANCIAL INTEREST CALCULATION REPORT\n";
cout << "========================================\n";
cout << "Principal (P) : $" << P << "\n";
cout << "Base Rate : " << R << "%\n";
cout << "Tenure (T) : " << T << " years\n";
cout << "Effective Rate : " << effectiveRate << "%"
<< (T > 5.0 ? " (+2% Long-Term Loyalty Bonus)" : "") << "\n";
cout << "----------------------------------------\n";
cout << "Simple Interest (SI) : $" << SI << " (Total: $" << (P + SI) << ")\n";
cout << "Compound Interest(CI): $" << CI << " (Total: $" << (P + CI) << ")\n";
cout << "Compounding Edge : +$" << (CI - SI) << "\n";
cout << "========================================\n";
return 0;
}
Step 1: Start.
Step 2: Read Principal P, Rate R, and Time T.
Step 3: If P <= 0 or R <= 0 or T <= 0, Print 'Inputs must be positive' and Stop.
Step 4: Check if T > 5:
If True: EffectiveRate = R + 2.0 (Bonus Tier).
Else: EffectiveRate = R.
Step 5: Compute SI = (P * EffectiveRate * T) / 100.
Step 6: Compute CI = P * ((1 + EffectiveRate / 100) ^ T) - P.
Step 7: Print EffectiveRate, SI, CI.
Step 8: Stop.
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> In[/Read P, R, T/]
In --> Valid{"Is P > 0, R > 0, and T > 0 ?"}
Valid -- No --> Err[/Print 'Invalid Inputs'/] --> Stop([Stop])
Valid -- Yes --> Bonus{"Is T > 5 ?"}
Bonus -- Yes --> Add["EffectiveRate = R + 2"]
Bonus -- No --> Base["EffectiveRate = R"]
Add --> Calc[SI = P * EffectiveRate * T / 100 CI = P * (1 + EffectiveRate/100)^T - P]
Base --> Calc
Calc --> Out[/Print SI, CI, Rate/] --> Stop
Flowchart (Visual ASCII Diagram)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read P, R, T |
+-----------------+
|
v
/ \
/ T > 5? \
\ /
YES / \ NO
v v
+-------+ +-------+
|Eff=R+2| |Eff=R |
+-------+ +-------+
\ /
v v
+-----------------------------+
| SI = P * Eff * T / 100 |
| CI = P * (1+Eff/100)^T - P |
+-----------------------------+
|
v
+-----------------+
| Print SI & CI |
+-----------------+
|
v
+-----------------+
| [ STOP ] |
+-----------------+
Dry-Run Trace Table
Execution Step
Var State 1
Var State 2
Evaluation
Output / Result
Case 1
P = 10,000
R = 8%
T > 5 (False) -> Rate = 8%
CI = 10000*(1.08)^3 - 10000 = 2,597.12
Case 2
P = 10,000
R = 8%
T > 5 (True) -> Rate = 10%
CI = 10000*(1.10)^6 - 10000 = 7,715.61
💡
Developer Key Takeaway
Applying conditional modifiers prior to calculation pipelines centralizes business rules and prevents code duplication.
Step 1: Start.
Step 2: Read Principal P, Rate R, and Time T.
Step 3: If P <= 0 or R <= 0 or T <= 0, Print 'Inputs must be positive' and Stop.
Step 4: Check if T > 5:
If True: EffectiveRate = R + 2.0 (Bonus Tier).
Else: EffectiveRate = R.
Step 5: Compute SI = (P * EffectiveRate * T) / 100.
Step 6: Compute CI = P * ((1 + EffectiveRate / 100) ^ T) - P.
Step 7: Print EffectiveRate, SI, CI.
Step 8: Stop.
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> In[/Read P, R, T/]
In --> Valid{"Is P > 0, R > 0, and T > 0 ?"}
Valid -- No --> Err[/Print 'Invalid Inputs'/] --> Stop([Stop])
Valid -- Yes --> Bonus{"Is T > 5 ?"}
Bonus -- Yes --> Add["EffectiveRate = R + 2"]
Bonus -- No --> Base["EffectiveRate = R"]
Add --> Calc[SI = P * EffectiveRate * T / 100 CI = P * (1 + EffectiveRate/100)^T - P]
Base --> Calc
Calc --> Out[/Print SI, CI, Rate/] --> Stop
Flowchart (Visual ASCII Diagram)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read P, R, T |
+-----------------+
|
v
/ \
/ T > 5? \
\ /
YES / \ NO
v v
+-------+ +-------+
|Eff=R+2| |Eff=R |
+-------+ +-------+
\ /
v v
+-----------------------------+
| SI = P * Eff * T / 100 |
| CI = P * (1+Eff/100)^T - P |
+-----------------------------+
|
v
+-----------------+
| Print SI & CI |
+-----------------+
|
v
+-----------------+
| [ STOP ] |
+-----------------+
Dry-Run Trace Table
Execution Step
Var State 1
Var State 2
Evaluation
Output / Result
Case 1
P = 10,000
R = 8%
T > 5 (False) -> Rate = 8%
CI = 10000*(1.08)^3 - 10000 = 2,597.12
Case 2
P = 10,000
R = 8%
T > 5 (True) -> Rate = 10%
CI = 10000*(1.10)^6 - 10000 = 7,715.61
💡
Developer Key Takeaway
Applying conditional modifiers prior to calculation pipelines centralizes business rules and prevents code duplication.
Step 1: Start.
Step 2: Read Principal P, Rate R, and Time T.
Step 3: If P <= 0 or R <= 0 or T <= 0, Print 'Inputs must be positive' and Stop.
Step 4: Check if T > 5:
If True: EffectiveRate = R + 2.0 (Bonus Tier).
Else: EffectiveRate = R.
Step 5: Compute SI = (P * EffectiveRate * T) / 100.
Step 6: Compute CI = P * ((1 + EffectiveRate / 100) ^ T) - P.
Step 7: Print EffectiveRate, SI, CI.
Step 8: Stop.
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> In[/Read P, R, T/]
In --> Valid{"Is P > 0, R > 0, and T > 0 ?"}
Valid -- No --> Err[/Print 'Invalid Inputs'/] --> Stop([Stop])
Valid -- Yes --> Bonus{"Is T > 5 ?"}
Bonus -- Yes --> Add["EffectiveRate = R + 2"]
Bonus -- No --> Base["EffectiveRate = R"]
Add --> Calc[SI = P * EffectiveRate * T / 100 CI = P * (1 + EffectiveRate/100)^T - P]
Base --> Calc
Calc --> Out[/Print SI, CI, Rate/] --> Stop
Flowchart (Visual ASCII Diagram)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read P, R, T |
+-----------------+
|
v
/ \
/ T > 5? \
\ /
YES / \ NO
v v
+-------+ +-------+
|Eff=R+2| |Eff=R |
+-------+ +-------+
\ /
v v
+-----------------------------+
| SI = P * Eff * T / 100 |
| CI = P * (1+Eff/100)^T - P |
+-----------------------------+
|
v
+-----------------+
| Print SI & CI |
+-----------------+
|
v
+-----------------+
| [ STOP ] |
+-----------------+
Dry-Run Trace Table
Execution Step
Var State 1
Var State 2
Evaluation
Output / Result
Case 1
P = 10,000
R = 8%
T > 5 (False) -> Rate = 8%
CI = 10000*(1.08)^3 - 10000 = 2,597.12
Case 2
P = 10,000
R = 8%
T > 5 (True) -> Rate = 10%
CI = 10000*(1.10)^6 - 10000 = 7,715.61
💡
Developer Key Takeaway
Applying conditional modifiers prior to calculation pipelines centralizes business rules and prevents code duplication.
Step 1: Start.
Step 2: Read Principal P, Rate R, and Time T.
Step 3: If P <= 0 or R <= 0 or T <= 0, Print 'Inputs must be positive' and Stop.
Step 4: Check if T > 5:
If True: EffectiveRate = R + 2.0 (Bonus Tier).
Else: EffectiveRate = R.
Step 5: Compute SI = (P * EffectiveRate * T) / 100.
Step 6: Compute CI = P * ((1 + EffectiveRate / 100) ^ T) - P.
Step 7: Print EffectiveRate, SI, CI.
Step 8: Stop.
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> In[/Read P, R, T/]
In --> Valid{"Is P > 0, R > 0, and T > 0 ?"}
Valid -- No --> Err[/Print 'Invalid Inputs'/] --> Stop([Stop])
Valid -- Yes --> Bonus{"Is T > 5 ?"}
Bonus -- Yes --> Add["EffectiveRate = R + 2"]
Bonus -- No --> Base["EffectiveRate = R"]
Add --> Calc[SI = P * EffectiveRate * T / 100 CI = P * (1 + EffectiveRate/100)^T - P]
Base --> Calc
Calc --> Out[/Print SI, CI, Rate/] --> Stop
Flowchart (Visual ASCII Diagram)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read P, R, T |
+-----------------+
|
v
/ \
/ T > 5? \
\ /
YES / \ NO
v v
+-------+ +-------+
|Eff=R+2| |Eff=R |
+-------+ +-------+
\ /
v v
+-----------------------------+
| SI = P * Eff * T / 100 |
| CI = P * (1+Eff/100)^T - P |
+-----------------------------+
|
v
+-----------------+
| Print SI & CI |
+-----------------+
|
v
+-----------------+
| [ STOP ] |
+-----------------+
Dry-Run Trace Table
Execution Step
Var State 1
Var State 2
Evaluation
Output / Result
Case 1
P = 10,000
R = 8%
T > 5 (False) -> Rate = 8%
CI = 10000*(1.08)^3 - 10000 = 2,597.12
Case 2
P = 10,000
R = 8%
T > 5 (True) -> Rate = 10%
CI = 10000*(1.10)^6 - 10000 = 7,715.61
💡
Developer Key Takeaway
Applying conditional modifiers prior to calculation pipelines centralizes business rules and prevents code duplication.
Given coefficients a, b, and c of ax^2 + bx + c = 0, find both roots by evaluating the discriminant D = b^2 - 4ac across all three cases: D > 0, D = 0, and D < 0.
💡 Answer hidden • Solve on your own before checking!
Variable Table
Variable
Data Type
Purpose / Role
a, b, c
Float
Quadratic coefficients (a != 0)
D
Float
Discriminant value (b^2 - 4ac)
root1, root2
Float/Complex
The two roots of the equation
Step-by-Step Algorithm
Step 1: Start.
Step 2: Read coefficients a, b, c.
Step 3: If a == 0, Print 'Not quadratic (Linear equation)' and Stop.
Step 4: Compute Discriminant D = (b * b) - (4 * a * c).
Step 5: If D > 0 (Two distinct real roots):
root1 = (-b + sqrt(D)) / (2 * a).
root2 = (-b - sqrt(D)) / (2 * a).
Print root1, root2.
Step 6: Else if D == 0 (Two equal real roots):
root1 = -b / (2 * a).
Print 'Real and Equal Roots:', root1.
Step 7: Else (D < 0, Complex conjugate roots):
realPart = -b / (2 * a).
imagPart = sqrt(-D) / (2 * a).
Print 'Complex Roots: realPart +/- imagPart * i'.
Step 8: Stop.
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> In[/Read a, b, c/]
In --> ChkA{a == 0 ?}
ChkA -- Yes --> Err[/Print 'Not Quadratic'/] --> Stop([Stop])
ChkA -- No --> CalcD[D = b*b - 4*a*c]
CalcD --> ChkD{D > 0 ?}
ChkD -- Yes --> OutReal[/Print 2 Distinct Real Roots/] --> Stop
ChkD -- No --> ChkZero{D == 0 ?}
ChkZero -- Yes --> OutEq[/Print Real & Equal Root/] --> Stop
ChkZero -- No --> OutComp[/Print Complex Roots with i/] --> Stop
Flowchart (ANSI Standard Visual SVG Diagram)
Flowchart (Visual ASCII Diagram)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read a, b, c |
+-----------------+
|
v
/ \
/ a == 0?\ ---YES--> [Print Not Quadratic] --> [STOP]
\ /
| NO
v
+-----------------+
| D = b*b - 4*a*c |
+-----------------+
|
v
/ \
/ D > 0? \ ---YES--> [Distinct Real Roots] --> [STOP]
\ /
| NO
v
/ \
/ D == 0?\ ---YES--> [Equal Real Roots] --> [STOP]
\ /
| NO
v
+-----------------------+
| Complex Roots (with i)| --> [STOP]
+-----------------------+
Dry-Run Trace Table
Execution Step
Var State 1
Var State 2
Evaluation
Output / Result
Case D > 0
1, -5, 6
D = 25 - 24 = 1
r1 = (5+1)/2 = 3
Roots: 3.0 and 2.0
Case D = 0
1, -4, 4
D = 16 - 16 = 0
r1 = 4/2 = 2
Roots: Real & Equal = 2.0
Case D < 0
1, 2, 5
D = 4 - 20 = -16
real = -2/2 = -1
Roots: -1 + 2i and -1 - 2i
💡
Developer Key Takeaway
Handling D < 0 prevents invalid mathematical domains like Math.sqrt(-16) from crashing production software with NaN errors.
C++ 17 / 20
C++ Program Implementation
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
double a, b, c;
cout << "Enter coefficients a, b, and c (ax^2 + bx + c = 0): ";
if (!(cin >> a >> b >> c)) {
cout << "Invalid input! Please enter numeric coefficients.\n";
return 1;
}
if (a == 0) {
cout << "\n[!] Error: Coefficient 'a' cannot be 0 in a quadratic equation.\n";
if (b != 0) {
cout << "Linear root (bx + c = 0): x = " << (-c / b) << "\n";
}
return 0;
}
double D = (b * b) - (4.0 * a * c);
cout << fixed << setprecision(4);
cout << "\n========================================\n";
cout << "Equation : (" << a << ")x^2 + (" << b << ")x + (" << c << ") = 0\n";
cout << "Discriminant D: " << D << "\n";
cout << "----------------------------------------\n";
if (D > 0) {
double sqrtD = sqrt(D);
double root1 = (-b + sqrtD) / (2.0 * a);
double root2 = (-b - sqrtD) / (2.0 * a);
cout << "Nature: REAL, UNEQUAL, AND DISTINCT ROOTS\n";
cout << "Root 1 = " << root1 << "\n";
cout << "Root 2 = " << root2 << "\n";
} else if (D == 0) {
double root = -b / (2.0 * a);
cout << "Nature: REAL AND EQUAL ROOTS\n";
cout << "Root 1 = Root 2 = " << root << "\n";
} else {
double realPart = -b / (2.0 * a);
double imagPart = sqrt(-D) / (2.0 * a);
cout << "Nature: COMPLEX CONJUGATE ROOTS\n";
cout << "Root 1 = " << realPart << " + " << imagPart << "i\n";
cout << "Root 2 = " << realPart << " - " << imagPart << "i\n";
}
cout << "========================================\n";
return 0;
}
Given an array Arr of N elements and a search Key, formulate an algorithm and flowchart to find the index of Key, terminating early as soon as the first match is found.
💡 Answer hidden • Solve on your own before checking!
Variable Table
Variable
Data Type
Purpose / Role
Arr[]
Array
Collection of N elements
N
Integer
Total number of elements in array
Key
Numeric
Target value to locate
i
Integer
Array index pointer from 0 to N-1
Found
Boolean Flag
0 if not found, 1 if found
Step-by-Step Algorithm
Step 1: Start.
Step 2: Read array size N and array elements Arr[0...N-1].
Step 3: Read search target Key.
Step 4: Initialize index i = 0 and flag Found = 0.
Step 5: While (i < N):
If (Arr[i] == Key):
Print 'Key found at position i'.
Found = 1.
Break loop (Go to Step 7).
Else:
i = i + 1.
Step 6: If (Found == 0):
Print 'Key not present in array'.
Step 7: Stop.
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> InArr[/"Read N and Array elements Arr[0..N-1]"/]
InArr --> InKey[/"Read target Key to search"/]
InKey --> Init["Initialize index i = 0, Found = 0"]
Init --> LoopCond{"Is i < N ?"}
LoopCond -- Yes --> MatchCond{"Does Arr[i] == Key ?"}
MatchCond -- Yes --> SetFound["Set Found = 1"]
SetFound --> OutFound[/"Print 'Key found at index i'"/]
OutFound --> EarlyExit[/"⚡ Early Exit Break Loop"/]
EarlyExit --> Stop([Stop])
MatchCond -- No --> Inc["i = i + 1"]
Inc --> LoopCond
LoopCond -- No --> ChkFound{"Is Found == 0 ?"}
ChkFound -- Yes --> OutNotFound[/"Print 'Key not present in array'"/]
OutNotFound --> Stop
ChkFound -- No --> Stop
Flowchart (ANSI Standard Visual SVG Diagram)
Flowchart (Visual ASCII Diagram)
+-----------------------------------+
| [ START ] |
+-----------------------------------+
|
v
/-----------------------------------\
/ Read N and Arr[0...N-1] \
/---------------------------------------\
|
v
/-----------------------------------\
/ Read search target Key \
/---------------------------------------\
|
v
+-----------------------------------+
| i = 0, Found = 0 |
+-----------------------------------+
|
+------->| (Loop Entry)
| v
| / \
| / i < \ NO (End of Array)
| / N ? \-------------------------+
| \ / |
| \ / |
| | YES v
| v / \
| / \ /Found ==\ NO
| /Arr[i]==\ YES / 0 ? \---------+
| / Key ? \---------->+ \ / |
| \ / | \ / |
| \ / | | YES |
| | NO v v |
| v +-------------+/-----------------\ |
| +---------+ | Found = 1 || Print 'Key not \ |
| | i = i+1 | | Early Exit || present in array'/ |
| +---------+ +-------------+\-----------------/ |
| | | | |
+--------+ v | |
/--------------\ | |
/ Print 'Found \ | |
/ at index i' / | |
\------------------/ | |
| | |
+-------+-------+------------+
|
v
+-------------------------------+
| [ STOP ] |
+-------------------------------+
Dry-Run Trace Table
Execution Step
Var State 1
Var State 2
Evaluation
Output / Result
Init
Arr = [14, 52, 98, 33]
Key = 98
Arr[0] = 14 != 98
Continue
Step 2
Arr = [14, 52, 98, 33]
Key = 98
Arr[1] = 52 != 98
Continue
Step 3
Arr = [14, 52, 98, 33]
Key = 98
Arr[2] = 98 == 98
Print 'Found at index 2' -> Immediate Exit!
💡
Developer Key Takeaway
An early exit break avoids unnecessary comparisons on remaining array elements, saving significant CPU cycles in large datasets.
C++ 17 / 20
C++ Program Implementation
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cout << "Enter number of elements in array N: ";
if (!(cin >> n) || n <= 0) {
cout << "Invalid array size N!\n";
return 1;
}
vector<int> arr(n);
cout << "Enter " << n << " integers:\n";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
int key;
cout << "Enter target key to search: ";
cin >> key;
int foundIndex = -1;
int comparisons = 0;
// Linear search with early exit
for (int i = 0; i < n; ++i) {
comparisons++;
if (arr[i] == key) {
foundIndex = i;
break; // Early exit break
}
}
cout << "\n========================================\n";
if (foundIndex != -1) {
cout << "SUCCESS: Target key [" << key << "] FOUND at index " << foundIndex
<< " (Position " << (foundIndex + 1) << " in array).\n";
cout << "Comparisons executed : " << comparisons << " of " << n
<< " (" << (n - comparisons) << " comparisons saved!)\n";
} else {
cout << "FAILURE: Target key [" << key << "] NOT FOUND in the array.\n";
cout << "Comparisons executed : " << comparisons << " (Full scan completed).\n";
}
cout << "========================================\n";
return 0;
}
Sample Terminal Output:
Input:
5
15 23 7 42 9
7
Output:
========================================
SUCCESS: Target key [7] FOUND at index 2 (Position 3 in array).
Comparisons executed : 3 of 5 (2 comparisons saved!)
========================================
Problem 9 • Math
Sum of Digits & Armstrong (Narcissistic) Number Check
Calculate the sum of digits of an integer N, and check if N is an Armstrong number (where the sum of the cubes of its digits equals the number itself, e.g. 153 = 1^3 + 5^3 + 3^3).
💡 Answer hidden • Solve on your own before checking!
Variable Table
Variable
Data Type
Purpose / Role
N
Integer
Positive integer input
Orig
Integer
Backup of original number
SumDigits
Integer
Linear sum of digits, initialized to 0
ArmstrongSum
Integer
Sum of cubes of digits, initialized to 0
Rem
Integer
Extracted digit (N % 10)
Step-by-Step Algorithm
Step 1: Start.
Step 2: Read positive integer N.
Step 3: Orig = N, SumDigits = 0, ArmstrongSum = 0.
Step 4: While (N > 0):
Rem = N % 10.
SumDigits = SumDigits + Rem.
ArmstrongSum = ArmstrongSum + (Rem * Rem * Rem).
N = floor(N / 10).
Step 5: Print 'Sum of Digits = SumDigits'.
Step 6: If (ArmstrongSum == Orig):
Print 'Orig is an Armstrong Number'.
Else:
Print 'Orig is NOT an Armstrong Number'.
Step 7: Stop.
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> In[/Read N/]
In --> Init[Orig = N SumDigits = 0 ArmstrongSum = 0]
Init --> Cond{N > 0 ?}
Cond -- Yes --> Calc[Rem = N % 10 SumDigits += Rem ArmstrongSum += Rem^3 N = N // 10] --> Cond
Cond -- No --> OutSum[/Print SumDigits/]
OutSum --> ChkArm{ArmstrongSum == Orig ?}
ChkArm -- Yes --> OutYes[/Print 'Armstrong Number'/] --> Stop([Stop])
ChkArm -- No --> OutNo[/Print 'Not Armstrong'/] --> Stop
Flowchart (Visual ASCII Diagram)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read N |
| Orig = N |
| Sum=0, ArmSum=0 |
+-----------------+
|
+------->|
| v
| / \
| / N > 0? \ ---NO---> [Print SumDigits]
| \ / |
| | YES v
| v / \
| +-----------------------+/Arm==Orig?\
| | Rem = N % 10 |\ /
| | Sum = Sum + Rem | |YES |NO
| | Arm = Arm + (Rem^3) | v v
| | N = N // 10 | [Yes] [No]
| +-----------------------+ \ /
+--------+ v v
[ STOP ]
Dry-Run Trace Table
Execution Step
Var State 1
Var State 2
Evaluation
Output / Result
Init
N = 153
Orig = 153
Arm = 0
Rem = 3 | Sum=3, Arm=27, N=15
Iter 2
N = 15
Orig = 153
Arm = 27
Rem = 5 | Sum=8, Arm=27+125=152, N=1
Iter 3
N = 1
Orig = 153
Arm = 152
Rem = 1 | Sum=9, Arm=152+1=153, N=0
Exit
N = 0
Orig = 153
Arm = 153
Print 'Sum = 9' and '153 is an Armstrong Number'
💡
Developer Key Takeaway
Single-pass extraction allows calculating multiple statistical metrics (sum of digits, Armstrong sum, digit count) in a single O(log10 N) loop.
C++ 17 / 20
C++ Program Implementation
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer: ";
if (!(cin >> n) || n < 0) {
cout << "Invalid input! Please enter a non-negative integer.\n";
return 1;
}
int original = n;
int sumOfDigits = 0;
int armstrongSum = 0; // Sum of cubes of digits
int temp = n;
while (temp > 0) {
int digit = temp % 10;
sumOfDigits += digit;
armstrongSum += (digit * digit * digit);
temp /= 10;
}
cout << "\n========================================\n";
cout << "Original Number : " << original << "\n";
cout << "Sum of Digits : " << sumOfDigits << "\n";
cout << "Sum of Cubed Digits : " << armstrongSum << "\n";
cout << "----------------------------------------\n";
if (armstrongSum == original) {
cout << "Result: " << original << " is a 3-DIGIT ARMSTRONG (Narcissistic) NUMBER!\n";
} else {
cout << "Result: " << original << " is NOT an Armstrong number.\n";
}
cout << "========================================\n";
return 0;
}
Sample Terminal Output:
Input:
153
Output:
========================================
Original Number : 153
Sum of Digits : 9
Sum of Cubed Digits : 153
----------------------------------------
Result: 153 is a 3-DIGIT ARMSTRONG (Narcissistic) NUMBER!
========================================
Problem 10 • Branching
Menu-Driven Temperature Conversion (Celsius ⇋ Fahrenheit)
Design an interactive menu-driven program algorithm and flowchart where the user chooses whether to convert from Celsius to Fahrenheit, Fahrenheit to Celsius, or Exit.
💡 Answer hidden • Solve on your own before checking!
Variable Table
Variable
Data Type
Purpose / Role
Choice
Integer
User menu selection (1, 2, or 3)
C
Float
Temperature in Celsius
F
Float
Temperature in Fahrenheit
Step-by-Step Algorithm
Step 1: Start.
Step 2: Display Menu:
1: Celsius to Fahrenheit
2: Fahrenheit to Celsius
3: Exit Program.
Step 3: Read user Choice.
Step 4: If Choice == 1:
Read temperature in Celsius (C).
Compute F = (C * 9 / 5) + 32.
Print 'Temperature in Fahrenheit is F'.
Go to Step 8.
Step 5: Else if Choice == 2:
Read temperature in Fahrenheit (F).
Compute C = (F - 32) * 5 / 9.
Print 'Temperature in Celsius is C'.
Go to Step 8.
Step 6: Else if Choice == 3:
Print 'Exiting program. Goodbye!'.
Go to Step 8.
Step 7: Else:
Print 'Invalid Choice! Please enter 1, 2, or 3'.
Go to Step 2 (Loop back to Menu).
Step 8: Stop.