Dado un número , la tarea es imprimir los múltiplos del dígito unitario de N del dígito unitario de N a N.
Nota : si el dígito de la unidad es 0 , imprima los múltiplos de 10 .
Ejemplos:
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
Enfoque sencillo
- Encuentre el dígito de la unidad del número de entrada. El dígito unitario de N será (N%10), es decir, el resto de dividir N por 10.
- Compruebe si el dígito de la unidad es 0.
- Si es así, entonces considere el múltiplo como 10.
- Imprima los múltiplos del dígito de la unidad hasta que sea menor o igual que el número de entrada.
A continuación se muestra la implementación del enfoque anterior:
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
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>
Producción:
9 18 27 36
Complejidad temporal: O(N)
Espacio auxiliar: O(1)