Sheet 2
Sheet 2
Programming Fundamentals
Lab Sheet #2
Objectives:
Learn how to think on paper before coding.
Learn how to construct your program step by step in detail (Algorithm).
Learn how to present your program algorithm in an efficient and organized
way.
Start you first C console programs.
Learn how to solve problems using (variables & data types, input & output
and operators).
Problems:
Print
Data-type size
Arithmetic expression1
Shift number
Sum of 3 numbers
Area of circle
Swap Task
Arithmetic expression2
Quotient & Remainder Task
Animal age
Elephant
Candies = happiness Task
Even or odd
Print
Problem statement:
Make a program to print an Integer (Entered by the User)
Input:
1 integer.
Output:
1 integer.
Example 1:
Input Output
Enter an integer: 25 You entered: 25
Data-type size
Problem statement:
Make a program to demonstrate the working of keyword long
Output:
Data types sizes.
Example 1:
Output
Size of int = 4 bytes
Input Size of long int = 8 bytes
Size of long long int = 8 bytes
Size of double = 8 bytes
Size of long double = 16 bytes
Arithmetic expression1
Problem statement:
Write a C program to read the values of x and y and print the results of the following
expressions in one line:
i. (x + y) / (x - y)
ii. (x + y)(x - y)
Input:
2 numbers(x, y) while (x ≠ y).
Output:
The results of these expressions in one line.
Example 1:
Input Output
X=6 Ex1: 5 / Ex2: 20
Y=4
Example 2:
Input Output
X=7 Ex1: 2.5 / Ex2: 40
Y=3
Shift number
Problem statement:
Write a C program to read a number (x) and shift it right 2 times and output the decimal
number after shifting.
Input:
Number (x).
Output:
the decimal number after shifting right 2 times.
Example 1:
Input Output
1
7
Explanation:
7 = 111 -> 001 = 1
Sum of 3 numbers
Problem statement:
Instead of using our simple calculator, make a program that take 3 numbers from user and
output their sum.
Input:
3 numbers: x, y, z.
Output:
Sum of (x, y and z).
Example 1:
Input
Output
13
-1 Sum: 22
10
Example 2:
Input
Output
5
7 Sum: 15
3
Area of circle
Problem statement:
Make a program that take the radius of the circle from user (r) and output the area of circle.
Notes:
Area of circle = π r2
Input:
Circle radius (r).
Output:
Circle area.
Example 1:
Input Output
3 Circle area: 28.27
Swap
Problem statement:
In you program make 2 variables (x, y), initialize x with 3 and y with 5 then swap their
values.
Challenge:
Don’t use third variable in swapping.
Arithmetic expression2
Problem statement:
Write a C program to evaluate the arithmetic expression:
((a + b / c * d - e) * (f - g))
Input:
Values (a, b, c, d, e, f, g).
Output:
The results of expression.
Example 1:
Input Output
A=5 Result: 100
B=4
C=2
D=3
E=1
F = 13
G=3
Quotient & Remainder
Problem statement:
Write a C program to compute quotient and remainder of two numbers.
Input:
2 numbers.
Output:
Quotient and remainder of input two numbers.
Example 1:
Input Output
Enter dividend: 25 Quotient = 6
Enter divisor: 4
Remainder = 1
Animal age
Problem statement:
We have animal lived for (n) days. Can you tell us how many years, months and days this
animal lived?
Notes:
Consider the whole year has 365 days and 30 days per month.
Input:
Number of days this animal lived (n).
Output:
Based on (n) days how many years, months and days lived this animal.
Example 1:
Output
Input
1 years
400 1 months
5 days
Example 2:
Input Output
1 years
366
0 months
1 days
Elephant
Problem statement:
An elephant decided to visit his friend. It turned out that the elephant's house is located at
point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step
the elephant can move 1, 2, 3, 4 or 5 positions forward.
Determine, what is the minimum number of steps he needs to make in order to get to his
friend's house.
Input:
x value.
Output:
The minimum number of steps he needs to make in order to get to his friend's house.
Example 1:
Input Output
12 3
Example 2:
Input Output
5 1
Candies = happiness
Problem statement:
To make people happy, sell candies!
The store bought a package of (n) candies, for (x) dollars each, and the store wants to know
the total profit the store is going to get after selling all (n) candies for (y) dollars each.
Input:
3 values (n, x, y).
Output:
Output one line containing the required answer.
Example 1:
Input Output
N=2 4
X=3
Y=5
Even or odd
Problem statement:
Write a program that reads an input (x). If (x) is even output 0 otherwise output 1 without
using conditions.
Input:
x value.
Output:
If (x) is even output 0 otherwise output 1.
Example 1:
Input Output
12 0
Example 2:
Input Output
5 1