Coding for DUMMIES 05 : Decision making in C

  


today lets talk about how decision making in c works.



In here if the condition is true the control is passed to the statement that is edited within the braces. If the condition is not satisfied the control is passed to the first statement that comes after the close braces.


In this program we can see two conditions. Tn here first, output will be checked by the first condition. If that condition satisfied the control passed to the next statement that executes under that particular if condition. If the condition does not satisfied the control passed to the next if condition and checks according to it. In here we do not need to use braces. Since there is only one statement per if condition.



In here in the if condition we can see a compound relational test. In here when executing the if condition first it evaluate the inner parenthesis. Then it execute the OR condition.



In this program the second if condition executes within the first if condition and this else statement is under the second if condition.



In here you can see a new keyword. Which is else if. We use else if  when there is multiple if condition to evaluate. This enhances the readability of the program.


This program is also same as the previous program, which used else if statements.


In this program we have used switch statement. We can use this statement when there is multiple situations to deal with . However it doesn't allow us to use expressions and conditions as a case statement. We use default statement to run when there is no match for any case. By using break; we are able to prevent the control passing sequentially all the other statements in the program even it found the matching case.

Now lets apply what we learned on some questions...

Question 01
Swap two values stored in two different variables.


#include <stdio.h>
int main()
{
int a, b, temp;
a = 10;
b = 20;
temp = 0;
printf("value in a before swap = %i\n" ,a);
printf("value in b before swap = %i\n" ,b);
temp=a;
a=b;
b=temp;
printf("value in a after swap = %i\n" ,a);
printf("value in b after swap = %i\n" ,b);   
return 0;
}


Question 02
Check whether an entered number is negative, positive or zero.


#include<stdio.h>
int main()
{
float num;
printf("Please Enter Number : ");
scanf("%f",&num);
if(num>0)
printf("Positive");
else if(num<0)
printf("Negative");
else
printf("ZERO");
return 0;
}

Question 03
Check whether an entered year is leap year or not.

#include<stdio.h>
int main()
{
int year;
printf("Enter the year : ");
scanf("%i", &year);
if (year%4 == 0)
printf("Year %i is a leap year",year);
else
printf("Year %i is not a leap year",year);
return 0;
}

Question 04
Write a program that asks the user to type in two integer values at the terminal. Test these
two numbers to determine if the first is evenly divisible by the second, and then display
an appropriate message at the terminal.

#include<stdio.h>
int main()
{
int val1,val2;
printf("Enter two values below\n");
scanf("%i",&val1);
scanf("%i",&val2);
if (val1 % val2 == 0)
printf("value %i is evenly divisible by value %i", val1,val2);
else
printf("value %i is not evenly divisible by value %i", val1,val2);
return 0;
}

Question 05
Write a program that accepts two integer values typed in by the user. Display the result of
dividing the first integer by the second, to three-decimal-place accuracy. Remember to
have the program check for division by zero.

#include<stdio.h>
int main()
{
float val1,val2;
printf("Enter two values below\n");
scanf("%f",&val1);
scanf("%f",&val2);
if (val2 != 0)
printf("%.2f  / %.2f = %.3f",val1,val2,val1/val2);
else
printf("Cannot divide by 0");
return 0;
}

Question 06
Write a program that takes an integer keyed in from the terminal and extracts and
displays each digit of the integer in English. So, if the user types in 932, the program
should display nine three two. Remember to display “zero” if the user types in just a 0.

#include <stdio.h>
int main()
{
int value,div;
int digit = 0,  inDigit = 0,inValue = 0;
printf("Enter the integer : ");
scanf("%i",&value);
if (value == 0)
printf("Zero");
else
{
while(value !=0)
{
inDigit = value%10;
inValue = inValue * 10 + inDigit;
value = value/10;
}
while(inValue !=0)
{
digit = inValue%10;
switch (digit)
{
case 0:
{
printf("Zero");
break;
}
case 1:
{
printf("One");
break;
}
case 2:
{
printf("Two ");
break;
}
case 3:
{
printf("Three ");
break;
}
case 4:
{
printf("Four ");
break;
}
case 5:
{
printf("Five ");
break;
}
case 6:
{
printf("Six ");
break;
}
case 7:
{
printf("Seven ");
break;
}
case 8:
{
printf("Eight ");
break;
}
case 9:
{
printf("Nine ");
break;
}
default:
{
printf("Invalid digit");
break;
}
}
inValue = inValue/10;    
}
}
return 0;
}


