facebook

Friday, 1 February 2013

Sample programs in c



Simple c programs



1)Factorial

long Fact ( int n )
{
if ( n>0 )
return( n * Fact (n-1) ); //recursion
else
return( 1 );
}

2) GCD
int GCD( int x, int y ) {
if ( x>=y AND x%y==0 )
return( y );
else if ( x
return( GCD( y, x ) );
else
return( GCD( y, x%y ) );
}

 
C programs are the basic building blocks of knowledge, which you can gain by deeply studying the simple C program examples. Here in this C programming tutorials on bay-six we will learn by looking at some sample programs in C, which surely will help you to boost your C programming knowledge. Have a look on these sample programs in C and enjoy! Stay tuned for more

3) Decimals to words
void DecToWord( int n )
{
char *wordarr[ ] = { "Zero", "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine" };
if (n>9)
DecToWord ( n/10 );
printf( "%s ", wordarr [n%10] );
}



4) Decimal to Binary
void DecToBin( int n ) /* prints decimal in binary */
{
if (n>1)
DecToBin ( n/2 );
printf( "%d", n%2 );
}
5) Fibonacci
int Fib ( int n ) /* returns nth Fibonacci number */
{
if ( n==1 || n==2 )
return( 1 );
else
return( Fib (n-1) + Fib (n-2) ); //Recursion
}

6) Decimal to hexadecimal conversion
void DecToHex( int n ) {
char *numarr[ ] = { "0", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "A", "B", "C", "D", "E", "F" };
if (n>15)
DecToHex ( n/16 );
printf( "%s", numarr[n%16] );
}
7) Power
double Pow( double a, int n ) {
if ( n==0 )
return( 1 );
else if ( n>0 )
return( a * Pow ( a, n-1 ) );
else
return( (1/a) * Pow ( a, n+1 ) );
}

8) program to find processor type
int checkCPU( void )
{
char far *s = "\270\001pP\235\234X\313";
return!!(((int(far*)())s) ()AND(( 0x88 + (( 286 | 386 )*4))<<4 span="">
}


int main( void )
{
printf( "CPU Type is %s\n", checkCPU () ? "386" : "286" );
return(0);
}
p

12 comments:

  1. the contents are quiet interesting. I will be waiting for your next post.

    ReplyDelete
  2. thank you for sharing your great experience among us.

    ReplyDelete
  3. Great article …Thanks for your great information, the contents are quiet interesting. I will be waiting for your next post.

    ReplyDelete
  4. keep posting such good and meaningful articles.

    ReplyDelete
  5. Thanks for sharing. I hope it will be helpful to many people that are searching for this topic. Keep posting guys and keep this forum a great place to learn things.

    ReplyDelete
  6. I really like to read your blogs as they contains very important and informative content. I appreciate your efforts. Cheers.

    ReplyDelete
  7. Hi, Neat post. Therе is an issue together with yοur sіte
    in internet explorer, cοuld teѕt thіs? IE still is
    the market chief and a laгgе section of
    рeоple will pаss ονer yоuг wοnderful writing beсausе οf this pгoblem.



    My ωebрage ... http://www.householdtipsntricks.com

    ReplyDelete
  8. I will probably be back again to browse much more, many thanks for the data.

    ReplyDelete
  9. I will probably be back again to browse much more, many thanks for the data.

    ReplyDelete
  10. This is wonderful. I am not quite familiar with the internet, but I believe that what I just read is some good material. Thanks for continuing to write such wonderful articles. God bless.

    ReplyDelete
  11. I really appreciate your professional approach. please keep it up good work !!

    ReplyDelete
  12. you're saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand.

    ReplyDelete