Programming Challenge #2

Here’s an exercise for you programming enthusiasts like myself. Good Luck! (:toilet: : I wrote this while on toilet and I enjoy the look of this emoji, TMI?)

A small insurance company asks you to create a program to determine the cost of automobile insurance premium for each family member, based on their ages. The basic insurance price is 1000 per year per person.

Write a program that prompts the user for numbers of drivers in a family. Then use a repetition control structure to process each family member by asking for the name and age of each driver. Display the insurance amount for each family member. Use a method/function that takes the driver’s age as input and returns the insurance premium amount.

Calculation for premium amounts are:
| Ages 16 to 19 | Additional 15% of basic price
| Ages 20 to 25 | Additional 5% of basic price
| Age over 25 | Discount 10% of basic price
| Any other age | No insurance

Sample input and output could look like this:

Please enter the number of drivers in your family: 3
Driver 1 name: Wawa
Driver 1 age: 31
The amount of insurance for Wawa: $900.00
Driver 2 name: Phoenix750
Driver 2 age: 20
The amount of insurance for Phoenix750: $1050.00
Driver 3 name: Morgan Freeman
Driver 3 age: 16
The amount of insurance for Morgan Freeman: $1150.00

4 Likes

Does it need to be written in any specific language?

-Phoenix750

I’ll be writing it in C++. I would say Python is also eligible for that particular program.

Nope, but I wrote it in java.

Alright! This was a fun 34 lines for me.

#include <iostream>
#include <string>
#include <vector>
 
int main() {
    std::cout << "Please enter the number of drivers in your family: ";
    int drivers; std::cin >> drivers;
 
    std::string name;
    unsigned int age;
 
    for (int i = 0; i < drivers; i++) {
        std::cout << "Driver " << i+1 << " name: ";
        std::cin >> name;
 
        std::cout << "Driver " << i+1 << " age: ";
        std::cin >> age;
 
        int cost = 1000;
 
        if (age < 16) {
            cost *= 0;
        } else if (age <= 19) {
            cost *= 1.15;
        } else if (age <= 25) {
            cost *= 1.05;
        } else {
            cost *= 0.9;
        }
 
        std::cout << "The cost of insurance for " << name << " is $" << cost \
        << ".\n\n";
    }
}

pastebin here
And I shared this Topic on twitter.
P.S. It’s C++.

1 Like

Wahoo! I didn’t think anyone would finish so quickly. I will make the next one more challenging.
:toilet: ^.^ Yum, Toilet.

I am currently writing a program in C++ as well. Expect it to be up later!

-Phoenix750

1 Like

there are additional letters in your code that doesn’t need to be included. the ‘std::cout’ doesnt have to be specified

just so you dont have to type as much

What exactly do you mean?

2 Likes

Just wondering: “any other age” gets the basic price for their insurance?

-Phoenix750

Any other ages means any age higher than 25 gets the basic plan. @Phoenix750

Yes. Meaning anyone under 16

nevermind, I misread.

for i in range(int(raw_input("Please enter the number of drivers in your family: "))): print("The amount of insurance for "+raw_input("Driver "+str(i+1)+" name: ")+": $"+str(str([1150 if (16 <= age <= 19) else ( 1050 if (20 <= age <= 25) else ( 900 if age > 25 else 0)) for age in [int(raw_input("Driver "+str(i+1)+" age: "))]])).strip("[]"))

Alright! This was a fun 1 line for me.

5 Likes

Sorry I couldn’t reply sooner, currently in class :3 But any other age meaning under 16 gets no insurance. So Merozey is correct.

1 Like

Once again I apologize for not responding sooner!

Has anyone tried java? I am excited to see what they got, my programming is still in its infancy. :toilet:

There is unreadable code. And then there’s JSchmoe code…

-Phoenix750

3 Likes
import java.util.Scanner;

class ChallengeTwo {
    private final int baseInsurance = 1000;
    private int numDrivers;
    private String driverName;
    private int driverAge;
    
    public ChallengeTwo() {
        
    }
    
    public static void main (String[] args) {
        ChallengeTwo ct = new ChallengeTwo();
        ct.start();
    }
    
    private void start() {
        System.out.print("Please enter the number of drivers in your family: ");
        Scanner sc = new Scanner(System.in);
        numDrivers = sc.nextInt();
        sc.nextLine();

        for (int i = 0; i < numDrivers; i++) {
            System.out.print("Driver " + (i + 1) + " name: ");
            driverName = sc.nextLine();
            System.out.print("Driver " + (i + 1) + " age: ");
            driverAge = sc.nextInt();
            sc.nextLine();
            System.out.println("The amount of insurance for " + driverName + ": $" + calcPremiums(driverAge));  
        }
    }
    
    private double calcPremiums(int age) {
        double premium = 0;
        if (age >= 16 && age <= 19) 
            premium = baseInsurance * 1.15;
        else if (age >= 20 && age <= 25)
            premium = baseInsurance * 1.05;
        else if (age > 25)
            premium = baseInsurance * 0.90;
        
        return premium;
    }
}
2 Likes