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:-

 


Write a Program in C++ to Print first n Natural numbers and their sum using for loop.

Program using for loop:- 

Syntax of for loop:-      
                                   for(initialization; test expression; re-initialization)
                                     {
                                        body of the loop
                                   
                                            }

Initialization Expression:- It is executed only once when the loop first starts.It provide an initial value for the local variable.

Test Expression:- It is executed every time through the loop before the body of the loop is executed. Mean it tell about the end value. There we will use relational operators. If the test expression is true, the body of the loop is executed, and if false, the control comes out of the loop.

Increment/Decrement (re-initialization) Expression:-It is always at the end of the loop, after the body of the loop.

Let us take an example:-

Write a Program in C++ to Print first n Natural numbers and their sum using for loop.

                   
                    #include<iostream.h>


 void main()
 {
 int n,i,sum=0;
cout<<"enter the value of n";
cin>>n;
cout<<"\n first "<<n<<"natural numbers are";

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

{
cout<<"\n"<<i<<"  ";

 sum=sum+i;




}

cout<<"\n the sum i ="<<sum;


 }

Output:- 

  

Write a Program in C++ to Print first n natural numbers and their sum using do-while loop.

Program using do-while loop:-

                           Syntax of do-while loop:-
       

                             do
                                   {

                                  body of loop
                                     
                                } while(condition);


The following points should be remembered while using the do-while loop:

- It is executed at least once.

-It is executed till the condition remains true and the control comes out of the loop when the  condition becomes false.

Let us take an example:-

Write a Program in C++ to Print first n natural numbers and their sum using do-while loop.



                       #include<iostream.h>


 void main()
 {
 int n,i=1,sum=0;
cout<<"enter the value of n";
cin>>n;
cout<<"\n first "<<n<<"natural numbers are";


do
{
cout<<"\n"<<i<<"  ";

 sum=sum+i;

 i++;


}   while(i<=n);

cout<<"\n the sum i ="<<sum;


 }


Output:-

                      

Write a Program in C++ to Print first n Natural numbers and their sum using while loop.

Program using while Loop:-
                                               Syntax of while Loop:-
               
               while(condition)
              {

                      body of loop
               }
       
 The following points should be remembered while using while loop:-

- It may not be executed even once if the condition is False initially.
- It is executed till the condition remains true and the control comes out of the loop when the        condition becomes false.

Let us take an example:-


:-Write a Program to Print first n Natural numbers and their sum using while loop.


 #include<iostream.h>


 void main()
 {
 int n,i=1,sum=0;
      cout<<"enter the value of n";
      cin>>n;
      cout<<"\n first "<<n<<"natural numbers are";

while(i<=n)

{
cout<<"\n"<<i<<"  ";

 sum=sum+i;

 i++;


}

cout<<"\n the sum i ="<<sum;

                                            }


 Output :-

Tuesday, 7 April 2015

Write a Program in C++ to Calculate the area of the circle until radius entered is zero using While Loop.

:-Write a Program in C++ to Calculate the area of the circle until radius entered is zero using While Loop.


#include<iostream.h>
#include<conio.h>
#include<math.h>  // for pow() function

main()
{
  float radius, area;
  cout<<"\n Enter the radius(to terminate enter 0):";

  cin>>radius;
  while(radius !=0)
  {
area= 3.14*pow(radius,2);

cout<<"area is  : "<<area <<"sq. units \n";

getch();

 cout<<"\n Enter the radius(to terminate enter 0):";

 cin>>radius;

  }

}

The Output of this program :- 


Write a program in C++ for Simulation of a Simple Calculator using Switch Case.

:-Write a program in C++ for Simulation of a Simple Calculator using Switch Case.

#include<iostream.h>

void main()
{
  int a,b,o;
  float c;
  cout<<"\n enter the two operands: ";
  cin>>a>>b;

  cout<<"\n enter 1 for addition";
  cout<<"\n enter 2 for subtruction";
  cout<<"\n enter 3 for multiplication";
  cout<<"\n enter 4 for division";
  cin>>o;

  switch(o)
  {
case 1:      c=a+b;
cout<<c;
break;
case 2:      c=a-b;
 cout<<c;
break;
case 3:     c=a*b;
cout<<c;
break;
case 4:     c=a/b;
cout<<c;
break;

default :   cout<<"wrong choice";



  }

}
The Output of this Program :-



Write a program in C++ for Simulation of a Simple Calculator using Switch Case.

:- Write a program in C++ for Simulation of a Simple calculator using Switch Case .


#include<iostream.h>

void main()
{
  int a,b;
  char o;
  cout<<"\n enter the two operands: ";
  cin>>a>>b;

  cout<<"\n enter the operator (+,-,*,/)";
  cin>>o;

  switch(o)
  {
case '+': cout<<endl<<a<<'+'<<b<<"  =  "<<a+b;
break;
case '-': cout<<endl<<a<<'-'<<b<<"  =  "<<a-b;
break;
case '*': cout<<endl<<a<<'*'<<b<<"  =  "<<a*b;
break;
case '/': cout<<endl<<a<<'/'<<b<<"  =  "<<a/b;
break;
default : cout<<"\n wrong choice";


  }

}

The Output of this Program:-