C# Program to Find the Factors of the Given Number
Posted by Superadmin on August 15 2022 15:45:18

C# Program to Find the Factors of the Given Number

 

 

This is a C# Program to display the factors of the entered number.

Problem Description

This C# Program Displays the Factors of the Entered Number.

Problem Solution

The factors of a number are all those numbers that can divide evenly into the number with no remainder.

Program/Source Code

Here is source code of the C# program which checks a given integer is odd or even. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Display the Factors of the Entered Number
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, x;
            Console.WriteLine("Enter the Number ");
            num = int.Parse(Console.ReadLine());
            Console.WriteLine("The Factors are : ");
            for (x = 1; x <= num; x++)
            {
                if (num % x == 0)
                {
                    Console.WriteLine(x);
                }
            }
            Console.ReadLine();
 
        }
    }
}
Program Explanation

In this C# program, we are reading the number using ‘num’ variable. Using for loop compute the factors of the entered number, the factors of a number are all those numbers that can divide evenly into the number with no remainder.

 

If condition statement is used to check the modulus of the value of ‘num’ variable value by the value of ‘x’ variable is equal to 0. If the condition is true then execute the statement and print the factors of the given number.

Runtime Test Cases
 
Enter the Number : 27
The Factors are : 
1
3
9
27