Home > Backend Development > C#.Net Tutorial > C# program to display factors of input number

C# program to display factors of input number

WBOY
Release: 2023-09-01 16:25:08
forward
971 people have browsed it

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());
Copy after login

After that, loop to find the factors -

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

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template