Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Thursday, 10 March 2022

21.Need of logical operators

 

Need of logical operators:

Some times multiple conditions need to be checked to test a possibility.

Logical operators are used to concatenate multiple relational expressions in order to take a logical decision.

 

Logical operators:

Logical operator

Meaning

&&

AND logical operator

||

OR logical operator

!

Not logical operator


 && (AND logical operator)

 

It is a logical operator used to concatenate multiple relational expressions to take a logical decision.

The "AND" logical expression gives true if all the relational expressions are true, returns false if any one of the condition is false.

It is represented with a series electrical circuit.

 

 

Truth table of AND logical operator:

 


A

B

A&&B

1

1

1

1

0

0

0

1

0

0

0

0


Example1:

 

 

 

 

 

 

 

 


Example2:

 

 

 

 

 

 

 

 


|| (OR logical operator)

 

It is a logical operator used to concatenate multiple relational expressions to take a logical decision.

The "OR" logical expression gives false if all the relational expressions are false, returns true if any one of the condition is true.

It is represented with a parallel electrical circuit.

 

Truth table of OR logical operator:

 


A

B

A||B

1

1

1

1

0

1

0

1

1

0

0

0

 

Example1:

 

 

 

 

 

Example2:

 

 

 

 

 

! (NOT logical operator):

 

It gives a negation of an expression.

 

A

!A

1

0

0

1

 

Example1:

 

 

 

 

Priority of operators so far:

 

Operators

Priority/Presidency

( )

1

!

2

* / %

3

+ -

4

< > <=  >=

5

==  !=

6

&&

7

||

8

=

9

 

 

Spcification1:

Accept any four digit number and print whether the number is a palindrome number or not.

 

With only relational expression:

 

temp=n;              

fourth=n%10;

n=n/10;

third=n%10;

n=n/10;

second=n%10;

n=n/10;

first=n%10;

rev=fourth*1000+third*100+second*10+first*1;

if(temp==rev)

   printf("Palindrome number");

else

   printf("Not a palindrome number");

 

 

















Program:


#include<stdio.h>
void main()
{
int n,first,second,third,fourth;
printf("Enter any four digit number:");
scanf("%d",&n);
fourth=n%10;
n=n/10;
third=n%10;
n=n/10;
second=n%10;
n=n/10;
first=n%10;
if(first==fourth && second==third)
   printf("Palindrome number");
else
printf("Not a palindrome number");
}
 

Execution1:

Enter any four digit number: 2332

Palindrome number

 

Execution2:

Enter any four digit number: 2342

Not a palindrome number

 

Specification2:

Accept any number and print whether the number is a single digit number or not.

 

Program:


#include<stdio.h>
void main()
{
int n;
printf("Enter any number:");
scanf("%d",&n);
if(n>=-9 && n<=9)
    printf("Single digit number");
else
    printf("Not a single digit number");
}
 

Execution1:

Enter any number: -8

Single digit number

 

Execution2:

Enter any number: 5

Single digit number

 

Execution3:

Enter any number: 45

Not a single digit number

 

Specification3:

Accept the marks of a student in 3 subjects and print whether pass or fail. The condition for pass is getting 50 marks in every subject.

 

Program:


#include<stdio.h>
main()
{
int math,physics,chemistry;
printf("Enter the marks in Math, Physics and chemistry..\n");
scanf("%d%d%d",&math,&physics,&chemistry);
if(math>=35)
 printf("he is pass in math");
else
printf("he is fail in math");
if(physics>=35) 
  printf("he is pass in physics");
  else
printf("he is fail in physics");
  if(chemistry>=35)  
  printf("he is pass in chemistry");
else
  printf("he is fail in chemistry");
}

Execution1:

Enter the mark in Math, Physics and chemistry…

60   80   92

Pass

 

Execution2:

Enter the mark in Math, Physics and chemistry…

52   86   12 

Fail

Specification4:

