Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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
Advertisements