[英]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.您的代码有两个问题,一是打印 fib0、fib1 的地址而不是它们的值,二是在打印两个值时仅使用一个格式说明符。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.