Saturday, 28 May 2016

Write a program that displays the following output using loop:

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

Solution:

#include<iostream.h>
#include<conio.h>
void main()
{
     int  i,j;
     char ch='A'
     for ( i= 1; i<=5; i++)
     {
          for(j=1; j<=i; j++)
            { if (j==1) ch= 'A';
            cout<<ch++; }
    cout<<endl;
    }
    getch();
}

Friday, 27 May 2016

Write a program to generate the following pyramid of digits using nested loop: 


Solution: 

#include<iostream.h> 
#include<conio.h> 
void main()
{
     int mid; 
    clrscr(); 
    for(int i=1; i<=10; i++)
    {
           cout<<" "; 
           mid =(2*i)-1;
           for(int j=1; j<=(10-i); j++)
              cout<<" "; 
           for(j=i; j<=mid; j++)
              cout<<(j%10); 
           for(j =(mid-1); j>=i; j--)
             cout<<(j%10);
          cout<<endl;
     }
      getch();
}


Write a program that displays a diamond of asterisks using loop: 

Solution: 

#include<iostream.h> 
#include<conio.h> 
void main()
{
    clrscr();
    int i, j; 
    for(i=0; i<=4; i++)
    {
         for(j=0; j<=5-i; j++)
             cout<<" "; 
         for(j=5-i; j<=5+i; j++)
              cout<<"*"; 
          cout<<endl;
     }
     for(i=2; i<= 5; i++)
     {
          for(j=0; j<=i; j++)
               cout<<" "; 
          for(j=i; j<=(10-i);j++)
              cout<<"*";
              cout<<endl;
      }
      getch();
}

Write a program that inputs the height of a triangle and display it using loop. For example if the user enters height as 5, the program should display the following. 

                        &&&&&&&&&
                           &&&&&&&
                             &&&&&
                                &&&
                                  &
Solution: 
#include <iostream.h>
#include<conio.h>
void main ()
{
    int i , n , j;
   clrscr();
   cout<<"Please Give the Value of  N as height";
   cin>>n;
   for(i = n; i >0; i++)
   {
      for(j=n-i; j>0; j--)
        cout<<" ";
      for(j = 2*i-1; j>0; j--)
       cout<<"&";
     cout<<"\n";
   }
   getch();
}

Write a program to print the following output using loop:

 BBBBBBBBB
 . BBBBBBB
 . . BBBBB
 . . . BBB
          B

Solution:

#include<iostream.h>
#include <conio.h>
void main()
{
   int i , j , k;
   for(i=1; i <=5; i++)
    {
         for(k = 1; k <=5-i; k++)
          cout<<" .";
         for(j=1; j <= 2*i-1; j++)
           cout<<"B";
         cout<<endl;
     }
     getch();
}

Wednesday, 25 May 2016

Write a program to print the following output using loop: 

  

Solution: 
                 #include<iostream.h> 
                #include<conio.h> 
                void main()
                {
                      int row, pound, star; 
                      int nrows = 6; 
                      for(row = 1; row <= nrows; row++)
                      {
                           for(pound = 1; pound <= nrow-row; pound++)
                                 cout<<"#"; 
                           for(star =1; star <= 2*row-1; star++){
                               if(star == 1 || star == 2*row-1)
                                    cout<<"*"; 
                               else 
                                     cout<<"#"; 
                          for(pound = 1; pound <=  nrow-row; pound++)
                                cout<<"#"; 
                           cout<<endl;
                      }
                }
                  getch();
              }

Write a program that uses nested loops to display the following lines.
 
                             1        2        4        6
                             2        2        4        6
                             3        2        4        6
                             4        2        4        6

Solution:

             #include<iostream.h>
             #include<conio.h>
             void main()
             {
                  clrscr();
                  for(int i = 1; i <= 4; i++)
                  {
                       cout<<i;
                       for(int j =2; j <= 6; j+= 2)
                             cout<<" "<<j;
                      cout<<endl;
                }
              getch();
            }