C# Program to display the Factors of the Entered Number


Firstly, let us enter the number.

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

Now loop through and find the mod of the entered number with i = 1 that increments after every iteration. If its 0, then print it, since it would be our factor.

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

Let us see the complete code to find factors of a number.

Example

 Live Demo

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

Output

Enter a Number

Updated on: 23-Jun-2020

209 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements