0% found this document useful (0 votes)
451 views

Exercises

The document contains 9 exercises asking the student to write C programs or functions to perform various tasks like printing output, prompting user input, performing calculations, passing values between functions, and conditionally printing messages. The exercises involve writing functions to calculate distance and smallest values, check if a number is even, get user input and total scores, and rewriting a program to use a user-defined function.

Uploaded by

ridwan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
451 views

Exercises

The document contains 9 exercises asking the student to write C programs or functions to perform various tasks like printing output, prompting user input, performing calculations, passing values between functions, and conditionally printing messages. The exercises involve writing functions to calculate distance and smallest values, check if a number is even, get user input and total scores, and rewriting a program to use a user-defined function.

Uploaded by

ridwan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

EXERCISES

1. Write a program which simply prints out: 6.23e+00

2. Write a complete C program to prompt a user for input the quantity of each items, receive the
input, do a calculation and print the input and the answer on the screen based on table item below.

Item Price / Item

Book RM 12.50

Pencil RM 0.80

Multifunction paper RM 10.50

Then modify the program to add another calculation and display the new result.

3. Write a function distance that calculates the distance between two points (x1,y1) and (x2,y2). All
number and return values should be of type float.

4. Write a function multiply that accept 2 integers and returns the multiplication of those two
integers.

5. Write a program that inputs a series of integers and passes them one at a time to function even,
which uses the remainder operator to determine if an integer is even. The function should take an
integer argument and return 1 if the integer is even and 0 otherwise.

6. Write a program that accepts 3 floating point numbers then pass these numbers to a function
called smallest that later return the smallest value of the three floating point numbers.

7. Write a main program that calculates total of three scores and this calculation must be done in a
separate function called total_score(). The main program should read three(3) scores and calls
the function total_score(). Then the average of these three scores is calculated. If the average is
> 40, then the status PASS is displayed. The sample output is as follows:

Please enter your name: Norashikin

Please enter the first score: 90.0

Please enter the second score: 60.0

Please enter the third score: 30.0


Name : Norashikin

Total = 180.0

Average = 60.0

Status = Pass

8. Rewrite the following program using a user-defined function.

#include<stdio.h>

main ()

int num;

printf("Enter an integer or 999:");

scanf("%d",&num);

do

printf("Number is: %d\n",num);

printf("Enter an integer or 999:");

scanf("%d",&num);

} while (num != 999);

/* end_do_while */

9. Write program that calls another function, named message. Ask the users for their annual income
in the main program. Pass the value of income from main program to the message function where
this function will print a congratulatory message if the user makes more than RM50,000 a year or
an encouragement message if the user makes less.

You might also like