Выведите кратные единичной цифры данного числа

Опубликовано: 2 Декабря, 2021

Учитывая число . Задача состоит в том, чтобы вывести кратные единицы единичной цифры N от единичной цифры N до N.
Примечание . Если цифра единицы равна 0, выведите число, кратное 10 .
Примеры:

 Ввод: 39 
Выход: 9 18 27 36
Пояснение: Цифра 39 - это 9 .
Таким образом, числа, кратные 9, между 9 и 39 равны:
9, 18, 27, 36

Ввод: 25
Выход: 5 10 15 20 25

Рекомендуется: сначала попробуйте свой подход в {IDE}, прежде чем переходить к решению.

Простой подход

  1. Найдите единичную цифру входного числа. Цифра единицы N будет (N% 10), т.е. остаток от деления N на 10.
  2. Убедитесь, что цифра единицы равна 0.
    • Если да, то считайте кратным 10.
  3. Выведите число, кратное единице, до тех пор, пока оно не станет меньше или равно введенному числу.

Ниже представлена реализация описанного выше подхода:

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

<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>
Выход:
 9 18 27 36

Хотите учиться на лучших видео и практических задачах, ознакомьтесь с базовым курсом C ++ для базового и продвинутого уровня C ++ и курсом C ++ STL для языка и STL. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .