简体   繁体   中英

What's wrong with this code?. I just learned programming a few minutes ago and I'm trying to make this

I just learned programming a few minutes ago and I'm trying to make this

#include <iostream>
int main(){
    int n, fibo_n, fibo_n1=1, fibo_n2=0
    cout<<"Enter the max term of the Fibonacci Sequence: "

    for (int i =1; i<n; i++){
        fibo_n=fibo_n1+fibo_n2
        fibo_n2=fibo_n1
        fibo_n1=fibo_n
        cout<<fibo_n<<" "
    }
    cout<<endl
    cin.get()
    return 0
}

There are two problems with your code one is you are printing addresses of fib0, fib1 instead of their value and the second is you are using only one format specifier while printing two values.

Here is the modified code.

#include <stdio.h>

void Fibonaci(int N);

void main(){
    int N;
    long hasil;
    printf("Enter the number of elements : ");
    scanf("%d", &N);
    Fibonaci(N);
 }
void Fibonaci(int N){
    int fib0=0, fib1=1, fib;
    printf("%d\n%d\n", fib0, fib1); //modified
    while(fib0<=N/2){
        fib=fib0+fib1;
        fib0=fib1;
        fib1=fib;
        printf("\n%d", fib1); //modified
    }
}
#include <stdio.h>

void Fibonaci(int N);

void main(){
    int N;
    long hasil;
    printf("Enter the number of elements : ");
    scanf("%d", &N);
    Fibonaci(N);
}
void Fibonaci(int N){
    int fib0=0, fib1=1, fib;
    printf("%d, %d\n", fib0, fib1);
    while(fib0<=N/2){
        fib=fib0+fib1;
        fib0=fib1;
        fib1=fib;
        printf("\n%d", fib1);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM