Saturday, May 21, 2016

A Program in C using DO-WHILE Loop to find square of the number

Program code:

#include<stdio.h>
#include<conio.h>
int main()
{
int num,op;
//clrscr();
do
{
printf("enter a number:\n");
scanf("%d",&num);
printf("square root of %d is %d\n",num,num*num);
printf("want to try another number? \n(enter 1 for yes /0 for no)\n");
scanf("%d",&op);
}
while(op==1);
//getch();
return 0;
}

output:

enter a number:
2
square root of 2 is 4
want to try another number?
(enter 1 for yes/0 for no)
n

Friday, February 13, 2015

C Program for Strassen Matrix Multiplication

#include<conio.h>
 #include<stdio.h>
int main(){
  int a[2][2],b[2][2],c[2][2],i,j;
  int m1,m2,m3,m4,m5,m6,m7;

  printf("Enter the 4 elements of first matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&a[i][j]);

  printf("Enter the 4 elements of second matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&b[i][j]);

  printf("\nThe first matrix is\n");
  for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",a[i][j]);
  }

  printf("\nThe second matrix is\n");
  for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",b[i][j]);
  }

  m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
  m2= (a[1][0]+a[1][1])*b[0][0];
  m3= a[0][0]*(b[0][1]-b[1][1]);
  m4= a[1][1]*(b[1][0]-b[0][0]);
  m5= (a[0][0]+a[0][1])*b[1][1];
  m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
  m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

  c[0][0]=m1+m4-m5+m7;
  c[0][1]=m3+m5;
  c[1][0]=m2+m4;
  c[1][1]=m1-m2+m3+m6;

   printf("\nAfter multiplication using \n");
   for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",c[i][j]);
   }

getch();
   return 0;
}

Output:


C Program for LRU (Last Recently Used) Page replacement technique implementation

#include<stdio.h> #include<conio.h> int n,ref[100],fs,frame[100],count=0; void input(); void show(); void cal(); void main() { printf("****************** LRU Page Replacement Algo *********************\n"); input(); cal(); show(); getch(); } void input() { int i; printf("Enter no of pages in Refrence String\t"); scanf("%d",&n); printf("Enter the reference string:"); for(i=0;i<n;i++) scanf("%d",&ref[i]); printf("Enter the Frame Size\t"); scanf("%d",&fs); } void cal() { int i,j,k=0,c1,c2[100],r,temp[100],t; frame[k]=ref[k]; count++; k++; for(i=1;i<n;i++) { c1=0; for(j=0;j<fs;j++) { if(ref[i]!=frame[j]) c1++; } if(c1==fs) { count++; if(k<fs) { frame[k]=ref[i]; k++; } else { for(r=0;r<fs;r++) { c2[r]=0; for(j=i-1;j<n;j--) { if(frame[r]!=ref[j]) c2[r]++; else break; } } for(r=0;r<fs;r++) temp[r]=c2[r]; for(r=0;r<fs;r++) { for(j=r;j<fs;j++) { if(temp[r]<temp[j]) { t=temp[r]; temp[r]=temp[j]; temp[j]=t; } } } for(r=0;r<fs;r++) { if(c2[r]==temp[0]) frame[r]=ref[i]; } } } } } void show() { printf("Page Faults = %d",count); }


Output:




Thursday, February 12, 2015

C program for number pattern

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,rng;
clrscr();
printf("enter the range");
scanf("%d",&rng);
for(i=1;i<=rng;i++)
{
printf("\n");
for(j=1;j<=i;j++)
printf("%2d",j);
}
getch();
}

output:


C program to create diamond pattern in C

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k,l,rng;
char c='*';
clrscr();
printf("enter the range");
scanf("%d",&rng);
for(i=rng;i>=0;i--)
{
printf("\n");
for(j=0;j<=i;j++)
printf("%c",c);
for(k=i;k<=2*rng-i;k++)
printf(" ");
for(l=0;l<=i;l++)
printf("%c",c);
}
for(i=0;i<=rng;i++)
{
printf("\n");
for(j=0;j<=i;j++)
printf("%c",c);
for(k=i;k<=2*rng-i;k++)
printf(" ");
for(l=0;l<=i;l++)
printf("%c",c);
}
getch();
}

output:

Program to print pyramid pattern in c

 #include <stdio.h>
 #include <conio.h>
