INTERNATIONAL UNIVERSITY
C/C++ Programming
Course ID: IT116
Lab 1
Branching statements
Full name + Student ID: ……………………………………………………………………………………………………
…………………………………………………………………………………………………….
Class: ……………………………………………………………………………………………………………………………….
Group: …………………………………………………………………………………………………………………………….
Date: ………………………………………………………………………………………………………………………………
_____________________________________________________________________________________
1
INTERNATIONAL UNIVERSITY
I. Objectives
1. Write simple computer programs in C language, use the input/output statements,
understand the fundamental data types and learn computer memory concepts.
2. Use assignment, arithmetic, relational and logical operators, learn the precedence
order of these operators and write simple decision-making / branching statements.
3. Use basic problem-solving techniques, develop algorithms through the process of
top-down, stepwise refinement, use the if-selection statement, the if…else…-
selection statement and switch-statement to select actions.
II. Pre-Lab Preparation
Students are required to review the theory of the topics before the lab time.
III. In-Lab Procedure
Exercise 1
Write a C program to determines whether an input number is even or odd.
Output:
Input an integer: 15
15 is an odd integer
Exercise 2
Write a C program to check whether an input year is a leap year or not.
(Hint: The leap year is defined by the following flow chart)
_____________________________________________________________________________________
2
INTERNATIONAL UNIVERSITY
Output:
Input a year :2024
2024 is a leap year.
Exercise 3
Write a C program to identify whether a triangle is Equilateral, Isosceles or Scalene based
on the given sides.
For Example:
Output:
Input three sides of triangle: 30 30 50
This is an isosceles triangle.
Exercise 4
Write a C program to input radius of a circle from user and find diameter, circumference,
and area of the circle.
For example:
Output: Enter the radius of a circle: 3
Diameter of circle = 6.00 units
Circumference of circle = 18.84 units
Area of circle = 28.26 sq. units
_____________________________________________________________________________________
3
INTERNATIONAL UNIVERSITY
Exercise 5
Write a C program that calculate the roots of the second order equation:
2
ax +bx +c=0
Hints: Use include<math.h>
For example:
Output:
Enter the Values of a : 1
Enter the Values of b : -4
Enter the Values of c : 3
The roots of equation are
First Root : 3.00
Second Root : 1.00
Exercise 6
Write a C program that calculate the total payment of Taxi service, in which:
- The first kilometer: 5$/km;
- The travelled distance is between 1 km and 30 km: 3$/km;
- The travelled distance is greater than 30 km: 2$/km;
Example
Output:
Enter total distance in km: 5
The total cost that a passenger has to pay: $17.00
Exercise 7
_____________________________________________________________________________________
4
INTERNATIONAL UNIVERSITY
Write a C program to read student’s name, student’s ID and marks of three subjects
(Literature, Math and English) and calculate the total, average and division.
Division is defined as follows:
Average ≥60: A
48 ≤Average ¿60: B
36 ≤Average ¿48: Pass
Average ¿36: Fail
For Example
Output:
Input the Name of the Student: Kien
Input the No of the student: 10
Input the marks of Literature, Math and English: 50 80 70
Name of Student: Kien
ID: 10
Marks in Literature: 50
Marks in Math: 80
Marks in English: 70
Total Marks = 200
Average = 66.67
Division: A
THE END
_____________________________________________________________________________________
5
INTERNATIONAL UNIVERSITY
Useful Operators
_____________________________________________________________________________________