Program that determines the class of the ship depending on its class ID.
Use logical operator
CLASS ID. SHIP CLASS
B or b BATTLESHIP
C or c. CRUISER
D or d. DESTROYER
F or f FRIGATE
//C program to determine class of ship based on class ID
#include <stdio.h>
#include <stdlib.h>
int main()
{
//Variable to hold user provided class character
char classOfShip;
//Prompt user to enter the class ID
printf("Enter the class of ship ID: ");
//Read user input
scanf("%c", &classOfShip);
//Check class using if statement
if(classOfShip == 'b' | classOfShip == 'B')
{
printf("Battle Ship");
}
else if(classOfShip == 'c' | classOfShip == 'C')
{
printf("Cruiser");
}
else if(classOfShip == 'd' | classOfShip == 'D')
{
printf("Destroyer");
}
else if(classOfShip == 'f' | classOfShip == 'F')
{
printf("Frigate");
}
else
{
printf("Unknown class. Please try again.");
}
}
Comments
Leave a comment