Find the Smallest of three Numbers in C, C++, Java, JavaScript, and Python. In the world of programming, solving problems is a fundamental skill that every coder must possess. Whether you’re a Java enthusiast, a C++ aficionado, or a Python wizard, mastering this skill is crucial. In this article, we will walk you through a step-by-step guide to finding the smallest of three numbers in Java, C++, and Python. So, let’s dive in and explore how to tackle this problem efficiently using a well-structured 6-step strategy.
Find the Smallest of Three Numbers:
Sure, Here’s the 6-step strategy to Solve this problem:-
Step 1. Understand the problem:
Before diving into coding, let’s ensure we understand the problem. We want to write a program that takes three numbers as input and finds the smallest among them.
Step 2. Design test data/test cases:
To ensure our program works correctly, let’s design some test cases:
Test Case 1:
Input: 5, 2, 8
Expected Output: 2 Test Case 2:
Input: 10, 10, 10
Expected Output: 10 Test Case 3:
Input: -3, 0, -1
Expected Output: -3
Step 3. Derive the solution (write pseudocode for the solution):
We’ll start by writing pseudocode to outline our solution:
FUNCTION findSmallest(a, b, c)
IF a <= b AND a <= c
RETURN a
ELSE IF b <= a AND b <= c
RETURN b
ELSE
RETURN c
END FUNCTION
Step 4. Test the solution
(do a dry run of the pseudocode for the test data and confirm it works):
Let's dry run the pseudocode with the first test case:
a = 5, b = 2, c = 8
Is 5 <= 2? No. Is 5 <= 8? Yes.
Return 5.
Step 5. Write the program/code:
Now that we have a clear understanding of the logic, we can write the code in our preferred programming language.
Program to Find the Smallest of Three Numbers in C, C++, Java, JavaScript, Python
#include <stdio.h>
double findSmallest(double a, double b, double c) {
if (a <= b && a <= c) {
return a;
} else if (b <= a && b <= c) {
return b;
} else {
return c;
}
}
int main() {
double num1, num2, num3;
printf("Enter three numbers:\n");
scanf("%lf %lf %lf", &num1, &num2, &num3);
double smallest = findSmallest(num1, num2, num3);
printf("The smallest number is: %lf\n", smallest);
return 0;
}
C#include <iostream>
using namespace std;
double findSmallest(double a, double b, double c) {
if (a <= b && a <= c) {
return a;
} else if (b <= a && b <= c) {
return b;
} else {
return c;
}
}
int main() {
double num1, num2, num3;
cout << "Enter three numbers:" << endl;
cin >> num1 >> num2 >> num3;
double smallest = findSmallest(num1, num2, num3);
cout << "The smallest number is: " << smallest << endl;
return 0;
}
C++import java.util.Scanner;
public class SmallestNumberFinder {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter three numbers:");
double num1 = scanner.nextDouble();
double num2 = scanner.nextDouble();
double num3 = scanner.nextDouble();
double smallest = findSmallest(num1, num2, num3);
System.out.println("The smallest number is: " + smallest);
scanner.close();
}
public static double findSmallest(double a, double b, double c) {
if (a <= b && a <= c) {
return a;
} else if (b <= a && b <= c) {
return b;
} else {
return c;
}
}
}
Javafunction findSmallest(a, b, c) {
if (a <= b && a <= c) {
return a;
} else if (b <= a && b <= c) {
return b;
} else {
return c;
}
}
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Enter three numbers: ', input => {
const [num1, num2, num3] = input.split(' ').map(Number);
const smallest = findSmallest(num1, num2, num3);
console.log(`The smallest number is: ${smallest}`);
readline.close();
});
JavaScriptdef find_smallest(a, b, c):
if a <= b and a <= c:
return a
elif b <= a and b <= c:
return b
else:
return c
num1, num2, num3 = map(float, input("Enter three numbers: ").split())
smallest = find_smallest(num1, num2, num3)
print("The smallest number is:", smallest)
PythonStep 6: Test the Code:
Compile and run the program. Test it with the provided test cases to ensure it produces the correct output.
Find the Smallest of three Numbers | Conclusion:
Congratulations! You’ve successfully learned how to find the smallest number among three using Java. You followed a step-by-step approach, from understanding the problem to writing and testing the code. This foundational skill will serve as a building block as you continue your coding journey.