Showing posts with label program. Show all posts
Showing posts with label program. Show all posts

Wednesday, February 18, 2015

C Program for Tower of Hanoi Problem

The Tower of Hanoi (also called the Tower of Brahma or Lucas Tower and sometimes pluralised) is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape as shown in below image.


C Program for Tower of Hanoi Problem


Also Read: Graphs: Introduction and Terminology
Also Read: C Program to Create a Binary Tree Using Recursion [Linked Representation]

The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
  1. Only one disk can be moved at a time.
  2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
  3. No disk may be placed on top of a smaller disk.
With three disks, the puzzle can be solved in seven moves. The minimum number of moves required to solve a Tower of Hanoi puzzle is 2n-1, where n is the number of disks.

Animated solution of the Tower of Hanoi Puzzle for T(4,3)


Tower of Hanoi Problem Animation


C Program using recursion is given below which finds solution for Tower of Hanoi Problem

#include<stdio.h>

void TOH(int,char,char,char);

void main()
{
    int n;
    printf("How many plates?");
    scanf("%d",&n);
    TOH(n,A,B,C);
}

void TOH(int n,char x,char y,char z)
{
    if(n>0)
    {
        TOH(n-1,x,z,y);
        printf("
%c -> %c",x,y);

        TOH(n-1,z,y,x);
    }
}

Output

Source: Wikipedia

6J4XXVUMND9H
Read more »

Tuesday, February 17, 2015

C program to print the following design


C++ program to print the following design:

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr(); //to clear the screen
int i,j,k,n;
  cout<<"How many lines?";
cin>>n;
  n*=2;

for(i=0;i<n;i+=2)
{
cout<<"
";

for(j=1;j<i;j+=2)
cout<<" ";
for(k=n-1;k>i;--k)
cout<<"*";
}
getch(); //to stop the screen
}
Read more »

C program to print following trianlge using character

C program to print following trianlge using character *

#include<stdio.h>
#include<conio.h>

