Skip to main content
Basic Programming In C
Basic Programming In C
Rx 2h

Assignment Manipulations

Exercise

Difficulty: Easy

Given 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: Easy

Give 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 of a if it is decremented?
  • Given short a = 32766;. What is the value of short b = a + 3;?
  • Given short b = 30000, c = 30000;. What is the value of short d = b * c;?

Sequence of Instructions

Difficulty: Easy

Given three integer variables x, y, z declared and initialized. Write the sequences of instructions to:

  • Display the maximum of x and y
  • 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 c and d comes first in the alphabet
  • Determine whether c is uppercase or lowercase
  • Transform c to obtain an uppercase letter (without knowing a priori whether it is lowercase or uppercase)
  • Determine whether c is a consonant or a vowel

Classic Boolean Expressions

Difficulty: Easy

Translate each of the following expressions into C:

  • x=y=z
  • x,y,z all 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 i representing a duration, display the equivalent duration in the form h:min:sec. Write the inverse: given 3 integer variables h, m, s, determine the equivalent duration in seconds.

for Loops

Simple Loops

Difficulty: Easy

Write 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: Easy

Write an algorithm to compute x^n using only multiplications.

Problem

Difficulty: Rx

If 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 17x923 and of 123345678987654x836548971236?
  • 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, compute 123456^654321 % 1234567. Give the advantages of this method over a brute force calculation.

while Loops

Terms of a Sequence

Difficulty: Easy

Let 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: Easy

Write 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: Rx

We 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: Rx

Design 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: Rx

A 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 which a + b + c = 1000. Find the product abc. —source Project Euler

Palindromes and Numbers

Difficulty: Hard

A 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: Rx

2520 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