C# program to display factors of entered number


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 19-Jun-2020

842 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements