Thursday, May 5, 2016

Math Functions



1. hypot - Calculate hypotenuses of right triangle.

#include <stdio.h>
#include <math.h>
int main()
{

     double H;
     H = hypotf(5 , 4);
     printf("Hypotenuse =%f", H);
     return 0;
 }




2. log - Calculate natural logarithm.

#include <stdio.h>
#include <math.h>
int main()
{
     int num=6;
     double log_val;
     log_val = log (num);
     printf("Log Value =%f", log_val);
     return 0;
 }




3. log10 - Calculate base 10 logarithm 


#include <stdio.h>
#include <math.h>
int main()
{
     double num=6.2;
     double log10_val;
     log10_val = log10(num);
     printf("Log base 10 Value =%f", log10_val);
     return 0;
 }




4. modf - Seperate Round Value and fractorial Part.

#include <stdio.h>
#include <math.h>
int main()
{
     double num=6.02;
     double a,b;
     a=modf(num,&b);
     printf("%f  %f\n\n",b,a);
     return 0;
 }




5. sin - Calculate sine value.

#include <stdio.h>
#include <math.h>
#define PI 3.14
int main()
{
     double angle=60;
     double result;
     result=sin(angle*(PI/180));
     printf("Sin(%lf)=%lf\n\n",angle,result);
     return 0;
 }




6. sinh - Calculates hyperbolic sine.

#include <stdio.h>
#include <math.h>

int main ()
{
  double input , result;
  input = log(4.0);
  result = sinh(input);
  printf ("The hyperbolic sine of %lf is %lf.\n", input, result );
  return 0;
}




7. sqrt - Finds square root.

#include <stdio.h>
#include <math.h>

int main ()
{
  double num=4.2, result;
  result = sqrt(num);
  printf ("The Square Root of %lf is %lf.\n",num,result);
  return 0;
}




8. tan - Calculates tangent.

#include <stdio.h>
#include <math.h>
#define PI 3.14
int main()
{
     double angle=60;
     double result;
     result=tan(angle*(PI/180));
     printf("Sin(%lf)=%lf\n\n",angle,result);
     return 0;
 }




9. tanh - Calculates hyperbolic tangent

#include <stdio.h>
#include <math.h>

int main ()
{
  double input, result;
  input = log(4.0);
  result = tanh (input);
  printf ("The hyperbolic tangent of %lf is %lf.\n", input, result );
  return 0;
}




10.atof - Convert String to Float Value.

#include<stdio.h>
#include<stdlib.h>

int main ()
{
    double a,b;
    char arr [10];
    printf ( "Enter a Number:\n" );
    gets (arr);
    a = atof (arr);
    printf ( "Printing a Float Number  %f\n" , a );
    return 0;
}




11. atoi - Converts the String to integer value.

  #include <stdlib.h>
  #include <stdio.h>

  int main()
  {
    char num1[10], num2[10];
    printf("Enter first Number:\n");
    gets(num1);
    printf("Enter second Number:\n");
    gets(num2);
    printf("The sum is: %d.", atoi(num1)+atoi(num2));
    return 0;
  }




12. atol - Converts the string into long int value.

  #include <stdlib.h>
  #include <stdio.h>

  int main(void)
  {
    char num1[10], num2[10];

    printf("Enter first Number:\n");
    gets(num1);
    printf("Enter second Number:\n");
    gets(num2);
    printf("The sum is: %ld.", atol(num1)+atol(num2));

    return 0;
  }





13. atoll - Converts string into long long integer value.

   #include <stdlib.h>
  #include <stdio.h>

  int main(void)
  {
    char num1[10], num2[10];

    printf("Enter first Number:\n");
    gets(num1);
    printf("Enter second Number:\n");
    gets(num2);
    printf("The sum is: %ld.", atoll(num1)+atoll(num2));

    return 0;
  }




14. labs - Returns Long absolute value of number.

  #include <stdlib.h>
  #include <stdio.h>

  int main(void)
  {
    long signed int a= -11 ;

    printf("a=%ld.",labs(a));

    return 0;
  }




