What is the output of the following code? [code] void setup() { Serial.begin(9600); int i,n,t1=0,t2=1,nt; n=4; for(i=1;i<=n;++i)

Thu Ha

New member
What is the output of the following code?
Code:
void setup() {
    Serial.begin(9600);
    int i,n,t1=0,t2=1,nt;
    n=4;
    for(i=1;i<=n;++i) {
        Serial.print(t1);
        Serial.print(“ ”);
        nt=t1+t2;
        t1=t2;
        t2=nt;
    }
}
void loop() {
    //Do Nothing
}
A. 0 1 1 2
B. 1 1 2 3
C. 1 2 3 5
D. 2 3 5 8
 
Answer: A
Explanation: The above code demonstrates the Fibonacci series in which 2 numbers in the series are added up together to make the third number and so on and so forth. The above code uses 0 and 1 as the initial two numbers so the series starts with 0 and 1 then goes on to print 4 more numbers since the for loop used here is limited to 4 only.
 

Tìm kiếm

Back
Top