Print multiples of Unit Digit of Given Number
Last Updated :
15 Apr, 2025
Given a number N The task is to print the multiples of the unit digit of N from the unit digit of N to N.
Note: If the unit digit is 0 then print the multiples of 10.
Examples:
Input : 39
Output : 9 18 27 36
Explanation : The unit digit of 39 is 9.
So the multiples of 9 between 9 and 39 are:
9, 18, 27, 36
Input : 25
Output : 5 10 15 20 25
Recursive Approach:
We can use a recursive function that takes in the current number and the unit digit of N. The function first checks if the current number is greater than N, in which case it returns without doing anything. Otherwise, it checks if the current number is a multiple of the unit digit, and if it is, it prints the number and calls itself with the next number. If the current number is not a multiple of the unit digit, it simply calls itself with the next number.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
// Recursive function to print the multiples of unit digit
void printMultiples(int current, int n, int unitDigit)
{
if (current > n) {
return;
}
if (current % unitDigit == 0) {
cout << current << " ";
printMultiples(current + unitDigit, n, unitDigit);
} else {
printMultiples(current + 1, n, unitDigit);
}
}
// Driver Code
int main()
{
int n = 39;
int unitDigit = n % 10;
if (unitDigit == 0) {
unitDigit = 10;
}
printMultiples(unitDigit, n, unitDigit);
return 0;
}
Java
import java.util.*;
class GFG {
// Recursive function to print the multiples of unit
// digit
static void printMultiples(int current, int n,
int unitDigit)
{
if (current > n) {
return;
}
if (current % unitDigit == 0) {
System.out.print(current + " ");
printMultiples(current + unitDigit, n,
unitDigit);
}
else {
printMultiples(current + 1, n, unitDigit);
}
}
// Driver Code
public static void main(String[] args)
{
int n = 39;
int unitDigit = n % 10;
if (unitDigit == 0) {
unitDigit = 10;
}
printMultiples(unitDigit, n, unitDigit);
}
}
Python
# Recursive function to print the multiples of unit digit
def printMultiples(current, n, unitDigit):
if current > n:
return
if current % unitDigit == 0:
print(current, end=" ")
printMultiples(current + unitDigit, n, unitDigit)
else:
printMultiples(current + 1, n, unitDigit)
# Driver Code
n = 39
unitDigit = n % 10
if unitDigit == 0:
unitDigit = 10
printMultiples(unitDigit, n, unitDigit)
C#
using System;
class Program
{
// Recursive function to print the multiples of unit digit
static void PrintMultiples(int current, int n, int unitDigit)
{
if (current > n) {
return;
}
if (current % unitDigit == 0) {
Console.Write(current + " ");
PrintMultiples(current + unitDigit, n, unitDigit);
} else {
PrintMultiples(current + 1, n, unitDigit);
}
}
// Driver Code
static void Main()
{
int n = 39;
int unitDigit = n % 10;
if (unitDigit == 0) {
unitDigit = 10;
}
PrintMultiples(unitDigit, n, unitDigit);
}
}
JavaScript
// Recursive function to print the multiples of the unit digit
function printMultiples(current, n, unitDigit) {
if (current > n) {
return;
}
if (current % unitDigit === 0) {
process.stdout.write(current + " ");
printMultiples(current + unitDigit, n, unitDigit);
} else {
printMultiples(current + 1, n, unitDigit);
}
}
// Main function
function main() {
const n = 39;
let unitDigit = n % 10;
if (unitDigit === 0) {
unitDigit = 10;
}
printMultiples(unitDigit, n, unitDigit);
}
// Call the main function
main();
Time Complexity: O(N)
Auxiliary Space: O(1)
Simple Approach:
- Find the unit digit of the input number. The unit digit of N will be (N%10), i.e., remainder upon dividing N by 10.
- Check if the unit digit is 0.
- If yes, then consider the multiple as 10.
- Print the multiples of unit digit until it is less than or equal to the input number.
Below is the implementation of the above approach:
C++
// C++ program to print multiples of
// Unit Digit of Given Number
#include <iostream>
using namespace std;
// Function to print the multiples
// of unit digit
void printMultiples(int n)
{
// Find the unit digit of
// the given number
int unit_digit = n % 10;
// if the unit digit is 0 then
// change it to 10
if (unit_digit == 0)
unit_digit = 10;
// print the multiples of unit digit
// until the multiple is less than
// or equal to n
for (int i = unit_digit; i <= n; i += unit_digit)
cout << i << " ";
}
// Driver Code
int main()
{
int n = 39;
printMultiples(n);
return 0;
}
Java
// Java program to print multiples of
// Unit Digit of Given Number
import java.io.*;
class GFG
{
// Function to print the multiples
// of unit digit
static void printMultiples(int n)
{
// Find the unit digit of
// the given number
int unit_digit = n % 10;
// if the unit digit is 0 then
// change it to 10
if (unit_digit == 0)
unit_digit = 10;
// print the multiples of unit digit
// until the multiple is less than
// or equal to n
for (int i = unit_digit; i <= n; i += unit_digit)
System.out.print( i + " ");
}
// Driver Code
public static void main (String[] args)
{
int n = 39;
printMultiples(n);
}
}
// This code is contributed by inder_mca
Python
# Python3 program to print multiples
# of Unit Digit of Given Number
# Function to print the multiples
# of unit digit
def printMultiples(n):
# Find the unit digit of
# the given number
unit_digit = n % 10
# if the unit digit is 0 then
# change it to 10
if (unit_digit == 0):
unit_digit = 10
# print the multiples of unit digit
# until the multiple is less than
# or equal to n
for i in range(unit_digit, n + 1,
unit_digit):
print(i, end = " ")
# Driver Code
n = 39
printMultiples(n)
# This code is contributed by Mohit Kumar
C#
// C# program to print multiples of
// Unit Digit of Given Number
using System;
class GFG
{
// Function to print the multiples
// of unit digit
static void printMultiples(int n)
{
// Find the unit digit of
// the given number
int unit_digit = n % 10;
// if the unit digit is 0 then
// change it to 10
if (unit_digit == 0)
unit_digit = 10;
// print the multiples of unit digit
// until the multiple is less than
// or equal to n
for (int i = unit_digit; i <= n; i += unit_digit)
Console.Write( i + " ");
}
// Driver Code
public static void Main ()
{
int n = 39;
printMultiples(n);
}
}
// This code is contributed by Ryuga
JavaScript
<script>
// JavaScript program to print multiples of
// Unit Digit of Given Number
// Function to print the multiples
// of unit digit
function printMultiples(n)
{
// Find the unit digit of
// the given number
var unit_digit = parseInt(n % 10);
// if the unit digit is 0 then
// change it to 10
if (unit_digit == 0) unit_digit = 10;
// print the multiples of unit digit
// until the multiple is less than
// or equal to n
for (var i = unit_digit; i <= n;
i += unit_digit)
document.write(i + " ");
}
// Driver Code
var n = 39;
printMultiples(n);
</script>
Time Complexity: O(N) //since one traversal from 1 to N is required to complete all operations hence the overall time needed for the algorithm is linear
Auxiliary Space: O(1) //since no extra array is used so the space taken by the algorithm is constant
Similar Reads
Smallest multiple of a given number made of digits 0 and 9 only We are given an integer N. We need to write a program to find the least positive integer X made up of only digits 9's and 0's, such that, X is a multiple of N. Note: It is assumed that the value of X will not exceed 106. Examples: Input : N = 5 Output : X = 90 Explanation: 90 is the smallest number
8 min read
Count of unique digits in a given number N Given a number N, the task is to count the number of unique digits in the given number. Examples: Input: N = 22342 Output: 2 Explanation:The digits 3 and 4 occurs only once. Hence, the output is 2. Input: N = 99677 Output: 1Explanation:The digit 6 occurs only once. Hence, the output is 1. Naive Appr
6 min read
Program to print ASCII Value of all digits of a given number Given an integer N, the task is to print the ASCII value of all digits of N. Examples: Input: N = 8Output: 8 (56)Explanation:ASCII value of 8 is 56 Input: N = 240Output:2 (50)4 (52)0 (48) Approach: Using the ASCII table shown below, the ASCII value of all the digits of N can be printed: DigitASCII V
5 min read
Cyclicity of Numbers - Unit Digits in Powers Number System is a method of representing numbers on the number line with the help of a set of symbols and rules. These symbols range from 0-9 and are termed digits. The Number System is used to perform mathematical computations ranging from great scientific calculations to calculations like countin
4 min read
Find the largest N digit multiple of N Given a number N, the task is to find the largest N-digit multiple of N. Examples: Input: N = 2 Output: 98 Explanation: 98 is the largest multiple of 2 and is of 2 digits.Input: N = 3 Output: 999 Explanation: 999 is the largest multiple of 3 and is of 3 digits. Approach: The idea is to make an obser
3 min read
First digit in product of an array of numbers Given an array of 'n' numbers. We need to find the first digit of product of these 'n' numbers Examples : Input : arr[] = {5, 8, 3, 7} Output : 8 Product of 5, 8, 3, 7 is 840 and its first digit is 8 Input : arr[] = {6, 7, 9} Output : 3 Recommended PracticeFirst DigitTry It! Background : First we st
7 min read