📜  打印给定数字的单位位数的倍数

📅  最后修改于: 2021-05-31 21:07:24             🧑  作者: Mango

给定一个数字N  。的任务是从N的单元位到N打印N的单元数位的倍数。
注意:如果单位数字为0,则打印10的倍数。
例子:

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

简单方法

  1. 查找输入号码的单位数字。 N的单位位数为(N%10),即N除以10后的余数。
  2. 检查单位数字是否为0。
    • 如果是,则将倍数视为10。
  3. 打印单位数字的倍数,直到它小于或等于输入数字。

下面是上述方法的实现:

C++
// C++ program to print multiples of
// Unit Digit of Given Number
#include 
 
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


Python3
# 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


输出:
9 18 27 36
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”