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#?
Submitted by Nidhi, on September 21, 2020

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

Program:

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.