Write a program which asks a user to enter a 3digit apartmen

Write a program, which asks a user to enter a 3-digit apartment number of a 3-story building, and responds which floor the room is located based on the first number. For example. Apt. 105 is located on the first floor, and Apt. 211 is located on the second floor. If the user enters an apartment numbers that does not exist, your program would return an error message. Example session are show n below. >>Apt Please enter an apartment number: 213 The apartment is on the second floor. >> >>Apt Please enter an apartment number: -5 Sorry, that apartment does not exist. >> Run four different values, so that your command-window session will show cases corresponding to the first floor, second floor, third floor, or \"none of above.\" 1m-file, the command window session

Solution

#include <stdio.h>
#include <string.h>
int main(void)
{
int aptNo,floor;
char strfloor[10];

printf(\"\ Please enter an apartment number\");
scanf(\"%d\",&aptNo);
if(aptNo<0)
{
printf(\"\ Sorry the apartment does not exist\");
}

floor = aptNo/100; //extraxt the first digit of 3 digit number


if (floor ==1 )
strcpy(strfloor,\"first\"); //choose the string for floor number
else if (floor == 2)
strcpy(strfloor,\"second\");
else if (floor == 3)
strcpy(strfloor,\"third\");


if(floor>0)
printf(\"The apartment is on %s floor \",strfloor);

   return 0;
}

output

Success time: 0 memory: 2172 signal:0

 Write a program, which asks a user to enter a 3-digit apartment number of a 3-story building, and responds which floor the room is located based on the first n

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site