Question 07
Input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer.
Calculate percentage and grade according to following:
a. Percentage >= 90% : Grade A
b. Percentage >= 80% : Grade B
c. Percentage >= 70% : Grade C
d. Percentage >= 60% : Grade D
e. Percentage >= 40% : Grade E
f. Percentage < 40% : Grade F

#include <stdio.h>
int main()
{
int mat,phy,che,bio,com;
float total,perc;
printf("Input marks for Physics : ");
scanf("%i",&phy);
printf("Input marks for Chemistry : ");
scanf("%i",&che);
printf("Input marks for Biology : ");
scanf("%i",&bio);
printf("Input marks for Mathematics : ");
scanf("%i",&mat);
printf("Input marks for Computer : ");
scanf("%i",&com);
total = phy+che+mat+bio+com;
perc = total/5;
if (perc>=90)
printf("Grade A");
else if (perc>=80)
printf("Grade B");
else if (perc>=70)
printf("Grade C");
else if (perc>=60)
printf("Grade D");
else if (perc>=40)
printf("Grade E");
else
printf("Grade F");
return 0;
}

Question 08
Input basic salary of an employee and calculate its Gross salary according to following:
(note: HRA and DA are allowances)
a. Basic Salary <= 10000 : HRA = 20%, DA = 80%
b. Basic Salary <= 20000 : HRA = 25%, DA = 90%
c. Basic Salary > 20000 : HRA = 30%, DA = 95%

#include<stdio.h>
int main()
{
float salary,da,hra;
printf("Enter the Salary : ");
scanf("%f",&salary);
if (salary <= 10000)
{
da=salary*0.8;
hra=salary*0.2;
salary = salary+da+hra;
printf("Gross salary : %.2f LKR",salary);
}
else if (salary <= 20000)
{
da=salary*0.9;
hra=salary*0.25;
salary = salary+da+hra;
printf("Gross salary : %.2f LKR",salary);
}
else
{
da=salary*0.95;
hra=salary*0.3;
salary = salary+da+hra;
printf("Gross salary : %.2f LKR",salary);
}
return 0;
}

Question 09
Write a program that acts as a simple “printing” calculator. The program should allow the
user to type in expressions of the form number operator: The following operators should
be recognized by the program: + - * / S E
The S operator tells the program to set the “accumulator” to the typed-in number.
The E operator tells the program that execution is to end.
The arithmetic operations are performed on the contents of the accumulator with the
number that was keyed in acting as the second operand. The following is a “sample run”
showing how the program should operate:
Begin Calculations
10 S Set Accumulator to 10
= 10.000000 Contents of Accumulator
2 / Divide by 2
= 5.000000 Contents of Accumulator
55 - Subtract 55
-50.000000
100.25 S Set Accumulator to 100.25
= 100.250000
4 * Multiply by 4
= 401.000000
0 E End of program
= 401.000000
End of Calculations.
Make certain that the program detects division by zero and also checks for unknown
operators.

#include<stdio.h>
int main()
{
float accumulator = 0.0,input;
char operator;
do 
{
printf("input number and operator : ");
scanf("%f %c",&input,&operator);
printf("\n");
switch(operator)
{
case 'S':
accumulator = input;
break;
case '+':
accumulator = accumulator + input;
break;
case '-':
accumulator = accumulator - input;
break;
case '*':
accumulator = accumulator * input;
break;
case '/':
{
if (input != 0)
{
accumulator = accumulator / input;
break;
}
else
{
printf("Cannot divide by 0");
break;
}
}
case 'E':
break;
default:
printf("Unknown operator\n");
break;
}
printf("accumulator value = %.6f\n",accumulator);
}
while(operator != 'E');
return 0;
}

