#include<stdio.h>
#include<conio.h>
int main()
{
int x,z;
printf("Enter Number:");
scanf("%d",&x);
z=(x>=1 && x<=100 ? 1 : 0);
printf("z=%d",z);
getch();
}
/*The logical operator checks the entered value whether it is in between 1 and 100.if the condition is
true,1 is assigned to z otherwise 0.*/
The program is written in the C language using the stdio.h
and conio.h
library functions. The purpose of the program is to check if an inputted number is within the range of 1 to 100 and return a value of 1 if it is or 0 if it is not.
Here is a step by step explanation of the program:
- Input:
int x
is declared as an integer type variable to store the inputted number. printf
function is used to prompt the user to enter a number.scanf
function is used to store the inputted number in the variablex
.- Calculation: The value of
x
is compared with the range of 1 to 100 using a conditional operator (Ternary operator). The expressionx >= 1 && x <= 100
checks ifx
is greater than or equal to 1 and less than or equal to 100. If the condition is true, the value 1 is assigned to the variablez
using the expression(x >= 1 && x <= 100 ? 1 : 0)
. If the condition is false, the value 0 is assigned to the variablez
. - Output: The value of
z
is displayed using theprintf
function. getch()
function is used to hold the output screen.
The program uses the logical AND operator &&
to check if the inputted number is within the specified range. The conditional operator ? :
is used to assign either 1 or 0 to z
based on the result of the comparison.
Thanks
Write a program to display 1 if inputted number is between 1 and 100 otherwise 0.use the logical AND operator.