如果num可被两个数字逻辑C#整除

时间:2016-07-11 04:16:17

标签: c# xamarin mono

我试图找出一个数字可以被两个数字整除的代码。例15既可以被3和5整除。

然后我可以说

Console.WriteLine("This Number is Divisible by 3 and 5!");

我如何进行编码以及将它放在哪里?请帮忙。

using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;
            Console.WriteLine("Enter A Number :");
            n = int.Parse(Console.ReadLine());
            if (n % 3 == 0)

            {
                Console.WriteLine("This Number is Divisible by 3 ");

            }

            else
            {
                Console.WriteLine("This Number is Not Divisible by 3");

            }
            Console.ReadLine();

        }
    }
}

4 个答案:

答案 0 :(得分:2)

以下是有关如何执行此操作的简单解决方案:

#include <utility>

std::pair<double, double> RK4(float t, float x, float dx, float h)
{
    /* snip */
    diff1 = (l1+2*l2+2*l3+l4)/float(6);
    diff2 = (k1+2*k2+2*k3+k4)/float(6);
    return {diff1, diff2};
}

int main()
{
    double x, dx;
    /* snip */
    for(int i = 1; i<=N; i++) {
        std::pair<double, double> diff = RK4(t,x,dx,h);
        // or use with C++11 and above for brevity
        auto diff = RK4(t,x,dx,h);
        x = x + diff.first;
        dx = dx + diff.second;
        t = t + h;
    }
    cout << x << " " << dx << "\n" ;
    return 0;
}

答案 1 :(得分:1)

df['Id'] = df['idSite'] 
df = df.drop('idSite', axis=1)

输出:

输入一个数字:21

数字可以被3整除,但不会被5整除

答案 2 :(得分:0)

我有一个简单的逻辑,它将打印所有因素。通过利用int.TryParse的优点来验证输入(如果输入不是数字或可转换为整数,那么它将显示无效的输入消息)。然后它将遍历给定数字的一半的数字并收集可分割的数字。

请考虑以下代码:

int numberInput;
List<int> factors = new List<int>();
Console.WriteLine("Enter A Number :");
if (int.TryParse(Console.ReadLine(), out numberInput))
{
    for (int i = 2; i <= numberInput/2; i++)
    {
        if (numberInput % i == 0)
        {
            factors.Add(i);
        }
    }
    if (factors.Count > 0)
    {
        Console.WriteLine("{0} is divisible by {1}", numberInput, String.Join(",",factors));
    }
    else 
    {
        Console.WriteLine("Number is Prime");
    }

}
else
{
    Console.WriteLine("Wrong Input");
}
Console.ReadKey();

这将输出为

  

"15 is divisible by 3,5"输入15

     

"20 is divisible by 2,4,5,10"输入20

答案 3 :(得分:0)

试试这个

    int n, c;
    c = 0;
        Console.WriteLine("Enter A Number :");
        n = int.Parse(Console.ReadLine());
        for (int i = 2; i < n; i++)
    { 
    if (n%i==0)
    {
        c++;
        if(c==1)
        {
        Console.WriteLine("This Number is Divisible by "+i);
        }
        else
        {
        Console.Write(" and "+i);
        }               
    }
      }