Sunday, 12 July 2015

Solution Of 5th Program

GUESSING  GAME

I hope this is the first guessing game u have ever made with c++


#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    char a;
    int tries=1;
do
{
    cout<<"Enetr Any alphabte from A to Z : ";
    cin>>a;
    if(a=='b'||a=='B')
    {
    cout<<"U win the game ";
    break;
    }
else
    {
    cout<<"U loss.Try again \n";
    tries++;
    }
}while(tries<=3);
getch();
}

Solution Of 4th question

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int i,j,k,num;
    cout<<"Enter Number : ";
    cin>>num;
    for(i=1;i>=num;i++)
    {
    for(j=1;j<=1;j++)
    {
    cout<<" * ";
    }
    for(k=1;k<2*(num-1);k++)
    {
    cout<<" ";
    }
                         
    for(j=1;j<=1;j++)
    {
      if(i==num&&j==num)
      {
           cout<<"";
      }
      else
      {
          cout<<" * ";
      }
      }              
    cout<<endl;
    }
    getch();
}

solution of 2nd question

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int num,i;
    cout<<"Enter The No : ";
    cin>>num;
    for(i=1;i<=10;i++)
    {
        cout<<num<<" * "<<i<<" = "<<num*i<<"\n";
    }
getch();
}

soution of 1st question

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int a=1,b=1,c=1;
    cout<<"Enter number 'a' ";
    cin>>a;
    cout<<"Enter number 'b' ";
    cin>>b;
    /* Swap the value of a to b and b to a */
    c=a;
    a=b;
    b=c;
    cout<<"After swaping a = "<<a<<"\n";
    cout<<"After swaping b = "<<b;
    getch();
}

Solution of 3rd question

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int i,j,num;
    cout<<"Enter Number : ";
    cin>>num;
    for(i=1;i<=num;i++)
    {
    for(j=1;j<=1;j++)
    {
    cout<<j<<" ";
    }
    cout<<endl;
    }
    getch();
}

Friday, 10 July 2015

Loops In C++

Loops

     These are also called as Repetitive control structure.Sometime We want a program or a part of program to repeat again and again then we use Loops.This type of program execution is called as looping.There Are three types of loops These are also called as 
  • While Loop
  • Do-While Loop
  • For Loop

While Loop:

  Structure of While loop.
while(condition)
{
      statement;
}
As from Above structure It is clear that how it work.First we shall Apply a condition.If condition is true then it repeats itself until the condition become false.Here is the example of while loop.I hope It shall Help You very much

Program 

/* 
In This program We shall find Table of the given number by the user with the help of while loop
*/
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x=1,y=1,z=1;
    cout<<"Enter A number to find table ";
    cin>>x;
    while(z<=10)
{
    cout<<x<<" * "<<y<<" = "<<x*y<<"\n";
    z++;           // Every time z increase by one
    y++;v         // Every time y increase by one
}
getch();
}

    

OuTput

DO-While Loop :

            In Do-while loop the whole program repeats itself again and again until it's condition become false..

STRUCTURE

do
{
    statement;
}while(condition);


Note : That the loop body is always executed at least once. One important difference between the while loop and the do-while loop the relative ordering of the conditional test and loop body execution. In the while loop, the loop repetition test is performed before each execution the loop body; the loop body is not executed at all if the initial test fail. In the do-while loop, the loop termination test is Performed after each execution of the loop body. hence, the loop body is always executed least once.

Program

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x=0,y=0,z=0;
do
{
    cout<<"Enter Number ";
    cin>>x;
    cout<<"Enter 2nd NUmber ";
    cin>>y;
    cout<<"The sum Of the numbers is = "<<x+y<<"\n";
    cout<<"Do U want to repeat The Program Enter '1' to repeat ";
    cin>>z;
}while(z==1);
getch();
}



OUTPUT

You can Repeat it even more than 100000000000000+ times if every time u want to repeat it..

FOR LOOP

Structure of for loop
for (initialization; decision; increment/decrement)
{
   statement(s);
}
In a for loop there are 3 values needed
  • Initialization of loop control variable
  • Testing of loop control variable
  • Update the loop control variable either by incrementing or decrementing.
Operation (i) is used to initialize the value. On the other hand, operation (ii) is used to test whether the condition is true or false. If the condition is true, the program executes the body of the loop and then the value of loop control variable is updated. Again it checks the condition and so on. If the condition is false, it gets out of the loop.

Program


#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x;
    cout<<"Enter Number ";
    cin>>x;
    for(int i=1;i<=10;i++)
    {
        cout<<x<<" * "<<i<<" = "<<x*i<<"\n";
    }
    getch();
}


OUTPUT



Jump Statements

The jump statements unconditionally transfer program control within a function.

  • goto statement
  • break statement
  • continue statement
The goto statement
goto allows to make jump to another point in the program.
goto pqr;
pqr:
 pqr is known as label. It is a user defined identifier. After the execution of goto statement, the control transfers to the line after label pqr.

PROGRAM


#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x;
    abc:
    cout<<"Enter Number ";
    cin>>x;
    goto abc;
    getch();
}

OUTPUT

IT SHALL CONTINUE TO UNLIMITED TIME


The break statement
The break statement, when executed in a switch structure, provides an immediate
exit from the switch structure. Similarly, you can use the break statement in
any of the loop. When the break statement executes in a loop, it immediately exits from the loop.
The continue statement
The continue statement is used in loops and causes a program to skip the rest of the body of the loop.
while (condition)            
{
  Statement 1; 
    If (condition)
      continue;     
    statement; 
}
The continue statement skips rest of the loop body and starts a new iteration.
The exit ( ) function
The execution of a program can be stopped at any point with exit ( ) and a status code can be informed to the calling program. The general format is exit (code) ;
where code is an integer value. The code has a value 0 for correct execution. The value of the code varies depending upon the operating system.