Program to Find the Largest of Two Numbers in C, C++, Java, JavaScript, Python
In the world of programming, solving problems is a fundamental skill that every coder must possess. One of the simplest yet essential tasks is finding the largest of two numbers. 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 largest of two 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.
Program to Find the Largest of Two Numbers
Step 1: Understand the Problem:
We need to write a program that takes two numbers as input and finds the largest of the two.
Step 2: Design Test Data/Test Cases:
Before diving into the code, it’s crucial to design test data to validate our solutions. Let’s consider two test cases:
Test Case 1: Input: num1 = 5, num2 = 8 Expected Output: Largest number is 8 ---------------------------------------------------- Test Case 2: Input: num1 = 10, num2 = 5 Expected Output: Largest number is 10
Step 3: Derive the Solution (Pseudocode):
Next, we will derive the solution using pseudocode to understand the logic without worrying about the specific syntax of the programming language.
1. Input num1 and num2
2. If num1 is greater than num2, then
3. Print "Largest number is num1"
4. Else,
5. Print "Largest number is num2"
Step 4: Test the Solution (Dry Run)
Before writing the actual code, let’s perform a dry run of the pseudocode with our test cases to ensure it works as expected.
Dry Run for Test Case 1:
Input: num1 = 5, num2 = 8
Step 1: Input num1 = 5, num2 = 8
Step 2: num1 is not greater than num2 (5 is not greater than 8)
Step 5: Print "Largest number is num2"
Output: Largest number is 8 (as expected)
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 Largest of Two Numbers in C, C++, Java, JavaScript, Python
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter the first number (num1): ");
scanf("%d", &num1);
printf("Enter the second number (num2): ");
scanf("%d", &num2);
if (num1 > num2) {
printf("Largest number is %d\n", num1);
} else {
printf("Largest number is %d\n", num2);
}
return 0;
}
C#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter the first number (num1): ";
cin >> num1;
cout << "Enter the second number (num2): ";
cin >> num2;
if (num1 > num2) {
cout << "Largest number is " << num1 << endl;
} else {
cout << "Largest number is " << num2 << endl;
}
return 0;
}
C++import java.util.Scanner;
public class LargestOfTwoNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number (num1): ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number (num2): ");
int num2 = scanner.nextInt();
if (num1 > num2) {
System.out.println("Largest number is " + num1);
} else {
System.out.println("Largest number is " + num2);
}
scanner.close();
}
}
Javaconst readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the first number (num1): ', (num1) => {
rl.question('Enter the second number (num2): ', (num2) => {
num1 = parseInt(num1);
num2 = parseInt(num2);
if (num1 > num2) {
console.log(`Largest number is ${num1}`);
} else {
console.log(`Largest number is ${num2}`);
}
rl.close();
});
});
JavaScriptnum1 = int(input("Enter the first number (num1): "))
num2 = int(input("Enter the second number (num2): "))
if num1 > num2:
print("Largest number is", num1)
else:
print("Largest number is", num2)
PythonStep 6: Test the Code
Finally, test the code with the provided test cases and additional inputs to ensure its correctness and efficiency. Click below the link to run code online.
Program to Find the Largest of Two Numbers | Conclusion:
Congratulations! You have successfully learned how to find the largest of two numbers using a systematic 6-step problem-solving strategy. Understanding the problem, designing test cases, deriving the solution, testing the logic, and writing the code are essential steps in every coder’s journey. With this newfound knowledge, you can confidently tackle more complex problems and become a proficient programmer. Happy coding!