C++: Learning the Basics – Right Alignment

Situation 1: Pattern shown below but the text is aligned to the right.

Screen Shot 2017-09-25 at 7.20.37 AM.png

#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     int sym, rows, space=5;
     char alpha;

     for (rows=5, row=1; rows>=1; rows--, row++)
     {
         alpha='A';
         for(space=0; space<rows-1; space++)
         {
              cout<<"\t";
         }
         for (sym=1; sym<=rows; sym++)
         {
               if(rows%2==0)
               {
                    cout<<"\t"<<sym;
               }
               else
               {
                    cout<<"\t"<<alpha;
                    alpha++;
               }
         }
         cout<<"\n";
     }
     getch();
}

Situation 1: Pattern shown below but the text is aligned to the right.

Screen Shot 2017-09-25 at 7.21.08 AM.png

#include<iostream.h>
#include<conio.h>
void main() 
{ 
    clrscr(); 
    int sym, rows, space=5; 
    char alpha; 

    for (rows=1, row=5; rows<=5; rows++, row--) 
    { 
         alpha='A'; 

         for(space=0; space<rows-1; space++) 
         { 
                  cout<<"\t"; 
         } 
         
         for (sym=1; sym<=rows; sym++) 
         { 
                  if(rows%2==0)  
                  { 
                        cout<<"\t"<<sym; 
                  } 
                  else 
                  { 
                        cout<<"\t"<<alpha; alpha++; 
                  } 
          } 
          cout<<"\n"; 
      } 
      getch(); 
}
Advertisements

3 thoughts on “C++: Learning the Basics – Right Alignment

      1. Great! Maybe when you are at Amazon you would also like my sci fi e book ” Fear is in the air”, about Ewa Lowe, flight attendant, serial killer with an alien DNA…

        Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s