Write a program to display 1 if inputted number is between 1 and 100 otherwise 0.use the logical AND operator.

#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:

  1. Input: int x is declared as an integer type variable to store the inputted number.
  2. printf function is used to prompt the user to enter a number.
  3. scanf function is used to store the inputted number in the variable x.
  4. Calculation: The value of x is compared with the range of 1 to 100 using a conditional operator (Ternary operator). The expression x >= 1 && x <= 100 checks if x 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 variable z using the expression (x >= 1 && x <= 100 ? 1 : 0). If the condition is false, the value 0 is assigned to the variable z.
  5. Output: The value of z is displayed using the printf function.
  6. 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.

Leave a Reply

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

Scroll to top