1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
using std::cin;
using std::endl;
void numberCounter(int);//hold values of each prime number
int i;
int counter2 = 0, counter3 = 0, counter5 = 0, counter7 = 0, counter11 = 0, counter13 = 0, counter17 = 0,
counter19 = 0, counter23 = 0, counter29 = 0, counter31 = 0, counter37 = 0, counter41 = 0, counter43 = 0,
counter47 = 0, counter53 = 0, counter59 = 0, counter61 = 0, counter67 = 0, counter71 = 0;
int main()
{
int number;
int save = 0;
int saveArray[100];
int primeArray[20] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71 };//the prime numbers
cout << "what number would you like to find the prime numbers to? " << endl;
cin >> number;
//while loop embedded in a for loop
//taking the modulus of each number starting from 2 if there is a remainder in the while
//the program goes back to the for loop for a new prime number from the array
for (i = 0; i < 21; i++ ) {
while ( number % primeArray[i] == 0) {
number = number / primeArray[i];
cout << primeArray[i] << ", ";
saveArray[save] = primeArray[i];//previous attempt at putting each prime number in a new array
save += 1;
numberCounter(save);//switch function with a list of all the values from the array.
}
}
cout << number << endl;
cout << counter2;
return 0;
}
void numberCounter(int i)
{
switch(i)
{
case 0:
counter2 = counter2 + 1;
break;
case 1:
counter3 += 1;
break;
case 2:
counter5 += 1;
break;
case 3:
counter7 += 1;
break;
case 4:
counter11 += 1;
break;
case 5:
counter13 += 1;
break;
case 6:
counter17 += 1;
break;
case 7:
counter19 += 1;
break;
case 8:
counter23 += 1;
break;
case 9:
counter29 += 1;
break;
case 10:
counter31 += 1;
break;
case 11:
counter37 += 1;
break;
case 12:
counter41 += 1;
break;
case 13:
counter43 += 1;
break;
case 14:
counter47 += 1;
break;
case 15:
counter53 += 1;
break;
case 16:
counter59 += 1;
break;
case 17:
counter61 += 1;
break;
case 18:
counter67 += 1;
break;
case 19:
counter71 += 1;
break;
}
}
|