Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

AGH Computer Science C Programming Laboratory 3

Uploaded by

michalgach.0ff
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

AGH Computer Science C Programming Laboratory 3

Uploaded by

michalgach.0ff
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Laboratory 03 – selection statements 16.10.

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).

c) Print the values read from the keyboard.


d) Use the if-else statement to print the value of the largest variable.

Test Data :
Enter two values: 4 5

1st Number = 4
2nd Number = 5
5 is maximum

2. [2 points] Write a C program to find the largest of three numbers.


a) Declare 4 integer variables: z,y,z,max.
b) Use the commands below to initialize the variables.
printf("Enter three values: ");
scanf("%d %d %d", &x, &y, &z);

c) Print the values read from the keyboard.


d) Use the if statement and the && operator to check if x is the largest number, if so assign it the
value of max.
e) Use the if statement and the && operator to check if y is the largest number, if so assign it the
value of max.
f) Use the if statement and the && operator to check if z is the largest number, if so assign it the
value of max.
g) Don't use the else keyword.
h) Print max.

Test Data:
Enter three values: 12 25 52

1st Number = 12,


2nd Number = 25,
3rd Number = 52.

The 53 is the greatest among three.

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.

Use the logical operators && and ||.

Fill the gaps.


if(………………………………………………………………………………………………){
printf("LEAP YEAR - %d\n", year);
} else {
printf("COMMON YEAR %d\n", year);
}

Leap Years 1800 - 2400

1804 1904 2004 2104 2204 2304


1808 1908 2008 2108 2208 2308
1812 1912 2012 2112 2212 2312
1816 1916 2016 2116 2216 2316
1820 1920 2020 2120 2220 2320
1824 1924 2024 2124 2224 2324
1828 1928 2028 2128 2228 2328
1832 1932 2032 2132 2232 2332
1836 1936 2036 2136 2236 2336
1840 1940 2040 2140 2240 2340
1844 1944 2044 2144 2244 2344
1848 1948 2048 2148 2248 2348
1852 1952 2052 2152 2252 2352
1856 1956 2056 2156 2256 2356
1860 1960 2060 2160 2260 2360
1864 1964 2064 2164 2264 2364
1868 1968 2068 2168 2268 2368
1872 1972 2072 2172 2272 2372
1876 1976 2076 2176 2276 2376
1880 1980 2080 2180 2280 2380
1884 1984 2084 2184 2284 2384
1888 1988 2088 2188 2288 2388
1892 1992 2092 2192 2292 2392
1896 1996 2096 2196 2296 2396
2000 2400
4.[4 points] Write a C program to find all roots of a quadratic equation ax2+bx+c=0 using if-else.

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.

Use the following algorithm:

Declarations and initializations


First if
First swap code
Second if
Second swap code
Third if
Third swap code
Fourth if
Fourth swap code
Fifth if
Fifth swap code

Print the result


Test Data:
Input: 1 5 7 3
Output: 1 3 5 7

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

Information for the user: Enter [number 1] [+ - * /] [number 2]


Data entered by the user: 12 # 34
Result: Invalid operator

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.

a) Declare three variables. Use scanf to initialize two of them.


b) Use switch to find maximum. Create two case labels, don't use default.
c) Print the maximum to the screen.
d) Use switch to check whether the maximum is even or odd. Create two case labels, don't use
default.
e) Print info to the screen.

Test Data :
Input: Enter two numbers: 12 13
Result:
13 is Maximum.
Maximum is Odd.

Input: Enter two numbers: 16 13


Result:
16 is Maximum.
Maximum is Even.

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.

You might also like