# 5.6 – Programs to practice – if else statement – part2

by on September 15, 2017

6)  Accept the salary of an employee and print the HRA, DA and gross salary. If salary exceeds 10000 then HRA is 15% of salary and DA is 25% of salary. If salary is less than or equal to 10000 then HRA is 10% of salary and DA is 15% of salary.

```#include<stdio.h>
int main()
{
long int sal;
float hra,da,gross;
printf("Enter the salary:");
scanf("%ld",&sal);
if(sal>10000)
{
hra=(float)sal*15/100;
da=(float)sal*25/100;
}
else
{
hra=(float)sal*10/100;
da=(float)sal*15/100;
}
gross=sal+hra+da;
printf("Salary %f",sal);
printf("\nHouse Rent Allowance %f",hra);
printf("\nDearness Allowence %f",da);
printf("\nGross salary %f",gross);
return 0;
}
```

Execution 1:
Enter the salary: 15000
House Rent Allowance 2250
Dearness Allowance 3750
Gross salary 21000

Execution 2:
Enter the salary: 7500
House Rent Allowance 750
Dearness Allowance 1125
Gross salary 9375

7) Accept any positive integer and print its equivalent generic root.

Show Logic

Generic root is equivalent single digit number to the given number.

Say for example 7656 is the given number, sum of all the digits 7+6+5+6 is 24, again the sum of digits 2+4 is 6. Hence generic root to 7656 is 6

7656  →   7+6+5+6  →   24   →   2+4   →    6

Logic:
Remainder to any positive integer divide by 9 would be the generic root.

5    %    9    →      5   (5)
8    %    9    →      8   (8)
10   %    9    →      1   (1)
17   %    9    →      8   (1+7 – 8)
256 %    9    →      4   (2+5+6 – 13 – 4)

It is not the case with multiples of 9 because remainder to any 9 multiple divide by 9 is always 0.
Fortunately generic root to any 9 multiple is 9 only

9   %    9    →    0   (9)
18  %    9    →    0   (1+8)
81  %    9    →     0   (8+1)
729 %    9    →    0   (7+2+9 – 18 – 1+8 – 9 )

When we divide a number by 9 and if the remainder is zero then the generic root is 9, otherwise remainder is the generic root.

```if(num%9==0)  /* 9 multiple */
printf("%d",9);
else
printf("%d",num%9);
```

Program:

```#include <stdio.h>
int main()
{
int num;
printf("Enter any positive integer: ");
scanf("%d",&num);
if(num%9==0)         /* 9 multiple */
printf("Generic root %d",9);
else
printf("Generic root %d",num%9);
return 0;
}
```

Execution 1:
Enter any positive integer: 7645
Generic root 4

Execution 2:
Enter any positive integer: 5
Generic root 5

Execution 3:
Enter any positive integer: 81
Generic root 9

8)  Accept any three digit number and print whether number is an Armstrong number or not.

Logic:
If the sum of cubes of individual digits is equal to the given number then the number is called Armstrong number.

Examples: 153, 370, 371, 407

```#include<stdio.h>
int main()
{
int n,first,second,third,sum,temp;
printf("Enter any three digit number:");
scanf("%d",&n);
temp=n;
third=n%10;
n=n/10;
second=n%10;
n=n/10;
first=n%10;
sum=first*first*first+second*second*second+third*third*third;
if(sum==temp)
printf("Armstrong number");
else
printf("Not an Armstrong number");
return 0;
}
```

Execution 1:
Enter any three digit number: 153
Armstrong number

Execution 2:
Enter any three digit number: 654
Not an Armstrong number

9)  Accept the pervious, current month readings of energy meter and print the number of units and bill. If number of units exceeds 500 then charge 5.75 per unit otherwise charge 3.25 per unit.

```#include<stdio.h>
int main()
{
int cmr,pmr,nu;
float bill;
printf("Enter the current month reading:");
scanf("%d",&cmr);
printf("Enter the previous month reading:");
scanf("%d",&pmr);
nu=cmr-pmr;
if(nu>500)
bill=nu*5.75;
else
bill=nu*3.25;
printf("No of units %d\nTotal bill %f",nu,bill);
return 0;
}
```

Execution 1:
Enter the current month reading: 500
Enter the previous month reading: 200
No of units 300
Total bill 975

Execution 2:
Enter the current month reading: 1500
Enter the previous month reading: 800
No of units 700
Total bill 4025

10) Accept three angles of a triangle and print whether the triangle is valid or not?

Logic: Sum of all the angles must be 180.

```#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter three angles of triangle:\n");
scanf("%d%d%d",&a,&b,&c);
if(a+b+c==180)
printf("Triangle is valid");
else
printf("Triangle is not valid");
return 0;
}
```

Execution 1:
Enter three angles of triangle:
45 60 67
Triangle is not valid

Execution 2:
Enter three angles of triangle:
90 40 50
Triangle is valid

11)  Accept any number and print its equivalent absolute value.

Logic:
The absolute value of a number may be thought of as its distance from zero.

```#include<stdio.h>
int main()
{
float x;
printf("Enter any number:");
scanf("%f",&amp;x);
if(x>=0)
printf("%f",x);
else
printf("%f",-1*x);//converting negative into positive
return 0;
}
```

Execution 1:
Enter any number:56.40
56.400000

Execution 2:
Enter any number:-45.50
45.500000

Previous post:

Next post: