Write a program that asks the user to input 5 integers. For each integer, squaring it, print out the original integer that its square is not divisible by 4 and 3.
Any help / comments would be nice.
*i'm new to c++*
Break it into pieces.
1. Write a program to prompt the user for an integer, then print it out.
2. Modify the program to "print out the original integer that its square is not divisible by 4 and 3." I'm not sure what this actually means. Can you clarify.
3. Modify the code to ask for 5 integer (use a loop) and then prints out the values (in a second loop). You'll use a vector<> or array to store the integers.
When you get stuck, post your code and we'll be happy to help.
I THINK I did the first part right, but i'm not sure what to do from here on
It's asking "if the squared number is NOT divisible by 3 and 4, print out the original number"
thanks :)
#include <iostream>
using namespace std;
int main( ){
int a, b, c, d, e;
cout << "Input 5 Intergers and Hit ENTER" << endl;
cin >> a, b, c, d, e;
As dhayden stated, the optimal way to do this would be to use vectors, but since you don't know how to even approach this problem and you said yourself that you are new to c++, I would not use vectors just yet.
For a noob making this program, I would declare 5 integers. Get input for each integer. Once you have the input, square each integer by multiplying it by itself. Then, you need to check if that integer is divisible by 4 or 3. There is something called a mod operator (I think?) and it is represented by a %. What this operator does is it gives you the remainder when you try to divide something out.
So for example.
4 % 2 = 0, because 4/2 = 2 with a remainder of 0.
4%3 = 1, because 4/3 = 1 with a remainder of 1
You can use the mod operator to determine if something is divisible or not.
I wrote up an example doing it for only one integer, and you can fill it in for the other 5.
Let me know what you don't understand.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
int main()
{
int MyNumber; //Integer to hold the user input for the integer
int MyNumberSquared; //Integer to hold the square of MyNumber
cout << "Enter an integer:"; //Display to use to enter an integer
cin >> MyNumber; //Get input for my number and place it in MyNumber
MyNumberSquared = MyNumber*MyNumber; //Multiply MyNumber by itself to square it
if ((MyNumber % 3) == 0) //If my number is evenly divisible by 3
{
cout << "The square of " << MyNumber << "(" << MyNumberSquared << ")" << " is evenly divisible by 3." << endl;
}
if ((MyNumber % 4) == 0) //If my number is evenly divisible by 3
{
cout << "The square of " << MyNumber << "(" << MyNumberSquared << ")" << " is evenly divisible by 4." << endl;
}
return 0;
}