Wednesday, 18 December 2013

TWO NUMBERS INTERCHANGE WITHOUT USING THE THIRD VARIABLE IN C++

#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int a,b;
  cout<<"enter  the value of number 1 =”;
  cin>>a;
  cout<<"enter  the value of number 2";
  cin>>b;
  a=a+b;
  b=a-b;
  a=a-b;
  /*THIS PROGRAM ACCEPT TWO NUMBERS AS INPUT AND GENERATE TWO NUMBER IN OUTPUT BY     SWAPPING THEIR VALUES*/
  cout<<"\n"<<"after interchanging value of"<<" number 1 ="<<a<<"\n"<<" number  2="<<b;
  getch();

}

C++ PROGRAM TO FIND THE G.C.D OF TWO NUMBERS USING NON-RECURSIVE FUNCTIONS

/* C++ PROGRAM TO FIND THE G.C.D OF TWO NUMBERS USING NON-RECURSIVE FUNCTIONS */
#include<iostream.h>
#include<conio.h>
int gcd(int m,int n);
void main()
{
    int m,n;
    clrscr();
    cout<<"Enter m,n values :";
    cin>>m>>n;
    cout<<"The G.C.D of given two numbers :"<<gcd(m,n);
    getch();
}
int gcd(int m,int n)
{
    if(m==n)
        return m;
    else
    {
        int gcd;
        while(n!=0)
        {
            int d=m%n;
            if(d==0)
                gcd=n;
            m=n;
            n=d;
        }
        return gcd;
    }
}

/*   OUTPUT
     ------
     Enter m,n values :6 8
     The G.C.D of given two numbers :2
*/

C++ PROGRAM TO CONVERT THE ROMAN NUMERAL INTO AN ARABIC INTEGER

/* A C++ PROGRAM TO CONVERT THE ROMAN NUMERAL INTO AN ARABIC INTEGER  */
#include<iostream.h>
#include<conio.h>
void main()
{
char s[10];
int sum=0,i=0;
clrscr();
cout<<"Enter roman numeral :";
cin>>s;
while(s[i]!='\0')
{
switch(s[i])
{
case 'm':sum+=1000;
break;
case 'd':if(s[i-1]=='c')
sum+=400;
else if(s[i-1]=='l')
   sum+=450;
else
sum+=500;
break;
case 'c':if(s[i+1]!='d')
{
if(s[i-1]=='l')
sum+=50;
else
sum+=100;
}
break;
case 'l':if(s[i+1]!='c')
sum+=50;
break;
case 'x':if(s[i-1]=='i')
sum+=9;
else
sum+=10;
break;
case 'v':if(s[i-1]=='i')
sum+=4;
else
sum+=5;
break;
case 'i':if(s[i+1]!='x' || s[i+1]!='v')
sum+=1;
break;
default :cout<<"Invalid Roman numeral:";
}
i++;
}
cout<<"The equivalent Arabic integer is :"<<sum;
getch();
}

/* OUTPUT
------
Enter roman numeral :i
The equivalent Arabic integer is :1
*/

Tuesday, 17 December 2013

Convert Temperature Celsius to Fahrenheit Using c++

#include<iostream.h>

#include<conio.h>

void main()

{

  clrscr();

  int choice;

  float temp,conv;

  cout<<"celsius to fahrenheit"<<"\n";

  cout<<"enter temp. in celsius";

  cin>>temp;

  conv=(1.8*temp)+32;

  cout<<conv;

  getch();

}

link : Convert Temperature fahrenheit to celsius

Convert Temperature Fahrenheit to Celsius Using c++

#include<iostream.h>

#include<conio.h>

void main()

{

  clrscr();

  int choice;

  float temp,conv;

  cout<<"fahrenheit to celsius"<<"\n";

  cout<<"enter temp. in fahrenheit";

  cin>>temp;

  conv=(temp-32)/1.8;

  cout<<"temp in celsius";

  cout<<conv;

  getch();

}

Link : Convert Temperature Celsius to Fahrenheit Using c++

Sunday, 15 December 2013

Convert Temperature using c++ programming

CONVERT TEMPERATURE
Fahrenheit to the Celsius or Celsius to Fahrenheit


#include<iostream.h>

#include<conio.h>

void main()

{

  clrscr();

  int choice;
 
  float temp,conv;

  cout<<"temperature conversion menu"<<"\n";

  cout<<"1.fahrenheit to celsius"<<"\n";

  cout<<"2.celsius to fahrenheit"<<"\n";

  cout<<"enter ur choice(1-2)";

  cin>>choice;

  if(choice==1)

    {

    cout<<"enter temp. in fahrenheit";

    cin>>temp;

    conv=(temp-32)/1.8;

    cout<<"temp in celsius";

    cout<<conv;

    }

  else

    {

    cout<<"enter temp. in celsius";

    cin>>temp;

    conv=(1.8*temp)+32;

    cout<<conv;
   
    }

  getch();

}

Sunday, 17 November 2013

Tower of Hanoi using c++

Tower of Hanoi Using c ++

#include<iostream.h>

#include<conio.h> 

void towers(int,char,char,char); 

void main()

{

int n; //Declare the variables to be used

clrscr();//Get the input for number of disks

cout<<"enter the no of disks : ";

cin>>n;towers(n,'A','C','B'); //Call the function

getch();

void towers(int n,char from,char to,char aux)

{

if(n==1) // If there is only one disk
    {

cout<<endl<<"move 1 from peg "<<from<<" to "<<to;return;

   }

towers(n-1,from,aux,to); //Recursive Call


cout<<endl<<"move "<<n<<" from peg "<<from<<" to "<<to;

towers(n-1,aux,to,from);

}

Information 

->Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in 1883.

->The game starts by having few discs stacked in increasing order of size. The number of discs can vary, but there are only three pegs.

->The Objective is to transfer the entire tower to one of the other pegs. However you can only move one disk at a time and you can never stack a larger disk onto a smaller disk. Try to solve it in fewest possible moves.

Solution of tower of Hanoi :-

-To get a better understanding for the general algorithm used to solve the Tower of Hanoi, try to solve the puzzle with a small amount of disks, 3 or 4, and once you master that , you can solve The same puzzle with more discs with the following algorithm.Recursive Solution for the Tower of Hanoi with algorithm Let’s call the three peg Src(Source), Aux(Auxiliary) and  st(Destination). 

Move the top N – 1 disks from the Source to Auxiliary tower

Move the Nth disk from Source to Destination tower

Move the N – 1 disks from Auxiliary tower to Destination tower. 

Transferring the top N – 1 disks from Source to Auxiliary tower can again be thought of as a fresh problem and can be solved in the same manner.

So once you master solving Tower of Hanoi with three disks, you can solve it with any number of disks with the above algorithm.

The puzzle is well known to students of Computer Science since it appears in virtually any introductory text on data structures or algorithms.         

A function solve with four arguments (number of disks) and three pegs (source, intermediary and destination) could look like this.

Algorithms
Solve (N, Src, Aux, Dst)
 If N is 0
Exit
Else solve (N-1, Src, Dst, Aux)
Move from Src to Dst
Solve(N -1 , Aux, Src, Dst)