Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it
Showing posts with label Pyramids. Show all posts
Showing posts with label Pyramids. Show all posts

30 Jan 2014

LOWER TRIANGULAR MATRIX

Write a program to print lower triangular matrix in c++

#include<iostream.h>
#include<conio.h>
void main()
{   clrscr();
 int a[10][10],b[10][10],c[10][10];
 int i, j, n, m;
 int p,q;
 cout<<"enter no of rows of matrix\n";
 cin>>m;
 cout<<"enter no of colomns of matrix\n";
 cin>>n;
 cout<<"\n\nenter the elements of matrix\n";
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"a["<<i+1<<"]["<<j+1<<"]:= ";
      cin>>a[i][j];
     }
   }
  cout<<"\n\nmatrix u have entered is"<<endl;
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"\t"<<a[i][j];
     }
    cout<<endl;
   }
 cout<<"Lower tringular matrix is"<<endl;
  for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
    {
     if(j<=i)
     {
      cout<<"\t"<<a[i][j];
     }
     else
     {
      cout<<"\t ";
     }
    }
    cout<<endl;
  }


getch();
}

2 Apr 2013

Print Prime Number Pyramid

Q) Write a program to print the PRIME NUMBER PYRAMID.


Sol)

#include<stdio.h>
#include<conio.h>

int prime(int num) ;
void main()
{
     clrscr();
     int i,j ;
     int number=2;
     printf("c-programcodes.blogspot.in")
     for(i=0;i<5;i++)
        {
              printf("\n");
              for(j=0;j<=i;j++)
                 {
                      while(!prime(number)) ;
                         {
                             number++;
                          }
                       printf("%d\t", number++)}
          }
      getch();
}

int prime(int num)
{
        int i, flag ;
        for(i=2;i<num;i++)
           {
                if(num%i!=0)
                      flag=1;
                else
                  {
                       flag=0;
                       break ;
                   }
              }
         if(flag==1 || num==2)
                return(1) ;
         else
                return(0);
 }

                             ===OUTPUT===
c-programcodes.blogspot.in
2

3     5

7     11    13

17   19    23    29

31    37    41    47

STAR PATTERN3

Q)Write a Program to Print The Following Star Pattern in c++
    *
   * *
  * * *
 * * * *
* * * * *

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

 void main()
{
    clrscr();
    int i,j,n,k;
    cout<<"Enter number of rows"<<endl;
    cin>>n;
    
    for(i=1;i<=n;i++)
          {
              for(j=n-1;j>=i;j--)
                {
                    cout<<" ";
                 }
             for(k=1;k<=i;k++)
                {
                   cout<<"*";
                }
            cout<<endl;
        }
    getch();
}

                                                    ===OUTPUT===
Enter number of rows
5
    *
   * *
  * * *
 * * * *
* * * * *

28 Mar 2013

STAR PATTERN5

Q) Write a Program to Print The Following Star Pattern in c++
     *
   ***
  *****
 *******
*********

Sol)


#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int i,j,n,k;
 cout<<"Enter number of rows"<<endl;
 cin>>n;
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n-i;j++)
  {
   cout<<" ";
  }
   for(k=1;k<=2*i-1;k++)
    {
     cout<<"*";
    }
    cout<<endl;
 }
 getch();
}

                                               ===OUTPUT===

STAR PATTERN1

Q) Write a Program to Print The Following Star Pattern in c++
      *
      **
      ***
      ****
      *****

Sol)

#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int i,j,n;

   cout<<"Enter number of rows"<<endl;
  cin>>n;
 
  for(i=1;i<=n;i++)
     {
         for(j=1;j<=i;j++)
           {
               cout<<"*";
           }
        cout<<endl;
     }
 getch();
}

                                     ===OUTPUT===

10 Mar 2013

STAR PATTERN4

Q.) Write a Program to Print The Following Star Pattern in c++
          *****
          ****
          ***
          **
          *

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

void main()
{

    clrscr();
    int i,j,n;
    cout<<"Enter number of rows"<<endl;
    cin>>n;
    for(i=n;i>=1;i--)
     {
         for(j=1;j<=i;j++)
             {
                  cout<<"*";
             }
        cout<<endl;
     }

   getch();
}

                                              =OUTPUT=

Enter number of rows 5
*****
****
***
**
*

1 Feb 2013

PASCAL TRIANGLE USING C PROGRAM

Q.) Write a C program to print PASCAL Triangle without using array
                                                or
      Write a C program to print PASCAL Triangle using for loop.

Sol )

#include<stdio.h>
#include<conio.h>

long fact(int) ;
int main()
{
      int line,i,j ;
      printf("Enter the number of lines : ") ;
      scanf("%d",&line) ;
   
      for(i=0;i<line;i++)
          {
               for(j=0;j<line-i-1;j++)
                         printf("  ");

               for(j=0;j<=i;j++)
                         printf("%ld ",fact(i)/(fact(j))*fact(i-j))) ;
               printf("\n") ;
           }
       return 0;
}

