Math Problem Statement
write a simple C++ program to print all the 4 digit numbers which are satisfying property of 7744 ie first two digits are same and last two digits are same and the number 7744 itself is a perfect square
Solution
Here is a simple C++ program that prints all four-digit numbers which satisfy the property of 7744. Specifically, it looks for numbers where the first two digits are the same, the last two digits are the same, and the number is a perfect square (like 7744):
cpp#include <iostream> #include <cmath> using namespace std; bool isPerfectSquare(int num) { int sqrtNum = sqrt(num); return (sqrtNum * sqrtNum == num); } int main() { // Loop through all 4-digit numbers for (int num = 1000; num < 10000; num++) { int firstTwoDigits = num / 100; // Get the first two digits int lastTwoDigits = num % 100; // Get the last two digits // Check if first two digits and last two digits are the same if (firstTwoDigits / 10 == firstTwoDigits % 10 && lastTwoDigits / 10 == lastTwoDigits % 10) { // Check if the number is a perfect square if (isPerfectSquare(num)) { cout << num << endl; // Print the number } } } return 0; }
Explanation:
- The program defines a helper function
isPerfectSquare()
to check if a number is a perfect square. - It then loops through all four-digit numbers (from 1000 to 9999).
- For each number, it checks if the first two digits are the same and if the last two digits are the same.
- Finally, it checks if the number is a perfect square. If all conditions are satisfied, it prints the number.
When you run this program, it will print numbers like 7744, which satisfy the given property.
Let me know if you want any additional details or clarifications!
Related questions:
- How can I modify this program to work for three-digit numbers instead of four-digit numbers?
- How can I find perfect squares that follow a different pattern, like the first and last digits being prime?
- What are other ways to check if a number is a perfect square without using the
sqrt()
function? - How would you optimize this program for larger ranges, like checking for 5-digit numbers?
- What is the significance of numbers like 7744 in number theory or mathematics?
Tip: Always check if your program works for edge cases (like the smallest and largest possible numbers) to ensure that it behaves correctly across the full input range.
Ask a new question for Free
By Image
Drop file here or Click Here to upload
Math Problem Analysis
Mathematical Concepts
Number Theory
Perfect Squares
Digit Patterns
Formulas
n = k^2 (where n is a perfect square, and k is an integer)
Theorems
Properties of Perfect Squares
Suitable Grade Level
Grades 9-12