King Hussein Faculty of Computing Sciences
Department of Computer Science
Structured Programming Lab – Section 1
Lab Assignment #1 – Writing Simple C Programs
Sunday 2/3/2025 @ 01:30-03:30 pm
Spring 2024/2025
Instructor:
Sawsan Alodibat (saw20219009@std.psut.edu.jo)
Total Mark / 10
Lab #1 Objectives
● Basics of Lab environment: Dealing with text editor, the compiler, the (code blocks) IDE
● Writing, compiling and debugging simple C programs
● Memory concepts: variables and datatypes (int, float, char)
● Assignment statement and arithmetic in C including character arithmetic
● Increment and decrement operators
● Basics of scanf and printf (using %d, %f)
● Other primitive data types: short, long and double
Page 1 of 4
Part I: Lab Example
Example – Simple Calculator Program
Objective: The student will implement a simple calculator program that performs the basic arithmetic
operations: addition, subtraction, multiplication, and division.
Introduction:
● Write a program where the user can input two numbers and the program will perform all four
basic arithmetic operations (addition, subtraction, multiplication, and division) on those
numbers.
● The program will display the result of each operation.
Steps to Follow:
1. Declare two float variables to store the numbers input by the user (Number1, and Number2).
2. Declare variables to store the results of each operation (additionResult, subtractionResult, and
multiplicationResult).
3. Show welcome message to the user (“Welcome to your name Calculator”)
4. Ask the user to input two numbers using scanf().
5. Perform addition, subtraction, and multiplication directly.
6. Display the results for each operation using printf().
7. Perform division and print it directly without defining a variable.
Sample Output
Welcome to Ahmad Calculator
Enter first number: 10
Enter second number: 4
Results:
Addition Result: 14.000000
Subtraction Result: 6.000000
Multiplication Result: 40.000000
Division Result: 2.500000
Page 2 of 4
PART II: Lab Exercise
On the desktop, create a folder named Lab1 followed by your id inside it Create project named
Lab1Ex1.
Exercise 1 – Conversion a unit in Meter into Kilometer, Inch, Centimeter, and Foot
Exercise Objectives
Using simple input and output statements
Using arithmetic operators
✔ Using comments
Problem Description
Write a program in C that converts an input numeric unit in Meter to give the equivalent value in KM,
CM, Inch, and Foot. Write a personalized greeting at the beginning.
● Greeting Message: The program should start by printing a message that says:
o Greetings from your full name.
● Prompt the user to enter a value in Meter.
● Convert the input unit in meter into Kilometer, Centimeter, Inch, and foot using the following
formulas:
o 1 Meter= 0.001 Kilometer.
o 1 meter=100 Centimeter.
o 1 meter=39.3701 Inch.
o 1 meter=3.2808 Foot.
● Print the result.
Sample Output
Greetings from Emad Mahmoud Alsheikh
Enter the unit value in meter:
23
The result is:
0.023 km
2300 cm
905.5123 inch
75.4584 foot
Page 3 of 4
PART III: Extra Lab Exercises
Exercise 1 – Swapping Two Variables
Write a C program that accepts two input integers, stores them in variables X and Y, respectively, and
then swaps the two variables’ values.
For example, if the user’s first input is 35 (stored in variable X) and the second input is 99 (stored in
variable Y), then the program has to swap the variables values, so that 35 will be stored in Y and 99 will
be stored in X.
● Solve the problem using a third temporary variable
● Re-solve the problem without using a temporary variable.
Exercise 2 – Sphere Volume
Write a C program that reads in the radius of a sphere, computes and prints the sphere’s volume based
on the following equation for computing volume of sphere:
Volume of Sphere = (4/3) π r3
Use the constant value 3.14159 for π.
Perform the calculation inside the printf statement.
Page 4 of 4