I’ve made it far enough in my the BeginnersCPP.com tutorials that I was able to tackle the first of their code challenges! The goal was to write a program that could determine whether or not a number was a prime number. I did have to look up how to determine whether there would be remainders when dividing, but it turns out that was very similar to Python! This is what I came up with:
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 |
#include using namespace std; int main(){ int number; cout << "Enter a number: \n"; cin >> number; if ((number == 3) || (number==5) || (number==7)){ cout <<"Your number is a prime number.\n"; } else if ((number%2==) || (number%3==) || (number%4==) || (number%5==) || (number%6==) || (number%7==) || (number%8==) || (number%9==)){ cout << "Your number is not a prime number.\n"; } else { cout << "Your number is a prime number.\n"; } return (); } |
I have been pleasantly surprised with C++. I thought that it was going to be more of a challenge to learn the syntax but so far it hasn’t been that bad. Now that I’ve gotten the basics of C++ down I might go back to some of the other Python courses that I was in the middle of before I had to switch gears for a bit. But I feel a little bit more confident for when I will need to go further into C++ or when I’ll be using it in school! Speaking of, I registered for my first two classes, which start in June. It seems like a while away but it’ll probably sneak up on me!