First 4 Programs Algorithms

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

Algorithms

Program 1. To find the greatest common divisor (GCD) for two numbers by using the
recursive function.

Step 1 .Define the recursive function.


Step 2 . Read the two integers a and b.
Step 3 . if i>j
step 4. then return the function with parameters i,j
step 5. if i==0
step 6. then return j
step 7. else return the function with parameters (i,j%i)
step 8. stop

Program 2. Binomial coeificient algorithm

Step 1. Define Recurssive function


Step 2. Read n and k
Step3. if k==0 or k==n
step 4. then return the value 1
step 5. else return the function with parameters [ ceff(n-1, k-1)+ceff(n-1, k)]
step 6. stop

Program 3. Fibonacci series Algorithm

Step 1. Define Recurssive function


Step 2. Read n is number of terms
step 3. for loop <--- 1 to n
Step 3. if n==0
step 4. then return 0
step 5. if n==1
step 6. then return 1
step 7. else return function with parameters (fibonacci(n-1) + fibonacci(n-2))
step 9. display febonacci series.
step 10. stop

Program 4. Tower of hanoi algotithm

Step 1. Start
step2. take n as number of disk
step 3. call the function of hanoi(n from, via to)
step 4. in n==1
step 5. move disk from source to destination
step 6. else hanoi(n-1,from,to,via)
step 7. move disk from source to destination
step 8. hanoi(n-1, via, from, to)
step 8. return to main
step 9. stop

You might also like