# Problem 2.1 *Reading from the keyboard *

Write a program which does the following:

- reads two doubles from the keyboard,
- prints the sum of the two doubles,
- prints the difference of the two doubles (first minus second),
- prints the square of the first double,
- reads two integers from the keyboard,
- computes the sum and product of the two integers,
- prints the sum and product of the integers,
- reads two chars from the keyboard,
- computes the sum and product of the two chars,
- prints the sum and product of the chars as decimal values and as chars.

*You can assume that the input will be correct.*

*Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.*

Testcase 2.1: input1.23.625() |
Testcase 2.1: outputsum of doubles=4.800000 difference of doubles=-2.400000 square=1.440000 sum of integers=7 product of integers=10 sum of chars=81 |

product of chars=1640 sum of chars=Q product of chars=h

# Problem 2.2 *Decimal, octal and hexadecimal numbers *(

Write a program which does the following:

- reads a char from the keyboard,
- and prints the char as character as well as in decimal, octal and hexadecimal notation.

*You can assume that the input will be correct.*

*Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.*

Testcase 2.2: input{ |
Testcase 2.2: outputcharacter={ decimal=123 octal=173 hexadecimal=7b |

Problem 2.3 Time calculation |

## Language: C

Write a program where you can enter integer numbers for weeks, days and hours as input from the keyboard. Your program should compute and output by printing on the screen the total number of hours.

*You can assume that the input will be correct.*

# Problem 2.4 *Area computations *(1 point)

## Due by Monday, September 20^{th}, 23:00 Graded automatically with testcases only Language: C

Write a program that reads from the keyboard three float values for the variables a, b and h. Compute and print on the screen the areas of: the square with the side a, the rectangle with the length a and the width b, a triangle with the base a and the height h, and a trapezoid with the bases a, b and the height h.

*You can assume that the input will be correct.*

*Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.*

# Testcase 2.4: inputTestcase 2.4: output

10square area=100.000000

14.5rectangle area=145.000000

5triangle area=25.000000 trapezoid area=61.250000

## Problem 2.5 *Pointers and their content *

Write a program which reads an integer variable a from the keyboard and prints the value on the screen. Then declare and initialize a pointer ptr_a pointing to a, print the address contained in the pointer variable on the screen, increase the value of a by 5 by using the pointer variable and print the modified value and the address of the variable on the screen as well. *You can safely assume that the input will be correct.*

Problem 2.6 Multiple pointers to same data |

Write a program which reads two double variables x and y from the keyboard. Then declare and initilize three pointers ptr_one, ptr_two and ptr_three such that ptr_one and ptr_two will both point to the variable x and ptr_three will point to y. By using printf show that ptr_one and ptr_two contain the same memory address and ptr_three contains a different address.

*You can assume that the input will be correct.*

Problem 2.7 Infinite loop by bad coding |

The program below prints

i is 8 i is 8

…

until you stop the execution by pressing Ctrl-C. Fix the program such that it prints 8, 7, 6, 5 and 4 as values for i.

#include <stdio.h> int main()

{

int i = 8; while (i >= 4) printf(“i is %d

”, i); i–;

printf(“That’s it.

”); return 0;

}

Problem 2.8 Divisible by 2 and 7? |

### Language: C

Write a program, where you can enter an integer from the keyboard. Determine whether the number is divisible by both 2 and 7. Then either print on the screen

“The number is divisible by 2 and 7” or

“The number is NOT divisible by 2 and 7”.

*You can safely assume that the input will be valid.*

# Testcase 2.8: inputTestcase 2.8: output

56The number is divisible by 2 and 7

## Problem 2.9 *Categorization of characters *

Write a program where you can enter a character from the keyboard. Then determine whether the character is a digit or a letter or some other symbol and print a corresponding message on the screen.

*You can safely assume that the input will be valid.*

Testcase 2.9: input ! |
Testcase 2.9: output! is some other symbol |

Problem 2.10 Days and hours |

### Language: C

Write a program where you can enter an integer n from the keyboard. Then a conversion table for 1 to n days should be printed on the screen as in the example below. Make sure that the integer value you entered for n is valid (positive and non-zero). If an invalid integer n was entered then repeat the entering until a valid value will be entered. *Use a while loop in your solution.*

- day = 24 hours
- days = 48 hours
- days = 72 hours

## Reviews

There are no reviews yet.