Dado el entero N, la tarea es imprimir todos los números menores que N, que son divisibles por 3 y 5.
Ejemplos:
Input : 50 Output : 0 15 30 45 Input : 100 Output : 0 15 30 45 60 75 90
Enfoque: Por ejemplo, tomemos N = 20 como límite, luego el programa debe imprimir todos los números menores que 20 que son divisibles por 3 y 5. Para esto, divida cada número de 0 a N por 3 y 5 y verifique su resto. Si el resto es 0 en ambos casos, simplemente imprima ese número.
A continuación se muestra la implementación:
C++
// C++ program to print all the numbers // divisible by 3 and 5 for a given number #include <iostream> using namespace std; // Result function with N void result(int N) { // iterate from 0 to N for (int num = 0; num < N; num++) { // Short-circuit operator is used if (num % 3 == 0 && num % 5 == 0) cout << num << " "; } } // Driver code int main() { // input goes here int N = 100; // Calling function result(N); return 0; } // This code is contributed by Manish Shaw // (manishshaw1)
Java
// Java program to print all the numbers // divisible by 3 and 5 for a given number class GFG{ // Result function with N static void result(int N) { // iterate from 0 to N for (int num = 0; num < N; num++) { // Short-circuit operator is used if (num % 3 == 0 && num % 5 == 0) System.out.print(num + " "); } } // Driver code public static void main(String []args) { // input goes here int N = 100; // Calling function result(N); } }
Python3
# Python program to print all the numbers # divisible by 3 and 5 for a given number # Result function with N def result(N): # iterate from 0 to N for num in range(N): # Short-circuit operator is used if num % 3 == 0 and num % 5 == 0: print(str(num) + " ", end = "") else: pass # Driver code if __name__ == "__main__": # input goes here N = 100 # Calling function result(N)
C#
// C# program to print all the numbers // divisible by 3 and 5 for a given number using System; public class GFG{ // Result function with N static void result(int N) { // iterate from 0 to N for (int num = 0; num < N; num++) { // Short-circuit operator is used if (num % 3 == 0 && num % 5 == 0) Console.Write(num + " "); } } // Driver code static public void Main (){ // input goes here int N = 100; // Calling function result(N); } //This code is contributed by ajit. }
PHP
<?php // PHP program to print all the numbers // divisible by 3 and 5 for a given number // Result function with N function result($N) { // iterate from 0 to N for ($num = 0; $num < $N; $num++) { // Short-circuit operator is used if ($num % 3 == 0 && $num % 5 == 0) echo $num, " "; } } // Driver code // input goes here $N = 100; // Calling function result($N); // This code is contributed by ajit ?>
Javascript
<script> // Javascript program to // print all the numbers // divisible by 3 and 5 // for a given number // Result function with N function result(N) { // iterate from 0 to N for (let num = 0; num < N; num++) { // Short-circuit operator is used if (num % 3 == 0 && num % 5 == 0) document.write( num+ " "); } } // Driver code // input goes here let N = 100; // Calling function result(N); // This code is contributed by Bobby </script>
Producción :
0 15 30 45 60 75 90
Método: Esto también se puede hacer verificando si el número es divisible por 15, ya que el MCM de 3 y 5 es 15 y cualquier número divisible por 15 es divisible por 3 y 5 y viceversa también.
C++
// C++ code to print numbers that // are divisible by 3 and 5 #include <iostream> using namespace std; int main() { int n = 50; for(int i = 0; i < 50; i++) { //lcm of 3 and 5 is 15 if(i % 15 == 0){ cout << i << " "; } } return 0; } // This code is contributed by laxmigangarajula03
Python3
# python code to print numbers that # are divisible by 3 and 5 n=50 for i in range(0,n): # lcm of 3 and 5 is 15 if i%15==0: print(i,end=" ")
Producción
0 15 30 45
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)