Hello!!
I realy need some help!! :sad: :cry:
With Fibonacci numbers :(
I found some programes, and they are a lot of help :cheesy: but... I need some more help :rolleyes:

#include <iostream> 
#include <conio.h> 
using namespace std;
int main ()
{
int a[128];
int fN;
int i;
a[0]=1;
a[1]=1;
cout << "How many numbers do you want to add: ";
cin >> i;
for ( fN = 2 ; fN <= i ; fN++ )
{
a[fN]=a[fN-1]+a[fN-2];
cout << a[fN] <<"\n";
}
getch();
}

The program outcome is:


How many numbers do you want to add: 20
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946


This is grate :mrgreen: but... i need something more! ;)

I need the programe outcome to be something like this:

How many numbers do you want to add: 20
How many elements in a number you want to output: 3


144
233
377
610
987


The program sould output all numbers that have 3 elements.
Ofcors if I enter 4 in "How many elements in a number you want to output:" The program sould output numbers that have 4 elements (for example 10946 atc.)

I hope you will understand me :confused: .... I`m not realy good in spelling... :sad:
Help me please ;) :o

Oh.. I forgot to asc... whether this programe is in C language??... If not I have I problem :( I need a programe in C..... ;(

If n is the number of digits you want to output, and f is a fibonacci number, you need to check that:

f > pow( 10, n - 1 ) - 1

and

f < pow( 10, n )

As it stands the program is in C++. To get it into C you will need to use library functions for input and output instead of cin and cout.

someting like that?? i cant make it work ....

#include  #include  using namespace std;int main (){int a[128];int fN;int i,n;a[0]=1;a[1]=1;cout > i;cout > n;for ( fN = 2 ; fN

ops, something like this>>>

#include <iostream> 
#include <conio.h> 
using namespace std;
int main ()
{
int a[128];
int fN;
int i,n;
a[0]=1;
a[1]=1;
cout << "How many numbers do you want to add: ";
cin >> i;
cout << "How many number of digits you want to output: ";
cin >> n;
for ( fN = 2 ; fN <= i ; fN++ )
{
a[fN]=a[fN-1]+a[fN-2];
cout << a[fN] <<"\n";
fN > pow( 10, n - 1 ) - 1;
fN < pow( 10, n );
}
getch();
}

Oh.. I forgot to asc... whether this programe is in C language??... If not I have I problem :( I need a programe in C..... ;(

Please learn to Format your code. Yours is hard to read.

To convert to C simply use printf() for output, and fgets()/sscanf() combination for input


Oh, and :confused: stop :mad: using :confused: so :mad: many :confused: smileys! ;)
They are not necessary and just get in the way.

OK ^-^ I`ll try not to use to many smileys.eh... it`s so hard to program in c... I cant make the programe work ...

OK ^-^ I`ll try not to use to many smileys.eh... it`s so hard to program in c... I cant make the programe work ...

So format it and post the new version. And ask specific questions about it... Complaining about C gets us nowhere.

#include <iostream> 
#include <conio.h> 
using namespace std;
int main ()
{
int a[128];
int fN;
int i;
int n;
a[0]=1;
a[1]=1;
cout << "How many numbers do you want to add: ";
cin >> i;
cout << "How many number of digits you want to output: ";
cin >> n;
for ( fN = 2 ; fN <= i ; fN++ )
{
a[fN]=a[fN-1]+a[fN-2];
cout << a[fN] <<"\n";
}
{
fN > pow( 10, n - 1 ) - 1;
fN < pow( 10, n );
cout << a[fN] <<"\n";
}
getch();
}

whats wrong? how sould it look?..

for ( fN = 2 ; fN <= i ; fN++ )
{
    a[fN]=a[fN-1]+a[fN-2];
 
    if( ( a[fN] > pow( 10, n - 1 ) - 1 ) && ( a[fN] < pow( 10, N ) ) )
        cout << a[fN] <<"\n";
}

If, for example, you want to output three digits,

a[fN] > pow( 10, 2 ) - 1 makes sure that a[fN] > 99
a[fN] < pow( 10, 3 ) makes sure that a[fN] < 1000

#include <iostream> 
#include <conio.h> 
#include <stdio.h>
#include <math.h>
using namespace std;
int main ()
{
int a[128];
int fN;
int i;
int n;
int N;
a[0]=1;
a[1]=1;
cout << "How many numbers do you want to add: ";
cin >> i;
cout << "How many number of digits you want to output: ";
cin >> n;
for ( fN = 2 ; fN <= i ; fN++ )
{
    a[fN]=a[fN-1]+a[fN-2];
 
    if( ( a[fN] > pow( 10, n - 1 ) - 1 ) && ( a[fN] < pow( 10, N ) ) )
        cout << a[fN] <<"\n";
}
getch();
}

strange, i can imput "How many numbers do you want to add: " and "How many number of digits you want to output: "; but, somehow the risult doesnt apear... whats wrong?...

#include <iostream> 
#include <conio.h> 
#include <stdio.h>
#include <math.h>
using namespace std;
int main ()
{
int a[128];
int fN;
int i;
int n;
int N;
a[0]=1;
a[1]=1;
cout << "How many numbers do you want to add: ";
cin >> i;
cout << "How many number of digits you want to output: ";
cin >> n;
for ( fN = 2 ; fN <= i ; fN++ )
{
    a[fN]=a[fN-1]+a[fN-2];
 
    if( ( a[fN] > pow( 10, n - 1 ) - 1 ) && ( a[fN] < pow( 10, N ) ) )
        cout << a[fN] <<"\n";
}
getch();
}

strange, i can imput "How many numbers do you want to add: " and "How many number of digits you want to output: "; but, somehow the risult doesnt apear... whats wrong?...

In the sequence, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 a three digit number, if that's the number of digits you want, doesn't appear until the tenth iteration, so are you going round the loop enough times? A four digit number doesn't appear until the fifteenth iteration.

no, usualy i write 20 iterations, and i want him to write 3 digit numbers.i tried and with 2 digit numbers and 4 digit numbers and atc.

The only thing I can think of is to try putting casts in front of the pow function:

if( ( a[fN] > (int)pow( 10, n - 1 ) - 1 ) && ( a[fN] < (int)pow( 10, N ) ) )

eh.. still doesnt work... i dont get it... it must work... i dont see any mistakes...
may be there sould be a "hold screen" function? or something?...

eh.. still doesnt work... i dont get it... it must work... i dont see any mistakes...
may be there sould be a "hold screen" function? or something?...

I can see the mistake:

if( ( a[fN] > pow( 10, n - 1 ) - 1 ) && ( a[fN] < pow( 10, N ) ) )

Note the upper case N in the second call to pow. In C N and n are two different variable names. You have declared N as an int, but, so far as I can see, having declared it you don't use it again. Most likely it is initialsied to zero, and pow( 10, N ) then always returns the value 1.

??..
its hard, and i dont get it. if i write "int" then i have to use it?.....................

??..
its hard, and i dont get it. if i write "int" then i have to use it?.....................

You have declared a variable called n, and you have declared another variable called N. You should pass n to the second pow function, but instead you are passing N.

sooo.. the programe sould look like this?

#include <stdio.h>
#include <math.h>
int main()
int i , a , b , c , x , n;
//n value from t user
Console.Write("Enter the value for n:");
n=Convert.ToInt32(Console.ReadLine());
a=0;
b=1;
for(i=2;i<=n;i++)
{
c=a+b;
a=b;
b=c;
Console.Write(c);
Console.Write("\n");
}

That still looks like C++ to me.

Use to scanf or gets to input data
If using gets, use atoi to convert a string of digits to an integer
Use printf to output the result.

?

#include <stdio.h>
#include <math.h>
int main()
int i , a , b , c , x , n;
//n value from t user
scanf("Enter the value for n:");
n=Convert.ToInt32(Console.ReadLine());
a=0;
b=1;
for(i=2;i<=n;i++)
{
c=a+b;
a=b;
b=c;
printf(c);
printf("\n");
}

loke this? is this programe correct? somehow i cant make it work ..

That still looks like C++ to me.

Use to scanf or gets to input data
If using gets, use atoi to convert a string of digits to an integer
Use printf to output the result.

Never ever use gets() for input. Here's why
Rarely ever use scanf() . Here's a series about why
Both links give alternate safe solutions.

could you tell me where there is a mistake?
the programe still doesnt work..

Pretty close with the last effort, but you still need to work on whether you're programming in C or C++.

#include <stdio.h>
#include <math.h>
int main() {                            /*!! missing */
    char buff[BUFSIZ];
    int i, a, b, c, x, n;
    printf("Enter the value for n:");   /*!! not scanf */
    /*!! this looks like C++ */
    /* n = Convert.ToInt32(Console.ReadLine()); */
    if (fgets(buff, sizeof buff, stdin) != NULL) {
        /* successful input */
        if (sscanf(buff, "%d", &n) == 1) {
            /* successful conversion */
            a = 0;
            b = 1;
            for (i = 2; i <= n; i++) {
                c = a + b;
                a = b;
                b = c;
                /*!! needs a format printf(c);*/
                printf("%d", c );
                printf("\n");
            }
        }
    }
    return 0;
}

howcome the programe doesnt work?? i dont get it... it sould work... i input n value, but then the programe just ends...

Oh, you mean all the numbers get printed then the whole window disappears?

Well you could just run the program from a proper command line, not from the IDE.

Or you could put something like
system("pause");
just before the return 0;

Or you could put something like
system("pause");
just before the return 0;

Salem! How could you!?!?!

Use getchar()

Salem! How could you!?!?!

I guess there must have been a pretty good reason...

Or maybe because system ("pause") is guaranteed to stop the console window no matter how much junk is left off in the input stream.When getchar ( ) is used, one must be pretty sure that the input stream does not have a pending '\n'.
But then again, sytem ( "pause" ) is bad... ;)

No good reason, just a quick and lazy answer :sad:

But then again, sytem ( "pause" ) is bad... ;)

why is it bad??

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.