Day 5 Sample code

Using do-while to repeat program code

int main() {
    int x, y;
    char done;
    do {
        cout << "Input two numbers:";
        cin >> x >> y;
        cout << "Sum:" << x + y << endl;

        cout << "Do again? (Type y to repeat, anything else to stop):";
        cin >> done;
    } while (done == 'y');
    return 0;
}
 

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";
} 

Find Factors

#include <iostream>
#include <math.h>
using namespace std;

int main() {
	int n;
	bool prime = true;
	cout << "Enter a number to find it's factors:";
	cin >> n;
	cout << "Factors: ";
	for (int i = 2; i<= sqrt(n) ; i++) {
		if (n%i == 0) {
			cout << i << " ";
			prime = false;
		}
	}
	if (prime) {
		cout << "Prime, no factor other then 1 and " << n << endl;
	}
	return 0;
}

Prime 1


Prime 1


Edit | Attach | Watch | Print version | History: r7 < r6 < r5 < r4 < r3 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r6 - 2016-09-13 - JimSkon
 
  • Edit
  • Attach
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback