Computer >> Máy Tính >  >> Lập trình >> C#

Chương trình C # để hiển thị các hệ số của số đã nhập


Trước tiên, hãy nhập số mà bạn muốn thừa số -

Console.WriteLine("Enter the Number:");
n = int.Parse(Console.ReadLine());

Sau đó, lặp lại để tìm các yếu tố -

for (i = 1; i <= n; i++) {
   if (n % i == 0) {
      Console.WriteLine(i);
   }
}

Ví dụ

Bạn có thể thử chạy đoạn mã sau để hiển thị các hệ số của một số -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
   class ApplicationOne {
      static void Main(string[] args) {
         int n, i;
         Console.WriteLine("Enter the Number:");
         n = int.Parse(Console.ReadLine());
         Console.WriteLine("Factors:");
         for (i = 1; i <= n; i++) {
            if (n % i == 0) {
               Console.WriteLine(i);
            }
         }
         Console.ReadLine();
      }
   }
}