Pls help me on this.
I really dont have an idea of solving this.
Here's the question ..

1. Write a c++ program that generates the following series: 0 -1 4 -9 16 -25 36 -49 64 -81 100

and also this one

2. write a program that generates the fibonacci series: 1 1 2 3 5 8 13 up to 50th term.

I was shocked when i saw this :-O It is not yet taking up. But pls. Help me. Thanks alot..

- Nell

Dani AI

Generated

Quick clarifications and runnable examples to complement the replies already posted by , , and .

The first sequence is simply the square of each integer with the sign flipped for odd indices (start from 0): 0, -1, 4, -9, ... . The Fibonacci task is the standard successive-sum sequence; the 50th term comfortably fits in 64-bit unsigned integers.

#include <iostream>

int main() {
    // alternating signed squares: 0 -1 4 -9 ... 100
    for (int n = 0; n <= 10; ++n) {
        long long val = 1LL * n * n;
        if (n & 1) val = -val;             // odd -> negative
        std::cout << val << (n == 10 ? '\n' : ' ');
    }

    // Fibonacci: first 50 terms (use unsigned long long)
    unsigned long long a = 1, b = 1;
    std::cout << a << ' ' << b;
    for (int i = 3; i <= 50; ++i) {
        unsigned long long c = a + b;
        std::cout << ' ' << c;
        a = b; b = c;
    }
    std::cout << '\n';
    return 0;
}
# alternating squares
print(' '.join(str(n*n if n%2==0 else -n*n) for n in range(11)))

# fibonacci (first 50 terms)
a, b = 1, 1
out = []
for _ in range(50):
    out.append(str(a))
    a, b = b, a + b
print(' '.join(out))

Notes: prefer an integer sign test (e.g. n & 1) over floating pow functions for speed and correctness. For C++, use unsigned long long for Fibonacci up to about F(93); beyond that a multiprecision library or Python is safer. Compilation example for the C++ snippet: g++ -std=c++11 file.cpp -o prog.

Recommended Answers

All 5 Replies

1. Write a c++ program that generates the following series: 0 -1 4 -9 16 -25 36 -49 64 -81 100

You simply need to print the squares of a number .
You can use square_of_num = num * num. Also every odd number has negative sign. It means if square_of_num < 0 multiply it with -1 .

2. write a program that generates the fibonacci series: 1 1 2 3 5 8 13 up to 50th term.

Loop till 50th term. Inside loop print your fibonacci number. Your logic will be that next number is equal to sum of previous two numbers . So to start with , you will have to take 2 numbers both initialized to 1.

Atleast start coding. Doesn't matter if it is wrong. We are here to help. But you need to start coding on your own first :)

Alright first things first... I see this is your first post... so

We are NOT going to give you the entire program, then you learn nothing, trying and getting some code going and asking for help with syntax, errors or middle snippets we will do any time

Ok so first problem, identify the sequence... what us different between character 1, character 2, and character 3... if you find the difference write a loop that takes a digit, adds that digit until you meet the top value (100)

The same goes for problem 2 but it is just the fibonaci seq, check google

1. Write a c++ program that generates the following series: 0 -1 4 -9 16 -25 36 -49 64 -81 100

and also this one
- Nell

Break it into two series
0 4 16 36 64 100 (square of even numbers starting from zero)
-1 -9 -25 -49 -81 (negative of square of odd numbers)
Mix both and you get the result

2. write a program that generates the fibonacci series: 1 1 2 3 5 8 13 up to 50th term.
- Nell

you need two variables, say i and j. Initially it looks like this
i = 1;
j = 1;
1. Add both and keep the sum in a variable.
2. copy j to i
3. copy sum to j and repeat the process

Break it into two series
0 4 16 36 64 100 (square of even numbers starting from zero)
-1 -9 -25 -49 -81 (negative of square of odd numbers)
Mix both and you get the result


you need two variables, say i and j. Initially it looks like this
i = 1;
j = 1;
1. Add both and keep the sum in a variable.
2. copy j to i
3. copy sum to j and repeat the process

Thanks for all the help. Im gonna try it. Ill post again and please check if its right. Thank you so much :D

for 0 -1 4 -9 16 -25 36 -49 64 -81 100

use number*number*(-1 raised to the power number)

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.