Wednesday, 29 April 2015

Write a Program in C++ to generate a patterns using for loop.

Program using for loop:-


1-Write a Program in C++ to generate the following pattern using for loop.

Pattern is-

1
12
123
1234
12345
123456
1234567
12345678

Coding:-

#include<iostream.h>

main()
 {
 int i, j,n;

 cout<<"enter the value of n ";

 cin>>n;

 for(i=1;i<=n;i++)

 {
 for(j=1;j<=i;j++)

 cout<<j;

 cout<<"\n";

 }

 }

Output :-


2-Write a Program in C++ to generate the following pattern using for loop.

Pattern is:-

1
22
333
4444
55555
666666

Coding:-

#include<iostream.h>

                 main()
 {
 int i, j,n;

 cout<<"enter the value of n ";

 cin>>n;

 for(i=1;i<=n;i++)

 {
 for(j=1;j<=i;j++)

 cout<<i;

 cout<<"\n";

 }

 }

Output:-

 


3-Write a Program in C++ to generate the following pattern using for loop.

Pattern is:- 
           
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Coding:-

 include<iostream.h>
main()
{
 int i, j,z=0 ;

 for(i=1;i<=5;i++)
   {

   for(j=1;j<=i;j++)
    {
     z++    ;

   cout<<" "<<z<<" " ;
     }
   cout<<"\n";


   }        }
Output:- 

 

4-Write a Program in C++ to Print table from 1 to 9 using for loop.

  Coding:-
              

  #include<iostream.h>
main()
{
 int i, j,z ;
 for(i=1;i<=9;i++)
   {
   for(j=1;j<=10;j++)
    {
     z=i*j;
   cout<<" "<<z;
     }
   cout<<"\n";

   }

Output:- 







5-Write a Program in C++ to generate a simple star pattern using for loop.

Pattern is:-

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

Coding:-

#include<iostream.h>

main()
 {
 int i, j,k,n;

 cout<<"enter the value of n ";

 cin>>n;

 for(i=1;i<=n;i++)

 {

 for(j=1;j<=n-i;j++)

 cout<<"  ";

 for(k=1;k<=i;k++)

 cout<<" *  ";

 cout<<"\n";

 }


 }


Output:-

                        



6:-Write a Program in C++ to generate a Diamond pattern using for loop.

Pattern is :-

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

Coding:- 

#include<iostream.h>

                    main()
 {

                   int i, j,k,n;



 for(i=1;i<=5;i++)

 {

 for(j=1;j<=5-i;j++)

 cout<<"  ";
 for(k=1;k<=i;k++)

 cout<<" *  ";

 cout<<"\n";

 }

 for(i=5-1;i>=1;i--)
{

 for(j=1;j<=5-i;j++)

 cout<<"  ";
 for(k=1;k<=i;k++)

 cout<<" *  ";

 cout<<"\n";

 }

 }


Output:-

 


No comments:

Post a Comment