Sunday 17 May 2015

Fibonacci number using C language

Fibonacci number using C language

The Fibonacci sequence is named after Italian mathematician Fibonacci His 1202 book Liber Abaci introduced the sequence to Western European mathematics, although the sequence had been described earlier in Indian mathematics. By modern convention, the sequence begins either with F0 = 0 or with F1 = 1. The Liber Abaci began the sequence with F1 = 1.
Fibonacci numbers are closely related to Lucas numbers in that they are a complementary pair of Lucas sequences. They are intimately connected with the golden ratio; for example, the closest rational approximations to the ratio are 2/1, 3/2, 5/3, 8/5, ... . Applications include computer algorithms such as the Fibonacci search technique and the Fibonacci heap data structure, and graphs called Fibonacci cubes used for interconnecting parallel and distributed systems. They also appear in biological settings such as branching in trees, phyllotaxis (the arrangement of leaves on a stem), the fruit sprouts of a pineapple the flowering of an artichoke, an uncurling fern and the arrangement of a pine cone's bracts.

for more detail: click here 


program:
#include <stdio.h>

// Fibonacci Series
/*
   Fibo(0)=1
   Fibo(1)=1
   Fibo(n) = Fibo(n-1)+Fibo(n-2)
*/

int Fibo(int no)
{
  int no1, no2;

  if (no==0)
    return 1;
  else
  if (no==1)
    return 1;
  else
  {
    no1 = Fibo(no-1);
    no2 = Fibo(no-2);
    return (no1+no2);
  }
}

void main()
{
  int i, number;

  clrscr();

  printf("Enter an integer : ");
    scanf("%d", &number);
  fflush(stdin);

  printf("Fibo(%d) = %d\n\n", number, Fibo(number));

  for (i=0; i<=number; i++)
    printf("Fibo(%d)\t= %10d\n", i, Fibo(i));

  getch();
}


Saturday 16 May 2015

greatest common divisor (gcd) using C language

greatest common divisor (gcd) using C Language

the Greatest Common Divisor (GCD) of two whole numbers, also called the Greatest Common Factor (GCF) and the Highest Common Factor (HCF), is the largest whole number that's a divisor (factor) of both of them. For instance, the largest number that divides into both 20 and 16 is 4. (Both 16 and 20 have larger factors, but no larger common factors -- for instance, 8 is a factor of 16, but it's not a factor of 20.) In grade school, most people are taught a "guess-and-check" method of finding the GCD. Instead, there is a simple and systematic way of doing this that always leads to the correct answer. The method is called "Euclid's algorithm." If you want to know how to truly find the Greatest Common Divisor of two integers.



for more Details: click here

program:
# include <stdio.h>

/* Definition of the Greatest Comman Divisor Function */
int GCD(int n, int m)
{
  if ((n>=m) && ((n%m)==0))
    return(m);
  else
    return GCD(m,(n%m));
}

/* main function */
void main()
{
  int no1, no2;
  int GCDresult;

  clrscr();
  printf("Program for GCD caluclation\n\n");

  printf("Input 2 integer numbers: ");
    scanf("%d%d", &no1,&no2);
  printf("\n\n");

  GCDresult = GCD(no1, no2);
  printf("no1 = %d\n", no1);
  printf("no2 = %d\n\n", no2);

  printf("Greatest Comman Diviser = %d\n", GCDresult);
  getch();
}

tower of hanoi using c language

Tower of Hanoi using c language

Tower of Hanoi

The Tower of Hanoi puzzle was invented by the French mathematician Edouard Lucas in 1883. He was inspired by a legend that tells of a Hindu temple where the puzzle was presented to young priests. At the beginning of time, the priests were given three poles and a stack of 64 gold disks, each disk a little smaller than the one beneath it. Their assignment was to transfer all 64 disks from one of the three poles to another, with two important constraints. They could only move one disk at a time, and they could never place a larger disk on top of a smaller one. The priests worked very efficiently, day and night, moving one disk every second. When they finished their work, the legend said, the temple would crumble into dust and the world would vanish.
Although the legend is interesting, you need not worry about the world ending any time soon. The number of moves required to correctly move a tower of 64 disks is 2641=18,446,744,073,709,551,615. At a rate of one move per second, that is 584,942,417,355 years! Clearly there is more to this puzzle than meets the eye.
Figure 1 shows an example of a configuration of disks in the middle of a move from the first peg to the third. Notice that, as the rules specify, the disks on each peg are stacked so that smaller disks are always on top of the larger disks. If you have not tried to solve this puzzle before, you should try it now. You do not need fancy disks and poles–a pile of books or pieces of paper will work.
image
Figure 1: An Example Arrangement of Disks for the Tower of Hanoi
How do we go about solving this problem recursively? How would you go about solving this problem at all? What is our base case? Let’s think about this problem from the bottom up. Suppose you have a tower of five disks, originally on peg one. If you already knew how to move a tower of four disks to peg two, you could then easily move the bottom disk to peg three, and then move the tower of four from peg two to peg three. But what if you do not know how to move a tower of height four? Suppose that you knew how to move a tower of height three to peg three; then it would be easy to move the fourth disk to peg two and move the three from peg three on top of it. But what if you do not know how to move a tower of three? How about moving a tower of two disks to peg two and then moving the third disk to peg three, and then moving the tower of height two on top of it? But what if you still do not know how to do this? Surely you would agree that moving a single disk to peg three is easy enough, trivial you might even say. This sounds like a base case in the making.
Here is a high-level outline of how to move a tower from the starting pole, to the goal pole, using an intermediate pole:
  1. Move a tower of height-1 to an intermediate pole, using the final pole.
  2. Move the remaining disk to the final pole.
  3. Move the tower of height-1 from the intermediate pole to the final pole using the original pole.
As long as we always obey the rule that the larger disks remain on the bottom of the stack, we can use the three steps above recursively, treating any larger disks as though they were not even there. The only thing missing from the outline above is the identification of a base case. The simplest Tower of Hanoi problem is a tower of one disk. In this case, we need move only a single disk to its final destination. A tower of one disk will be our base case. In addition, the steps outlined above move us toward the base case by reducing the height of the tower in steps 1 and 3


 for more detail: click hera
program:
 # include <stdio.h>

/* Definition of the Tower Of Hanoi generator function */
void TowerOfHanoi(char peg1, char peg2, char  peg3, int no)
{
  if (no==1)
    printf("   Move Disk from %c ==> %c\n", peg1, peg3);
  else
  {
    TowerOfHanoi(peg1, peg3, peg2, no-1);
    TowerOfHanoi(peg1, peg2, peg3, 1);
    TowerOfHanoi(peg2, peg1, peg3, no-1);
  }
}

/* main function */
int main()
{
  int noDisks;

//  clrscr();
  printf("Solving Tower of Hanoi puzzle\n\n");
  printf("Input the number of disc: ");
    scanf("%d", &noDisks);
  if (noDisks<=0)
    printf("No of disks = %d is an INVALID value\n", noDisks);
  else
  {
    printf("Moving %d disks from peg A to peg C\n\n");
    TowerOfHanoi('A', 'B', 'C', noDisks);
  }

//  getch();
}