Friday, July 22, 2011

Cast Operator in C

In most cases cast operator use to convert values form one variable type to another variable type.As an example if you want to find ASCII value of a character in C, you can use the cast operator to find it.. Below example will demonstrate the simple usage of Cast operator

/**************************************/
/*                                    */
/* Operators Demo # 3 - Cast operator */
/*                                    */
/**************************************/

#include <stdio.h>

main()
{
      int i;
      char ch;
      
      printf("---------------------------------------------------------------\n");
      printf("This program going to convert Charctor value to ASCI value\n\n");
      printf("-----------------------------------------------------------------\n");
      
      printf("Enter your Charctor:\n");
      scanf("%c",&ch);
      
      printf("Program detected your Letter as %c: ",ch);
      printf("\n Ok... Now see the ASCII value");
      
      i=(int) ch; /* This the cast operator*/
      printf("ASCII value is %d:\n",i);
      
      system("pause");
      
      
      }


  


Loading

Thursday, July 21, 2011

Find minimum and maximum value of the array


This program help you to find maxim and minimum value of the array in java

public class MinMax {

 /**
  * author  :  damith.uwu@gmail.com
  * date  : 2011-07-23
  * 
  * This program will guide you to find the minimum and maximum number
  * of an array in java
  * 
  */
 
  
 public static void main(String[] args) {
  
  MinMax myObj= new MinMax(); // create a new object from minmax class
  myObj.findMin(); // calling the methods
  myObj.findMax();
  
  
 }
 
 public void findMin() // method for find min value of the array
 {
  int [] myArray = new int[5]; // declare an array
  
  myArray[0] = 30; // initialize the array
  myArray[1] = 4;
  myArray[2] = 67;
  myArray[3] = 12;
  myArray[4] = 8;
  
  
  int min = myArray[0]; // declare variable for min value and initialize it using array
  
  for(int i=0;i<myArray.length;i++) // count until end of array
  {
   if(myArray[i]<min)
   {
    min = myArray[i]; // assign new value to min variable
   }
  }
  
  System.out.println("Minimum value of the array is :" + min ); // print it
 }
 
 public void findMax() // method for find max value of the array
 {
  int [] myArray = new int[5];
  
  myArray[0] = 30;
  myArray[1] = 4;
  myArray[2] = 67;
  myArray[3] = 12;
  myArray[4] = 8;
  
  
  int max = myArray[0];
  
  for(int i=0;i<myArray.length;i++)
  {
   if(myArray[i]>max)
   {
    max = myArray[i];
   }
  }
  
  System.out.println("Maximum value of the array is :" + max );
 }

}


Loading