Ads 468x60px

Labels

Featured Posts

Basic C and C++ Programming

CPP Programming

Java Programming

Oracle

DOT Net Technology

Ms Sql Server

Dot Net

Tuesday, 23 July 2013

Tower Of Hanoi C Code Using Recursion


How to Play:
To play the Towers of Hanoi game, you must move the discs from peg A to peg B by moving one disc at a time.  A larger disc can never sit atop a smaller disc.  Peg C can be used as a holding area.
In the true problem, there are a total of 64 discs.
The number of moves needed to solve the game, based upon the number of discs in the game, can be expressed recursively.
 represents the number of moves based upon the number of discs, n.

Here is the source code of the C program for solving towers of hanoi. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.

Monday, 17 September 2012

Reverse of a string using pointers

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
char *a;
clrscr();
printf("enter a string");
scanf("%s",a);
i=0;
while(*(a+i)!='\0')
i++;
for(j=i-1;j>=0;j--)
printf("%c",*(a+j));
getch();
}

C Program for Factorial of a Number using Recursive Functiol

#include<stdio.h>
#include<conio.h>
long int factorial(int n);
void main()
{
int n;
clrscr();
printf("Enter the number:\n\n");
scanf("%d",&n);
printf("Factorial of %d is %ld",n,factorial(n));
getch();
}
long int factorial(int n)
{
if(n<=1)
{
return(1);
}
else
{
n=n*factorial(n-1);
return(n);
}
}

Thursday, 13 September 2012

Switch Case control structure


SWITCH stmt is used for handling n number of decisions at a time.
 Hence it is also known as multi way decision making stmt.
SWITCH STATEMENT can't be applied to float and double.
 Break stmt transfers the control out of the switch stmt.
Syntax

switch(choice)
  {
  case 1:block of stmt:-1;
  break;
  case 2:block of stmt:-2;
  break;
  …………………….
  ……………………..
  ………………………
  case n:    block of stmt:-n;
  break;
  default: invalid stmt;
  break;
  }


sample program 

Logical operators workshop


Logical && and logical || 
logical && : If all are true then only true otherwise false.
logical || : if any one is true then it is true otherwise false.
Program on logical operators 
 A company insures its drivers in the following cases:
− If the driver is married.
− If the driver is unmarried, male & above 30 years of age.
− If the driver is unmarried, female & above 25 years of age.
In all other cases the driver is not insured. If the marital status, sex and age of the driver are

Else-if Control Structure


Else-if:
      A Multipath decision is a chain of ifs in which the statement associated with each else is an if.
Syntax:
  if(condition-1)
  stmt-1;
     else if(condition-2)
         stmt-2;
  else if(condition-3)
     stmt-3;
      ………………….
      else if(condition-n)
        stmt-n;
  else
  defalut stmt;
stmt-x;

Sample Program :

If else control structure


C  uses the keyword if-else to implement the decision control instruction.
Syntax
if(test condition-1)
{
             if(test condition-2)
  {
                block of stmt-1;
  }
  else
  {
                  block of stmt-2;
               }
}
  else
  {
     block of stmt-3;
  }
Stmt-x;

Note: braces are mandatory when, corresponding to if or else statement, multiple steps are to be executed.
Sample program