usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

How to find all Factors of a Number in C#



What are Factors? | Maths Explanation for C# Kids

Factors are the complete set of integers that will divide a particular number without remainder.
Take 36 as an example, it's complete set are:

1, 2, 3, 4, 6, 9, 12, 18, and 36.

Other than prime numbers, every other number has at least one factor - not considering 1 as factor.
Where there is just one factor, then this factor is the square root of the number in question;
In this guide, we'll explore the math behind factors-of-numbers and walk through how to code a C# algorithm for listing factors in a simple and fun way.


Code Logic for Factorising Numbers in C# - Fun Maths Exercise

Actually, we've been doing factors over the last two demonstrations (lessons).

We can implement a C# algorithm for factorising a number by simply checking for our factors using the square-root of number range.
We'll start from 1 (which is always a factor).
For each found factor, we'll get the corresponding complementary factor by dividing the number (whose factors we are trying to find), by the found factor.
This Math activity and C# script help primary school students understand factorization by listing all factors of a number.


Create a new C# class file; Project, Add Class.
Call it ListFactors.

Type out the adjoining C# code for listing factors.


Note: You can comment out the C# code for the main class from the previous lesson if you have been following.


So! C# Fun Practice Exercise - List Factors

As a fun practice exercise, feel free to try out your own different numbers, and see how the C# code lists the factors of those numbers.









<< PreviousNext >>