Exercise 1: Conditional Operator
A Parking Payment Machine only have RM1 and RM5 for balance return and can only return either RM1 or RM5 per transaction. It only accepts RM5,RM10,RM20 and RM50. Ahmad has been parking for 4 ½ hrs and the exact amount for his parking is RM6. You need to create a program to evaluate the return balance with the amounts of either RM5 or RM1 depending on the user payment. If the remainder of the transaction can be divided by 5 then use RM5 for the balance or else use RM1. Use conditional operator (? : ) to create your program. Use also appropriate scanf and printf for the program.
Coding
#include<stdio.h>
#include<stdio.h>
main()
{
int input1, balance1, totalrm,Phr,returnbal,bilduit;
/*calculate the total hours of parking first 2 hrs rm5 subsequent 1hr rm2*/
printf(“This machine only accepts RM5,RM10,RM20 and RM50\n”);
scanf(“%d”,&input1);
Phr=4.5;
totalrm=6;
returnbal=input1-totalrm;
balance1=(returnbal%5==0)?5:1;
bilduit=returnbal/balance1;
printf(“the balance amount = RM%d and there are %d RM%d,”,returnbal,bilduit,balance1);
}
Exercise 2: if-else statement
Write a program that calculates the total hours of parking and print on the screen the amount to pay. It requires the input from the user how many hours he/she parked (format hours ex:1.5).
First 2 hrs RM2 subsequent 1hr RM2 for non-member
For members: First 2 hrs free, subsequent 1hour RM2.
Coding:
#include<stdio.h>
main()
{
int baki;//input1, balance1, totalrm,Phr,returnbal,bilduit;
char ans;
float hour;
/*please insert ticket(actually insert your parking hours*/
printf("How many hours did you park (ex:1.5, 0.5)\n");
scanf("%f",&hour);
printf("are you a member(y/n))\n");
scanf(" %c",&ans);
//printf("%c",ans);
if(ans == 'y')
{
/*calculate the total hours of parking first 2 hrs free subsequent 1hr rm2*/
if(hour<=2)
{
printf("admission is free");
}
else
{
baki=(((int)hour)-2)*2;
printf("please pay RM%d",baki);
}
}
/*calculate the total hours of parking first 2 hrs rm2 subsequent 1hr rm2*/
else
{
if(hour<=2)
{
printf("please pay RM2");
}
else
{
baki=2+(((int)hour)-2)*2;
printf("please pay RM%d",baki);
}
}
EXERCISE ON FUNCTIONS
Coding: opt 1
#include<stdio.h>
#include<math.h>
float findroot(float det,float a,float b,float c)
{
float root1,root2,root3,r4,i4;
if(det>0)
{
printf("the input in subprogram displays: a=%.2f,b=%.2f,c=%.2f\n",a,b,c);
root1=-b+sqrt(det/(2*a));
root2=-b-sqrt(det/(2*a));
printf("root1=%.2f,root2=%.2f",root1,root2);
return;
}
else if(det==0)
{
printf("the input in subprogram displays: a=%.2f,b=%.2f,c=%.2f \n",a,b,c);
root3=-b/(2*a);
printf("root1=root2=%.2f",root3);
return;
}
else
{
printf("the input in subprogram displays: a=%.2f,b=%.2f,c=%.2f\n",a,b,c);
r4=-b/(2*a);
i4=sqrt(-det)/(2*a);
printf("root1=%.2f+i%.2f and root 2=%.2f-i%.2f",r4,i4,r4,i4);
return;
}
}
int main()
{
float a,b,c,determinant;
printf("this program solves for eq ax^2+bx+c=0");
printf("\nplease input the value for a,b and c, a must not be zero\n");
scanf("%f %f %f",&a,&b,&c);
printf("a=%.2f,b=%.2f,c=%.2f\n",a,b,c);
determinant=b*b-4*a*c;
findroot(determinant,a,b,c);
}
coding: Option 2
#include<stdio.h>
#include<math.h>
//original question input a=1.2,b=2.3, c=3.4
float findroot(float det); //dummy or prototype
float a,b,c; //declare global variables
int main()
{
//no need to declare the global variables again
//float a,b,c,determinant;
float determinant;
printf("this program solves for eq ax^2+bx+c=0");
printf("\nplease input the value for a,b and c, a must not be zero\n");
scanf("%f %f %f",&a,&b,&c);
printf("a=%.2f,b=%.2f,c=%.2f\n",a,b,c);
determinant=b*b-4*a*c;
findroot(determinant);
}
float findroot(float det)
{
float root1,root2,root3,r4,i4;
if(det>0)
{
printf("the input in subprogram displays: a=%.2f,b=%.2f,c=%.2f\n",a,b,c);
root1=-b+sqrt(det/(2*a));
root2=-b-sqrt(det/(2*a));
printf("root1=%.2f,root2=%.2f",root1,root2);
return;
}
else if(det==0)
{
printf("the input in subprogram displays: a=%.2f,b=%.2f,c=%.2f \n",a,b,c);
root3=-b/(2*a);
printf("root1=root2=%.2f",root3);
return;
}
else
{
printf("the input in subprogram displays: a=%.2f,b=%.2f,c=%.2f\n",a,b,c);
r4=-b/(2*a);
i4=sqrt(-det)/(2*a);
printf("root1=%.2f+i%.2f and root 2=%.2f-i%.2f",r4,i4,r4,i4);
return;
}
}