Help with assignment

Hi! If someone has time, could you look over this assignment that i have for school?

It shouldnt be long but i dont know how to write the code so if someone could hint what i should do it would be great.

Ok so the text is:

Specify the natural number n for which it is worth that the n^2 - 3 is divisible by the square of the number larger than 1.

So basically you need to enter a number from the keyboard and the program should tell me back if the problem above is true for that number or not.

Would be greatly appreciated if someone helped out.
closed account (48T7M4Gy)
Try some pseudocode:

1. input a number
2. square the number and subtract 23 from it
3. store this as the reference number
3. for all numbers (integers) greater than 1 test whether it will divide evenly into the reference
4. if it does then display the number
5. continue looping up to square root of the reference.
Hmmm ok so i did something... but its not working well...
mainly when i want to enter the number n or m its not working properly as in it accepts numbers which are over 1000
it fixes itself if i turn the signs to the other side but my professor said that i should do it this way. Im not sure where excately the mistake is.

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
26
27
28
29
30
31
32
33
#include <iostream>
#include <cmath>

using namespace std;
int i, j, k, q, m;
double s;

int main () {
do{
    cout<<"Enter number n [1-1000]:"<<endl;
    cin>>i;}
while((i>=1) && (i<=1000));

j=(pow(i,2)-2);

cout<<"n^2-3 is:"<<j<<endl;

do{
cout<<"Enter number m, which will be squared and then used as a divisor [2-1000]:"<<endl;
cin>>m;}
while ((m>=2) && (m<=1000));

q=pow(m,2);

s=j/q;

if (s==int()) {
    cout<<"Number "<<j<<" is divisible with the squared number "<<m<<endl;}
else{
    cout<<"Number "<<j<<" is not divisible with the squared number "<<m<<endl;}

return 0;
}
Last edited on
Your do...while loop from lines 9 - 12 doesn't look right. It will continually loop for as long as the condition (i>=1) && (i<=1000) is true. In other words, it will keep asking the user to input a value, until the user enters a value that's outside the range 1 - 1000, at which point it will proceed to use that value.

Solved it, thank you :)
You're welcome! Glad it worked out :)
Topic archived. No new replies allowed.