Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
how to split number x where x1 is perfect square and i,e 50=49+1
till end

What I have tried:

C++
#include <stdio.h>

int main()
{
    int a, n;
    printf("Enter a number: ");
    scanf("%d", &n);
    for(a = 0; a <= n; a++)
    {
        if (n == a * a)
        {
            printf("YES");
            return 0;
        }
    }
    printf("NO");
    return 0;
}</stdio.h>
Posted
Updated 6-Mar-16 23:48pm
v2
Comments
Jochen Arndt 7-Mar-16 5:06am    
If I understand the assignment correct, you have to look for the square that is closest to the input but not greater.

A solution might be to calculate the square until it is greater than the input value. Then subtract one from the root to get the closest square.

With your example of 50, the loop would go up to 64 = 8 * 8. Then the closest square is 7 * 7.
Afzaal Ahmad Zeeshan 7-Mar-16 5:38am    
You are not at all splitting it. You are just printing if the number is equal to the square of another number in the loop. In that case, if the input is 64, 49, or other similar ones. Only then the output would be yes. Because the square of them has to be equal to the number itself, too.

1 solution

Start by looking at the problem:
X = Y*Y + Z*Z, where X is a perfect square.

I'd probably start by identifying all the perfect squares less than X, and then start looking for "combinations", by taking the largest, adding the lowest and seeing if it matches.
If it does, you're done.
If it's lower than X, try the next lowest and repeat.
If it's greater than X, try the next highest and repeat.
If Y==Z it doesn't exist.

But this is your homework, so I won't give you any code!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900