Write a program to determine the value of b depending on the inputted value of a. The variable a is used with conditional operator.

c
#include<stdio.h>
#include<conio.h>
main()
{
	int a,b;
	printf("Enter any integer either above 5 or below 5-");
	scanf("%d",&a);
	b=(a>5 ? 3:4);
	printf("Calculated value of b is:- %d",b);
	getch();
}


/*The value b will be 3 if the value of a is grater then 5.Otherwise,it will be 4 for 
any no less then 5*/

The above program uses conditional operator to determine the value of ‘b’ based on the inputted value of ‘a’. The conditional operator ‘?’ is used in this program to assign a value to the variable ‘b’. The syntax for the conditional operator is:

condition ? expression1 : expression2

If the condition is true, expression1 is evaluated and its value is returned. If the condition is false, expression2 is evaluated and its value is returned. In this program, the condition is whether ‘a’ is greater than 5 or not. If ‘a’ is greater than 5, ‘b’ is assigned the value 3, otherwise ‘b’ is assigned the value 4.

If the value of a entered by the user is greater than 5, then the expression a>5 is true, and b will be assigned the value 3 (as specified by the first part of the conditional operator ?). If the value of a is less than or equal to 5, then the expression a>5 is false, and b will be assigned the value 4 (as specified by the second part of the conditional operator :).

The user is asked to enter an integer either above 5 or below 5. This value is stored in the variable ‘a’. Then, the value of ‘b’ is determined using the conditional operator. If ‘a’ is greater than 5, then ‘b’ is assigned the value 3. If ‘a’ is not greater than 5, then ‘b’ is assigned the value 4. The calculated value of ‘b’ is displayed on the screen.

Thanks

Write a program to determine the value of b depending on the inputted value of a. The variable a is used with conditional operator.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top