Home  >  Article  >  Backend Development  >  C# program to display factors of input number

C# program to display factors of input number

WBOY
WBOYforward
2023-09-01 16:25:08927browse

C# program to display factors of input number

First, enter the number you want to factor -

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

After that, loop to find the factors -

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

Example

You can try running the following code to display the factors of a number -

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

The above is the detailed content of C# program to display factors of input number. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete