5.5 – Programs to practice – if else statement – Part1

by subbu on September 15, 2013

1)  Accept two numbers and print whether they are equal or not equal.

find equal or not

/* find equal or not */
#include<stdio.h>
int main()
{
float a,b;
printf("Enter two numbers:\n");
scanf("%f%f",&a,&b);
if(a==b)
  printf("Equals");
else
  printf("Not equals");
return 0;
}

Execution 1:
Enter two numbers:
25.56    45.78
Not equals

Execution 2:
Enter two numbers:
25.50     25.50
Equals

2)  Accept the age of a person and print whether has right to vote or not.

has right to vote or not

#include<stdio.h>
int main()
{
int age;
printf("Enter the age of a person:");
scanf("%d",&age);
if(age>=18)
  printf("Has right to vote");
else
  printf("Has no right to vote");
return 0;
}

Execution 1:
Enter the age of a person: 45
Has right to vote

Execution 2:
Enter the age of a person: 15
Has no right to vote

3)  Accept any three digit number and print whether the number is a palindrome number or not.

Logic:
If the reverse of a number is equal to the same number then the number is called palindrome number.
In case of a three digit number the first and third digits must be equals.

palindrome number or not

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

Execution 1:
Enter any three digit number: 343
Palindrome number

Execution 2:
Enter any three digit number: 345
Not a palindrome number

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

Logic:
In case of a four digit number, it is not enough to compare first and fourth digits.
Hence we need to compare the actual number with its reverse.
“n” will become zero while separating individual digits from the number.
That is why the value of “n” (original value) is assigned to “temp” to compare with reverse later.

palindrome number or not 4 digit number

#include<stdio.h>
int main()
{
int n,first,second,third,fourth,rev,temp;
printf("Enter any four digit number:");
scanf("%d",&n);
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");
return 0;
}

Execution 1:
Enter any four digit number: 2345
Not a palindrome number

Execution 2:
Enter any four digit number: 2442
Palindrome number

5)  Accept the salary, sales of a sales person and print the salary, commission and the gross salary. Grant 7% commission if the sales exceeds to 35000 otherwise grant 3% commission.

#include<stdio.h>
int main()
{
long int salary,sales;
float comm,gross;
printf("Enter the salary:");
scanf("%ld",&salary);
printf("Enter the sales:");
scanf("%ld",&sales);
if(sales>35000)
 comm=(float)sales*7/100;
else
 comm=(float)sales*3/100;
gross=salary+comm;
printf("Salary %f",salary);
printf("\nCommission %f",comm);
printf("\nGross salary %f",gross);
return 0;
}

Execution 1:
Enter the salary: 15000
Enter the sales: 48000
Salary 15000
Commission 3360
Gross salary 18360

Execution 2:
Enter the salary: 10000
Enter the sales: 32000
Salary 10000
Commission 960
Gross salary 10960

Previous post:

Next post: