Tuesday, 17 May 2022

Multithreading in JAVA using Thread class

 class MyThread extends Thread

{

int n;

MyThread(int x)

{

n=x;

}

public void run()

{

System.out.println("Thread"+n+"Starts");

for(int i=1;i<=10;i++)

{

System.out.println("Thread"+n+"Iteration"+i);

}

System.out.println("Thread"+n+"Ends");

}

}

class ThreadDemo

{

public static void main(String args[])

{

System.out.println("Main Thread Starts");

MyThread t1=new MyThread(1);

MyThread t2=new MyThread(2);

MyThread t3=new MyThread(3);

t1.start();

t2.start();

t3.start();

System.out.println("Main Thread Ends");

}

}

Download Java Code











Sunday, 15 May 2022

Program to implement Binary Search

 //Binary Search

#include<iostream.h>

#include<conio.h>

int binary_search(int a[],int n,int item)

{

int loc=-1,l=0,r=n-1,mid;


while(l<=r)

{

mid=(l+r)/2;

if(a[mid]==item)

{

loc=mid;

break;

}

else if(a[mid]<item)

{

l=mid+1;

}

else

{

r=mid-1;

}

}

return loc;

}

void main()

{

int a[]={2,4,5,7,11,13,15,16,22,30},p,k;

clrscr();

cout<<"Enter Item to searched";

cin>>k;

p=binary_search(a,10,k);

if(p==-1)

cout<<"Element not found"<<endl;

else

cout<<"Element found at location"<<p<<endl;

getch();

}

Download C++ Code

#linearsearch

#string #stringpermutations #strlen #getch #cprogramming #cpp #c++

#auto #break #case #char

#const #continue #default #do

#double #else #enum #extern

#float #for #goto #if

#int #long #register #return

#short #signed #sizeof #static

#struct #switch #typedef #union

#unsigned #void #volatile #while


#Keywords  #Identifier

#Variables  #Constants

#datatypes

#Input #output #functions #Binarysearch

Saturday, 14 May 2022

Program to implement Circular Queue using c++

 #include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#define MAX 5

int a[MAX],rear=-1,front=-1;

void enqueue(int item)

{

if(front==((rear+1)%MAX))

{

cout<<"Queue is Full\n";

}

else if(front==-1)

{

front=0;

rear=0;

a[rear]=item;

}

else

{

rear=(rear+1)%MAX;

a[rear]=item;

}

}

int dequeue()

{

int item=-1;

if(front==-1)

{

return -1;

}

else if(front==rear)

{

item=a[front];

rear=front=-1;

}

else

{

item=a[front];

front=(front+1)%MAX;

}

return item;

}

int isempty()

{

if(front==-1)

return 1;

else

return 0;

}

int isfull()

{

if(rear==MAX-1)

return 1;

else

return 0;

}

int peek()

{

if(front!=-1)

return a[front];

else

return -1;

}

void display()

{

int i;

if(front==-1)

{

cout<<"Queue is empty";

}

else

{

for(i=front;i<=rear;i++)

cout<<a[i]<<"\n";

}

if(front>rear)

{

for(i=front;i<=MAX-1;i++)

cout<<a[i]<<"\n";

for(i=0;i<=rear;i++)

cout<<a[i]<<"\n";

}

}

void main()

{

int n,item,k,loc;

char str[20];

clrscr();

while(1)

{

cout<<"\nPress 1 to enqueue\n";

cout<<"Press 2 to dequeue\n";

cout<<"Press 3 to Display\n";

cout<<"Press 4 exit\n";

cout<<"Press 5 to check empty\n";

cout<<"Press 6 to check full\n";

cout<<"Press 7 to Peek or Peep\n";

cin>>n;

switch(n)

{

case 1:

cout<<"Enter Item\n";

cin>>item;

enqueue(item);

break;

case 2: item=dequeue();

if(item==-1)

cout<<"Queue is Empty\n";

else

cout<<"Item deleted is "<<item;

break;

case 3:display();

       break;

case 4:

exit(1);

break;

case 5: if(isempty())

cout<<"Queue is Empty\n";

else

cout<<"Queue is not Empty\n";

break;

case 6: if(isfull())

cout<<"Queue is Full\n";

else

cout<<"Queue is not Full\n";

break;

case 7: item=peek();

if(item==-1)

cout<<"Queue Empty";

else

cout<<"Item on front is "<<item;

break;

default: cout<<"Wrong Input";

}

}

}

Download c++ Code

#linearsearch

#string #stringpermutations #strlen #getch #cprogramming #cpp #c++

#auto #break #case #char

#const #continue #default #do

#double #else #enum #extern

#float #for #goto #if

#int #long #register #return

#short #signed #sizeof #static

#struct #switch #typedef #union

#unsigned #void #volatile #while


#Keywords  #Identifier

#Variables  #Constants

#datatypes

#Input #output #functions

#Operators 

Linear search program using C++

 #include<iostream.h>

#include<conio.h>

int linear_search(int a[],int n,int item)

{

int i,loc=-1;


for(i=0;i<n;i++)

{

if(a[i]==item)

{

loc=i;

break;

}

}

return loc;

}                                   //  Best-  O(1)     Worst- O(n)

void main()    //Average  (1+2+3+4+...n)/n = (n*(n+1)/2)/n = (n+1)/2

//O(n)

{

int a[]={12,38,5,56,39,99,34,3,44,1000},p,k;

clrscr();

cout<<"Enter Item to searched";

cin>>k;

p=linear_search(a,10,k);

if(p==-1)

cout<<"Element not found"<<endl;

else

cout<<"Element found at location"<<p<<endl;

getch();

}

Download C++ Code 

#linearsearch

#string #stringpermutations #strlen #getch #cprogramming #cpp #c++

#auto #break #case #char

#const #continue #default #do

#double #else #enum #extern

#float #for #goto #if

#int #long #register #return

#short #signed #sizeof #static

#struct #switch #typedef #union

#unsigned #void #volatile #while


#Keywords  #Identifier

#Variables  #Constants

#datatypes

#Input #output #functions

#Operators 


Print all Permutations of a String

C Program to Print all Permutations of a String



 #include<stdio.h>

#include<conio.h>

void swap(char str[],int i,int j)

{

char ch;

ch=str[i];

str[i]=str[j];

str[j]=ch;

}

void fxn(char str[],int l,int r)

{

int i;

if(l==r)

{

printf("%s\n",str);

}

for(i=l;i<=r;i++)

{

swap(str,i,l);

fxn(str,l+1,r);

swap(str,i,l);

}


}

void main()

{

char str[]="ABCD";

clrscr();

fxn(str,0,strlen(str)-1);

getch();

}

output:


Download C Code

#string #stringpermutations #strlen #getch #cprogramming #cpp #c++

#auto #break #case #char

#const #continue #default #do

#double #else #enum #extern

#float #for #goto #if

#int #long #register #return

#short #signed #sizeof #static

#struct #switch #typedef #union

#unsigned #void #volatile #while


#Keywords  #Identifier

#Variables  #Constants

#datatypes

#Input #output #functions

#Operators