如果一个数字可以被两个数字逻辑C#整除
If a num is divisible by two number logic C#
我想知道如果一个数字可以被两个数字整除,代码会是什么样子。例 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();
}
}
}
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)
{
if (n % 5 == 0)
{
Console.WriteLine("This Number is Divisible by 3 and 5!");
}
else
{
printf("\nThe number is divisible by 3 but not by 5");
}
}
else if (n % 5 == 0)
{
Console.WriteLine("The number is divisible by 5 but not by 3");
}
else
{
Console.WriteLine("\nThis Number is Not Divisible by 3 and 5!");
}
Console.ReadLine();
}
}
}
输出:
输入一个数字:21
数字可以被 3 整除但不能被 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 && n % 5 == 0)
{
Console.WriteLine("This Number is Divisible by 3 and 5 ");
}
else
{
Console.WriteLine("This Number is Not Divisible by 3 and 5");
}
Console.ReadLine();
}
}
}
我有一个简单的逻辑给你,它将打印所有因素。通过利用 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"
for an input 15
"20 is divisible by 2,4,5,10"
for an input 20
试试这个
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);
}
}
}
我想知道如果一个数字可以被两个数字整除,代码会是什么样子。例 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();
}
}
}
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)
{
if (n % 5 == 0)
{
Console.WriteLine("This Number is Divisible by 3 and 5!");
}
else
{
printf("\nThe number is divisible by 3 but not by 5");
}
}
else if (n % 5 == 0)
{
Console.WriteLine("The number is divisible by 5 but not by 3");
}
else
{
Console.WriteLine("\nThis Number is Not Divisible by 3 and 5!");
}
Console.ReadLine();
}
}
}
输出:
输入一个数字:21
数字可以被 3 整除但不能被 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 && n % 5 == 0)
{
Console.WriteLine("This Number is Divisible by 3 and 5 ");
}
else
{
Console.WriteLine("This Number is Not Divisible by 3 and 5");
}
Console.ReadLine();
}
}
}
我有一个简单的逻辑给你,它将打印所有因素。通过利用 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"
for an input15
"20 is divisible by 2,4,5,10"
for an input20
试试这个
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);
}
}
}