int main()
{
   int row, c, p, temp;

   printf("Enter the number of rows in pyramid ");
   scanf("%d",&p);

   temp = p;

   for ( row = 1 ; row <= p ; row++ )
   {
      for ( c = 1 ; c < temp ; c++ )
         printf(" ");

      temp--;

      for ( c = 1 ; c <= 2*row - 1 ; c++ )
         printf("*");

      printf("\n");
   }

   return 0;
}

output



Bitstuffing Program in C

#include<stdio.h> #include<conio.h> #include<string.h> int main() { int i, j,count=0,nl; char str[100]; printf("enter the bit string: "); gets(str); for (i=0;i<strlen(str);i++) { count=0; //the following code search the six ones in given string for (j=i;j<=(i+5);j++) { if(str[j]=='1') { count++; } } //if there is six ones then folling code execute to bit stuffing after five ones if(count==6) { nl=strlen(str)+2; for (;nl>=(i+5);nl--) { str[nl]=str[nl-1]; } str[i+5]='0'; } } puts(str); getch(); }

Monday, February 9, 2015

C program to read two matrices and add them

#include<stdio.h>
#include<conio.h>
main()
{                
/*declaration of three array with same size */
  int a[10][10],b[10][10],c[10][10],i,j,m,n;
printf("Enter the size of A and B metrix\n");
scanf("%d%d",&m,&n);
     /* reading system */
printf("Enter the elements of A metrix\n");
for(i=0;i<m;i++)
  for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the elements of B metrix\n");
for(i=0;i<m;i++)
  for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
                /* Addition Logic */
for(i=0;i<m;i++)
  for(j=0;j<n;j++)
  c[i][j] = a[i][j]+b[i][j];
clrscr();
           /*display entire matrix */
printf("\nA matrix is :\n\n");
for(i=0;i<m;i++)
{
   for(j=0;j<n;j++)
printf("%4d", a[i][j]);
   printf("\n");
}
printf("\nB matrix is :\n\n");
 
for(i=0;i<m;i++)
{
   for(j=0;j<n;j++)
printf("%4d", b[i][j]);
   printf("\n");
}
 
printf("\nC matrix is :\n\n");
for(i=0;i<m;i++)
{
     for(j=0;j<n;j++)
printf("%4d", c[i][j]);
printf("\n");
}
 
getch();
}

Friday, April 4, 2014

C Program for DFS(Depth first search)

/*Program to perform depth first traversal on a graph */

#include <stdio.h>

int  v , adj[10][10] , visited[10] ;
/* Assuming maximum 9 vertices. */

void dfs(int k);

void main()
{
 int i,j;

 clrscr();

 /* Creating graph of v vertices */
 printf("Enter the number of vertices in the graph: ");
 scanf("%d",&v);

 for(i=1 ; i<=v ; i++)
   for(j=1 ; j<=v ; j++)
   {
     printf("Is vertex %d adjacent to vertex %d ? ",j,i);
     printf("Enter 1 if yes and 0 if no. ");
     scanf("%d",&adj[i][j]);
    }

 /* Making all the vertices as unvisited */
 for(i=1;i<=v;i++)
   visited[i]=0;

 printf("Depth first Search of the graph is as shown:\n");
 for(i=1;i<=v;i++)
   if(visited[i]==0)
     dfs(i);
     /* Initiating dfs from vertex i if it is not visited */

getch();
}

void dfs(int k)
{
  int j;
  visited[k]=1;           /* Visiting vertex k */
  printf("%d ",k);
  for(j=1; j<=v ; j++)
    if(adj[k][j] == 1)
      if(visited[j] ==0)
dfs(j);      /* Initiating dfs from vertex j */
}

/* The first if checks whether vertex j is adjacent to vertex k
     The second if checks whether j is visited or not */

Advanced Java program to Display IP

