In: Computer Science
Write a C++ program that accepts a positive integer number from the keyboard . The purpose of the program is to find and the display all the square pair numbers between 1 and that number. The user should be able to repeat the process until he/she enters n or N in order to terminate the process and the program.
Square numbers are certain pairs of numbers when added together gives a square number and when subtracted also gives a square number. This program displays all the pairs of square numbers .
#include <iostream>
#include<cmath>
using namespace std;
int main() {
int n;
char ch;
while(true){
cout<<"Enter a positive number";//read number from user
cin>>n;
for(int i=1;i<=n;i++){
if(floor(sqrt(n+i))==ceil(sqrt(n+i))){//check if sum of numbers
result in square number
if(floor(sqrt(n-i))==ceil(sqrt(n-i))){//check if difference of
numbers is square number
cout<<i<<" "<<n<<endl;//print the
numbers
}
}
}
cout<<"Do you want to (c)ontinue or (n)ot?";
cin>>ch;//read choice if user wants to continue with another
number
if(ch=='n')
break;
}
return 0;
}
Screenshots:
The screenshots are attached below for reference.
Please follow them for output.
Please let me know below in the comments section in case of any help needed. Thank you.