Friday 25 January 2013

A program to reverse the words of a sentence

eg. i am a good boy
result: boy good a am i.


#include<stdio.h>
#include<conio.h>
void main()
{
char a[20],c[20];
char x=' ';
int k,l,m,i,j,r,b[10];
j=0;
for(i=0;i<20;i++)
c[i]=' ';
clrscr();

printf("enter a sentence:");
for(i=0;i<20;i++)
scanf("%c",&a[i]);
for(i=0;i<20;i++)
printf("%c",a[i]);
for(i=0;i<20;i++)
{
if(a[i]==x)
{
b[j]=i;
j++;

}
}
//printf("%d",j);
r=0;
/*for(i=0;i<j;i++)
printf("%d",b[i]+1);*/
printf("\n");
for(m=1;m<=j;m++)
{
l=b[j-m];
for(i=r;i<20;i++)
{
if(a[l+1]!='\0'&& a[l+1]!=' ' &&l<20)
{
c[i]=a[l+1];
l++;
r++;
}
}
r++;
c[r]=' ';
}


for(i=0;i<20;i++)
{
printf("%c",c[i]);
}
getch();
}

NOTE: give one space before entering the input and many spaces after completion of sentence so that input exceeds the no. of elements in array.






Remove character from one string which matches in other


eg.s1="abcde" ,s2="xyzab" then result "cde".




#include<stdio.h>
#include<conio.h>
void main()
{
char a[10],b[10];
int i,j,k;
for(i=0;i<10;i++)
a[i]='\0';

printf("enter string a:");
scanf("%s",&a);
printf("enter string b:");
scanf("%s",&b);
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(a[i]==b[j])
{
for(k=i;k<10;k++)
a[k]=a[k+1];

}
}
}

printf("%s",a);
getch();
}


Thursday 24 January 2013

Removing duplicacy in a string with C


#include<stdio.h>
#include<conio.h>
void main()
{
char a[10];
char x;
int i,j,k;
clrscr();
printf("enter a string:");
scanf("%s",&a);

for(j=0;j<10;j++)
{
x=a[j];
for(i=j+1;i<10;i++)
{
if(a[i]==x)
{
//printf("%d",i);

for( k=i;k<10;k++)
{
a[k]=a[k+1];

}
}
}
}
printf("%s",a);
getch();
}

input: microsoftim
ouptut: microsft

Conversion of decimal to binary,octal,hexadecimal in C/C++


#include<stdio.h>
#include<conio.h>
void main()
{
int s,a,k,c,i,b[10],m[10];
int n=2;
clrscr();
i=0;
c=0;
printf("enter a number:");
scanf("%d",&a);
while(a>0)
{
b[i]=a%n;

a=a/n;

i++;
c++;

}
k=0;
for(i=c-1;i>=0;i--)
{
m[k]=b[i];
k++;
}
for(i=0;i<c;i++)
{
printf("%d",m[i]);
}

getch();
}

input: 9
output:1001
if you want to calculate octal then replace n=8, for hexadecimal, n=16;

Sunday 20 January 2013

Elevator Program in Java


import java.io.*;

class elevator
{
int currentfloor;
int headingfloor;
boolean movement;
boolean doors;

void goToFloor(int s)
{
headingfloor=s;
System.out.println("\nLift is heading to floor="+headingfloor);
}

void openDoors()
{
doors=true;
System.out.println("\nDoors are open\n");
}

void closeDoors()
{
doors=false;
System.out.println("\nDoors are close\n");
}

void goingUp()
{
movement=true;
}

void goingDown()
{
movement=false;
}

void print(int h)
{
currentfloor=h;
System.out.print("\n@@Heading please wait@@\n");
if(headingfloor>currentfloor)
{
System.out.print("\nMovement up\n");
}
else
{
System.out.print("\nMovement down\n");
}
}
}


class ElevatorProgram
{
public static void main(String args[]) throws IOException
{
elevator ob=new elevator();
elevator obdup=new elevator();
ob.openDoors();
System.out.print("\nPresent floor=");
BufferedReader stdin1 = new BufferedReader ( new InputStreamReader( System.in ) );
String h;
h=stdin1.readLine();
int a= Integer.parseInt(h);
System.out.print("\nDesired floor=");
BufferedReader stdin = new BufferedReader ( new InputStreamReader( System.in ) );
String l;
l=stdin.readLine();
int u= Integer.parseInt(l);
ob.goToFloor(u);

obdup.goingUp();
obdup.goingDown();
ob.print(a);
obdup.closeDoors();
}
}

Matrix Multiplication in java




class MatrixMultiply{

        public static void main(String[]args)  {

          int array[][] ={{1,2,3},{4,5,6},{7,8,9}};

          int array1[][] ={{1,2,3},{4,5,6},{7,8,9}};

          int array2[][] = new int[3][3];

          int x= array.length;

          System.out.println("Matrix 1: ");

            for(int i = 0; i <x; i++) {

            for(int j = 0; j <x; j++) {

              System.out.print(" "+ array[i][j]);

            }

          System.out.println();

          }

          int y= array1.length;

          System.out.println("Matrix 2: ");

            for(int i = 0; i <y; i++) {

            for(int j = 0; j <y; j++) {

              System.out.print(" "+array1[i][j]);

            }

          System.out.println();

          }

            for(int i = 0; i <x; i++) {

            for(int j = 0; j <y; j++) {

              for(int k = 0; k <y; k++){

                array2[i][j] +=array[i][k]*array1[k][j];

              }

            }

           }

          System.out.println("Multiply of both matrix : ");

          for(int i = 0; i <x; i++) {

            for(int j = 0; j <y; j++) {

              System.out.print(" "+array2[i][j]);

            }

          System.out.println();

          }

        }

      }

A simple banner with applet in java


/* A simple banner applet. This applet creates a thread that scrolls the message contained in msg right to left across the applet's window. */
import java.awt.*;
import java.applet.*;
/*
<applet code="SimpleBanner" width=600 height=300>
</applet>
*/
public class SimpleBanner extends Applet implements Runnable {
String msg = "HELLO";
Thread t = null;
int state;
boolean stopFlag;
// Set colors and initialize thread.
public void init()
   {
       setBackground(Color.pink);
       setForeground(Color.red);
   }
// Start thread
                 public void start()
                     {
                          t = new Thread(this);
                          stopFlag = false;
                          t.start();
                    }
// Entry point for the thread that runs the banner.
                public void run()
                    {
                          char ch;
// Display banner
                         for( ; ; ) {
                             try
                               {
                                 repaint();
                                 Thread.sleep(5000);
                                 ch = msg.charAt(0);
                                 msg = msg.substring(1, msg.length());
                                 msg += ch;
                                 if(stopFlag)
                                        break;
                            } catch(InterruptedException e) {}
                     }
}
// Pause the banner.
public void stop()
                      {
           stopFlag = true;
                              t = null;
                      }
// Display the banner.
                 public void paint(Graphics g)
                     {
                        g.drawString(msg, 80, 10);
                     }
    }

A moving Banner in Java with Applet