void main()
{
int i,j,k,n;
clrscr(); //to clear the screen
printf("How many lines?");
scanf("%d",&n);

for(i=0;i<n;++i)
{
printf("
");

for(j=0;j<i;++j)
printf(" ");
for(k=n;k>i;--k)
printf("*");
}
getch(); //to stop the screen
}
Read more »

Monday, February 16, 2015

C program to convert decimal number to binary number

C program to convert decimal number to binary number

#include<stdio.h>

#include<conio.h>

void main()

{
int d,n,i,j,a[50];
clrscr();

printf("Enter a number:");

scanf("%d",&n);

if(n==0)

 printf("
The binary conversion of 0 is 0"):

else
{
printf("
The binary conversion of %d is 1",n);


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

{
d=n%2;
a[i]=d;
n=n/2;
}

for(j=i-1;j>0;--j)

printf("%d",a[j]);
} getch();
}
Read more »

C program to print the truth table for XY Z

C program to print the truth table for XY+Z

#include<stdio.h>
#include<conio.h>

void main()
{
int x,y,z;
clrscr(); //to clear the screen
printf("X Y Z XY+Z");

for(x=0;x<=1;++x)
for(y=0;y<=1;++y)
for(z=0;z<=1;++z)
{
if(x*y+z==2)
printf("

%d %d %d 1",x,y,z);

else
printf("

%d %d %d %d",x,y,z,x*y+z);

}
getch(); //to stop the screen
}
Read more »

C program to find average of list of numbers entered through keyboard


#include<stdio.h>
#include<conio.h>

void main()
{
int i,n,sum=0,e;
float avg;
clrscr(); //to clear the screen

  printf("How many elements:");
scanf("%d",&n);
printf("Enter all the elements one by one:");
for(i=0;i<n;++i)
{
scanf("%d",&e);
sum+=e;
}
avg=sum/n;
printf("
Average=%f",avg);

getch(); //to stop the screen
}
Read more »

Sunday, February 15, 2015

C Program to convert a decimal number to binary number


#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int d,n,i,j,a[50];

cout<<"Enter a number:";
cin>>n; cout<<"
The binary conversion of "<<n<<" is 1";
for(i=1;n!=1;++i)
{
d=n%2;
a[i]=d;
n=n/2;
}

for(j=i-1;j>0;--j)
cout<<a[j];

getch();

}
Read more »

C program that reads a file containing integers and appends at its end the sum of all the integers

C program that reads a file containing integers and appends at its end the sum of all the integers

#include<stdio.h>
#include<conio.h>
#include<process.h>

main()
{
int a,i,n,sum=0;
FILE *fp;
clrscr();

//Writing numbers to the file
fp=fopen("DATA","w");
if(fp==NULL)
{
printf("File could not open!!");
exit(0);
}

printf("How many numbers?");
scanf("%d",&n);
printf("Enter numbers in the file:
");


for(i=0;i<n;++i)
{
scanf("%d",&a);
putw(a,fp);
}
fclose(fp);

//Reading the file and doing sum
fp=fopen("DATA","r");
if(fp==NULL)
{
printf("File could not open!!");
exit(0);
}

while((a=getw(fp))!=EOF)
sum+=a;

fclose(fp);

//Appending sum to the file
fp=fopen("DATA","a");
if(fp==NULL)
{
printf("File could not open!!");
exit(0);
}

putw(sum,fp);
fclose(fp);

//Displaying file after append
fp=fopen("DATA","r");
if(fp==NULL)
{
printf("File could not open!!");
exit(0);
}

printf("
File after append:
");

while((a=getw(fp))!=EOF)
printf("%d ",a);

fclose(fp);
getch();
}
Read more »

C Program to print following triangle


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


code:
#include<iostream.h>
#include<conio.h>


void main()
{
clrscr();
int n,i,j;
cout<<"Ener size of Triangle(ex-4):";
cin>>n;
cout<<"
";



for(i=0;i<n;++i)
{
for(j=0;j<=i;++j)
cout<<"*";
cout<<"
";

}
getch();
}
Read more »

Friday, February 13, 2015

C Program to print a man using graphics

C++ Program to print a man using graphics


#include<graphics.h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main()
{
   int gdriver=DETECT,gmode;
   initgraph(&gdriver, &gmode, "c:\turboc3\bgi");

   //for head
   ellipse(320,95,360,0,25,20);
   line(298,85,341,85);
   circle(310,90,2);
   circle(330,90,2);
   arc(320,100,200,-20,10);

   //for neck
   line(313,115,313,125);
   line(328,115,328,125);

   //For centre part
   arc(320,225,72,107,100);
   line(290,129,290,200);
   line(350,129,350,200);
   line(290,193,350,193);
   line(290,200,350,200);


   //for legs
   line(290,200,285,280);
   line(320,225,305,280);
   line(322,225,335,280);
   line(350,200,355,280);

   //for right hand
   line(290,129,255,165);
   line(255,165,290,200);
   line(290,149,275,165);
   line(275,165,290,182);

   //for left hand
   line(350,129,385,165);
   line(385,165,350,200);
   line(350,149,365,165);
   line(365,165,350,182);

   //for shoes
   line(285,280,275,287);
   line(275,287,305,287);
   line(305,280,305,287);

   line(335,280,335,287);
   line(335,287,365,287);
   line(355,280,365,287);


   //for name
   settextstyle(2,HORIZ_DIR,4);
   outtextxy(293,150,"The Crazy");
   outtextxy(292,160,"Programmer");

   getch();
   closegraph();
}
Read more »

Java program to reverse a given number

Java program to reverse a given number

class Reverse
{
public static void main(String...s)
{
int n,rev=0,i;
n=Integer.parseInt(s[0]);

while(n!=0)
{
i=n%10;
rev=(rev*10)+i;
n/=10;
}

System.out.println("
Reverse="+rev);

}
}
Read more »

Thursday, February 12, 2015

How to Write Compile and Run Your First Java Program

Before running Java programs you need to install and setup java. For installation guide read: Java Installation: Downloading, Installing and Setting Path

How to write and run Java programs?

  • Open notepad and write below program.


class demo
{
                public static void main(String...S)
                {
                                System.out.println("Java Programming");
                }
}

First Java Program

  • Now save the file with name demo.java on desktop. Here you can replace demo with any other name that you want.
  • Then open command prompt (cmd) and type cd desktop and press enter to change current directory to desktop. We changed directory because our program is saved on desktop.

cd desktop

  • To compile java program type javac demo.java and hit enter. Type java demo to run the program. Remember that here we have to compile with program name and execute with class name that we have used in program. The java program name and class name can be different.

compile and run


Understand the Program


Understand the Program


System: Get reference id of the device on which program is running. Here the device is cmd.
out: Store the reference id of device.
println: Throw the output on device.


A.java
A.java
A.java


class A
 {

}
class A
{
public static void main()
{
}
}
compile
Yes
Yes
Yes
.class file
Not created
Created
Created
execute
No
No
Yes
Read more »

Tuesday, February 10, 2015

C program to print truth table of XY Z

C++ program to print truth table of XY+Z

#include<iostream.h>
#include<conio.h>

void main()
{
int x,y,z;
clrscr();
cout<<"X Y Z XY+Z";

for(x=0;x<=1;++x)
for(y=0;y<=1;++y)
for(z=0;z<=1;++z)
{
if(x*y+z==2)
cout<<"

"<<x<<" "<<y<<" "<<z<<" 1";
else
cout<<"

"<<x<<" "<<y<<" "<<z<<" "<<x*y+z;
}
getch();
}
Read more »