Thursday, 21 February 2013

a program to set nth bit of an integer to 0


#include<stdio.h>
#include<conio.h>
void main()
{
int n=2;
int f=12;
clrscr();
f&=~(1L<<n);
printf("%d",f);
getch();
}


Explanation:
f &=~(1L<<n)
=> f = f & ~ (1L<<n)
in binary; f : 0000 1100
             1L: 0000 0001
   (1L<<2) : 0000 0100  ; bits are shifted to left 2 times.
  ~(1L<<2) : 1111 1011 ; complement the bits.
  &  is a bitwise AND operator, it will perform AND operation and result will be stored to f  so;
                f : 0000 1000
output will be 8.



A program to set nth bit of an integer to 1


#include<stdio.h>
#include<conio.h>
void main()
{
int n=2;
 int f=8;
clrscr();
f |= (1L<<n);
printf("%d",f);
getch();
}

Explanation:
f |= (1L<<n)
=> f = f | (1L<<n)
in binary; f : 0000 1000
             1L: 0000 0001
   (1L<<2) : 0000 0100  ; bits are shifted to left 2 times.
   | is a bitwise or operator, it will perform or operation and result will be stored to f  so;
                f : 0000 1100
output will be 12.


Wednesday, 20 February 2013

array of pointer


#include<iostream.h>
#include<conio.h>
void sort(char *name[],const int size);
void main()
{
char *name[]={"saurabh","amit","sushant","rohan"};
sort(name,4);
getch();
}
void sort(char *name[],const int size)
{ clrscr() ;
for(int i=0;i<size;i++)

cout<<name[i]<<"\t";
}

accessing elements of array of any dimention with single loop with pointer


#include<stdio.h>
#include<conio.h>
int sum(int *ptr,const int row,const int col);
void main()
{
int a[][3]={1,2,3,4,5,6,7,8,9};
int *ptr;
clrscr();
ptr=&a[0][0];
printf("%d",sum(ptr,3,3));
getch();
}
int sum(int *ptr,const int row,const int col)
{
int s=0;
for(int i=0;i<row*col;i++)
{
s=s+*(ptr+i);
}
return s;
}
NOTE: program will sum all the elements of array.

pointer to array


#include<stdio.h>
#include<conio.h>
int highest(int *ptr,const int row,const int col);
void main()
{
int *ptr;
int temp[3][4]={3,4,2,6,5,8,9,12,0,4,7,10};
ptr=&temp[0][0];
printf("%d",highest(ptr,3,4));
getch();
}
int highest( int *ptr,const int row,const int col)
{
int h=0;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
if(*(ptr+i*col+j)> h)
h=*(ptr+i*col+j);
return h;
}

NOTE: here *(ptr+i*col+j)=temp[i][j].
program finds the highest value element of 3x4 array. 

passing array to function



#include<stdio.h>
#include<conio.h>
const int row=3,col=4;
int high(int temp[3][4])
{
int i,j,h=0;
for( i=0;i<row;i++)
{
for( j=0;j<col;j++)
{
if(h<temp[i][j])
h=temp[i][j];
}
}
return h;
}

void main()
{
int temp[][4]={12,23,45,10,13,24,24,56,30,45,28,90};
printf("%d",high(temp));
getch();
}

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