Thursday, 24 January 2013

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);
    }
}

Monday, 29 October 2012

Priority Sheduling in C

#include<conio.h>
#include<stdio.h>
void main()
 {
   clrscr();
   int x,n,p[10],pp[10],pt[10],w[10],t[10],i;
   float atat,awt;
   printf("Enter the number of process : ");
   scanf("%d",&n);

   for(i=0;i<n;i++)
    {
      printf("\nProcess no %d : ",i+1);
      printf("\nenter process burst time:");
      scanf("%d" ,&pt[i]);
      printf("enter proccess priority");
      scanf("%d",&pp[i]);
      p[i]=i+1;
    }
  for(i=0;i<n-1;i++)
   {
     for(int j=i+1;j<n;j++)
     {
       if(pp[i]>pp[j])
       {
     x=pp[i];
     pp[i]=pp[j];
     pp[j]=x;
     x=pt[i];
     pt[i]=pt[j];
     pt[j]=x;
     x=p[i];
     p[i]=p[j];
     p[j]=x;
      }
   }
}
w[0]=0;
awt=0;
t[0]=pt[0];
atat=t[0];
for(i=1;i<n;i++)
 {
   w[i]=t[i-1];
   awt+=w[i];
   t[i]=w[i]+pt[i];
   atat+=t[i];
 }
printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time   Priority \n");
for(i=0;i<n;i++)
  printf("\n %d \t\t %d  \t\t %d \t\t %d \t\t %d \n",p[i],pt[i],w[i],t[i],pp[i]);
awt/=n;
atat/=n;
printf("\n Average Wait Time : %4f \n",awt);
printf("\n Average Turn Around Time : %4f \n",atat);
getch();
}











Saturday, 21 April 2012

A Menu Based Program in shell script & Creating Backup of Home directory


1. The menu application should have at least four choices.
2. One of the choices should allow the user of the application to backup all of the files in their home directory into a special “backup” directory.
If the “backup” directory does not exist, your script should create it.
If the “backup” directory is not empty, display a message to the user that it is not empty, ask the user if they want to delete all the files. If they say yes, delete the files and perform the backup. If they say no, display a message that the backup was not performed.
3. One of the choices should display the current date and time.
4. One of the choices should display the current month calendar.
5. One of the choices should be the execution of a command that is often used.
6. If the choice is invalid, redisplay the menu.




Desgined Code:-
#! bin/bash

#menu

echo enter [1] to backup                                                        

echo enter [2] to see  current date and time

echo enter [3] to see current month calender

echo enter [4] to execute the command

read a                 # a is the variable to store 1,2,3,4....

case "$a" in

1)cd /                # change dir to root

cd /home/saurabh      # change dir to home/saurabh

mkdir backup          # make a dir named backup in home/saurabh

cd backup             # change dir to backup

[ "$(echo .* *)" == ". .. *" ] && a=0||a=1      # it searches files and folder in backup dir if there is no files and folders

                                                #  then a is set to 0, if it contains then a is set to one



if [ $a -eq 0 ]                                 # if a=0 then display backup directory is empty

then

echo backup directory is empty

fi

if [ $a -eq 1 ]                                 # if a=1 then display backup directory has files

then

echo backup directory has files

echo if you want to remove all the files enter 1 if not enter any another no.

read b             # b is a variable to store a no.

if [ $b -eq 1 ]

then

rm -r *            # it removes all the files and folder from backup dir.

fi
[ "$(echo .* *)" == ". .. *" ] && echo now dir is empty|| echo dir is non empty  
#if backup dir has files and    folder then it shows dir is non empty

#if backup dir has no files and folder then it shows dir is empty
                 
fi

echo enter 5 to backup your all files and folder if you dont want.... enter any other no.

read c           # c is a variable to store a no.

if [ $c -eq 5 ]

then

cd /            # change dir to root

cp -r home home/saurabh/backup 
# copy all the files and folder of home to home/saurabh/backup
fi ;;


2) echo todays date and time is $(date);; # shows todays date and time

3) cal ;;                                 # shows calender of month

4) touch sau.c;;                          #create a file...

*) bash menu.sh;;                         # redisplay the menu after entering any other no. other than 1,2,3,4

esac

Input:-
bash menu.sh

Output#1:-
enter [1] to backup

enter [2] to see current date and time

enter [3] to see current month calender

enter [4] to execute the command

1
backup directory is empty

enter 5 to backup your all files and folder if you dont want.... enter any other no.
5
All the files and folder of home directoty will be copied & stored to home/saurabh/backup
directory.
Output#2:-
enter [1] to backup

