Starting from:
$30

$24

Review 1 Solution


Fundamentals: Basic C statements and Conditional Statements

    1. The following table shows some of the telephone area codes in Turkey. Please note that it is not the complete list.

Adana
322
Izmir
232
Aksaray
382
Istanbul
Anatolia – 216

Thrace - 212
Mersin
329

Sample run:
Enter an area code: 329
Mersin

Write a C program with if statements that reads a code and returns the major city that belongs to. If the user is entering a code which is not in the table below then your program needs to return “Area code cannot be recognized”. Please make sure that you differentiate between Anatolia and Thrace area codes in Turkey. c.

    2. Rewrite the program in (b) using a switch statement.

    3. Write a C program using if..else chain that takes the speed of a car (in km/h) as an integer input and determines whether the car is going very slowly, slowly, medium
speed, fast speed or very fast speed. Use the speed intervals below:
Speed > 10, very slow
Speed > 30, slow
Speed > 50, medium
Speed > 70, fast

Speed > 90, very fast

Sample run:
Enter speed: 65
        65 s medium speed

    4. Write a program that takes a date in month/day/year format and then displays the date in “legal” form. For example,

Enter date (dd/mm/yy): 19/07/14
Dated this 19th  day of July,2014.


Loops and Nested Loops

    5. Write a program using a for loop that prints out the following sequence exactly including commas: 2, 4, 6, 8, 10, 12, 14, 16

    6. Write a program using a while loop that calculates and prints the average of several integers. Assume that the last value to read with scanf() is sentinel number 9999.

A typical sequence would be 10 8 11 7 9 9999 Indicating that the average of all the values preceding 9999 is to be calculated..

Sample run:
Enter the values: 10 8 11 7 9 9999
Average is 9.00
    7. Write a nested loop that displays the following output:

0

01
012
0123
01234
012345
01234

0123
012
01
0

    8. Write a C program that prints a square of different sizes. The size of the square is given as input to the user. Your program must use only three output statements, one of each of the following forms: printf(“* ”); printf(“ ”); printf(“\n”);

Sample run:
Enter the size of the square: 5

*****
*****
*****
*****
*****
Functions

    9. Write a function to return the volume of a sphere with the input argument for the radius of the sphere. The formula for the volume of the sphere is ,4/3πr3, where r is the radius of the sphere.

Sample run: Enter radius: 2 Volume is 33.5

    10. Write a C function TwoMinimum that computes and returns the minimum of two integer values.

Sample run:

Enter two numbers: 5 10 Minimum number is 5


    11. Modify your program from b to compute the minimum of three integer values. Give two different implementations: (1) use the function TwoMinimum to compute the minimum of three values and (2) write a separate function ThreeMinimum that computes the minimum of three integer values.

Sample run:

Enter three numbers: 5 10 1 Minimum number is 1

    12. Write a program that take a depth (in kilometers) inside the earth as input data; compute and display the temperature at this depth in degrees Celsius and degrees Fahrenheit. The relevant formulas are as follows: Include two functions in your pogram.
Function celcuis_at_depth should compute and return the Celsius temperature at a

depth measured in kilometers. Function Fahrenheit_at_depth should convert a Celsius
temperature to Fahrenheit.
Celsius = 10 * depth + 20
Fahrenheit = 9/5 * Celsius + 32

Sample run:
Enter depth: 125
Celcius is 1270.00
Fahrenheit is 2.318

More products