In: Computer Science
1.
#include <stdio.h>
int main(){
double oldOdometer, newOdometer, gallonsGas;
printf("Enter the old odometer reading: ");
scanf("%lf", &oldOdometer);
printf("Enter the new odometer reading: ");
scanf("%lf", &newOdometer);
printf("Enter the number of gallons: ");
scanf("%lf", &gallonsGas);
double mileage = (newOdometer - oldOdometer) / gallonsGas;
printf("The mileage is: %lf\n", mileage);
}
2.
#include <stdio.h>
int main(){
int arr[] = {1, 2, 3, 4, 5};
int sum = 0, i;
for(i = 0; i < 5; i++) {
sum += arr[i];
}
printf("Sum is: %d\n", sum);
}
---------------------------------------------
#include <stdio.h>
int main(){
int arr[5];
int sum = 0, i;
for(i = 0; i < 5; i++) {
arr[i] = 2 * (i + 1);
sum += arr[i];
}
printf("Sum is: %d\n", sum);
}
3.
#include <stdio.h>
int main(){
int tickets, age;
printf("Enter the age: ");
scanf("%d", &age);
printf("Enter the number of tickets received: ");
scanf("%d", &tickets);
double totalCost = 0;
if(age < 21){
totalCost = 1500 + 250 * tickets;
} else if(age < 24) {
totalCost = 1200 + 250 * tickets;
} else {
totalCost = 1000 + 200 * tickets;
}
printf("Age: %d, Insurance cost: %lf\n", age, totalCost);
}