Assignment Manipulations
Exercise
Difficulty: EasyGiven two integer variables x and y. What are the successive values taken by x and y in the two following scenarios:
int x = 0;
x = x + 1;
x = x + 1;
x = 1 + x;
int y = -1;
x = x + y;
y = y + x;
x = x + y;
y = y + x;
x = x + y;
y = y + x;
int x = 7, y = -2;
x = y; y = x;
x = y; y = 1;
x = y;
x = 2; y = 10; x = y; y = x;
x = 2; y = 10;
int z = x; x = y; y = z;
Sequence of Assignments
Difficulty: EasyGive the values of the integer variables a, b, and c (which are declared and initialized) following the block of instructions:
if (c - b == b)
{
a = a + 1;
c = c + b;
b = a;
}
else
{
b = a;
a = a - 1;
c = c * b;
}
Integer Operations
Difficulty: Easy- Given
unsigned int a = 0;. What is the value ofaif it is decremented? - Given
short a = 32766;. What is the value ofshort b = a + 3;? - Given
short b = 30000, c = 30000;. What is the value ofshort d = b * c;?
Sequence of Instructions
Difficulty: EasyGiven three integer variables x, y, z declared and initialized. Write the sequences of instructions to:
- Display the maximum of
xandy - Display the absolute value of
z - Display the parity of the integer
x - Display the maximum of
x,y,z
Given character variables c and d declared and initialized. Write the sequences of instructions to:
- Determine which of
canddcomes first in the alphabet - Determine whether
cis uppercase or lowercase - Transform
cto obtain an uppercase letter (without knowing a priori whether it is lowercase or uppercase) - Determine whether
cis a consonant or a vowel
Classic Boolean Expressions
Difficulty: EasyTranslate each of the following expressions into C:
x=y=zx,y,zall different from each other (pairwise distinct)x<y<=z- at least two numbers equal among
x,y,z - exactly two numbers equal among
x,y,z - at most two numbers equal among
x,y,z
Simple Problems
Difficulty: Easy- Given the birth dates of two people provided as day, month, year (where each value is an integer). Determine which person is older (1 or 2).
- Given an integer
irepresenting a duration, display the equivalent duration in the formh:min:sec. Write the inverse: given 3 integer variablesh,m,s, determine the equivalent duration in seconds.
for Loops
Simple Loops
Difficulty: EasyWrite the sequences of instructions to:
- Display the integers from 1 to 10 separated by spaces, then from 10 to 1.
- Add the integers from 1 to 100 and display the result
- Add the even integers from 6 to 2048 and display the result.
- Display the list of multiples of (3 or 5 or both) less than 60.
Power Computation
Difficulty: EasyWrite an algorithm to compute x^n using only multiplications.
Problem
Difficulty: RxIf we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. —source Project Euler
Modular Multiplication and Exponentiation
Difficulty: Hard- What is the last decimal digit of
17x923and of123345678987654x836548971236? - We see that to know
(a * b) % 10, it suffices to compute(a % 10) * (b % 10). This principle generalizes for any integer (i.e. not necessarily 10). Inspired by exercise 28, compute123456^654321 % 1234567. Give the advantages of this method over a brute force calculation.
while Loops
Terms of a Sequence
Difficulty: EasyLet u_n be defined by u_0 = 1515 and u_(n+1) = 3*u_n + 42 for n in N. Write an algorithm that computes the first k such that u_k is strictly greater than 1 million.
Terms of a Sequence (bis)
Difficulty: EasyWrite an iterative algorithm to compute the quotient and remainder of the Euclidean division of a by b without using division or modulo. If a = 37 and b = 5, your algorithm should compute q = 7 and r = 2.
Approximate Value of Pi
Difficulty: RxWe know that π/4 = arctan(1) = Σ(n=0 to ∞) ((-1)^n)/(2*n+1). Write an algorithm that computes an approximate value of π to within epsilon.
Sequences
Difficulty: RxDesign an algorithm that computes and displays the smallest integer n such that Σ(i=1 to n) (Σ(j=1 to i) (i+j)) >= 1000.
Pythagorean Triplet
Difficulty: RxA Pythagorean triplet is a set of three natural numbers, a b c, for which:
a² + b² = c². For example,3² + 4² = 9 + 16 = 25 = 5². There exists exactly one Pythagorean triplet for whicha + b + c = 1000. Find the productabc. —source Project Euler
Palindromes and Numbers
Difficulty: HardA palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is
9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. —source Project Euler
Divisors
Difficulty: Rx2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest number that is evenly divisible by all of the numbers from 1 to 20? —source Project Euler