Answer to Question #161204 in C for Angeline Moran

Question #161204

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


1
Expert's answer
2021-02-04T10:42:49-0500
//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.");
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS