Pages

Friday 6 September 2013

BINARY SEARCH



#include<stdio.h>
void main()
 {
  int a[100];
  int i,j,n,temp,s,top,bottom,mid;
  printf("\n Enter the Limit  :  ");
  scanf("%d",&n);
  printf("\n Enter the %d Elemets......\n",n);
  for(i=0;i<n;i++)
   scanf("%d",&a[i]);
  printf("\n Enter the Searching Element  :  ");
  scanf("%d",&s);
 for(i=0;i<n-1;i++)
  {
   for(j=i+1;j<n;j++)
    {
     if(a[i]>a[j])
      {
       temp=a[i];
       a[i]=a[j];
       a[j]=temp;
      }
    }
  }
 if(n%2==0)
 {
 top=n+1;
 bottom=0;
 }
 else
  {
    top=n;
    bottom=0;
 }

 do
  {
   mid=(bottom+top);
   if(s<a[mid])
    {
     top=mid-1;
    }
   else
    {
     bottom=mid+1;
    }
  }while((s!=a[mid])&&(top>=bottom));
 if(s==a[mid])
   printf("\n Se Searching element is found  \n");
 else
   printf("\n Searching Element is Not found  \n");
 }


Thursday 5 September 2013

TEMPERATURE CONVERSION



import java.awt.*;
import java.awt.event.*;
import jpb.*;

// Driver class
public class ConvertTemp {
  public static void main(String[] args) {
    Frame frame = 
      new ConvertTempFrame("Temperature Conversion");
    frame.setSize(150, 75);
    frame.setVisible(true);
  }
}

// Frame class
class ConvertTempFrame extends Frame {
  private TextField fahrenField = new TextField();
  private TextField celsiusField = new TextField();

  // Constructor
  public ConvertTempFrame(String title) {
    // Set title for frame and choose layout
    super(title);
    setLayout(new GridLayout(2, 2));

    // Add Fahrenheit label and text field to frame; attach
    // listener to text field
    add(new Label("Fahrenheit"));
    add(fahrenField);
    fahrenField.addActionListener(new FahrenheitListener());

    // Add Celsius label and text field to frame; attach
    // listener to text field
    add(new Label("Celsius"));
    add(celsiusField);
    celsiusField.addActionListener(new CelsiusListener());

    // Attach window listener
    addWindowListener(new WindowCloser());
  }

  // Listener for fahrenField
  class FahrenheitListener implements ActionListener {
    public void actionPerformed(ActionEvent evt) {
      String fahrenheitString = fahrenField.getText();
      double fahrenheit = Convert.toDouble(fahrenheitString);
      double celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
      celsius = Math.rint(celsius * 100.0) / 100.0;
      celsiusField.setText(celsius + "");
    }
  }

  // Listener for celsiusField
  class CelsiusListener implements ActionListener {
    public void actionPerformed(ActionEvent evt) {
      String celsiusString = celsiusField.getText();
      double celsius = Convert.toDouble(celsiusString);
      double fahrenheit = celsius * 9.0 / 5.0 + 32.0;
      fahrenheit = Math.rint(fahrenheit * 100.0) / 100.0;
      fahrenField.setText(fahrenheit + "");
    }
  }

  // Listener for window
  class WindowCloser extends WindowAdapter {
    public void windowClosing(WindowEvent evt) {
      System.exit(0);
    }
  }
}


AVERAGE OF A SERIES



// Computes the average of a series of scores


import jpb.*;


public class AverageScores {

  public static void main(String[] args) {
    // Prompt user to enter number of scores
    SimpleIO.prompt("Enter number of scores: ");
    String userInput = SimpleIO.readLine().trim();
    int numberOfScores = Integer.parseInt(userInput);
    System.out.println();

    // Create array to hold scores

    int[] scores = new int[numberOfScores];

    // Prompt user to enter scores and store them in an array

    int i = 0;
    while (i < scores.length) {
      SimpleIO.prompt("Enter score #" + (i + 1) + ": ");
      userInput = SimpleIO.readLine().trim();
      scores[i] = Integer.parseInt(userInput);
      i++;
    }

    // Compute sum of scores

    int sum = 0;
    i = 0;
    while (i < scores.length) {
      sum += scores[i];
      i++;
    }

    // Display average score

    System.out.println("\nAverage score: " +
                       sum / scores.length);
  }
}


