2016-07-11 29 views
-4

我想知道如果一個數字可以被兩個數字整除,代碼的外觀如何。例15既是雙方3和5如果一個num可以被兩個數字邏輯C整除#

整除那我就可以說

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(); 

     } 
    } 
} 
+0

你只需要檢查這個數字是被3整除和5或任何其他數字也?或者像2,3,4,5等特定範圍的數字? –

+0

不清楚你在問什麼。爲什麼你不能在你的例子中檢查數字是否可以被15整除?這個問題似乎是關於數學,而不是編程。請更具體地說明你在解決問題時遇到的困難。 –

+0

要更清楚一點,如果我輸入數字9,我希望它說「這個數字可以被3整除」,如果我輸入數字10,我希望它說「這個數字可以被5整除」,最後如果我輸入一個像15這樣的數字我希望它說「這個數字可以被3和5整除」。 – Kai

回答

1
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

+0

此代碼不起作用,因爲如果輸入數字10,它會輸出Console.WriteLine(「這個數字不能被3和5整除」); – Kai

+0

@凱工作正常,因爲你的預期? –

+1

感謝Manu看起來很棒!唯一的是改變printf到Console.WriteLine :) – Kai

2

這裏是你如何能做到這一點簡單的解決方案:

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(); 

    } 
} 
} 
+0

但是我仍然喜歡單獨的Console.WriteLine(「這個數字不能被3分割」);或Console.WriteLine(「這個數字不能被5分割」);因爲如果我輸入了9或10的數字呢? – Kai

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

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); 
     }    
    } 
     } 
相關問題