import java.awt.*;
import java.applet.*;
/*
<applet code="ParamBanner" width=300 height=50>
<param name=message value="Java makes the Web move!">
</applet>
*/
public class ParamBanner extends Applet implements Runnable
          {
String msg;
Thread t = null;
int state;
boolean stopFlag;
// Set colors and initialize thread.
public void init()
    {
               setBackground(Color.cyan);
               setForeground(Color.red);
                       }
// Start thread
public void start()
                       {
                 msg = getParameter("message");
if(msg == null) msg = "Message not found.";
msg = " " + msg;
t = new Thread(this);
stopFlag = false;
t.start();
       }

Sunday 6 January 2013

Button click and event handling with applet in java


package event1;
import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;


public class Event1 extends Applet implements  ActionListener {
   Button btn1,btn2;
   public void init()
    {
  setLayout(new FlowLayout());
   btn1=new Button("blue");
   btn2=new Button("green");
btn1.addActionListener(this);
btn2.addActionListener(this);
add(btn1);
add(btn2);
// setSize(400,400);
//setBounds(200,180,200,140);
//setVisible(true);
}
  public void actionPerformed(ActionEvent e)
   {
       if(e.getSource()==btn1)
       {
          setBackground(Color.BLUE);
       }
       else if(e.getSource()==btn2)
        setBackground(Color.GREEN);   
   }
}

A moving vehicle with applet in java


package animation1;
import java.applet.Applet;
import java.awt.*;

public class Animation1 extends Applet implements Runnable{
  private int x,y,x1,y1,x3,y3;
  Thread th;
    public void init()
   {  x=50;
   y=50;
   x1=50;
   y1=200;
   x3=150;
   y3=200;
  
       setForeground(Color.BLACK);
     
           //repaint();
          
       
     
   }
    public void start()
    {
        th=new Thread (this);
        th.start();
       
    }
   public void run()
   {
        for(int i=0;i<1000;i++)
       { 
           x=x+3;
          // y++;
           x1=x1+3;
          // y1++;
           x3=x3+3;
          // y3++;
            repaint();
           try{
              
         Thread.sleep(50);
              
           }
          catch(InterruptedException e){}
       }
   }
  /* public void update(Graphics g)
    {
        paint(g);
    }*/
   public void paint(Graphics g)
   {  
       g.drawRect(x,y,150,150);
       g.drawOval(x1,y1,50,50);
       g.drawOval(x3,y3,50,50);
       
             
   
            }
}

Addition of two number with applet and event handling


package applet2;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Applet2 extends Applet implements ActionListener{
 
   Label lbl1,lbl2,lbl3,lbl4;
   TextField txt1,txt2;
   Button btn;
   public void init()
   { setLayout(new GridLayout(4,2));
      setBackground(Color.GRAY);
       lbl1=new Label("enter first Number");
       lbl2=new Label("enter second Number");
       lbl3=new Label("Sum");
       lbl4=new Label();
       txt1=new TextField(20);
       txt2=new TextField(20);
       btn=new Button("Result");
        Component add1 = add(lbl1);
       Component add2=add(txt1);
     
      Component add3= add(lbl2);
       Component add4=add(txt2);
      Component add5= add(lbl3);
       Component add6=add(lbl4);
       Component add7=add(btn);
       btn.addActionListener(this);
   }
   public void actionPerformed(ActionEvent e)
   {
       int s1,s2,s3;
       if(e.getSource()==btn);
       {
         s1=Integer.parseInt(txt1.getText());
          s2=Integer.parseInt(txt2.getText());
          s3=s1+s2;
          lbl4.setText(Integer.toString(s3));
       }
   }
   
}

Saturday 5 January 2013

Add subtract divide multiply in java

package calculator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


class cal extends JFrame implements ActionListener
{
    // cal cl=new cal();
    int s3,s1,s2;
    JLabel num1,num2,res,rs;
    JPanel panel1,panel2;
    JButton add,sub,mul,div;
    JTextField n1,n2,n3;
    cal()
    {
     panel1=new JPanel(new FlowLayout());
      panel2=new JPanel(new FlowLayout());
     num1=new JLabel("Number1");
     num2=new JLabel("Nmber2");
     res=new JLabel("Result");
     rs=new JLabel("rgjfjj");
     n1=new JTextField(5);
     n2=new JTextField(5);
     n3=new JTextField(5);
     add=new JButton("ADD");
     sub=new JButton("SUB");
     mul=new JButton("MUL");
     div=new JButton("DIV");
    
     add.addActionListener(this);
     mul.addActionListener(this);
     sub.addActionListener(this);
     div.addActionListener(this);
   Component add1= panel1.add(num1);
  Component add2=  panel1.add(n1);
   Component add3= panel1.add(num2);
   Component add4= panel1.add(n2);
   Component add5= panel1.add(res);
    Component add6 = panel1. add(n3);
    Component add7 = panel2.   add(add);
    Component add8 = panel2.   add(sub);
    Component add9 = panel2.   add(mul);
    Component add10 = panel2.  add(div);
     add(panel1,BorderLayout.NORTH);
     add(panel2,BorderLayout.SOUTH);
    
    
      
     }
   
    public void actionPerformed(ActionEvent e)
   {
      int s1=Integer.parseInt(n1.getText());
      int s2=Integer.parseInt(n2.getText());
     
     
       if(e.getSource()==add)
       {
           s3=s1+s2;
      System.out.println(s3);
       n3.setText(Integer.toString(s3));
       }
       else if(e.getSource()==sub)
       {
           s3=s1-s2;
      System.out.println(s3);
       n3.setText(Integer.toString(s3));
       }
        else if(e.getSource()==div)
       {
           s3=s1/s2;
      System.out.println(s3);
       n3.setText(Integer.toString(s3));
       }
       else if(e.getSource()==mul)
       {
           s3=s1*s2;
      System.out.println(s3);
       n3.setText(Integer.toString(s3));
       }
}
}

public class Calculator {
  
    public static void main(String[] args) {
       cal cl=new cal();
       cl.setBounds(200, 150, 120, 550);
       cl.setTitle("calculator");
       cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       cl.setResizable(true);
       cl.setVisible(true);
       cl.setSize(500,200);
    }
}