i dont know how to explain this

Hey!

i am trying to write a program that would get all possible variants of four variables squared added together would make a number

i mean this:

the user types 5

the program outputs 0^2 + 0^2 +1^2 + 2^2

please help! i've been stuck on this for allmost four days and cant figure it out!!!!
closed account (z05DSL3A)
Post what you have done.
i havent done anything yet that is worth posting. I cant simply think of a basic way of getting it going
closed account (z05DSL3A)
If I understand you correctly, this should get you on the right lines:
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
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int comp[4]={0};
    int input(5);

    for(int i(0); i < 4; ++i)
    {
        comp[i] = (int)sqrt((double) input);
        input = input - (comp[i] * comp[i]);
    }

    for(int i(0); i < 4; ++i)
    {
        cout << comp[i] << " ";
    }

    cout << endl;

    system ("pause");
    return 0;
}
yes this is exactly what i was searching for, but i want to get multiple answers from one number,for example:

number 4.

4 = 2^2 +0^2+0^2+0^0;
4 = 1^2+1^2+1^2+1^2;

and could you please explain the logic about this, and explain the 13th and 14th lines?
closed account (z05DSL3A)
Line 13, casts the input variable to a double type and passes it in the sqrt() function to calculate the square root. The result is the cast back to an int and stored in the array.

Line 14, calculates the 'remainder' used in the next iteration. So it calculates the square of the final result from line 13 and subtracts it from the initial value.

e.g. the square root of 5 is 2.23... converted back to an int is 2. This will leave 1 as the starting point for the next iteration.
Topic archived. No new replies allowed.