C# program to print the all factors of a given number

Here, we are going to learn how to print the all factors of a given number in C#? By Nidhi Last updated : April 15, 2023

Printing Factors of a Number

Here we will enter an integer number from the keyboard and check the print all factors of the given number.

C# code for printing factors of a number

The source code to print the all factors of a given number is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to print the all factors of a given number.

using System;

class Factors
{
    static void PrintFactors(int number)
    {
        int iLoop = 0;
        Console.WriteLine("The all factors of " + number + " are :");

        for (iLoop = 1; iLoop <= number; iLoop++)
        {
            if (number % iLoop == 0)
            {
                Console.Write(iLoop + " ");
            }
        }
    }
    static void Main(string[] args)
    {
        int number= 0;
        
        Console.Write("Enter an integer number: ");
        number = int.Parse(Console.ReadLine());

        PrintFactors(number);

        Console.WriteLine();
    }
}

Output

Enter an integer number: 9
The all factors of 9 are :
1 3 9
Press any key to continue . . .

Explanation

Here, we created a class Factors that contains two methods PrintFactors() and Main(). In the PrintFactors(), Here we find factors of the given number, if a given number is divided a number completely it means the remainder is 0 then we print that number.

The Main() method is the entry point of the program, here we read an integer number and passed to the PrintFactors() method that will print factors of a number on the console screen.

C# Basic Programs »


Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.