Day 5 Sample code
Prime 1 - not quite right
#include<iostream>
using namespace std;
int main() {
int num,count=0;
cout<<"Enter the number to check : ";
cin>>num;
int i = 2;
while (i<num){
if(num%i==0) { // checks if the number is fully divisible or not
count++;
}
i++;
}
if(count==0) {
cout<<"It is a Prime Number \n";
} else {
cout<<"It is not a Prime Number \n";
}
}
Prime correct (for 1)
#include<iostream>
using namespace std;
int main() {
int num,count=0;
cout<<"Enter the number to check : ";
cin>>num;
int i = 2;
while (i<num){
if(num%i==0) { // checks if the number is fully divisible or not
count++;
}
i++;
}
if(num < 2 || count>0) {
cout << num <<" is not a Prime Number \n";
} else {
cout << num <<" is a Prime Number \n";
}
}
Prime faster
#include<iostream>
using namespace std;
int main() {
int num,count=0;
cout<<"Enter the number to check : ";
cin>>num;
int i = 2;
while (i<num && num%i!=0){
i++;
}
if (i==num) {
cout<<"It is a Prime Number \n";
} else {
cout<<"It is not a Prime Number \n";
cout<<"The Number " << num << " is divisable by " << i << "\n";
}
}
Prime with multiway
#include<iostream>
using namespace std;
int main() {
int num,count=0;
cout<<"Enter the number to check : ";
cin>>num;
int i = 2;
while (i<num && num%i!=0){
i++;
}
if (num<2) {
cout << num << " is not prive by definition";
} else if (i==num) {
cout << num <<" is a Prime Number \n";
} else {
cout<<"It is not a Prime Number \n";
cout<<"The Number " << num << " is divisable by " << i << "\n";
}
}
Prime with for statement
#include<iostream>
using namespace std;
int main() {
int num,count=0;
bool prime = true;
cout<<"Enter the number to check : ";
cin>>num;
for(int i=2;i<num;i++){
if(num%i==0) { // checks if the number is fully divisible or not
prime = false;
}
}
if(prime) {
cout<<"It is a Prime Number \n";
} else {
cout<<"It is not a Prime Number \n";
}
}
Prime with break
#include<iostream>
using namespace std;
int main() {
int num,count=0;
bool prime = true;
cout<<"Enter the number to check : ";
cin>>num;
if (num > 1) {
for(int i=2;i<num;i++){
if(num%i==0) { // checks if the number is fully divisible or not
prime = false;
break;
}
}
if(prime) {
cout<<num << " is a Prime Number \n";
} else {
cout<<num << " is not a Prime Number \n";
}
} else {
cout << num << " is not prime by definition";
}
}
Prime using square root
#include<iostream>
using namespace std;
#include <math.h>
int main() {
int num,count=0;
bool prime = true;
cout<<"Enter the number to check : ";
cin>>num;
int max = sqrt(num);
if (num > 1) {
for(int i=2;i<=max;i++){
if(num%i==0) { // checks if the number is fully divisible or not
prime = false;
break;
}
}
if(prime) {
cout<<num << " is a Prime Number \n";
} else {
cout<<num << " is not a Prime Number \n";
}
} else {
cout << num << " is not prime by definition";
}
}
Prime list
#include<iostream>
using namespace std;
#include <math.h>
int main() {
int max,count=0;
bool prime = true;
cout<<"Enter the maximum number to check : ";
cin>>max;
for (int j=2 ; j<=max; j++) {
prime = true;
for(int i=2;i<= sqrt(j);i++){
if(j%i==0) {
prime = false;
break;
}
}
if(prime) {
cout << j << " ";
}
}
cout << "\nDone listing primes less than or equal to " << max << ".\n";
}
Prime 1
Prime 1
Prime 1