/*import java.net.*;

public class ShowIP
{
public static void main(String[] args)
{
try
{
InetAddress ad = InetAddress.getLocalHost();
int h = ad.hashCode();
String c = ad.getCanonicalHostName();
String a = ad.getHostAddress();
String n = ad.getHostName();

System.out.println (ad.getHostAddress());
System.out.println (ad.getHostName());
System.out.println ("home-7077aa2ac2" + "'s IP is "+ad.getHostAddress());
System.out.println (ad +"" +h +""+c +"\n" +a +"" +n);
   }
   catch (UnknownHostException ex)
   {
    System.out.println (ex);
   }

}
}
 class pwd {
public static void main(String[] args) {
    String curDir = System.getProperty("user.dir");
    System.out.println (curDir);
    }

}*/
import java.net.InetAddress;
class ShowIP
{
public static void main(String[] args)
{
try
{
InetAddress ad;

ad = InetAddress.getByName("home-7077aa2ac2");
System.out.println (ad);
ad = InetAddress.getLocalHost();
System.out.println (ad);
System.out.println ("\n\n");
System.out.println (ad.getAddress()+"\n"+ad.getCanonicalHostName()+"\n"+
ad.getHostAddress()+"\n"+ad.getHostName()+"\n"+
ad.getLocalHost()+"\n"+ad.isMulticastAddress()+"\n"+
ad.toString());

byte [] b;
b = ad.getAddress();
System.out.println (b);
String str ="";
for (int i = 0; i<b.length; i++)
{
if (i>0)
{
str += ".";
}
str += b[i];
}
System.out.println (str);
   }
   catch (Exception ex)
   {
    System.out.println (ex);
   }
}
}

Thursday, April 3, 2014

java program for calender

import java.util.*;

class MCalendar
{
public static void main(String [] args)
{
// StringCharacterIterator ad = new StringCharacterIterator();

GregorianCalendar cal = new GregorianCalendar();
System.out.println (cal.getTime());
System.out.println (cal.getActualMaximum(Calendar.YEAR));
System.out.println (cal.isLeapYear(Calendar.YEAR));
System.out.println (cal.getTime());
System.out.println (cal.get(Calendar.HOUR));

Random r = new Random();
System.out.println (r.nextInt(44));
System.out.println (r.nextInt(44));
System.out.println (r.nextInt(44));
System.out.println (r.nextInt(44));
System.out.println ("\n\n");
System.out.println (r.nextInt(4));
System.out.println (r.nextInt(4));
System.out.println (r.nextInt(4));
System.out.println (r.nextInt(4));





}

}

Java program for zero one diamond pattern

import java.*;
class ZeroOneDiamond
{
public static void main(String[] args)
{
int n,i,j,k;
if(args.length>0)
{
for(i=0;i<args.length;i++)
{

try
{
n=Integer.parseInt(args[i]);
}
catch(NumberFormatException nfe)
{
System.out.print("\n\nThis is not a valid Integer,\nSo considering it as 0 \n\n");
n=0;
}
if(n<0) n=-n;
for(i=0;i<n;i++)
{
for(j=0;j<40-i;j++)
{
System.out.print(" ");
}
for(k=0;k<=i;k++)
{
if(k%2==0)
{
System.out.print(1+" ");
}
else
{
System.out.print(0+" ");
}
}
System.out.print("\n");
}


for(i=n-1;i>0;i--)
{
for(j=40-i;j>=0;j--)
{
System.out.print(" ");
}
for(k=i;k>0;k--)
{
if(k%2==0)
{
System.out.print(1+" ");
}
else
{
System.out.print(0+" ");
}
}
System.out.print("\n");
}

}
}
}
}

Java program on swing package

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

class MyJFrame extends JFrame
{

JLabel jl1=new JLabel("Name");
JButton jb1=new JButton("Click Now");
JTextField jt1=new JTextField("Type Here",20);
JPanel pnl=new JPanel();
JPanel pnl2=new JPanel();
Container c;
public MyJFrame(String title)
{
super(title);
c=getContentPane();
setSize(300,200);
pnl.add(jl1);
pnl.add(jt1);
pnl2.add(jb1);
c.add("North",pnl);
c.add("South",pnl2);

jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb1)
{
jb1.setText("Your Name is :"+jt1.getText());
//jb1.invalidate();
jt1.setText(jb1.getText());
}
}
});
}
public  static void main(String[] args)
{
JFrame j=new MyJFrame(args[0]);
j.show();
j.setVisible(true);
}
}
}

Java program to demonstrate swing progress bar

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

class SwingProgress extends JFrame implements ActionListener
{
JProgressBar jp;
JButton jb;
JPanel jp1,jp2;

public SwingProgress()
{
jp1=new JPanel();
jp2=new JPanel();
jp=new JProgressBar(0,-5,5);
jb=new JButton("Increment the Progress");
jp1.add(jp);
jp2.add(jb);

this.add(jp1);
this.add(jp2);
jb.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)
{
 
}
public static void main(String[] args)
{
SwingProgress s=new SwingProgress();
s.setSize(300,200);
s.setVisible(true);
}
}

Wednesday, April 2, 2014

Java program for Maximum and Minimum number

