Topic 6. Loop programming structures · Exercise 6.4 · independent work · ~90 min

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 (% 10 and / 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 is n / 10. The loop while (t > 0) { digit = t % 10; ...; t /= 10; } traverses all digits, from right to left.
  • Do not modify the original n – work with a copy int t = n; if you need n after the loop.
  • A reversed number is built: reversed = reversed * 10 + digit;, starting from 0.
  • d is a divisor of n if n % 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 i for which both x % i == 0 and y % i == 0. The least common multiple (LCM) is x / GCD * y.
  • The evaluation order in x / gcd * y prevents 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.

Level A · BasicDigits of a number
Read a positive integer 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
Level B · StandardDivisors and prime number
Read an integer 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
Level C · ChallengeGCD and LCM
Read two positive integers 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 = 1200 and 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
For A: three variables (digits, sum, reversed) and a single loop that updates them all in each step.
Hint 2
For B: for (int i = 1; i <= n; i++) if (n % i == 0) {...}; it is prime when the divisor count equals 2.
Hint 3
For C: 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
Test the program with numbers 7 (prime), 12 (has 6 divisors), 1000 (larger number of divisors).

Self-Check

Before submitting, check whether:

  • ☐ The original number n is 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 x and y.
  • ☐ All three tasks yield the correct result even when one number is divisible by the other.

Check and Solution

Answer the questions in the quiz “Check 6.4” (levels A, B, and C). The program runs on the server, and you get immediate feedback; multiple attempts are permitted. The Solution unlocks only after that.