Friday, May 3, 2013

Program to Find HCF of Two Numbers Without Recursion

Program to Find HCF of Two Numbers Without Recursion

 The following C program using iteration finds the HCF of two entered integers. The HCF stands for Highest Common Factor.
Here is the source code of the C program to find the HCF of two entered integers. The C program is successfully compiled and run on a turbo cpp.

#include <stdio.h>
main()
{
int q, m, n, temp;
printf("\nEnter values of two numbers: ");
scanf("%d %d", &m, &n);
if (m == 0)
{
printf("\nHCF of number is: %d", n);
goto end;
}
if (n == 0)
{
printf("\nHCF of numbers is: %d", m);
goto end;
}
if (n > m)
{
temp = m;
m = n;
n = temp;
}

q = 1;
while (q != 0)
{
q = m % n;
if (q != 0)
{
m = n;
n = q;
}
}
printf("\nHCF of number is: %d", n);
end:
getch();
}
what is recursive programming?
Recursive programming is a powerful technique that can greatly simplify some programming tasks. In summary, recursive programming is the situation in which a procedure calls itself, passing in a modified value of the parameter(s) that was passed in to the current iteration of the procedure. Typically, a recursive programming environment contains (at least) two procedures: first, a procedure to set up the initial environment and make the initial call to the recursive procedure, and second, the recursive procedure itself that calls itself one or more times.


Cautions For Recursive Programming
While recursive programming is a powerful technique, you must be careful to structure the code so that it will terminate properly when some condition is met. In the Fact procedure, we ended the recursive calls when N was less than or equal to 1. Your recursive code must have some sort of escape logic that terminates the recursive calls. Without such escape logic, the code would loop continuously until the VBA runtime aborts the processing with an Out Of Stack Space error. Note that you cannot trap an Out Of Stack Space error with conventional error trapping. It is called an untrappable error and will terminate all VBA execution immediately. You cannot recover from an untrappable error.

Program to Find HCF of Two Numbers using Recursion

 Program to Find HCF of Two Numbers using Recursion

 The following C program using iteration finds the HCF of two entered integers. The HCF stands for Highest Common Factor.
Here is the source code of the C program to find the HCF of two entered integers. The C program is successfully compiled and run on a turbo cpp.
#include <stdio.h>
int hcf(int, int);
main()
{
int h, i, a, b;
printf("\nEnter values of two numbers: ");
scanf("%d %d", &a, &b);
h = hcf(a, b);
printf("\nHCF of numbers is: %d", h);
getch();
}
int hcf(int a, int b)
{
if (a%b == 0)
return b;
else
return hcf(b, a%b);
}

 what is recursive programming?
Recursive programming is a powerful technique that can greatly simplify some programming tasks. In summary, recursive programming is the situation in which a procedure calls itself, passing in a modified value of the parameter(s) that was passed in to the current iteration of the procedure. Typically, a recursive programming environment contains (at least) two procedures: first, a procedure to set up the initial environment and make the initial call to the recursive procedure, and second, the recursive procedure itself that calls itself one or more times.



Cautions For Recursive Programming
While recursive programming is a powerful technique, you must be careful to structure the code so that it will terminate properly when some condition is met. In the Fact procedure, we ended the recursive calls when N was less than or equal to 1. Your recursive code must have some sort of escape logic that terminates the recursive calls. Without such escape logic, the code would loop continuously until the VBA runtime aborts the processing with an Out Of Stack Space error. Note that you cannot trap an Out Of Stack Space error with conventional error trapping. It is called an untrappable error and will terminate all VBA execution immediately. You cannot recover from an untrappable error.


c Program to Find Factorial of a Number without using Recursion

 Program to Find Factorial of a Number without using Recursion


Factorial of a Number :
Factorial of any number which should be non negative is equal to Product of all the integers less than or equal to that particular number. It is denoted by n! where n is any positive number and its factorial will be equal to :
n!=n*(n-1)*(n-2)*(n-3)... and so on till integer value 1 comes.
Let us take the example of 5! . It will be equal to 5*4*3*2*1 => 120 .
0! according to number systems is treated as 1. In this program, we shall use Recursion technique to find the Factorial of any positive number. Copy the Source code and save it as *.c . Compile and run thereafter the C Program.
#include <stdio.h>
main()
{
int n, i;
long fact=1;
printf("\nEnter any number: ");
scanf("%d", &n);
for (i=1; i<=n; i++)
fact = fact*i;
printf("\nFactorial = %ld", fact);
getch();
}

Recursion is a method of solving problems based on the divide and conquer mentality. The basic idea is that you take the original problem and divide it into smaller (more easily solved) instances of itself, solve those smaller instances (usually by using the same algorithm again) and then reassemble them into the final solution.