enter [2] to see current date and time

enter [3] to see current month calender

enter [4] to execute the command

2   
todays date and time is Sun Apr 15 13:29:12 IST 2012


Output#3:-
enter [1] to backup

enter [2] to see current date and time

enter [3] to see current month calender

enter [4] to execute the command

3

     April 2012

Su Mo Tu We Th Fr Sa

 1  2  3  4  5  6  7

 8  9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30


Output#4:-

enter [1] to backup

enter [2] to see current date and time

enter [3] to see current month calender

enter [4] to execute the command

8      

enter [1] to backup

enter [2] to see current date and time

enter [3] to see current month calender

enter [4] to execute the command



 Limitations:-
To backup the files of Home directory all the files should have read permissions for all user group and others...otherwise it will show permission denied.


When I excuted the program I got errors as:-

cp: cannot stat `home/saurabh/Desktop/assignment/shell/modes/f1': Permission denied

cp: cannot stat `home/saurabh/Desktop/assignment/shell/modes/modes.sh': Permission denied

cp: cannot stat `home/saurabh/Desktop/assignment/change/f1': Permission denied

cp: cannot stat `home/saurabh/Desktop/assignment/change/d1': Permission denied


 References:-
Beginning Linux Programming(Text Book)


Tuesday, 10 April 2012

File Management System In Shell Script



echo------------------------------FILE MANAGEMENT SYSTEM--------------------------------------------
echo give a path of folder which contain some files:
cd /
read a
cd $a
echo enter 1 to List files stored along with their sizes.
echo enter 2 to Create files
echo enter 3 to Allow changes to files
echo enter 4 to Delete files
echo enter 5 to store information of files
echo enter 6 to move files between the directories
read n
case "$n" in


1) ls -s;;
2) echo enter file_name and write in file
   read f
   cat> $f;;
3) echo give path of folder to make changes in the files
   read c
   cd /
   cd $c
   echo folder contain files:-
   ls
   echo give the name of file you want to make changes
   read d
   echo "enter 1 for read,write,execute to user group and others"
   echo "enter 2 for read and write to users and group"
   echo "enter 3 for read write and execute to only users"
   read m
   case "$m" in
   1) chmod 777 $d;;
   2) chmod 660 $d;;
   3) chmod 700 $d;;
   esac
   echo changes happened:-
   ls -l;;
4) echo files are:-
   ls
   echo enter no. of files you want to delete
   read countt
   for(( i=1 ; i<=$countt ; i++ ))
   do
   echo give filename to delete
   read e
   rm -r $e
   echo file $e is deleted
   echo now files are:-
   ls
   done;;


5) echo files are:-
   ls
   echo enter name of file you want to see the information and store it in status file.
   read g
   echo information of file is:
   stat $g
   touch status
   stat $g>>status
   echo information of file is stored in file name status and its path is $a/status;;


6) echo enter the path of source directory from where you want to copy the files:
   read p
   echo enter the path of destination directory to where you want to move the files:
   read q
   cd /
   cd $p
   echo files of source dir are:-
   ls
  
   echo enter no. of files you want to move.
   read count
   for(( i=1 ; i<=$count ; i++ ))
   do
   cd /
   cd $p
   echo files of source dir are:-
   ls
   echo enter the name of file you want to move
   read name
   mv /$p/$name  /$q
   cd /
   cd $q
   echo content of destination dir $q now are:
   ls
   done ;;
  
esac

Note:- To run the program save it as filename.sh and give a command bash filename.sh






Sunday, 8 April 2012

Gauss Seidal Method in C to solve equations.

Applied Gauss Seidal method to solve following system of equation up to given no. of iterations.
(1) 4x+y-z=7
(2) 2x+3y+z=4
(3) x+y-2z=2

#include<conio.h>
#include<stdio.h>
void main()
{
float x,y,z,a,b,c;
int i,n;
clrscr();
printf("enter no of iteration n");
scanf("%d",&n);
x=0;
y=0;
z=0;
a=(7-y+z)/4;
b=(4-(2*a)-z)/3;
c=((-1)*(2-a-b))/2;
printf("x            y           z");
printf("\n%f  %f  %f",a,b,c);
x=a;
y=b;
z=c;
for(i=1;i<n;i++)
{
x=(7-y+z)/4;
y=(4-(2*x)-z)/3;
z=(-1)*(2-x-y)/2;
printf("\n%f  %f  %f ",x,y,z);
}
getch();
}