This C program defines a function to print the first 6 Fibonacci numbers. It initializes the first two Fibonacci numbers to 0 and 1, then uses a while loop to iteratively calculate and print each subsequent number by adding the previous two numbers, updating the variables, and incrementing the counter until 6 numbers have been printed. The main function simply calls the Fibonacci function.
Convert to study materialsBETA
Transform any presentation into ready-made study materialselect from outputs like summaries, definitions, and practice questions.
1 of 1
Download to read offline
More Related Content
Fibonacci surce code
1. // include the library for print menssages at console
#include <stdio.h>
// Author: German Augusto Niebles Alvarez
// version 1.0
// Created on 2016/12/17
// C Program which prints the six first fibonacci numbers
// define fibonacci function
void oneFibonacciSecuence()
{
int maxNumero;
int unFibo1;
int unFibo2;
int unCounter;
// store value for counter, upper limit of the loop
maxNumero = 6;
// init counter in 2 for the firt two fibonnaci numbers
unCounter = 2;
printf("The first 6 fibonacci numbers aren");
// init first fibonacci number
unFibo1 = 0;
// init second fibonacci number
unFibo2 = 1;
printf("%unFibo1", unFibo1);
printf("n");
// make a loop for calculate next four fibonacci numbers
while (unCounter<=maxNumero){
printf("%unFibo2" , unFibo2);
printf("n");
// update next fibo number 2 in secuence
unFibo2 = unFibo1 + unFibo2;
// calculate net fibo number 1 in secuence
unFibo1 = unFibo2 - unFibo1;
unCounter = unCounter + 1;
} // end while
} // end function
// Main program
int main()
{
// call fibonacci function
oneFibonacciSecuence();
return 0;
} // end main