I have an assignment where I am given a sequence and I must turn the user's input (int) into that sequence. For example:

number : 2
The sequence starting at 2 is : 2 4 16 37 58

The sequence is: 2^2=4, 4^2=16, 1^2+6^2=37, 3^2+7^2=58, etc....

how would I compute the sequence? and if the user enters a 2 digit number would I just assign them n1 and n2?


This is what I have so far [c++]:

int n1, n2, n, sum=0;

cout<<"number: ";
cin>>n1>>n2;

n=pow(n1, 2)+pow(n2, 2);

for(n=1; n<162; ++n) //162 is the largest number b/c 9^2+9^2=162
{
cout<<sum<<endl;
sum+=n;
}

//when i run it it gives me a long line of integers that go up to 12880

hi,
I got ur question.
If u wanna do like that u have to take modulus for the two digit number.
I will give u sample code

int n1, n2, n=0, sum=0;

cout<<"number: ";
cin>>n1;

while(n1!=0)
{
n2=n1%10;
n=n+pow(n2, 2);
n1=n1/10;
}
for(n=1; n<162; ++n) //162 is the largest number b/c 9^2+9^2=162
{
cout<<sum<<endl;
sum+=n;
}

thanks for yur guy's help! now that i think about it, i feel dum for not figuring it out before. oh, well, my mind isn't very clear at 2:00 am in the morning.
here is my code (c++):

int n1, n2, n, sum=0;

    cout<<"number: ";
    cin>>n;

    n1=n/10;
    n2=n%10;

    while(n<200)
    {
        cout<<n<<" ";
        n1=n/10;
        n2=n%10;
        n=pow(n1, 2)+pow(n2, 2);

    }

my next step is to only print out the sequence up to 58 if 58 occurs in the sequence. If 1 occurs, I must print out that it is insipid.

a)if any term in the sequence equals 1, then all successive terms are 1
b)if any term in the sequence equals 58, then the sequence cycles:
…, 58, 89, 145, 42, 20, 4, 16, 37, 58, 89, 145…..

It is known that either condition a or b MUST occur.

An integer N0 is called insipid if condition (a) occurs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.