long fact(int num)
{
          long f=1;
          int i=1;
          while(i<=num)
             {
                   f=f*i ;
                   i++ ;
              }
          return f;
}

                                :: SAMPLE OUTPUT ::
Enter the number of lines : 6
                           1
                       1       1
                     1    2    1
                  1    3    3   1
                1   4     6    4   1
              1  5  10    10   5  1

To print the FLOYD's TRIANGLE

Q.) Write a C-program to print Floyd's Triangle .
                               or
        How to print Floyd's Triangle in C.

Sol)
#include<stdio.h>
#include<conio.h>

int main()
{
   int i,j,r,k=1 ;
   
   printf("Enter the range : ") ;
   scanf("%d",&r) ;
   
   printf("FLOYD's TRIANGLE \n\n") ;
   for(i=1;i<=r;i++)
        {
            for(j=1;j<=i;j++,k++)
                       printf("%d",k) ;
             printf("\n") ;
         }
    return 0 ;
}  

18 Jan 2013

Number Pyramid 7

Q) Write a program to generate a following number structure:

54321
54321
54321
54321
54321

Sol )

#include<stdio.h>
#include<conio.h>
int main( )
{
 int num, r, c ;

printf("Enter the loop repeat number(rows):: ") ;

scanf("%d",&num);
for(r=1; num>=r; r++)
    {
          for(c=5; c>=1; c--)
                 printf("%d",c) ;
          printf("\n") ;
     }

return 0 ;

}

Number Pyramid 6

Q.) Write a program to generate a following number structure :

12345
12345
12345
12345
12345

Sol)

#include<stdio.h>
#include<conio.h>
int main( )
{

int num, r, c ;
printf("Enter loop repeat number(rows)::") ;

scanf("%d",&num) ;
for(r=1; num>=r; r++)
  {
    for(c=1; c<=num; c++)
        printf("%d",c) ;
    printf("\n") ;
  }

return 0 ;

}

17 Jan 2013

Number Pyramid 5

Q.) Write a program to generate a following triangle :-


    #
    ##
    # # #
    # # # # 
    # # # # #

Sol)

#include<stdio.h>
#include<conio.h> 

int main( )

{
int num, r, c;

printf("Enter loop repeat number(rows): " );

scanf("%d", &num) ;

for (r=1; num>=r; r++)
 { 
      for(c=1; c<=r; c++)
               printf("#") ;
      printf("\n") ;    
}

return 0 ;

}

15 Jan 2013

Number pyramid 4

Q) Write a PROGRAM to generate the following triangle :-

                                            # # # # #
                                               # # # #
                                                  # # #
                                                     # #
                                                        #

Sol)
 #include<stdio.h>
#include<conio.h>

int main( )

{
int num, r=1, c, sp ;

printf(" Enter loop repeat number(rows): ") ;
scanf("%d",&num);

for( ; num>=1;num--,r++)
{
   for(sp=r; sp>1; sp--)
        printf(" ");
   for(c=1; c<=num; c++)
        printf("#") ;
   printf("\n");
}

return 0;
}

Number pyramid 3

Q) Write a program to generate a following structure:

@@@@@
@@@@@
@@@@@
@@@@@
@@@@@

Sol)

#include<stdio.h>
#include<conio.h>

int main( )
{

int num, r, c;

printf(" Enter loop repeat number(rows): ");

scanf("%d", &num);

for(r=1; num>=r; r++)
{
    for(c=1; c<=num; c++)

          printf("@") ;

printf("\n") ;

}
return 0;
}


Number pyramid 2


Q) Write a C program to print the following :-

1
2 3
4 5 6
7 8 9 1
2 3 4 5 6

Sol)

#include<stdio.h>


void pyramid(int );    //prototype

int main( )
{
int num;

printf(" Enter the number of rows : ") ;

pyramid(num) ;     // Call

return 0;
}
void pyramid(int n  )    //definition
{
int r,c,x=1,y=1;
for(r=1;r<=n; r++)
   {   
     for(c=1; c<=r; c++,x++)
      {
       if(x<=9)
           printf("%d", x);
      else
         {
           printf("%d", y);
           y++;
         }
      }
printf("\n");

   }
}

Number pyramid 1

 Q.) Write a program to print the following number pyramid:

1
123
12345
1234567      

Sol)

#include<stdio.h>

int main()
int num, r, c, p=1;

printf("Enter the number of rows: ");

scanf("%d", &num) ;

for(r=1; r<=num; r++,p=p+2)
{
   for(c=1; c<=p; c++)
      {
        printf("%d",c) ;
       }
   printf("\n") ;
}

return 0 ;

}