Week 4
Week 4
#include <stdio.h>
int main()
{
int number = 3, remainder;
remainder = number % 2; //the remainder of dividing number by 2
if (remainder == 0){
printf("The number is even.\n");
}else{
printf("The number is odd.\n");
}
return 0;
}
The if-else statement checks if number is even or odd. If the remainder of dividing number by
2 is equal to 0, then number is even. Otherwise, number is odd.
run the program in your machine.
What is the output of the program if the value of number in the fourth line of the program is
changed to:
number The output of the program
1010
111
10
1
2. Modify the program above so that the user enters the value of number using the keyboard.
3. Change the program so that the user enters two integer values: namber1 and number2. If the
remainder of dividing number1 by number2 is equal to 0, then the program should print on the
screen “Number1 is a multiple of number2 “. Otherwise, the program should output a
message “Number1 is not a multiple of number2 “.
Page 1 of 4
CS 1160, Computing Fundamentals-Lab 2016/17 Week of Oct. 31- Nov 29, 2016
#include <stdio.h>
int main()
{
int age;
char ticket_type;
if (ticket_type == 'A') {
if (age > 70)
printf("Ticket price is 5 JOD");
else
printf("Ticket price is 15 JOD");
}else{
if (age > 70)
printf("Ticket is free");
else
printf("Ticket price is 10 JOD");
}
return 0;
1. The program above asks the user for his age and the ticket type to compute the price of the
ticket. Implement and run the program in your machine.
2. Modify the program above to perform the following functionality:
a. If age is greater than 70 and ticket_type is ‘C’, print “Ticket price is free”;
b. If age is greater than 70 and ticket_type is ‘B’, print “Ticket price is 5 JOD”;
c. If age is greater than 70 and ticket_type is ‘A’, print “Ticket price is 10 JOD”;
d. If age is less than or equal to 70 and ticket_type is ‘C’, print “Ticket price is 10 JOD”
e. If age is less than or equal to 70 and ticket_type is ‘B’, print “Ticket price is 15 JOD”;
f. If age is less than or equal to 70 and ticket_type is ‘A’, print “Ticket price is 20 JOD”;
EXERCISE 3: Write a C program that compute the perimeter of Circle, Rectangle, Triangle and Square.
The program ask the user to specify the shape, and then it reads the required value to compute the
perimeter for that specified shape.
Note: the perimeter for the shapes:
𝐂𝐢𝐫𝐜l𝐞 = 𝟐 × 𝒓𝒂𝒅𝒊𝒖𝒔 × 𝛑
S𝐪𝐮𝐚𝐫𝐞 = 𝟒 × 𝐬𝐢𝐝𝐞
Page 2 of 4
CS 1160, Computing Fundamentals-Lab 2016/17 Week of Oct. 31- Nov 29, 2016
EXERCISE 4: Write a program that tests whether the formula 𝒂𝟐 + 𝒃𝟐 = 𝒄𝟐 is true for three integers
entered as input. (Such a triple is a Pythagorean triple and forms a right-angle triangle with these
numbers as the lengths of its sides).
EXERCISE 5: Write a C program that calculate the total electricity bill according to the following
specifications:
1. The program asks the user to specify the user type (Personal P, Industrial I).
2. The program asks the user to enter electricity unit charges.
3. The program calculates the total electricity bill according to the following tables:
Personal Industrial
For first 160 units 33 Fils/unit For first 250 units 16 Fils/unit
For next 100 units 72 Fils/unit For next 200 units 55 Fils/unit
For next 100 units 86 Fils/unit For next 200 units 70 Fils/unit
For unit above 360 114 Fils/unit For unit above 650 93 Fils/unit
EXERCISE 6: whenever you go to buy, something and you pay more than the items total price, you will
get back some change including Dollars, Quarters, Dimes, Nickels, and cents. Write a program that will
calculate how many dollars (100 cents), how many quarters (25 cents), how many dimes (10 cents),
how many nickels (5 cents), and how many cents a buyer will get back. Your program should ask for
the amount to be returned in the following format (12 dollars and 42 cents for example) then print the
result.
EXERCISE 7: Write a program that reads an integer composed of three digits using scanf. The
program should display in English each individual digit of the number. For example, if the user enters
139, the program should display
One Three Nine
Hint: to extract the individual digits of a three-digit number, use the following algorithm
Ones = number % 10
Number = number – ones
Tens = number %100
Number = number – tens
Tens = tens/10
Hundreds = (number % 1000)/100
Example: if number is equal to 139, then ones = 9, tens= 3, hundreds = 1
Page 3 of 4
CS 1160, Computing Fundamentals-Lab 2016/17 Week of Oct. 31- Nov 29, 2016
EXERCISE 8: LOOPING:
1. The positive numbers 1, 2, 3... are known as natural numbers. Write c program that takes a
positive integer (let say n) as an input from the user and calculates the sum up to n.
2. Use the While or for statement to write a program that computes and displays the factorial of
a positive integer n entered by the user. The factorial of n is given by the following formula:
𝑛! = 𝑛(𝑛 − 1)(𝑛 − 2) … (2)(1)
For example, the factorial of n = 5 is
5! = 5(4)(3)(2)(1) = 120
Page 4 of 4