Thursday 25 July 2013

SEQUENTIAL SEARCH



#include<stdio.h>

void main()
 {
  int a[100];
  int i,n,num,flag=0;
  printf("\n Enter the Limit :  ");
  scanf("%d",&n);
  printf("\n Enter the %d Elements.......\n",n);
  for(i=0;i<n;i++)
   scanf("%d",&a[i]);
  printf("\nEnter the Number which you want to search :  ");
  scanf("%d",&num);
  for(i=0;i<n;i++)
   {
    if(a[i]==num)
     {
      flag=1;
      break;
     }
   }
  if(flag==0)
   printf("\n The searching Element %d is not found in the Array   \n",num);
  else
    printf("\nThe Searching Element is in %d th position   ",i+1);

 }



Tuesday 16 July 2013

BUBBLE SORT



#include<stdio.h>
void main()
 {
  int a[100];
  int n,i,j,temp;
  printf("\n Enter the Limit  :  ");
  scanf("%d",&n);
  printf("\n Enter %d elements.......\n",n);
  for(i=0;i<n;i++)
   scanf("%d",&a[i]);
  for(i=0;i<n-1;i++)
   {
    for(j=n-1;j>i;j--)
     {
      if(a[j-1]>a[j])
       {
        temp=a[j-1];
        a[j-1]=a[j];
        a[j]=temp;
       }
     }
   }
  printf("\n Array after Bubble Sort......\n");
   for(i=0;i<n;i++)
    printf("\t %d\t",a[i]);
 }



Sunday 7 July 2013

SUM OF ARRAY



#include<stdio.h>
void main()
 {
  int a[100],i,n,sum=0;
  printf("\n Enter the limit   :");
  scanf("%d",&n);
  printf("\nEnter the %d Numbers.....\n",n);
  for(i=0;i<n;i++)
   {
    scanf("%d",&a[i]);
    sum+=a[i];
   }
  printf("\nThe sum is %d  :",sum);
 }


Tuesday 23 April 2013

LINEAR SORT




#include<stdio.h>
void main()
 {
  int a[100];
  int i,j,temp,n;
  printf("\n Enter the Limit  :");
  scanf("%d",&n);
  printf("\n Enter the %d Elements.....\n",n);
  for(i=0;i<n;i++)
   scanf("%d",&a[i]);
  for(i=0;i<n-1;i++)
   {
    for(j=i+1;j<n;j++)
     {
      if(a[i]>a[j])
       {
        temp=a[i];
        a[i]=a[j];
        a[j]=temp;
       }
     }
   }
  printf("\n Array after Linear sort......\n");
   for(i=0;i<n;i++)
    printf("\t%d\t",a[i]);
 }


Monday 4 March 2013

MATRIX ADDITION



/* matrix Addition */



# include<stdio.h>
    void main()
      {
        int a[50][50],b[50][50],i,j,m,n;
        printf("\n\n\t\tProgram to perform Matrix Addition\n");
        RETRY:
        printf("\nWARNING: Order of both Matrices must be same to perform Addition");
        printf("\nEnter the order of matrices\n");
        scanf("%d%d",&m,&n);
        printf("\nEnter the first matrix\n");
        for(i=0;i<m;i++)
           {
                for(j=0;j<n;j++)
                        scanf("%d",&a[i][j]);
           }
        printf("\nEnter the second matrix\n");
        for(i=0;i<m;i++)
           {
                for(j=0;j<n;j++)
                        scanf("%d",&b[i][j]);
           }
        printf("\n\n");
        for(i=0;i<m;i++)
           {
                printf("\n\t");
                for(j=0;j<n;j++)
                        printf(" %d",a[i][j]);
           }
        printf("\n\n\t  +\n");
        for(i=0;i<m;i++)
           {
                printf("\n\t");
                for(j=0;j<n;j++)
                        printf(" %d",b[i][j]);
           }
        printf("\n\n\t  =\n");
        for(i=0;i<m;i++)
           {
                printf("\n\t");
                for(j=0;j<n;j++)
                        printf(" %d",a[i][j]+b[i][j]);
  }
        printf("\nEnter 1 to Retry & 0 to Exit\n");
        scanf("%d",&m);
        if (m==1)
                goto RETRY;
   }