Sei sulla pagina 1di 2

Write a program that computes the value of 10 terms of sin(x) by using the following

formula:

x 2i −1 x3 x5 x 7
sin( x) = ∑ ( −1)
i +1
= x − + − + ...
i =1 ( 2i − 1)! 3! 5! 7!
Note that x should be in radian:
π
Radian = Degree ×
180

#include <stdio.h>

float sine(float x);

int main()
{
float x,y;

printf("Enter the x (in angle) :");


scanf("%f",&x);
y=x*3.1415/180;

printf("sin(%.2f) = %.5f\n",x,sine(y));
return 0;
}

float sine(float y)
{
float sum=0,xpower,fact;

int i,j,sign=-1;

for(i=1;i<=10;i++)
{
sign=-sign;
xpower=1;
fact=1;
for(j=1;j<=2*i-1;j++)
{
xpower=xpower*y;
fact=fact*j;
}
sum=sum+sign*xpower/fact;
}

return sum;
}
A parking garage charges a 2.00 YTL minimum fee to park for up to three hours. The
garage charges an additional 0.50 YTL per hour for each hour in excess of three hours. The
maximum charge for any given 24-hour period is 10.00 YTL. Assume that no car parks for
a longer than 24 hours at a time. Write a program that will calculate and print the parking
charges for each of three customers who parked their cars in this garage yesterday. You
should enter the hours parked for each customer. Your program should print the results in a
neat tabular format, and should calculate and print the total of yesterday’s receipts. The
program should use the function calculateCharges to determine the charge of each
customer. Your outputs should appear in the following format:

Car Hours Charge


-------------------------
1 1.5 2.00
2 4.0 2.50
3 24.0 10.00
TOTAL 29.5 14.50

#include <stdio.h>
double calculateCharges(float);
int main(void)
{
float hour1,hour2,hour3;
double charge1,charge2,charge3;
printf("Enter the first customer parking hour :");
scanf("%f",&hour1);
charge1=calculateCharges(hour1);
printf("Enter the second customer parking hour :");
scanf("%f",&hour2);
charge2=calculateCharges(hour2);
printf("Enter the third customer parking hour :");
scanf("%f",&hour3);
charge3=calculateCharges(hour3);
printf("\n\n%s%10s%12s\n","Car","Hour","Charge");
printf("-------------------------\n");
printf("%d%12.2f%12.2f\n",1,hour1,charge1);
printf("%d%12.2f%12.2f\n",2,hour2,charge2);
printf("%d%12.2f%12.2f\n",3,hour3,charge3);
printf("%s%8.2f%12.2f\n","TOTAL",hour1+hour2+hour3,charge1+charge2+charge3);
return 0;
}
double calculateCharges(float h)
{ double charge=0.0;
if(h==24.00)
charge=10.0;
else{
if(h<=3.00)
charge=2.0;
else
charge=2.0+(h-3.00)*0.5;
}
return charge;
}

Potrebbero piacerti anche