Exercise 6.4 – Digits of a number, divisors and prime numbers
Learning Objectives
By the end of the exercise, the student will:
- extract digits of a number using a loop (
% 10and/ 10) and process each digit - find divisors of a number using a loop and determine whether the number is prime based on them
- determine the greatest common divisor (GCD) and least common multiple (LCM) of two numbers
- combine loops and branching (
for + if,while + if)
Quick Reference
- The last digit of a number is
n % 10, and the number without its last digit isn / 10. The loopwhile (t > 0) { digit = t % 10; ...; t /= 10; }traverses all digits, from right to left. - Do not modify the original
n– work with a copyint t = n;if you neednafter the loop. - A reversed number is built:
reversed = reversed * 10 + digit;, starting from 0. dis a divisor ofnifn % d == 0. A number is prime if it has exactly two divisors (1 and itself).- The greatest common divisor (GCD) of two numbers is the largest
ifor which bothx % i == 0andy % i == 0. The least common multiple (LCM) isx / GCD * y. - The evaluation order in
x / gcd * yprevents integer overflow: divide first, then multiply.
Example Input and Output
| Input (keyboard) | Output (screen) |
|---|---|
1204 | Digits: 4 |
Tasks
Work in order. Level A is for everyone, Level B is standard, Level C is a challenge.
Below each task is an editor with starter code. Replace each // TODO with your code, adjust the input if needed, and click Run. Code in the editor is not saved, so copy it if you want to keep it; final evaluation is performed in the quiz at the end.
n (up to 9 digits). Using a while loop, print on three lines: Digits: ... (number of digits), Sum: ... (sum of digits), and Reversed: ... (number with reversed digits; e.g. 1200 reverses to 21).Input (keyboard) – data read by the program, each on a separate line; you can modify them:
/*****************************************************************************
Technical School Pirot
Exercise - 6.4 - Digits of a number, divisors and prime numbers
*****************************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
int n;
scanf("%d", &n);
int t = n;
int digits = 0;
while (t > 0)
{
digits++;
t /= 10;
}
printf("Digits: %d
", digits);
// TODO: complete the program with the sum of digits and the reversed number
return 0;
}
1204
Expected output for the above input:
Digits: 4 Sum: 7 Reversed: 4021
n (2 ≤ n ≤ 10000). Using a for loop, check all numbers from 1 to n and print on three lines: Divisors: ... (how many divide n), Sum: ... (sum of divisors), and Prime: ... (1 if n is prime, otherwise 0).Input (keyboard) – data read by the program, each on a separate line; you can modify them:
/*****************************************************************************
Technical School Pirot
Exercise - 6.4 - Digits of a number, divisors and prime numbers
*****************************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
int n;
scanf("%d", &n);
int t = n;
int digits = 0;
while (t > 0)
{
digits++;
t /= 10;
}
printf("Digits: %d
", digits);
// TODO: complete the program with divisors, sum and prime test
return 0;
}
7
Expected output for the above input:
Divisors: 2 Sum: 8 Prime: 1
x and y (each on a separate line). Find the greatest common divisor and print on two lines: GCD: ... and LCM: ... (least common multiple).Input (keyboard) – data read by the program, each on a separate line; you can modify them:
/*****************************************************************************
Technical School Pirot
Exercise - 6.4 - Digits of a number, divisors and prime numbers
*****************************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
int n;
scanf("%d", &n);
int t = n;
int digits = 0;
while (t > 0)
{
digits++;
t /= 10;
}
printf("Digits: %d
", digits);
// TODO: complete the program with GCD and LCM calculation
return 0;
}
12 18
Expected output for the above input:
GCD: 6 LCM: 36
Explore (Ungraded)
First predict the result, then verify it in the program.
- What happens in Task A for
n = 1200and the reversed number? What does it mean that the result is 21? - Up to what number is it sufficient to check for divisors to determine whether a number is prime? (Hint:
i * i <= n.) - How would GCD be computed using Euclid's algorithm (with a
while (y != 0)loop)? Compare its performance with the solution in the task.
Hints
Open hints one by one, only when you get stuck.
Hint 1
digits, sum, reversed) and a single loop that updates them all in each step.Hint 2
for (int i = 1; i <= n; i++) if (n % i == 0) {...}; it is prime when the divisor count equals 2.Hint 3
for (int i = 1; i <= x && i <= y; i++) if (x % i == 0 && y % i == 0) gcd = i; – the last found value is the greatest.Hint 4
Self-Check
Before submitting, check whether:
- ☐ The original number
nis not lost in Task A. - ☐ Number 1 is not covered (n is at least 2), and a prime number has exactly two divisors.
- ☐ GCD is a common divisor, and LCM is divisible by both
xandy. - ☐ All three tasks yield the correct result even when one number is divisible by the other.