A mock for my 2nd in class test using 'if' statements. Not THE most efficient way of doing it, but it functions, and the code is fairly neat. I was happy with this.
Here is the source code:
#include<stdio.h> #include<stdlib.h> int main() { int fuelType, engineSize, cost; printf("Please enter 1 if your car is petrol, and 2 if diesel: "); scanf("%d", &fuelType); printf("\nPlease enter your engine size in cubic cm: "); scanf("%d", &engineSize); if((fuelType==1)) { if((engineSize<=1900)) { cost=30; printf("\nYour running costs for 100 miles will be %d pounds.\n", cost); } else if((engineSize>1900)&&(engineSize<=2100)) { cost=40; printf("\nYour running costs for 100 miles will be %d pounds.\n", cost); } else if((engineSize>2100)) { cost=50; printf("\nYour running costs for 100 miles will be %d pounds.\n", cost); } } else if((fuelType==2)) { if((engineSize<=1900)) { cost=35; printf("\nYour running costs for 100 miles will be %d pounds.\n", cost); } else if((engineSize>1900)&&(engineSize<=2100)) { cost=45; printf("\nYour running costs for 100 miles will be %d pounds.\n", cost); } else if((engineSize>2100)) { cost=55; printf("\nYour running costs for 100 miles will be %d pounds.\n", cost); } } return 0; }
















