You are on page 1of 4

COLLEGE OF ENGINEERING & TECHNOLOGY

Department: Computer Engineering (Cairo Branch)

Course : Structured Programming Course Code: CC112


Topics: Arithmetic and Logical expressions Sheet: 2
____________________________________________________________
Level 1:
Topic Program
Arithmetic 1) Write the C language symbol of the arithmetic operation to get the remainder of division

comparison 2) Write the C language symbol of the following comparison operations


Not equal
Equal
Less than or equal

Logical 3) Write the C language symbol of the following logical operations


And
Or
Not

4) Write the truth table of the following logical operations


And
Or
Not

Arithmetic, 5) Arrange the following operators from the highest to the lowest priority
logical, + - / ( ) * < > && != || == % !
comparison
6) Find the values of x and y for the following code:
Arithmetic
int m, n;
double p, x, y;
m=3;
n=2;
p = 2.0 ;
x=m/p;
y=m/n;

7) Find the values of x and n for the following code:

int n;
double x;
x = 9 * 0.5 ;
n = 9 * 0.5 ;

Type 8) Copy the following program to your IDE, run it and look at the results, then answer the
Casting questions

#include <stdio.h>
int main () {
int i = 9 , j ;
float a , b ;
char c1 = '9', c2 = 'a', c3 = 'A', c4;
printf ( "The difference between %c and %d is %d\n", c1, i, c1-i );
1
Version 15a
c4 = c2 - c3;
printf ( "The difference between capital letters and small letters is: %d\n", (int)c4 ) ;
i = c3 ;
printf ( "The ascii code for %c is %d\n", (char)i, i ) ;
j = c3 + 7 ;
printf ( "Adding %d to %c turns it to %c\n", (int)c4, (char)j, (char)(j +c4) ) ;
a = c2/c3;
b = (float) c2/c3;
printf ( "Dividing %c by %c = %f\nHowever, dividing %c by %c can also be = %f",
c2, c3, a, c2, c3, b ) ;
return 0;
}

a) What is the effect of putting (int) before c4?


b) What is the effect of putting (char) before i ?
c) What was the effect of putting (float) when calculating the value of b?
d) In not more than three sentences, explain why the values for parts a and b are different?

9) Trace the statements in the following program and decide on the values that will be printed
on the screen before running it in your IDE to check your answers
int main () {
int i = 3;
int j = 9;
int k = 2;
int m, n, o ;
m = ++i % k + j ;

printf ( " i = %d\n j = %d\n k = %d m = %d\n", i, j, k, m ) ;


printf ( "_______________________________\n" ) ;

n = j % i + m-- ;
printf ( " i = %d\n j = %d\n k = %d n = %d\n", i, j, k, n ) ;
printf ( "_______________________________\n" ) ;

k += 8 ;
printf ( " i = %d\n j = %d\n k = %d\n",i, j, k ) ;
printf ( "_______________________________\n" ) ;

o=n/k*m%k;
printf ( " i = %d\n j = %d\n k = %d\n", i, j, k ) ;
printf ( "_______________________________\n" ) ;
return 0;
}

10) Trace the following program if the user enters the numbers 20 and 10. Tr to explain in
words the function of the program.

#include <stdio.h>
int main(void){
float x, y, result ;
printf ( Please enter two numbers: ) ;
scanf ( %f%f, &x, &y ) ;
result = ( x + y ) / 2 ;
printf ( The result is: %f, result ) ;
return 0 ;
}

2
Version 15a
Level 2:
Topic Program
Logical & 11) Compute the following expressions if x=3,y=4,z=5:
comparison a.
operations b.
c.
d.
e.
f.
g.

12) Evaluate the following expressions if x=12.5,y=9.2,m=5, and n=2


a.
b.
c.
d.

13) Evaluate each of the following expressions if a is 5,b is 10,c is 15 and flag is 1:
a.
b.
c.
d.

Arithmetic 14) Write a C program that reads a distance in meters and converts it to centimeters and
millimeters. The program should display the results on the screen

15) Write a C program that reads the length and breadth of a rectangle and calculates and
displays the area and perimeter of the rectangle

16) Write a C program to take a depth (in kilometers) inside the earth as input data; compute
and display the temperature at that depth in degrees Celsius and Fahrenheit. The relevant
formulas are:
Celsius = 10 x (depth) + 20 (Celsius temperature at depth in km)
Fahrenheit = 1.8 x (Celsius) + 32

Level 3:
Topic Program
Arithmetic 17)
Write a program that computes the distance between two points in the x,y
coordinate system. Recall that the points are labeled as (x,y) pairs and that the
distance between (x1,y1) and (x2,y2) is given by the formula
Distance

18) Write a program to perform the functionality of a simple calculator to do basic operators.
Make it a scientific calculator for added complexity.

19) Write a program to express a given number of seconds in terms of hours, minutes and
seconds and output the result. Use distinct variables for distinct quantities. For example, for
representing the total number of seconds which can be greater than 59 and the seconds part
of the output which can never be greater than 59

20) Write a program that reads from the user an integer value less than 65,536 and store it in a
variable of type unsigned int. The value the user inputs will be stored in two bytes. You are
required to extract the value stored in each of the bytes and print each separately. For
example if the user inputs 65000, the following binary representation will be in the
memory
3
Version 15a
High Order Byte Low Order Byte
11111101 11100110
Your program output will be as follows High order byte = 253 Low order byte = 232
(Hint: for variables representing the low order byte and the high order byte use the type
unsigned char)

4
Version 15a

You might also like