Program 4 - CSU1128 - CSE 2026 - Shoolini University

Program to find max between 2 digits.

Explanation

The following code is a basic C program that determines the maximum of two numbers entered by the user. The program uses two different methods to find the maximum of two numbers, the first method uses an if loop and the second method uses the conditional operator (ternary operator).

The program starts by including the header file <stdio.h> which contains functions such as printf, scanf, etc. used in input/output operations. The main function contains the main logic of the program.

In the main function, two float variables d and m are declared and initialized. The user is prompted to enter two numbers separated by a space using scanf function, which are stored in the variables d and m.

The first method of finding the maximum of two numbers uses an if loop. The if loop checks if the value of d is greater than m. If the condition is true, then the maximum number is d, and it is printed along with a message. If the condition is false, then the maximum number is m, and it is printed along with a message.

The second method of finding the maximum of two numbers uses the conditional operator (ternary operator). The conditional operator evaluates the expression (d > m) and assigns the value of d to j if the expression is true, and the value of m if the expression is false. The maximum number is stored in the variable j, and it is printed along with a message.

Finally, the program returns 0 which is a standard value indicating successful execution of the program.

Other functions present in the <stdio.h> header file are:

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128)
 * Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <stdio.h>
int main()
{
    float d, m;
    printf("\n\n Program to find max between 2 digits. \n\n");
    printf("Enter 2 numbers with a space - (Example: 20.3 50.55) - and press enter: ");
    scanf("%f %f", &d, &m);

    // Using if Loop
    if (d > m)
    {
        printf("\n Using if Loop \n\n \tMaximum number is %.2f from the entered numbers %.2f and %.2f \n", d, d, m);
    }
    else
    {
        printf("\n Using if Loop \n\n \tMaximum number is %.2f from the entered numbers %.2f and %.2f \n", m, d, m);
    }

    // Without using if loop
    float j = (d > m) ? d : m;
    printf("\n Without using if Loop \n\n \tMaximum number is %.2f from the entered numbers %.2f and %.2f \n", j, d, m);
    return 0;
}                    
                

Output

Program to find max between 2 digits

Enter 2 numbers with a space - (Example: 20.3 50.55) - and press enter: 4 65

Using if Loop
 Maximum number is 65.00 from the entered numbers 4.00 and 65.00

Without using if Loop
 Maximum number is 65.00 from the entered numbers 4.00 and 65.00