Fibonacci Series and Factorial Calculation C++ Program, Assignments of Object Oriented Programming

This c++ program defines a calc class with fib and fact methods. The fib method takes an integer input n and prints the first n numbers in the fibonacci series. The fact method calculates the factorial of the given integer n and prints it. The main function initializes the calc object and calls the fib method.

Typology: Assignments

2020/2021

Uploaded on 05/26/2021

aniruddh-singh-1
aniruddh-singh-1 🇮🇳

5

(1)

4 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include <iostream>
using namespace std;
class calc {
int n;
void fact();
public:
void fib();
};
main() {
calc C;
C.fib();
}
void calc::fib() {
cout<<"Number= ";
cin>>n;
fact();
int a=0,b=1,c=0;
cout<<"\nFibonacci series= ";
for(int i=1;i<=n;i++)
{
if (i==1) {
cout<<a<<",";
continue;
}
if (i==2) {
cout<<b<<",";
continue;
}
c=a+b;
a=b;
b=c;
cout<<c<<",";
}
}
void calc::fact() {
int fact=1;
for(int i=1;i<=n;i++) {
fact=fact*i;
}
cout<<"\nFactorial= "<<fact;
}

Partial preview of the text

Download Fibonacci Series and Factorial Calculation C++ Program and more Assignments Object Oriented Programming in PDF only on Docsity!

#include using namespace std; class calc { int n; void fact(); public: void fib(); }; main() { calc C; C.fib(); } void calc::fib() { cout<<"Number= "; cin>>n; fact(); int a=0,b=1,c=0; cout<<"\nFibonacci series= "; for(int i=1;i<=n;i++) { if (i==1) { cout<<a<<","; continue; } if (i==2) { cout<<b<<","; continue; } c=a+b; a=b; b=c; cout<<c<<","; } } void calc::fact() { int fact=1; for(int i=1;i<=n;i++) { fact=fact*i; } cout<<"\nFactorial= "<<fact; }