Popular Logics for implementation to check basic skills of fresher

Factorial
long Factorial( int n ) {
if ( n>0 )
return( n * Factorial(n-1) );
else
return( 1 );
}
Fibonacci
int Fibonacci( int n ) {
if ( n==1 || n==2 )
return( 1 );
else
return( Fibonacci(n-1) + Fibonacci(n-2) );
}
GCD
int GCD( int a, int b )
{
if ( a>=b && a%b==0 )
return( b );
else if ( a<b )
return( GCD( b, a ) );
else
return( GCD( b, a%b ) );
}
Power
double Power( double x, int n ) {
if ( n==0 )
return( 1 );
else if ( n>0 )
return( x * Power( x, n-1 ) );
else
return( (1/x) * Power( x, n+1 ) );
}
Reverse Printing
void ReverseChar( void ) {
char ch;
if ( (ch=getchar( ))!='n' )
ReverseChar( );
putchar( ch );
}
Decimal to binary conversion
void ToBin( int n ) /*
{
if (n>1)
ToBin( n/2 );
printf( "%d", n%2 );
}
Decimal to hexadecimal conversion
void ToHex( int n )
{
char *htab[ ] = { "0", "1", "2", "3", "4", "5", "6", "7", "8","9", "A", "B", "C", "D", "E", "F" };
if (n>15)
ToHex( n/16 );
printf( "%s", htab[n%16] );
}
Printing a decimal in words
void InWord( int n ) {
char *wtab[ ] = { "Zero", "One", "Two", "Three", "Four","Five", "Six", "Seven", "Eight", "Nine" };
if (n>9)
InWord( n/10 );
printf( "%s ", wtab[n%10] );
}

No comments:

Popular Posts