AGH Computer Science C Programming Laboratory 3
AGH Computer Science C Programming Laboratory 3
2024
1. [2 points] Write a C program to find maximum between two numbers using if-else.
a) Declare 2 integer variables: x,y.
b) Use the commands below to initialize the variables.
printf("Enter two values: ");
scanf("%d %d", &x, &y);
Function scanf - reads data from stdin (keyboard) and stores them according to the parameter
format ("%d %d") into the locations pointed by the additional arguments (&x, &y).
Test Data :
Enter two values: 4 5
1st Number = 4
2nd Number = 5
5 is maximum
Test Data:
Enter three values: 12 25 52
3. [4 points] Write a C program that checks whether a given year is a leap year. Use if-else
1
a) Declare one variable of type integer year.
b) Using the code snippet below, read the year from the keyboard.
printf("Enter year : ");
scanf("%d", &year);
c) Use the following algorithm to check whether a given year is leap year.:
If the year is exactly divisible by 4 and the year is not divisible by 100 or the year is exactly divisible
by 400 then the year is a leap year.
else the year is a normal year.
a) Enter the coefficients of the quadratic equation from the keyboard. Use scanf.
2
b) Find discriminant of the given equation, using the formula
discriminant = b*b - 4*a*c.
Compute roots based on the nature of discriminant.
c) If discriminant > 0 then,
root1 = (-b + sqrt(discriminant) ) / (2*a)
root2 = (-b – sqrt(discriminant) ) / (2*a).
d) If discriminant == 0 then,
root1 = root2 = -b / (2*a).
e) else if discriminant < 0 then, roots are complex numbers.
f) Print roots.
Test Data: 1 5 6
Roots are real.
Root1= -2.00
Root2= -3.00
Test Data: 1 5 7
Roots are complex.
No real solution.
5. [4 points] Write a C program to sort four numbers using only five comparisons (if).
Use swap code from Lab 02. Use the printf and scanf functions in the program.
6. [2 points] Write a C program to create a calculator that performs basic arithmetic operations
(addition, subtraction, multiplication and division) using switch/case. The calculator should take
3
two numbers and the operator from the user and then print the result of the operation according to
the entered operator.
a) Declare three variables. Two to hold the values and the third to hold the operator. What types
should be variables? Use scanf to give them values.
b) The switch decides what operation to perform based on the value of the operator.
c) Create 4 case labels for 4 arithmetic operations.
d) Use default when the user-supplied symbol does not match any of the four arithmetic operations.
e) Print the result on the screen.
Test Data :
Information for the user: Enter [number 1] [+ - * /] [number 2]
Data entered by the user: 123 + 34
Result: 123.00 + 34.00 = 157.00
7. [2 points] Write a C program that finds the maximum of the two numbers entered by the user
with a switch statement, and check whether the maximum is even or odd using switch.
Test Data :
Input: Enter two numbers: 12 13
Result:
13 is Maximum.
Maximum is Odd.
Next time:
Laboratory 04 – Loops
To prepare for the next class, read the lecture or book chapter on loops. Check how sample
programs from the lecture or book work. Check if you can modify them in any way you want. Write
some example programs that use loops.