Determining the Smallest of Two Values using the Ternary Operator in C


This is a basic C program that prompts the user to enter two integers (a and b) and then uses the ternary operator (? :) to determine which of the two values is the smallest. The ternary operator is a shorthand way of writing an if-else statement.

The program starts with the inclusion of the "stdio.h" header file, which contains the functions used for input and output (such as printf and scanf).

The main() function is the entry point of the program. It starts by declaring two variables, "a" and "b", to store the user-entered values. The program then uses the printf function to prompt the user to enter the first value and assigns the user input to the variable "a" using the scanf function. Similarly, the program prompts the user to enter the second value and assigns the user input to the variable "b" using the scanf function.

The ternary operator (? :) is used to check whether "a" is greater than "b". If it is, the program will print "First value is Smallest :%d", followed by the value of "a". If "a" is not greater than "b", the program will print "Second value is Smallest :%d", followed by the value of "b".

Finally, the program returns 0, indicating that the program has completed successfully.

Source Code

#include<stdio.h>
int main()
{
    int a,b;
    printf("\nEnter the First value:");
    scanf("%d",&a);
    printf("\nEnter the Second value:");
    scanf("%d",&b);
    a>b?printf("First value is Smallest :%d",a):printf("Second value is Smallest :%d",b);
    return 0;
}
To download raw file Click Here

Output

Enter the First value: 3
Enter the Second value: 5
First value is Smallest : 3

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C