Use Math.Round

Tuesday, August 4th, 2009

 

using System;

public class MainClass {
    public static void Main() {

        Console.WriteLine(Math.Round(4.4));
        Console.WriteLine(Math.Round(4.5));
        Console.WriteLine(Math.Round(4.6));
        Console.WriteLine(Math.Round(5.5));
        Console.WriteLine(Math.Round(4.54, 1));
        Console.WriteLine(Math.Round(4.55, 1));
        Console.WriteLine(Math.Round(4.65, 1));
        Console.WriteLine(Math.Round(4.56, 1));
    }
}

 

Calculate square root: how to use sqrt

Sunday, July 26th, 2009

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

int main ()
{
  double param, result;

  param = 1024.0;

  result = sqrt (param);

  printf ("sqrt(%lf) = %lf\n", param, result );

  return 0;
}

           
       

Our own power function

Sunday, July 19th, 2009

  
#include <stdio.h>

int power(void);

int m, e;

int main(void)
{
  m = 2;
  e = 3;

  printf("%d raised to the %d power = %d", m, e, power());

  return 0;
}

int power(void)
{
  int temp, temp2;

  temp = 1;
  temp2 = e;
  for( ; temp2> 0; temp2–) 
      temp = temp * m;

  return temp;
}

           
       

Math.E

Thursday, July 16th, 2009

/*
 * Output:
 
E = 2.718281828459045

 */
public class MainClass {
  public static void main(String args[]) {
    System.out.println("E = " + Math.E);
  }
}
           
       

Max function

Saturday, July 4th, 2009

print max(2, 3)