Question about Computers & Internet
Hi,
I think you are one good student with interest in C++. I am reminded of my days in college. When my teacher asked me the same question, I wrote it in three formats. I am putting them all here. Wish you a very good learning time in the world of C/C++
Type 1:
********
#include<stdio.h>
void main()
{
int n;
int fibo(int,int,int);
printf("\nEnter number of terms in series:");
scanf("%d",&n);
fibo(n,0,1);
}
fibo(int n,int a,int b)
{
int c;
printf(" %d ",a);
c=b;
b+=a;
a=c;
if(n>0) fibo(n-1,a,b);
return 0;
}
Type 2:
********
#include<stdio.h>
void main()
{
int i,j,a=0,b=1,c;
printf("\nEnter number of terms in the series:");
scanf("%d",&i);
for(j=1;j<=i;j++)
{
printf(" %d ",a);
c=b;
b=a+b;
a=c;
}
}
Type 3:
********
#include<stdio.h>
void main()
{
int i,a=0,b=1,c;
printf("\nEnter number of terms in series:");
scanf("%d",&i);
while(i>0)
{
printf(" %d ",a);
c=b;
b=a+b;
a=c;
i--;
}
}
**************************
Come back anytime for any program in basic C++. All my old codes wrote during my college days are lying without any use. :)
Have a nice day.
Posted on Nov 28, 2008
Here is a Pascal stub
================
Program Fibonacci;
const
nmax = 500;
type
t_Farray = array [0 .. nmax-1] of LongInt ;
var
Farray : t_Farray;
i : word;
Begin
Farray [0] := 0; writeln(Farray [0]); // Initialize the ******
Farray [1] := 1; writeln(Farray [1]); //
for i:=2 to nmax-1 do begin
Farray[i] := Farray[i-2] + Farray[i-1];
Writeln (Farray[i]
end;
end.
Posted on Nov 13, 2008
Ok! Here's a snippit from some code I wrote in beginning c++ classes. However, don't just cut and paste it. Know how it works since it's important in algorithm examination!
Just tested it, compiles and runs perfectly on GCC 4.0.1 on my mac, as well as my linux boxen. Windows may take some changes to output data.
/***********************************************
* Steven Parker
* Calculating Fib Sequence
* after two starting values, each number is the
* sum of the two preceding numbers
***********************************************/
#include <iostream>
using namespace std;
int main() {
int whentostop = 20; //How many values of sequence to calculate
int fib[whentostop]; //Hold fib numbers
for (int i=0; i < whentostop; i++) {
if (i == 0)
fib[0] = 0;
else if (i == 1)
fib[1] = 1;
else
fib[i] = fib[i-1] + fib[i-2];
cout << "Fibonacci[" << i << "]: " << fib[i] << endl;
}
return 0;
}
Posted on Nov 13, 2008
Dec 17, 2013 | Texas Instruments TI-84 Plus Silver...
Apr 02, 2011 | Casio FX9750GII Graphic Calculator
Mar 04, 2011 | Texas Instruments TI-84 Plus Calculator
Jul 02, 2010 | Toshiba Satellite A135-S2276 Notebook
Jun 08, 2010 | Casio FX-250HC Calculator
Oct 08, 2009 | Texas Instruments Explorer Plus Scientific...
Mar 02, 2009 | Casio FX-300MS Calculator
Jun 10, 2008 | Microsoft Office Outlook 2003 for PC
Mar 30, 2008 | Casio FX-115ES Scientific Calculator
Mar 01, 2021 | The Computers & Internet
177 people viewed this question
Usually answered in minutes!
×