Question 10
Input electricity unit charges and calculate total electricity bill according to the given
condition:
a. For first 50 units Rs. 0.50/unit
b. For next 100 units Rs. 0.75/unit
c. For next 100 units Rs. 1.20/unit
d. For unit above 250 Rs. 1.50/unit
e. An additional surcharge of 20% is added to the bill

#include <stdio.h>
int main()
{
int unit;
float bill,totalBill;
const float SURCHARGE = 0.2;
printf("Input Electricity Units : ");
scanf("%i",&unit);
if (unit<=50)
bill = unit*0.5;
else if (unit<=150)
bill = (unit-50)*0.75+50*0.5;
else if (unit<=250)
bill = (unit-150)*1.2+50*0.5+100*0.75;
else
bill = (unit-250)*1.5+50*0.5+100*0.75+100*1.2;
totalBill = bill + bill*SURCHARGE;
printf("Total Bill : %.2f LKR",totalBill);
return 0;
}


Question 11
An envelope manufacturing company hires people to make envelopes. They provide all
the raw material needed and pay at the following rates. Write a program to input the no of
envelopes made and to calculate and print the amount due
Envelopes Rate
1-1000 75 cents
1001-1500 1 rupee
1501-2000 1 rupee and 15 cents
2001- 1 rupee and 25 cents

#include <stdio.h>
int main()
{
int envelop;
float amount;
printf("Input number of envelops made  : ");
scanf("%i",&envelop);
if (envelop >0 && envelop<=1000)
amount = envelop*0.75;
else if (envelop>1000 && envelop <=1500)
amount = (envelop-1000)*1+1000*0.75;
else if (envelop>1500 && envelop <=2000)
amount = (envelop-1500)*1.15+1000*0.75+500*1;
else if (envelop>2000)
amount = (envelop-2000)*1.255+1000*0.75+500*1+500*1.15;
printf("Total due amount : %.2f LKR",amount);
return 0;
}

Question 12
Find the number of separate Notes and coins required to represent a given monetary
value. E,g, 2700 required 1 - 2000 note, 1 - 500 note and 2 - 100 notes.

#include <stdio.h>
int main()
{
int money,i;
const int MONEY_V[10] = {5000,1000,500,100,50,20,10,5,2,1};
int note[10] = {0,0,0,0,0,0,0,0,0,0};
printf("Enter monetery value : ");
scanf("%i",&money);
for(i=0;i<10;i++)
{
note[i] = money / MONEY_V[i];
money = money % MONEY_V[i];
}
for(i=0;i<10;i++)
{
if(i<=5)
printf("%5i notes : %2i\n",MONEY_V[i],note[i]);
else
printf("%5i coins : %2i\n",MONEY_V[i],note[i]);
}
return 0;
}

Question 13
Display Age, Birthday, and Gender using a given National Identity Card number

#include<stdio.h>
int main()
{
long long int id_num,i,age;
int B_day = 0;
int B_year = 0;
long long int id_store[12];
printf("Enter the ID card number (12 digits) : ");
scanf("%lli",&id_num);
for(i=11;i>=0;i--)
{
id_store[i] = id_num % 10;
id_num = id_num /10;
}
for(i=0;i<4;i++)
{
B_year = B_year*10+id_store[i];
}
printf("Birth Year : %i\n",B_year);
printf("Age : %i\n",2024-B_year);
/*gender*/
for(i=4;i<7;i++){
B_day = B_day*10+id_store[i];
}
if (B_day >= 0 && B_day <=365)
printf("Gender : Male\n");
else if (B_day >=500 && B_day <=865)
printf("Gender : Female\n");
return 0;
}

Now we hope you have learned about C operators and how to use them in programming well . so until next make sure to practice programming everyday. practice what makes you a good programmer.

contribution : - Sandakelum Kumarasiri 5737    @SK2208
                        Ranishka Gunathilake   5641  @RG761 
                        Mahesh Wijenayaka    5734 @MW1513