Nothing Special   »   [go: up one dir, main page]

Fibonaci Series

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

#include <iostream>

using namespace std;

int main() {
int n, t1 = 0, t2 = 1, nextTerm = 0;

cout << "Enter the number of terms: ";


cin >> n;

cout << "Fibonacci Series: ";

for (int i = 1; i <= n; ++i) {


// Prints the first two terms.
if(i == 1) {
cout << t1 << ", ";
continue;
}
if(i == 2) {
cout << t2 << ", ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;

cout << nextTerm << ", ";


}
return 0;
}

Output

Enter the number of terms: 10


Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

program to print cross or X as pattern

#include <iostream>
using namespace std;

int main()
{
int n;
cout << "Enter the value of n \n";
cin >> n;

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= n; j++)


{
if (j == i || j == (n + 1 - i))
cout << "$";
else
cout << " ";
}
cout << endl;
}
return 0;
}

C++ program to print half pyramid

#include <iostream>
using namespace std;

int main()
{
int rows;

cout << "Enter number of rows: ";


cin >> rows;

for(int i = 1; i <= rows; ++i)


{
for(int j = 1; j <= i; ++j)
{
cout << "* ";
}
cout << "\n";
}
return 0;
}

C++ program to print inverted half pyramid

#include <iostream>
using namespace std;

int main()
{
int rows;

cout << "Enter number of rows: ";


cin >> rows;

for(int i = rows; i >= 1; --i)


{
for(int j = 1; j <= i; ++j)
{
cout << "* ";
}
cout << endl;
}

return 0;
}

C++ to rint full pyramid

#include <iostream>
using namespace std;

int main()
{
int space, rows;

cout <<"Enter number of rows: ";


cin >> rows;

for(int i = 1, k = 0; i <= rows; ++i, k = 0)


{
for(space = 1; space <= rows-i; ++space)
{
cout <<" ";
}

while(k != 2*i-1)
{
cout << "* ";
++k;
}
cout << endl;
}
return 0;
}

int main()
{
int arr[50], size, insert, i, pos;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Enter element to be insert : ";
cin>>insert;
cout<<"At which position (Enter index number) ? ";
cin>>pos;
// now create a space at the required position
for(i=size; i>pos; i--)
{
arr[i]=arr[i-1];
}
arr[pos]=insert;
cout<<"Element inserted successfully..!!\n";
cout<<"Now the new array is : \n";
for(i=0; i<size+1; i++)
{
cout<<arr[i]<<" ";
}
getch();
return 0;
}

username for inderscience


Haftom1Kahsay

age
1. kal'ab araya 17
2. amanuel abraha 18
3. mearg abadi 18
4. filmon g.tnsae 18
5. dawit negasi 18
6. Luam berihu 18
7. michael kebede 18
8. novel tesfa'alem 17
9. nardos weldekiros 19
10. yordanos yohannes 20
11. sibagads mulugeta 16
12. naod fitsum 19
13. haysem abdulkadir 17

You might also like