Python/C/C++/JAVA

Basic Practice Programs with Code and Concept

By D.S

Largest number among two numbers

In this program, we will use an if..else statement to determine which of the two numbers entered is the largest. The computer will first check if the first number is greater than the second one. If it is, then the first number is the largest. Otherwise, if the second number is greater, then it is considered the largest. This simple program demonstrates the use of conditional statements in programming.

(a.) C Code

#include <stdio.h>

    int main() {
        int num1 = 20, num2 = 10;

        if (num1 > num2)
            printf("%d is larger than %d
", num1, num2);
        else
            printf("%d is larger than %d
", num2, num1);

        return 0;
    }
Output:-
20 is larger than 10

(b.) C++ Code

#include <iostream>
    using namespace std;

    int main() {
        int num1 = 20, num2 = 10;

        if (num1 > num2)
            cout << num1 << " is larger than " << num2 << endl;
        else
            cout << num2 << " is larger than " << num1 << endl;

        return 0;
    }
Output:-
20 is larger than 10

(c.) Python Code

num1 = 20
    num2 = 10

    if num1 > num2:
        print(f"{num1} is larger than {num2}")
    else:
        print(f"{num2} is larger than {num1}")
Output:-
20 is larger than 10

(d.) Java Code

public class LargestNumber {
        public static void main(String[] args) {
            int num1 = 20, num2 = 10;

            if (num1 > num2)
                System.out.println(num1 + " is larger than " + num2);
            else
                System.out.println(num2 + " is larger than " + num1);
        }
    }
Output:-
20 is larger than 10