Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Print multiples of Unit Digit of Given Number in C Program
In C programming, finding multiples of the unit digit of a given number involves extracting the last digit and then finding all numbers that divide it evenly. The unit digit of any number can be obtained using the modulo operator (%) with 10.
Syntax
int unitDigit = number % 10;
Algorithm
START
Step 1 ? Declare variables num, unitDigit and i
Step 2 ? Input number num
Step 3 ? Store num%10 in unitDigit to fetch unit digit
Step 4 ? Print unitDigit
Step 5 ? Loop for i=2 and i<=unitDigit/2 and ++i
IF unitDigit%i==0
Print i
End IF
Step 6 ? End For Loop
STOP
Example
This program extracts the unit digit and finds all its proper divisors (multiples) −
#include <stdio.h>
int main() {
int num, unitDigit, i;
printf("Enter a number: ");
scanf("%d", &num);
unitDigit = num % 10; // storing unit digit
printf("Unit digit of %d is: %d<br>", num, unitDigit);
printf("Multiples of %d are: ", unitDigit);
// Special case for 0 and 1
if (unitDigit == 0) {
printf("All numbers are multiples of 0<br>");
} else if (unitDigit == 1) {
printf("1 has no proper multiples<br>");
} else {
// Find proper divisors (multiples) from 2 to unitDigit/2
int found = 0;
for (i = 2; i <= unitDigit / 2; i++) {
if (unitDigit % i == 0) {
printf("%d ", i);
found = 1;
}
}
if (!found) {
printf("No proper multiples found");
}
printf("<br>");
}
return 0;
}
The output of the above code is −
Enter a number: 326 Unit digit of 326 is: 6 Multiples of 6 are: 2 3
How It Works
- The expression
num % 10extracts the unit digit by finding the remainder when divided by 10. - We loop from 2 to
unitDigit/2because no number greater than half of the unit digit can divide it (except itself). - For each number
i, ifunitDigit % i == 0, theniis a proper divisor (multiple). - Special handling is added for digits 0 and 1 to avoid confusion.
Conclusion
This program demonstrates how to extract the unit digit using modulo operation and find its proper divisors efficiently. The algorithm runs in O(n/2) time where n is the unit digit value.
Advertisements
