Hey guys. Made this piece of code this for my TI for the upcoming SAT. Then I thought that it has a lot of good intro c++ stuff. So I'm going to do a break-down of the code. Hope you enjoy!!
First, the whole code.
#include <iostream.h>
int main(){
//vars
int inNum;
bool check = true;
//setup
cout << "\nPlease enter the number: ";
cin >> inNum;
// actual calcs
for (int i = 2; i < inNum && check;){
if (inNum%i != 0){
i++;
}else{
check = false;
}
} //end for
if (check == false){
cout << "\nNumber is not prime!\n\n";
}else if (check == true){
cout << "\nNumber is prime!\n\n";
}else{
cout << "\n\n\nWTF?\n\n";
}
return 0; //for syntax only!!!
}//end of main
Okay, #1
#include <iostream.h>
This just tells the program that we want to be able to process basic input. This is used for a lot of things, but I'm just getting stuff from the keyboard.
#2
int main(){
//vars
int inNum;
bool check = true;
We declare the main method, of the int type. We then declare the variables we want to use in the program. Personally, I like to declare all of my vars upfront. Here we use an int (basically a simple number) and a bool (logic statement (true/false)).
#3
//setup
cout << "\nPlease enter the number: ";
cin >> inNum;
This is a VERY important step! This is where we use the IO functions of C++. cout tells the program to print the specified text, and cin takes the inputed data, and stores it in the variable we already declared (int inNum;).
*the \n is used to make a new line. The // is used for comments, and not used in the actual program.
#4
for (i = 2; i < inNum && check;){
if (inNum%i != 0){
i++;
}else{
check = false;
}
} //end for
Okay, now we're moving into real programming. We used a for loop here. In a nutshell, I instructed the program to create a new variable (int i), and set its value to 1. Then, I told the program to keep running while i < inNum AND check is still true. After this, I could have created a number of other statements, but I only needed two. This is the real use of the for loop. Normally, it is used to declare, check, and increment a value. Since you can do all of this from a single statement, it is better for this than a while loop.
After that, we introduce the if/else statment. These take a statment, check if they are true, and execute the code depending on the outcome.
Next, I use the mod (%) sign. This is just returning the remainder of inNum / the current value of i. We then check to see if the remainder does not equal (!=) 0. This tells us that the number is not prime, so we can keep checking for higher values of i (i++ (adding 1 to the value of x) ). If we find that it ever DOES equal 0, it cannot be prime and we set the boolean logic variable check to false. This means that the loop can no longer continue, and we stop checking for values.
#5
//if/else
if (check == false){
cout << "\nNumber is not prime!\n\n";
}else if (check == true){
cout << "\nNumber is prime!\n\n";
}else{
cout << "\n\n\nWTF?\n\n";
}
This is the basic programming logic statement. Rember we set check to false if the inputed number is not prime. In *MOST* programming, you will use = to assign a value, and == to compare something's value to another value.
Here, we look to see if check is false or true. If check is false, the number is not prime, and we report this to the user. If check is true, we tell the user. If a magnet was used to wipe a specifc point of your memory in the split second it took to check this, it outputs this to the user. Can't be to prepared, can you?
#6
return 0; //for syntax only!!!
}//end of main
We then end to code. The return of 0 isn't required, but is good for syntax.
I tried to do the best I could with the syntax. This is the first tut I've ever done, so any pointers would be helpful.
As usual, any feedback appriciated!