5.9 – Programs to practice – Logical expressions

by subbu on September 19, 2013

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

With relational expressions:

temp=n;
-------
-------
rev=fourth*1000+third*100+second*10+first*1;
if(temp==rev)
  printf("Palindrome number");
else
  printf("Not a palindrome number");
/* with logical expression */
#include<stdio.h>
int 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");
return 0;
}

Execution 1:
Enter any four digit number: 2332
Palindrome number

Execution 2:
Enter any four digit number: 2342
Not a palindrome number

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

With relational expressions:

if(n>=-9)
{
  if(n<=9)
    printf("Single digit number");
  else
    printf("Not a single digit number");
}
else
   printf("Not a single digit number");
/* With logical expression */
#include<stdio.h>
int 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");
return 0;
}

Execution 1:
Enter any number: -8
Single digit number

Execution 2:
Enter any number: 5
Single digit number

Execution 3:
Enter any number: 45
Not a single digit number

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

Logic:
Two digit number or not

#include<stdio.h>
int 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");
return 0;
}

Execution 1:
Enter any number: -50
Two digit number

Execution 2:
Enter any number: 55
Two digit number

Execution 3:
Enter any number: 9
Not a two digit number

4) Accept any year and print whether the year is a leap year or not.

With relational expression:

if(year%100==0)
{
  if(year%400==0)
    printf("Leap year");
  else
    printf("Not a leap year");
}
else
{
  if(year%4==0)
    printf("Leap year");
  else
    printf("Not a leap year");
}
/* with logical expression */
#include<stdio.h>
int main()
{
int year;
printf("Enter any year:");
scanf("%d",&year);
if(year%100==0&&year%400==0||year%100!=0&&year%4==0)
   printf("Leap year");
else
   printf("Not a leap year");
return 0;
}

Execution 1:
Enter any year: 1900
Not a leap year

Execution 2:
Enter any year: 2000
Leap year

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

#include<stdio.h>
int main()
{
int math,phys,chmst;
printf("Enter the marks in Math, Physics and chemistry..\n");
scanf("%d%d%d",&math,&phys,&chmst);
if(math>=50 && phys>=50 && chmst>=50)
  printf("Pass");
else
  printf("Fail");
return 0;
}

Execution 1:
Enter the mark in Math, Physics and chemistry…
60   80   92
Pass

Execution 2:
Enter the mark in Math, Physics and chemistry…
52   86   12
Fail

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

#include<stdio.h>
int 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");
return 0;
}

Execution 1:
Enter any alphabet: e
Vowel

Execution 2:
Enter any alphabet: p
Consonant

7) Accept three sides of box and print whether the box is a cube or not?

#include<stdio.h>
int main()
{
int b,w,h;
printf("Enter breadth, width and height of a box:\n");
scanf("%d%d%d",&b,&w,&h);
if(b==w&&w==h)
  printf("Cube");
else
  printf("Not a cube");
return 0;
}

Execution 1:
Enter breadth, width and height of a box:
23 45 55
Not a cube

Execution 2:
Enter breadth, width and height of a box:
50  50  50
Cube

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

#include<stdio.h>
int main()
{
int age;
float percent;
char gender;
printf("Enter the gender m/f :");
scanf("%c",&gender);
printf("Age:");
scanf("%d",&age);
printf("Enter the percentage in qualifying exam:");
scanf("%f",&percent);
if(gender=='m' && percent>=60 && age>25 && age<34)
  printf("Selected");
else
  printf("Not selected");
return 0;
}

Execution 1:
Enter the gender m/f : m
Age: 30
Enter the percentage in qualifying exam: 75
Selected

Execution 2:
Enter the gender m/f : f
Age: 34
Enter the percentage in qualifying exam: 54
Not selected

9) Accept the gender, age of a person and print whether major or minor.

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

#include<stdio.h>
int main()
{
char gender;
int age;
printf("Enter the gender m/f:");
scanf("%c",&gender);
printf("Enter the age:");
scanf("%d",&age);
if(gender==’m’ && age>=21||gender==’f’ && age>=18)
  printf("Major");
else
  printf("Minor");
return 0;
}

Execution 1:
Enter the gender m/f: m
Enter the age: 27
Major

Execution 2:
Enter the gender m/f: f
Enter the age: 20
Major

Execution 3:
Enter the gender m/f: m
Enter the age: 15
Minor

Previous post:

Next post: