C# program to display factors of entered number

CsharpServer Side ProgrammingProgramming

Firstly, enter the number for which you want the factors −

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

After that, loop through to find the factors −

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

Example

You can try to run the following code to display 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();
      }
   }
}
raja
Published on 25-Jul-2018 10:36:16
Advertisements