Tuesday 5 March 2013

calculate factorial and double factorial of a given number-


#include <iostream.h>
using namespace std;
//factorial function
int factorial(int n)
{
int res = 1,i;
for (i=1;i<=n;i++)
{
 res *= i;
}
return res;
}
//double factorial function
int dfact(int n)
{
int i;double res=1.0;
for(i=n;i>=1;i-=2)
{
res *=i;
}
return res;
}
int main( )
{
int n;
  cout << "Enter the number=";
  cin >> n;
  cout << n << "! = " <<factorial(n) << endl; //factorial output
  cout << n << "!! = " <<dfact(n) << endl; //double factorial output
  system("pause");
}

1+(1+2)+(1+2+3)+..........


#include<iostream.h>
#include<conio.h>
main()
{
      int a=1;
      cout<<a;
      for(int i=1;i<5;i++)
      {
              cout<<"+(";
              for(int j=1;j<=i;j++)
              {
              cout<<j<<"+";
              }
              cout<<i+1<<")";
       }
              getch();
}

teamviewer 8.0.17292




click here to download

Print a diamond shape using asterisks in C/C++

#include<iostream.h>
#include<conio.h>
main()
{
int sum=3;
for(int i=-sum;i<=sum;i++)
{
for(int j=-sum;j<=sum;j++)
{
if(abs(i)+abs(j)<=sum)
{cout<<"*";}
else{cout<<" ";}
}
cout<<endl;
}
getch();
}



The above code will prints-
*
***
*****
*******
*****
***
*
Wait I haven’t done yet. You can have fun with it. Just change the ‘ if ‘ condition. You will get new design :) . Wanna see???. Lets take a look-
1if(abs(i)*abs(j)  <= NUM)
prints-
***
***
*******
*******
*******
***
***
1if( abs(i)+abs(j)==NUM)
prints-
*
*  *
*      *
*          *
*      *
*  *
*
1if( abs(i)==abs(j))
prints-
*          *
*      *
*  *
*
*  *
*      *
*          *
1if( abs(i)==0||abs(j)==0)
prints-
*
*
*
*******
*
*
*
1if( abs(i)>=abs(j))
prints-
*******
*****
***
*
***
*****
*******
1if(abs(i) <=abs(j))
prints-
*          *
**      **
***  ***
*******
***  ***
**      **
*          *

to print * in triangular shape

    *
   **
  ***
 ****
*****

#include<iostream.h>
#include<conio.h>
main()
{
int sum=3;
for(int i=-sum;i<=sum;i++)
{
for(int j=-sum;j<=sum;j++)
{
if(abs(i)+abs(j)<=sum)
{cout<<"*";}
else{cout<<" ";}
}
cout<<endl;
if(i==0)
{break;}
}
getch();
}