Wednesday, 23 January 2019

Chapter:Operators and Expressions(part-3)

Question: a)Write a flowchart to find the sum of the first N prime number, Where n should be given by the user
b) What is logical operator

Answer: a)


b) Logical operators

Logical operator also called as Boolean  operators.
An expression that combine two or more expression is termed as a logical expression. for combining this expression we logical expression.
Logical operators return 0 for false and 1 for true.the operands maybe constant, variable or exponential

C has the operators:


Operators
meaning
&&

||

!

AND

OR

NOT

AND Operator

This operator give the net result true if the both condition are true otherwise result is false.

Condition1
condition2
result
False

False

True

True

False

True

False

True
False

False

False

True
Boolean table
Example:/*Write a program if input number is between 1 to 100 then 1 otherwise 0 by using logical AND operator.*/

#include<stdio.h>
#include<conio.h>
void main()
{
int x,z;
clrscr();
printf("Enter the number");
scanf("%d", &x);
z=(x>= 1 && x<=100? 1:0);
printf("z=%d",z);
getch();
}
Output : Enter the number : 10
Z=1

OR Operator

This operator gives us the net result  false. if both the condition have the value false otherwise result is true.

Condition1
condition2
result
False

False

True

True

False

True

False

True
False

True

True

True

Exmaple:/*Write a program if input number is 1 or 100 then 1 otherwise 0 by using logical OR operator.*/

#include<stdio.h>
#include<conio.h>
void main()
{
int x,z;
clrscr();
printf("Enter the number");
scanf("%d", &x);
z=(x== 1 || x=100 ? 1:0);
printf("z=%d",z);
getch();
}
Output : Enter the number : 100
Z=1

NOT operator

This is unary operator And it neglect the value of the condition.
if the value of condition is false then it gives the result  true. if value of condition is true then it gives the result false.

Condition
result
False
 true
true
false

/*Write a program if input number is except 100 otherwise 0. by using logical NOT operator.*/

#include<stdio.h>
#include<conio.h>
void main()
{
int x,z;
clrscr();
printf("Enter the number");
scanf("%d", &x);
z=(x != 100 ? 1: 0);
printf("z=%d",z);
getch();
}
Output : Enter the number : 100
Z=0

1 comment:

Basic Electronics Engineering (EC101)

Basic Electronics Engineering (EC101)