C# Program to display the Factors of the Entered Number

CsharpProgrammingServer Side Programming

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
raja
Published on 17-Sep-2018 08:52:45
Advertisements