Dado el entero N, la tarea es imprimir todos los números menores que N, que son divisibles por 5 o 7.
Ejemplos:
Input : 20 Output : 5 7 10 14 15 20 Input: 50 Output: 5 7 10 14 15 20 21 25 28 30 35 40 42 45 49 50
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 5 o 7. Para esto, divida cada número de 0 a N por 5 y 7 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 5 or 7 for a given number # include<bits/stdc++.h> using namespace std; // Result generator with N int NumGen(int n) { // Iterate from 0 to N for(int j = 1; j < n + 1; j++) { // Short-circuit operator is used if (j % 5 == 0 || j % 7 == 0) cout << j << " "; } return n; } // Driver code int main() { // Input goes here int N = 50; // Iterating over generator function NumGen(N); return 0; } // This code is contributed by Code_Mech
Java
// Java program to print all the numbers // divisible by 5 or 7 for a given number import java.util.*; class GFG{ // Result generator with N static int NumGen(int n) { // Iterate from 0 to N for(int j = 1; j < n + 1; j++) { // Short-circuit operator is used if (j % 5 == 0 || j % 7 == 0) System.out.print(j + " "); } return n; } // Driver code public static void main(String args[]) { // Input goes here int N = 50; // Iterating over generator function NumGen(N); } } // This code is contributed by AbhiThakur
Python3
# Python3 program to print all the numbers # divisible by 5 or 7 for a given number # Result generator with N def NumGen(n): # iterate from 0 to N for j in range(1, n+1): # Short-circuit operator is used if j % 5 == 0 or j % 7 == 0: yield j # Driver code if __name__ == "__main__": # input goes here N = 50 # Iterating over generator function for j in NumGen(N): print(j, end = " ")
C#
// C# program to print all the numbers // divisible by 5 or 7 for a given number using System; class GFG{ // Result generator with N static int NumGen(int n) { // Iterate from 0 to N for(int j = 1; j < n + 1; j++) { // Short-circuit operator is used if (j % 5 == 0 || j % 7 == 0) Console.Write(j + " "); } return n; } // Driver code public static void Main() { // Input goes here int N = 50; // Iterating over generator // function NumGen(N); } } // This code is contributed by Code_Mech
Javascript
<script> // JavaScript program to print all the numbers // divisible by 5 or 7 for a given number // Result generator with N function NumGen(n) { // Iterate from 0 to N for(let j = 1; j < n + 1; j++) { // Short-circuit operator is used if (j % 5 == 0 || j % 7 == 0) document.write(j + " "); } return n; } // Driver code // Input goes here let N = 50; // Iterating over generator function NumGen(N); </script>
Producción:
5 7 10 14 15 20 21 25 28 30 35 40 42 45 49 50
Complejidad de tiempo: O(N)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Sahildeosekar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA