Search This Blog

Tuesday 28 June 2011

Silly Pieces - Part 1

Temperature Conversion from Farenheit to Celcius :
        T(in F) = (9/5) T (in C) + 32
  1. int main()
  2. {
  3.        float T_C,T_F;
  4.  
  5.        printf ("Enter temperature in celsius: ");
  6.        scanf("%f",&T_C);
  7.  
  8.        T_F=(1.8*T_C)+32;
  9.        printf("\n\nTemperature in farenheit: %.2f",f);
  10.  
  11.        return 0;
  12. }
Factorial of a Number :
        Factorial of a number, n = n! = n.(n-1).(n-2).(n-3)...3.2.1
                 This  method is iterative(looping) method. Another method called recursive method
 is shown in exponential series evaluation.
  1. int main()
  2. {
  3.         int number,fac_num;

  4.         printf ("Enter a number: ");
  5.         scanf("%d",&number);
  6.          
  7.         int i;
  8.         for( i = 1 ;i <= number ; i++ ){
  9.               fac_num*=i;
  10.         }
  11.         printf("Factorial: %d",fac_num);
  12.         return 0; 
  13. }
Number Reversal :
     If u give input = 123 , the output will be 321
        123 = 1( 100)+2(10)+3(1) 
        321 = 3( 100)+2(10)+1(1) 
  1. int  main()
  2. {
  3.        int digit,number,reversed;
  4.  
  5.        printf ("Enter a number: ");
  6.        scanf ("%d",&number);

  7.        reversed = 0;
  8.        while(number>0) {
  9.                  digit = number % 10;
  10.                  reversed = ( reversed * 10 ) + digit;
  11.                  number /= 10;
  12.        }
  13.  
  14.        printf ("The reverse of the number : %d ,reversed);
  15.        return 0;
  16. }
Evaluation of Exponential Series ( up-to 50 terms) : 
                  e^x = \sum_{n = 0}^{\infty} {x^n \over n!} = 1 + x + {x^2 \over 2!} + {x^3 \over 3!} + {x^4 \over 4!} + \cdots.
  1. //function to calculate factorial (recursive )
  2. float fac(float i)
  3.   {
  4.      if( i == 1)
  5.         return( 1 );
  6.      else
  7.         return( i * fac (i-1) );
  8.   }

  9. int main()
  10. {
  11.    float x, exp_x, numer , denom ;
  12.    float n;

  13.    printf ("\n Enter the value of x: ") ;
  14.    scanf ("%f",&x) ; 

  15.    for(n = 0 ; n <= 50 ; i++ ){
  16.         numer  = pow (x,n);
  17.         denom = fac ( n );
  18.         exp_x = exp_x+numer/denom;
  19.       }

  20.    printf ("\n e^%f=%e",x,exp_x);
  21.    return 0;
  22. }
Binary to Decimal Conversion:
           For conversion from any number system to decimal system the formula is 
                  Decimal Value = Dn( nth-Weight ) +. . . . .+ D1( 1st-Weight ) + D0(0th-Weight).
           For 4 digit binary number
                  Decimal Value = D3 ( 8 ) + D2 ( 4 ) + D1 ( 2 ) + D0 ( 1 ).

  1. int main()
  2. {
  3.       int decimal,binary;
  4.     
  5.       printf ("Enter the binary code:");
  6.       scanf ( "%d",&binary);
  7.  
  8.       int temp, i;
  9.       decimal = 0;
  10.       while(binary > 0){
  11.                 temp = binary % 10 ;
  12.                 decimal += temp * pow(2,i);
  13.                 binray /= 10;
  14.                 i++;
  15.       }
  16.  
  17.       printf("\n\nEquivalent decimal value: %d",decimal);
  18.  
  19.       return 0; 
  20. }

No comments:

Post a Comment