Accept any alphabet and print whether the alphabet is a vowel or consonant.

 

Program:


#include<stdio.h>
void main()
{
char ch;
printf("Enter any alphabet:");
scanf("%c",&ch);
/* We can also check capitals */
if(ch==’a’ || ch==’e’ || ch==’i’ || ch==’o’ || ch==’u’)
   printf("Vowel");
else
   printf("Consonant");
}

Execution1:

Enter any alphabet: e

Vowel

 

Execution2:

Enter any alphabet: p

Consonant

 

Specification5:

Accept the gender, age, percentage of marks of a person and print whether selected or not. Here selection criteria are that, a first class male with the age between 25 and 34.

 

Program:


#include<stdio.h>
void main()
{
int age;
float percent;
char gender;
printf("Enter the gender m/f :");
scanf("%c",&gender);
printf("Age:");
scanf("%d",&age);
scanf("%f",&percent);
if(gender==’m’ && percent>=60 && age>=25 && age<=34)
    printf("Selected");
else

    printf("Not selected");
}

 
class="MsoNormal">

Execution1:

Enter the gender m/f : m

Age: 30

Enter the percentage in qualifying exam: 75

Selected

 

Execution2:

Enter the gender m/f : f

Age: 34

Enter the percentage in qualifying exam: 54

Not selected

 

Specification6:

Accept gender and age of person and print whether major or minor.

 

Logic: Male should have 21 years and female must have 18 years to become majors.

 

Program:


#include<stdio.h>
void main()
{
int n;
printf("Enter any number:");
scanf("%d",&n);
if(n>=-9 && n<=9)
  printf("Sigle digit number");
if((n>=-99&&n<=10) || (n>=10&&n<=99))
  printf("Two digit number");
if((n>=-999&&n<=-100) || (n>=100&&n<=999))
  printf("Three digit number");
if(n>999)
  printf("Biggest number");
if(n<-999 code="" mallest="" number="" printf="">

Execution1:

Enter the gender m/f: m

Enter the age: 27

Major

 

 

 

Execution2:

Enter the gender m/f: f

Enter the age: 20

Major

 

Execution3:

Enter the gender m/f: m

Enter the age: 15

Minor

 

Specification7:

Accept any number and print whether the given number is a two digit number or not.

 

Logic:

 

 

 

 

 

 

 

 


Program:


#include<stdio.h>
void main()
{
int n;
printf("Enter any number:");
scanf("%d",&n);
if((n>=-99&&n<=-10)||(n>=10&&n<=99))
    printf("Two digit number");
else
    printf("Not a two digit number");
}
 

Execution1:

Enter any number: -50

Two digit number

 

Execution2:

Enter any number: 55

Two digit number

 

 

 

Execution3:

Enter any number: 9

Not a two digit number

 

Specification8:

Accept any number and print whether the number is a single digit, two digit, three digit, biggest or smallest number.

 

Logic:

 

 

 

 

 

 

 

 

 

 


Here we got more than two possibilities, hence we cannot solve the problem with if-else rather we use simple if

Simple if is capable to test any number possibilities.

 

Program:


#include<stdio.h>
void main()
{
int n;
printf("Enter any number:");
scanf("%d",&n);
if(n>=-9 && n<=9)
  printf("Sigle digit number");
if((n>=-99&&n<=10) || (n>=10&&n<=99))
  printf("Two digit number");
if((n>=-999&&n<=-100) || (n>=100&&n<=999))
  printf("Three digit number");
if(n>999)
  printf("Biggest number");
if(n<-999 code="" mallest="" number="" printf="">

 

 

Execution1:

Enter any number: 256

Three digit number

 

Execution2:

Enter any number: -25

Two digit number

 

Execution3:

Enter any number: 5

Single digit number

 

Execution4:

Enter any number: 2256

Biggest number

 

Execution5:

Enter any number: -1000

Smallest number

0 comments:

Post a Comment