Skip to content

Commit 9e8c2e9

Browse files
committed
[New] Prime Numbers
1 parent 7f6c7e1 commit 9e8c2e9

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

perfectNumbers/main.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// main.cpp
3+
// perfectNumbers
4+
//
5+
// Created by samuel muloki on 12/05/2019.
6+
// Copyright © 2019 line23. All rights reserved.
7+
//
8+
9+
/* Note : According to Wikipedia : In number theory, a perfect number is a positive integer
10+
that is equal to the sum of its proper positive divisors, that is, the sum of its positive
11+
divisors excluding the number itself (also known as its aliquot sum). Equivalently, a
12+
perfect number is a number that is half the sum of all of its positive divisors (including
13+
itself).
14+
*/
15+
16+
#include <iostream>
17+
18+
using namespace std;
19+
20+
int main(int argc, const char * argv[]) {
21+
int num, i=1, sum=0;
22+
cout <<"Enter a number: ";
23+
cin >> num;
24+
while(i < num) {
25+
if (num % i == 0)
26+
sum = sum + i;
27+
i++;
28+
}
29+
if (sum == num && sum != 0) {
30+
cout <<num<<" is a perfect number";
31+
} else
32+
cout <<num<<" is not a perfect number";;
33+
cout <<endl<<endl;
34+
return 0;
35+
}

primeNumbers/main.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// main.cpp
3+
// primeNumbers
4+
//
5+
// Created by samuel muloki on 12/05/2019.
6+
// Copyright © 2019 line23. All rights reserved.
7+
//
8+
9+
#include <iostream>
10+
11+
using namespace std;
12+
13+
int main(int argc, const char * argv[]) {
14+
int i, num;
15+
cout <<"Enter any number: ";
16+
cin >> num;
17+
if (num == 1) {
18+
cout <<"Smallest prime number is 2";
19+
}
20+
for (i = 2; i < num; i++) {
21+
if (num % i == 0) {
22+
cout <<num<<" is not a prime number";
23+
break;
24+
}
25+
}
26+
if (num == i) {
27+
cout <<num<<" is a prime number";
28+
}
29+
cout <<endl<<endl;
30+
return 0;
31+
}

0 commit comments

Comments
 (0)