"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain
Showing posts with label Iteration(patterns). Show all posts
Showing posts with label Iteration(patterns). Show all posts

Friday, March 5, 2010

Write a program using nested loops to produce the following design:

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


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

void main()
{

int n;

cout<<"\n Enter height :";
cin>>n;

for(int r = 1, sp = n-1 ; r<= n ; r++, sp--)
{
for (int k = 0; k<=sp ; k++)
cout<< " " ;

for(int c = 1; c < 2*r ; c++)
{
if(c==1 || c == 2*r - 1 || r == n )
  cout << "&";
else
  cout<< " " ;
}
cout<< endl;
}
getch();
}

Thursday, March 4, 2010

Write program using nested loops to produce the following design:

& & & & &
   & & &   
       &      


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

void main()
{
int n;

clrscr();
cout<<"\n Enter height of a triangle :";
cin>>n;

for(int i = n, sp = 0; i >0 ; i --, sp++)
{
for(int k=0; k<=sp; k++)
cout<<" "<<" ";

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

cout<<"\n";
}

getch();
}

Write a program using nested loops to produce the following design:

A
A B
A B C
A B C D
A B C D E


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

void main()
{
char ch='A';

int n;
cout<< "\n Enter the height of the triangle :";
cin>> n;
for(int i=1; i< = n ; i++)
{
ch = 'A';
for(int j = 1; j< = i; j++)
{
cout<< ch<< " ";
ch++;
}
cout<< "\n";
}

getch();
}
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.