Showing posts with label Patterns. Show all posts

C++ Program to Print Pascal Triangle

Pascal's triangle is a triangular array of the binomial coefficients.
triangle

Example

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

void main()
{
int bin,p,q,r,x;
clrscr();
bin=1;
q=0;
cout<<"\t\t\tDisplay pascal Triangle";
cout<<"\n\n\nHow Many Row Do you want to input:";
cin<<r;

cout<<"\nPascal's Triangle:\n";

while(q<r)
{
for(p=40-3*q;p>0;--p)
cout<<" ";
for(x=0;x<=q;++x)
{
if((x==0)||(q==0))
bin=1;
else
bin=(bin*(q-x+1))/x;
cout<<"      ";
cout<<bin;
}

cout<<"\n\n\n";
++q;
}
getch();
}
Output


C++ Program to Print Diamond of Stars

Using C++ language you can print diamond of stars, here you need to print two triangle, simply print first triangle and second triangle is reverse of first triangle.

C++ Program to Print Diamond of Stars


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

void main()
{
 int n, c, k, space = 1;
 clrscr();
 cout<<"\n\nEnter number of rows: ";
 cin>>n;
 space = n - 1;

  for (k = 1; k<=n; k++)
  {
    for (c = 1; c<=space; c++)
      cout<<" ";

    space--;

    for (c = 1; c<= 2*k-1; c++)
      cout<<"*";

    cout<<"\n";
  }
 
  space = 1;
 
  for (k = 1; k<= n - 1; k++)
  {
    for (c = 1; c<= space; c++)
      cout<<" ";
 
    space++;
 
    for (c = 1 ; c<= 2*(n-k)-1; c++)
      cout<<"*";
 
    cout<<"\n";
  }
getch();
}
Output


C++ Program to Print number Series


In c++ language you can print any number series. Here i will show you how to print number series in c language with explanation. In case of print number series you need to focus on common difference between two numbers..

C program to Calculate 1 + 2 + 3 + 4 + 5 + ... + n series

Example

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

void main()
{
int i,n,sum=0;
clrscr();
    n=10;
    for(i=1;i<=n;i++)
    {
        sum+=i;
    }
    cout<<"Sum: "<<sum;
getch();
}

Output

Sum: 55

C program to Calculate (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n) series

Example

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

void main()
{
int i,n,sum=0;
 clrscr();
 n=10;
    for(i=1;i<=n;i++)
    {
        sum+=i*i;
    }
    cout<<"Sum: "<<sum;
getch();
}

Output

Sum: 385

C program to Calculate (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) series

Example

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

void main()
{
 int i,j,n,sum=0;
 clrscr();
    n=10;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            sum+=j;
        }
    }
    cout<<"Sum: "<<sum;
getch();
}

Output

Sum:385

C++ Program to Print Alphabet Pattern


In C++ language you can print any Alphabet Pattern. Here i will show you how to print Alphabet Pattern in C++ language with explanation.

C++ Program to print Alphabet Pattern

#include<conio.h>
#include<iostream.h>
void main()
{
  int i,j,n;
  char c;
  clrscr();
  cout<<"Eneter the no of lines to be printed: ";
  cin>>n;
  c='A';
  for(i=0;i<n;i++)
   {
   for(j=0;j<=i;j++)
   { 
   if(c=='Z')
   break;
  cout<<c;
  c++;
   }
  cout<<endl;
  }
  getch();
}

Output

Enter the no of lines to be printed: 5
  
A
BC
DEF
GHIJ
KLMNO

Example

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
void main()
{
    int i,j,n,k;
    char c='A';
    clrscr();
    cout<<"Enter the no of lines to be printed: ";
    cin>>n;
    for(i=0;i<=n;i++)
    {
       for(j=0;j<=i;j++)
       {
    cout<<" ";
       }
       for(k=n-i-1;k>=0;k--)
       {
   cout<<c;
   c++;
       }
       cout<<endl;
    }
    getch();
} 

Output

Enter the no of lines to be printed: 5
ABCDE
 FGHI
  JKL
   MN
    O

Example

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

void main()
{
char s[]="india";
int i,j;
clrscr();
for(i=0;s[i];i++)
{
for(j=0;j<=i;j++)
cout<<s[j];
cout<<"\n";
}
getch();
}

Output

  I
  IN
  IND
  INDI
  INDIA

Example

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

void main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if(j%2==0) 
cout<<"A";
else 
cout<<"*";
}
cout<<"\n";
}
getch();
}

Output

*
*A*
*A*A*
*A*A*A*

C++ Program to Print number Pattern


In C++ language you can any number Pattern. Here i will show you how to print all number Pattern in C++ language with explanation.

C++ Program to Print number Pattern

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

void main()
{
int i,j, k=1;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<i;j++)
{
cout<<k;
k++
}
cout<<"\n";
}
getch();
}

Output

1
23
456
78910

C++ Program to Print number Pattern

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

void main()
{
  int i, j;
    for(i=1;i<=5;i++)
    {
        for(j=1;j<=i;j++)
        {
            cout<<i;
        }
        cout<<"\n";
    }

getch();
}

Output

1
2 2 
3 3 3
4 4 4 4
5 5 5 5 5

Example

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

void main()
{
  int i,j,k;
  k=1;
  for(i=1;i<=5;i+=2)
  {
    for(j=5;j>=1;j--)
    {
      if(j>i)
        cout<<" ";
      else
        cout<<k++;
    }
    cout<<"\n";
  }
getch();
}

Output

      1
   2 3 4
5 6 7 8 9