1. Palindromic numbers
The task is to print those three-digit numbers that remain the same when their digits are reversed. For instance, 121 is a palindromic number, 123 is not. Let us draw a structogram or a flow-chart before writing the C program!
Solution
The sophisticated solution is based on the observation that these numbers can be obtained when counting from 10 to 99 and adding the first digit to each number to the end (for instance, after 10, 1 has to be printed to obtain 101, or, after 57, 5 has to be added to get 575, etc.). In the C program, it might be worth to use two nested loops. The first digit can not be 0, hence the first loop goes from 1 to 9, while the nested loop has to count from 0 to 9.
The figure at the bottom depicts a non-standard, but very descriptive solution. Compare the two solutions, which is easier to understand?
#include <stdio.h> int main(void) { int a, b; a = 1; while (a < 10) { b = 0; while (b < 10) { /* aba: palindromic! */ printf("%d%d%d ", a, b, a); b = b + 1; } a = a + 1; } return 0; }
#include <stdio.h> int main(void) { for (int a = 1; a <= 9; a = a + 1) for (int b = 0; b <= 9; b = b + 1) printf("%d%d%d ", a, b, a); return 0; }
2. Square of a given size
|....|
+----+ |....| |....| |....| |....| +----+
Write a program that draws a rectangle to the console out of .
, |
, −
and +
symbols. Ask the user to enter the height and
the width of the rectangle.
Hint: We have created a line drawing program in the lab, which is a good starting point for this problem. Put this line drawing code into another loop to draw the square. The first and the last line need special treatment.
Solution
#include <stdio.h> int main(void) { int n; printf("Size of the square: "); scanf("%d", &n); /* first line: +----+ */ printf("+"); for (int x=0; x<n; x=x+1) printf("-"); printf("+\n"); /* middle lines: |....| like the first one, with different characters. */ for (int y=0; y<n; y=y+1) { printf("|"); for (int x=0; x<n; x=x+1) printf("."); printf("|\n"); } /* last line: +----+ like the first one. */ printf("+"); for (int x=0; x<n; x=x+1) printf("-"); printf("+\n"); return 0; }
o ooo ooooo
As we saw before, we can print to the screen in left-to-right direction, from top to bottom. With this in mind, let us write a program that asks the user to enter a number (n), and draws a triangle of circles to the screen using letter "o" having the given number of rows. For instance, in case of n=3 the output should be as shown in the figure.
Solution
···o ··ooo ·ooooo ooooooo
The principle of the solution
To solve the task it is recommended to draw a sketch to a sheet of paper. Having counted the number of leading spaces and the number of letter "o" in the subsequent lines, the solution follows.
- First try to render row i. Observe that there are (n-i) leading spaces and 2*i-1 letter "o" in that row.
- The leading spaces and letter "o"-s should be printed by loops.
- An other loop is needed to iterate over the lines to render.
#include <stdio.h> int main(void) { int height; printf("Enter the height of the triangle: "); scanf("%d", &height); /* loop for the subsequent lines */ for (int row = 1; row <= height; row = row+1) { /* printing the leading space characters */ for (int x = 1; x <= height-row; x = x+1) printf(" "); /* printing the letter "o"-s */ for (int x = 1; x <= row*2-1; x = x+1) printf("o"); /* end of the current row, go to the next line */ printf("\n"); } return 0; }
75│3 25│5 5│5 1│
At week 1 we wrote a pseudocode to print the prime factors of a number, using the algorithm learned in the elementary school. Now let us implement this algorithm in C. Write a C program that asks the user to enter a number and prints its prime factors to the screen!
Compare this C program with the pseudocode we wrote at week 1!
Solution
The principle of the solution
- We have to go on trying the divisions till number 1 is obtained. Hence, we will need a loop like
while (number != 1)
- If the number is divisible with the current divisor candidate, we perform the division and print it. If it is not divisible, an other candidate divisor is chosen.
- When trying to find divisors, we always start with the smallest one and move toward the bigger ones.
- Is it a problem if the candidate divisor is not a prime number (like 4, or 6)? Why?
In the C programming language the %
operator denotes the remainder of the division (dividend % divisor
→ remainder, for instance 9%7
yields 2
). This is the easiest way to check divisibility.
The solution below always prints an entire row at once, for instance 150|2. Printing this row is necessary when we have the check for divisibility already done, and the remainder turned out to be 0. The output 150|2 means that 150 is divided by 2; hence this line must be printed before actually performing the division, to be able to print the number before the division. Consequently, the last line, consisting of number 1, must be printed separately at the end.
#include <stdio.h> int main(void) { int number; printf("Enter a number: "); scanf("%d", &number); int divisor; divisor = 2; while (number > 1) { if (number % divisor == 0) { /* if divisible */ printf("%5d|%d\n", number, divisor); number = number / divisor; } else divisor = divisor + 1; /* not divisable */ } printf("%5d\n", 1); return 0; }
Somebody might prefer an alternative solution, where the number to be divided is printed first, and the divisor is printed only afterwards, after passing the test for divisibility. This is a correct solution as well. In this case the number (as given by the user) must be printed before the loop. At the same time, the "1" does not have to be printed at the end, as it will appear at the last division.
#include <stdio.h> int main(void) { int number; printf("Enter a number: "); scanf("%d", &number); int divisor; divisor = 2; printf("%5d|", number); while (number > 1) { if (number % divisor == 0) { /* if divisible */ printf("%d\n", divisor); number = number / divisor; printf("%5d|", number); } else divisor = divisor + 1; /* not divisible */ } return 0; }
It might be worth considering to pre-compute the prime numbers beforehands and use them as candidate divisors in the algorithm (instead of incrementing the candidate divisor by one every time). How much would the program be more efficient?
Write a program that asks the user to enter a decimal number and prints it as a roman number to the screen. For instance, 89→LXXXIX. It is enough to do the conversion till 99, when the user enters a bigger number, print a message that the maximal supported number is 99.
Solution
The principle of the solution
- During the conversion, we need to go from the bigger roman numbers to the smaller ones.
- What we have already printed to the screen, can be substracted from the variable. For instance, in case of 89, after having 80 printed (LXXX) only 9 remains to convert, which can be converted as if the user input was 9.
Let us draw a flow-chart or a structogram before writing the C program! Before formalizing the algorithm try testing it with a couple of examples, to explore the order of printing of the subsequent roman characters. Observe that, while XC (90), L (50) etc. can occur only once, some other values (including X (10)) can occur several times!
#include <stdio.h> int main(void) { int x; printf("Enter a number: "); scanf("%d", &x); if (x < 1) printf("The number must be positive.\n"); else if (x > 99) printf("Only numbers less than 100 are supported.\n"); else { if (x >= 90) { printf("XC"); x = x-90; } if (x >= 50) { printf("L"); x = x-50; } if (x >= 40) { printf("XL"); x = x-40; } while (x >= 10) { printf("X"); x = x-10; } if (x >= 9) { printf("IX"); x = x-9; } if (x >= 5) { printf("V"); x = x-5; } if (x >= 4) { printf("IV"); x = x-4; } while (x >= 1) { printf("I"); x = x-1; } printf("\n"); } return 0; }