import java.lang.*;
import java.io.*;
import java.util.*;
class ConsoleIO
{
private static String line="";
private static BufferedReader br= new BufferedReader (new InputStreamReader(System.in));
private static StringTokenizer toks=new StringTokenizer(line);
public static int readInt() throws NumberFormatException, IOException
{
if(!toks.hasMoreTokens())
{
line=br.readLine();
toks=new StringTokenizer(line);
}
return Integer.parseInt(toks.nextToken());
}
}


class ReadASetOfIntegers extends Object
{
public static void main(String[] args) throws IOException
{
int i,sum,n,max=-1,min=-1;


System.out.print("\r\nInput the size \r\n");
 n=ConsoleIO.readInt();
int[] a=new int[n];
System.out.print("\nEnter "+n+" integers, to Calculate Average.....\r\n");




for(i=0;i<n;i++)
{
a[i]=ConsoleIO.readInt();

if (i==0)
{
max=a[i];
min=a[i];
}
else
if(a[i]>max)
{
max=a[i];
}
else
if (a[i]<min)
{
min=a[i];
}
}
System.out.print("\n\nThe Maximum of all the " +n+" Numbers is : "+ max+ "\n\nThe minimum  of all the " +n+" Numbers is : "+ min+"\n\n");
}
}

Java applet program for List Box

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class listbox extends Applet
{
List lst = new List();
 public listbox()
 {

 lst.add("Mercury");
 lst.add("Venus");
 lst.add("Earth");
 lst.add("JavaSoft");
 lst.add("Mars");
 lst.add("Jupiter");
 lst.add("Saturn");
 lst.add("Uranus");
 lst.add("Neptune");
 lst.add("Pluto");
 lst.setSize(300,200);
 add(lst);
 }
}

Java program to display current time

import mypackages.ConsoleIO.ConsoleIO;
import java.io.*;
class CurrentTime
{
public static void main(String [] srgs)
{
long time;
String s1="",s2="",s3="";

System.out.print("Enter your full name : ");
try
{
s1=ConsoleIO.readString();
s2=ConsoleIO.readString();
s3=ConsoleIO.readString();
}
catch(IOException ioe)
{
}

s1 = s1.toUpperCase();
s2.toUpperCase();
s3.toUpperCase();

System.out.println("Your first name is : "+s1);
System.out.println("Your middle name is : "+s2);
System.out.println("Your last name is : "+s3);

time=System.currentTimeMillis();
System.out.println("Current time is :- "+(int)time/3600000+":"+(int)time/60000+":"+(int)time/1000);

}


}

Java program for queue

import java.util.*;
class Queue
{
private LinkedList l=new LinkedList();

public void insert(Object o)
{

l.add(o);

}
public int size()
{
 return l.size();
}
public Object remove()
{
try{
return l.remove();
}catch(NoSuchElementException n)
{
System.out.print("\nQueue is Empty  ");
}
return null;
}
public Object[] display()
{
Object[] a=new Object[l.size()];

for(int i=0;i<l.size();i++)
a[i]=l.get(i);

return a;
}

}
class test
{
public static void main(String[] args)
{
Queue q=new Queue();
Object[] a;


q.insert(new Integer(1));

q.insert(new Float(1.5));

q.insert("rajesh");

System.out.println("\nThe Size of Queue : "+q.size()+"\n\n");

a=q.display();

for(int i=0;i<a.length;i++)
System.out.println(a[i]);

System.out.print("\n\n");

for(int j=0;j<3;j++)
System.out.println(q.remove());

System.out.println("\nThe Size of Queue : "+q.size()+"\n\n");

System.out.println(q.remove());

a=q.display();
System.out.print("\n\n");
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}

Advanced java program on swing

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

class MyJFrame extends JFrame
{

JLabel jl1=new JLabel("Name");
JButton jb1=new JButton("Click Now");
JTextField jt1=new JTextField("Type Here",20);
JPanel pnl=new JPanel();
JPanel pnl2=new JPanel();
Container c;
public MyJFrame(String title)
{
super(title);
c=getContentPane();
setSize(300,200);
pnl.add(jl1);
pnl.add(jt1);
pnl2.add(jb1);
c.add("North",pnl);
c.add("South",pnl2);

jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb1)
{
jb1.setText("Your Name is :"+jt1.getText());
//jb1.invalidate();
jt1.setText(jb1.getText());
}
}
});
}
public  static void main(String[] args)
{
JFrame j=new MyJFrame(args[0]);
j.show();
j.setVisible(true);
}
}