#include <stdio.h>
float premium(int age)
{
if (age < 20)
{
return 1000*1.15;
}
else if (age < 26)
{
return 1000*1.05;
}
else if (age > 25)
{
return 1000*0.9;
}
}
int main(int argc, char *argv[])
{
printf("Number of drivers in your family: ");
int num;
scanf("%i", &num);
char names[num][20];
int ages[num];
for(int i = 0; i<num; i++)
{
printf("Enter name of the driver[%d]:", i+1);
scanf("%s", names[i]);
printf("Enter age of the driver[%d]:", i+1);
scanf("%i", &ages[i]);
if (ages[i] > 15)
{
printf("The amount of insurance for %s: %f\n", names[i], premium(ages[i]));
}
else
{
printf("The amount of insurance for %s: No insurance\n", names[i]);
}
}
}
Ohh nice!!! Good job fellas!
Good old Python:
#!/usr/bin/env python
basic = 1000
def get_premium(age):
if 15 < age < 20:
return "$%.2f" % (basic + (15.0/100) * basic)
elif 19 < age < 26:
return "$%.2f" % (basic + (5.0/100) * basic)
elif age > 25:
return "$%.2f" % (basic - (10.0/100) * basic)
else:
return "*No insurance!*"
num = int(raw_input("Enter number of drivers in the family: "))
for i in range(num):
name = raw_input("Enter name: ")
age = int(raw_input("Enter age: "))
print "%s's premium: %s" % (name, get_premium(age))
Why not 1.15 * basic
rather than (basic + (15.0/100) * basic)
?
I was just going by the formula for getting the percentage… didn’t really think about simplifying it. That looks a lot nicer though
A Perl version using a lookup table, and featuring the use of last
#!/usr/bin/perl
sub raw_input{ print $_[0]; $a=<>; chomp($a);return $a;}
@d = ((16,0), (20,1.15), (26,1.05), (200, .9));
$base = 1000;
$n = raw_input ("Please enter the number of driver in your family :");
for ($i = 0, $j = 1; $i < $n; $i++, $j++)
{
$name = raw_input ("Driver $j Name : ");
$age = raw_input ("Driver $j Age : ");
die "Invalid Age" if $age > 200;
for ($k =0; $k < $#d; $k+=2) {last if ($age < $d[$k]);}
print "The amount of insurance for $name: " . $base * $d[$k + 1] . "\n";
}
This topic was automatically closed after 30 days. New replies are no longer allowed.