Print multiples of Unit Digit of Given Number

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

Simple Approach

  1. Find the unit digit of the input number. Unit digit of N will be (N%10), i.e. remainder upon dividing N by 10.
  2. Check if the unit digit is 0.
    • If yes then consider the multiple as 10.
  3. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

9 18 27 36


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.