📌  相关文章
📜  打印给定数字的所有可被 3 和 5 整除的数字

📅  最后修改于: 2022-05-13 01:54:19.052000             🧑  作者: Mango

程序打印给定数字的所有可被 3 和 5 整除的数字

给定整数 N,任务是打印所有小于 N 的数字,这些数字可以被 3 和 5 整除。
例子 :

Input : 50
Output : 0 15 30 45 

Input : 100
Output : 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 
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


Javascript


输出 :

0 15 30 45 60 75 90