File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Math/All factors of a given Number Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ // We include de I/O library
2
+ #include < iostream>
3
+ /* We include the stdenteredNumeramespace so we don't have to write std::
4
+ Everytime we want to use "cout" or "cin" */
5
+ using namespace std ;
6
+
7
+ int main ()
8
+ {
9
+ // We declare our variables
10
+ int enteredNumer, i, factorization;
11
+
12
+ // Ask for a number
13
+ cout << " Enter a number of your choice (positive and integeer): " ;
14
+ // Assing the entered value to the "enteredNumer" variable
15
+ cin >> enteredNumer;
16
+
17
+ cout << " These are the factors for " << enteredNumer << " : " << endl;
18
+
19
+ factorization = enteredNumer;
20
+
21
+ // Every number has 1 as it's factor, so we'll say this right away
22
+ cout << 1 << endl;
23
+
24
+ // Check the factors (starting with 2, until the number is reached)
25
+ for (i = 2 ; i <= enteredNumer; ++i)
26
+ {
27
+ // If the divided number has 0 as the division reminder, it'll be a factor
28
+
29
+ while (factorization % i == 0 ){
30
+ // Print this number everytime
31
+ cout << i << endl;
32
+
33
+ // As it is a factor, let's divide by it to find the next one!
34
+ factorization = factorization / i;
35
+ };
36
+ }
37
+
38
+ return 0 ;
39
+ }
40
+
You can’t perform that action at this time.
0 commit comments