给定整数N,任务是打印小于N的所有数字,这些数字可以被3和5整除。 例如:
null
Input : 50Output : 0 15 30 45 Input : 100Output : 0 15 30 45 60 75 90
方法: 例如,让我们以N=20为限制,那么程序应该打印所有小于20的数字,这些数字可以被3和5整除。为此,将0到N之间的每个数字除以3和5,然后检查其余数。如果余数在两种情况下均为0,则只需打印该数字。 以下是实施情况:
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> |
输出:
0 15 30 45 60 75 90
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END