15. llabs - Returns long long absolute value of number.

  #include <stdlib.h>
  #include <stdio.h>

  int main( ){

    char num[10];
    printf("Enter a number:\n");
    gets(num);
    return llabs(atoll(num));

}





16. ldiv - Returns the quotient and remainder for Long div structure value.

  #include <stdlib.h>
  #include <stdio.h>
 
  int main(void)
  {
    ldiv_t n;
 
    n = ldiv(17L, 3L);
 
    printf("Quotient and remainder: %ld %ld.\n", n.quot, n.rem);
 
    return 0;
  }



17. Abs - Return Absolute Value for Integer Number .

  #include <stdlib.h>
  #include <stdio.h>

  int main( ){

    int a=-10 , result;
    result=abs(a);
    printf("Absolute Value of -10 is %d\n\n ",result);
    return 0;

}




18. pow - Calculates a value raised to a power.

#include <stdio.h>
#include <math.h>
int main( )
{
    int a ;
    a = pow ( 5, 2 ) ;
    printf ( "%d", a ) ;

    return 0;
}





19. isdigit - Find whether the given character is Numeric or not.

#include<stdio.h>
#include<ctype.h>

int main()
{
    char a;
    printf("Enter Element:\n");
    scanf( "%c", &a );
    if( isdigit(a) )
        printf( "This is a Numeric character: %c\n", a );
    else
        printf( "This is NOT a Numeric character: %c\n", a );
    return 0;
}




20. isxdigit - Find whether the given character is HexaDecimal Digit or not .

#include<stdio.h>
#include<ctype.h>

int main()
{
    char a;
    printf("Enter Number to check Hexa Condition :\n");
    scanf( "%c", &a );
    if( isdigit(a) )
        printf( "This is a Hexa Decimal character: %c\n", a );
    else
        printf( "This is NOT a Hexa Decimal character: %c\n", a );
    return 0;
}




21. cos - Calculates Cosine value.

#include <stdio.h>
#include <math.h>
#define PI 3.14159265

int main ()
{
  double angle, result;
  angle = 40.0;
  result = cos (angle*PI/180);
  printf ("The cosine of %lf degrees is %lf.\n", angle , result );
  return 0;
}




22. cosh - Calculates hyperbolic cosine.

#include <stdio.h>
#include <math.h>
#define PI 3.14159265

int main ()
{
  double angle, result;
  angle = 40.0;
  result = cos (angle*PI/180);
  printf ("The cosine of %lf degrees is %lf.\n", angle , result );
  return 0;
}





23. exp - Raising the exponential e to the x  power.

#include <math.h>
#include <stdio.h>

int main(void)
{
      float x , exp_result ;
      x=4.2;
      exp_result=exp(x);
      printf("Value of exponential of %f is %f.", x , exp_result);
      return 0 ;
}





24. fabs - Finds absolute value of Float

#include <math.h>
#include <stdio.h>

int main()
{
    printf("%1.1f , %1.1f", fabs(2.0), fabs(-4.0));

    return 0;
}




25. floor - Finds smallest integer less than or equal to argument.

#include <math.h>
#include <stdio.h>

int main( )
{

    printf("Floor Value is %f", floor(6.9));
    return 0 ;

}





26. ceil - Determine largest integer greater than or equal to argument.


#include <math.h>
#include <stdio.h>

int main( )
{

    printf("Floor Value is %f", ceil(6.9));
    return 0 ;

}




27.rand - Generates sequences of Pseudonumbers.


  #include <stdlib.h>
  #include <stdio.h>

  int main( )
  {
    int i;

    for(i=0; i<10; i++){
      printf("%d \n", rand());
    }

    return 0;
  }





28. fmod - Finds floating-point remainder.


 #include <math.h>
#include <stdio.h>

int main()
{
    printf("%1.1f", fmod(11.0, 2.0));

    return 0;
}








Share this

0 Comment to "Math Functions"

Post a Comment