Introduction to Computer Programming (C Programming) (GSDR2222)
Higher Institute of Transport and Logistics (HITL)
Semester: Second Semester
Level: 200
Year: 2018
TUTORIAL IN C PROGRAMING
Exercise 1: Write a C program which calculates the factorial of a given positive number (display
an error message if the number entered is negative and loop until the user enters a positive
number)
0! = 1, 1!= 1; 2!= 2*1; 3=3*2*1; 4= 4*3*2*1
Exercise 2: Write a C program to display the number of positive and negative numbers entered
by the user (the program stops when the user enters 0).
Exercise 3: Write a program which informs the user about the action to perform when he
enters the color of the traffic light (if the user press on 1(red), your program instructs him to
STOP; 2(green): CROSS; 3(orange): WAIT OR CROSS
Exercise 4: Write a program to print a big ‘C’
######
## ##
#
#
#
#
## ##
#####
Exercise 4: Write a function which gets a string as input and returns the reverse of that string
Exercise 5: Write a program that gets a string from the user, then use the previous function to
get the reverse of the string and verify if the string is a palindrome.
Exercise 6: Write a C program that reads N numbers and counts the number of positive
numbers and prints the average of all positive values.
Exercise 7: Write a C program that reads N numbers and sum all odd values between them.
Exercise 8: Write a C program to print all numbers between 1 and 500 which divided by a
specific number and the remainder will be 3.
Test Data:
Input an integer: 25
Expected Output:
3, 28, 53, 78, 103, 128, 153, 178, 203, 228, 253, 278, 303, 328, 353, 378, 403, 428, 453, 478
Exercise 9: Write a C program to print 3 numbers in a line, starting from 1 and print n lines.
Accept number of lines (n, integer) from the user.
Test Data:
Input number of lines: 5
Expected Output :
1 2 3
www.schoolfaqs.net
4 5 6
7 8 9
10 11 12
13 14 15
Exercise 10: Write a C program that reads two integers p and q, print p numbers of lines in a
sequence of 1 to b in a line.
Test Data :
Input of lines: 5
Number of characters in a line: 6
Expected output:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
Exercise 11: Write a C program to read an array of length 6 and find the smallest element and
its position.
Exercise 12: Implement the insertion sort alogarithm in C language.
Exercise 13: Write a C program to print your name, date of birth and mobile number.
Expected Output:
Name :Alexandra Abramov
DOB :July 14, 1975
Mobile :237 675505050
Exercise 14: Write a C program to compute the perimeter and the area of a rectangle with a
height of 7 inches and width of 5 inches.
Expected Output:
Perimeter of the rectangle = 24 inches
Area of the rectangle = 35 square inches
Exercise 15: Write a C program to convert specified days into years, weeks and days.
Note: Ignore leap year
Test Data :
Number of days : 1329
Expected Output :
Years: 3
Weeks: 33
Days: 3
Exercise 16: Write a C program that accepts two integers from the user and calculate the
product of the two integers.
Test Data :
Input of the first integer: 25
Input of the second integer: 15
www.schoolfaqs.net
Expected Output:
Product of the above two integers = 375
Exercise 17: Write a C program to calculate a bike’s average consumption from the given total
distance (integer value) traveled (in km) and spent fuel (in liters, float number – 2 decimal
point).
Test Data :
Input total distance in km: 350
Input total fuel spent in liters: 5
Expected output:
Average consumption (km/lt) 70.000
Exercise 18: Write a C program to convert a given integer (in seconds) to hours, minutes and
seconds
Test Data:
Input seconds: 25300
Expected Output:
There are:
H:M:S – 7:1:40
Exercise 19: Write a C program to read a password until it is correct. For the wrong password
print “Incorrect password” and for correct password print “Correct password” and quit the
program. The correct password is 1234.
Test Data:
Input password: 1234
Expected Output:
Correct password
Exercise 20: Create the following structures in C:
1. Create an array of 5 Restaurant (AR)
2. Create a menu M1(Rice), and you assign the 5 Restaurants to the list of restaurants of that
menu(LR)
Struct Restaurant {
Char[100] name;
Char [50] address:
}
Struct Menu {
Char[100] name;
Int price;
Restaurant *LR;
}
Struct Customer{
Char[100] name;
Int account;
Restaurant R;
Menu M;
}
www.schoolfaqs.net
3. Create the second menu M2 (Eru) and you assign the 3 first Restaurants to the list of
restaurants of that menu (LR)
4. Create a customer with 5000f on his account, assign the last restaurant of AR (AR[0]) to the
customer and also the first menu (M1).
5. Display the list of Restaurants, the list of Menu and list of customers.
6. Save the list of restaurant in a file called restaurant.txt, Menu in menu.txt and Customers in
customer.txt
www.schoolfaqs.net