Sunteți pe pagina 1din 5

c++ program to generate Fibonacci

series
#include<iostream>

using namespace std;

main()
{
int n, c, first = 0, second = 1, next;

cout << "Enter the number of terms of Fibonacci series you want" << endl;
cin >> n;

cout << "First " << n << " terms of Fibonacci series are :- " << endl;

for ( c = 0 ; c < n ; c++ )


{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout << next << endl;
}

return 0;
}

Factorial

#include<iostream>

using namespace std;

int main()

{
int num,factorial=1;

cout<<" Enter Number To Find Its Factorial: ";

cin>>num;

for(int a=1;a<=num;a++)

factorial=factorial*a;

cout<<"Factorial of Given Number is ="<<factorial<<endl;

return 0;

Calculate Factorial Using Recursion


#include<iostream>
using namespace std;

int factorial(int n);

int main()
{
int n;

cout << "Enter a positive integer: ";


cin >> n;

cout << "Factorial of " << n << " = " << factorial(n);

return 0;
}

int factorial(int n)
{
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}

C++ Programming Code to Reverse String

Following C++ program ask to the user to enter a string to reverse it and display the
reversed string on the screen:

/* C++ Program - Reverse String */

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char str[100], temp;
int i=0, j;
cout<<"Enter the String : ";
gets(str);
i=0;
j=strlen(str)-1;
while(i<j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
cout<<"Reverse of the String = "<<str;
getch();
}

C++ Program to Merge two Arrays in third array

In this program we enter an elements in any two array and then these two array (elements of array)

are store in third array.

Example

#include<iostream.h>
#include<conio.h>

void main()
{
int a[10],b[10],c[20],i;
clrscr();
cout<<"Enter Elements in 1st Array: ";
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"Enter Elements in 2nd Array: ";
for(i=0;i<10;i++)
{
cin>>b[i];
}
cout<<"\nElements of Array After Merge: ";
for(i=0;i<10;i++)
{
c[i]=a[i];
c[i+10]=b[i];
}
for(i=0;i<20;i++)
{
cout<<c[i];
}
getch();
}

Swap 2 num without using 3rd var

x = x + y; // x now becomes 15
y = x - y; // y becomes 10
x = x - y; // x becomes 5