Q:

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

belongs to collection: C# Basic Programs | basics

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | basics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to find the HCF of two given numbers... >>
<< C# program to check the given number is a perfect ...