Applet code to move object

//Applet code to move object form left to right.

import java.awt.*;
import java.applet.*;
import java.lang.Thread;
import java.lang.*;

public class Applet1 extends Applet
{
Thread t;
int i;
int x=14,y=14;

        public void init()
        {
         t=new Thread();
        }
public void paint(Graphics g)
{
for(x=14;x<=getWidth();x=x+5)
        {
g.setColor(Color.BLACK);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.RED);
g.fillOval(x,y,40,40);
repaint();
        try
{
t.sleep(50);
}
catch(Exception e)
{}
        }
        }
}

execution step:--

c:\javac Applet1.java

c:\appletviewer Applet1

0 comments :

C Program To Append Contents Into A File


#include< conio.h >
#include< stdio.h >
#include< process.h >
void main()
{
FILE *fp;
char c;
clrscr();
printf("Contents of the file =>\n");
fp=fopen("demo.txt","r");
while(!feof(fp))
{
c=fgetc(fp);
printf("%c",c);

 }
fp=fopen("demo.txt","a");
if(fp==NULL)
{
printf("file can't append\n");
exit(1);
}
printf("\nEnter the string to append\n");
while(c!='.')
{
c=getche();
fputc(c,fp);
}
fclose(fp);
printf("file contents after appending\n");
fp=fopen("demo.txt","r");
while(!feof(fp))
{
c=fgetc(fp);
printf("%c",c);
}
}

----------------------------------------------------------
OUT PUT:
----------------------------------------------------------
Contents of the file =>
This is c prorgam.

Enter the string to append
I upload C++ prorams soon.

File contents after appending
This is c prorgam.I upload C++ prorams soon.

0 comments :

C Program To Copy The Contents Of A One File To Another File


#include< stdio.h >
#include< conio.h >
#include< stdlib.h >
void main()
 {
FILE *fs,*ft;
char ch;
clrscr();
 fs = fopen("file1.txt","r");
 if(fs==NULL)
  {
 printf("Cannot open source file ! Press key to exit.");
 getch();
 exit(0);
  }
ft = fopen("file2.txt","w");
 if(ft==NULL)
{
printf("Cannot copy file !");
 fclose(fs);
 getch();
 exit(0);
 }
while(1)
 {
ch = getc(fs);
if(ch==EOF)
{
 break;
}
else
putc(ch,ft);
}
printf("File copied succesfully!");
fclose(fs);
fclose(ft);
}

----------------------------------------------------------
OUT PUT:
----------------------------------------------------------

File copied successfully!

0 comments :

C Program To Read And Write The Contents Into Of A File


#include< conio.h >
#include< stdio.h >
#include
void main()
{
FILE *fp;
char c;
clrscr();
fp=fopen("demo.txt","w");
if(fp==NULL)
{
printf("can't open file\n");
exit(1);

}
printf("write data and to stop press =>'.'\n");
while(c!='.')
{
c=getche();
fputc(c,fp);
}
fclose(fp);
printf("\n Read Data from File =>\n");
fp=fopen("demo.txt","r");
while(!feof(fp))
printf("%c",getc(fp));
getch();
}

----------------------------------------------------------
OUT PUT:
----------------------------------------------------------
Write data and to stop press => ’.’
C Programs.
 Read Data from File =>
C Programs.

0 comments :

implement C++ program for abstract classes

#include< iostream.h >
#include< conio.h >
class item
{                                                                                          
private:
int a,b,c,d;
public:
void get();
void multiply();
void sum();
};
void item::get()
{
cout<<"enter a & b :" ;
cin>>a>>b;
}
void item::sum()
{
c=a+b;
cout<<"\n sum is"<< c;
}
void item::multiply()
{
d=a*b;
cout<<"\n multiplication is "<< d;
}
void main()
{
clrscr();
item t;
t.get();
t.sum();
t.multiply();
getch();
}

-------------------------------------------------------
Out put
-------------------------------------------------------
Enter a & b : 5
4
Sum is 9
Multiplication is 20

0 comments :

implement C Program of towers of hanoi


Tower of hanoi is a historical problem, which can be easily expressed using recursion. There are N disks of decreasing size stacked on one needle, and two other empty needles. It is required to stack all the disks onto a second needle in the decreasing order of size. The third needle can be used as a temporary storage. The movement of the disks must confirm to the following rules,

1. Only one disk may be moved at a time
2. A disk can be moved from any needle to any other.
3. The larger disk should not rest upon a smaller one.

#include < stdio.h >
#include < conio.h >

void move ( int, char, char, char ) ;

void main( )
{
    int n = 3 ;
    clrscr( ) ;
    move ( n, 'A', 'B', 'C' ) ;
    getch( ) ;
}

void move ( int n, char sp, char ap, char ep )
{
    if ( n == 1 )
        printf ("\nMove from %c to %c ", sp, ep ) ;
    else
    {
        move ( n - 1, sp, ep, ap ) ;
        move ( 1, sp, ' ', ep ) ;
        move ( n - 1, ap, sp, ep ) ;
    }
}

0 comments :

implement c program for A bubblesort routine


//program for bubblesort routine

# include <stdio.h>
# include <stdlib.h>
void bubblesort(int array[],int size);

void main()
{
int values[10],j;
for(j=0;j<10;j++)
values[j] = rand()%100;
/*unsorted*/
printf("\nUnsorted values.\n");
for(j=0;j<10;j++)
printf("%d ",values[j]);
/*sorted*/
printf("\nSorted values.\n");
bubblesort(values,10);
for(j=0;j<10;j++)
printf("%d ",values[j]);

}

void bubblesort(int array[],int size)
{
int tmp ,i,j;
for(i = 0;i <size;i++)
for(j=0;j < size;j++)
if(array[i] < array[j])
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}

0 comments :

implement c program to check palindrome string


//program to check whether a string is a palindrome

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(void)
{
char a[50],b[50];
clrscr();
printf("Enter the string:");
gets(a);
strcpy(b,a);
strrev(a);
if(strcmp(b,a) == 0)
{
printf("The given string is a palindrome!!!");
}
else
{
printf("The given string is not a palindrome!!!");
}
getch();
}

0 comments :

implement c program to count the no words in file


//Program to count the number of words in an input text file

#define NULL 0
FILE *fpt;
void main()
{
 char name[20],c;
 int nw=0;
 clrscr();
 printf("Enter the name of file to be checked:-  ");
 gets(name);
 fpt=fopen(name,"r");
 if (fpt==NULL)
 {
  printf("ERROR - can/'t open file %s",name);
  getch();
  exit(0);
 }
 else
 {
 while ((c=getc(fpt))!=EOF)
 {
  switch(1)
  {
   case 1:
 if (c==' ')
 {
   point: // do
 //    nw=nw+1-1;
  while((c=getc(fpt))==' ');

  if (c!=' ')
nw=nw+1;
  if(c=='
')nw--;
 }


 //  case 3:
 if(c=='
'){
   goto point;}

  }  } }
 printf("The no. of words in %s is %d.  ",name,nw);
 getch();
}

0 comments :

implement c program to Count Blanks,Tabs and Newlines


//Program to Count Blanks,Tabs and Newlines

#include<stdio.h>
int main(void)
{
        int nb,nt,nl,c;
        nb=nt=nl=0;

while((c=getchar())!=EOF)
{
if(c==' ')
++nb;
if(c==' ')
++nt;
if(c=='
')
++nl;
}
printf("No. of Blanks is %d,No. of Tabs is %d and No. of Newlines is %d",nb,nt,nl);
return 0;
}

0 comments :

implement c program for Merge sort


//implement program for Merge sort

#include<stdio.h>

void getdata(int arr[],int n)
{    int i;
      printf("enter the data:");
  for(i=0;i<n;i++)
    {
     scanf("%d",&arr[i]);
    }
}

void display(int arr[],int n)
{
 int i;
 printf(" ");
 for(i=0;i<n;i++)
    {
     printf("%d ",arr[i]);
    }
 getchar();
}

void sort(int arr[],int low,int mid,int high)
{
 int i,j,k,l,b[20];
 l=low;
 i=low;
 j=mid+1;
 while((l<=mid)&&(j<=high))
   {
    if(arr[l]<=arr[j])
      {
       b[i]=arr[l];
       l++;
      }
    else
      {
       b[i]=arr[j];
       j++;
      }
    i++;
   }
 if(l>mid)
   {
    for(k=j;k<=high;k++)
       {
        b[i]=arr[k];
        i++;
       }
   }
 else
   {
    for(k=l;k<=mid;k++)
       {
        b[i]=arr[k];
        i++;
       }
   }
 for(k=low;k<=high;k++)
    {
     arr[k]=b[k];
    }
}

void partition(int arr[],int low,int high)
{
 int mid;
 if(low<high)
   {
    mid=(low+high)/2;
    partition(arr,low,mid);
    partition(arr,mid+1,high);
    sort(arr,low,mid,high);
   }
}
void main()
{
 int arr[20];
 int n;
 printf("Enter number of data:");
 scanf("%d",&n);
 getdata(arr,n);
 partition(arr,0,n-1);
 display(arr,n);
 getchar();
}

0 comments :

implement c program for Fibonacci series


//program to prints the Fibonacci series

#include<stdio.h>
#include<conio.h>
void main(void)
{
int i,j,k,n;
clrscr();
i=0;
j=1;
        printf("Fibonacci series is :--");
printf("%d %d ",i,j);
for(n=0;n<=5;n++)
{
k=i+j;
i=j;
j=k;
printf("%d ",k);
}
getch();
}

0 comments :

Implement C program for Character Generation


#include<stdio.h>
#include<conio.h>

#include<graphics.h>

void main()
{
 
int gd=DETECT,gm,i,j;
int a[20][20]=

{0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0},
{0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0},
{0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0}};

0 comments :

Implement C program for Scan Line Polygon Filling


#include <stdio.h>
#include <conio.h>
#include <graphics.h>

main()
{
int n,i,j,k,gd,gm,dy,dx;
int x,y,temp;
int a[20][2],xi[20];
float slope[20];

clrscr();
printf("\n\n\tEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");

for(i=0;i<n;i++)
{
printf("\tX%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}

a[n][0]=a[0][0];
a[n][1]=a[0][1];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");

5 comments :

C program for 3D Tranformations


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>

int xp[2],yp[2],z;
void display();
void translate();
void scaling();
void rotation();
void matrixmul(int [4][4]);

void main()
{
int gd=DETECT,gm;
int ch,i;
initgraph(&gd,&gm,"c:\\tc\\bgi");
for(i=0;i<2;i++)
{
printf("\nEnter X-coordinate of vertex %d : ",i+1);
scanf("%d",&xp[i]);
printf("\nEnter Y-coordinate of vertex %d : ",i+1);
scanf("%d",&yp[i]);
}
printf("\nEnter The Z-axis For 3d Figure : ");
scanf("%d",&z);
clrscr();
cleardevice();
display(xp,yp);
getche();

0 comments :

implement Perspective projection of 3D objects


#include<stdio.h>
#include<math.h>
#include<graphics.h>
main()
{
int x1,y1,x2,y2,gd,gm;
int ymax,a[4][8];
float par[4][4],b[4][8];
int i,j,k,m,n,p;
int xp, yp, zp, x, y, z;

a[0][0] = 100; a[1][0] = 100; a[2][0] = -100;
a[0][1] = 200; a[1][1] = 100; a[2][1] = -100;

a[0][2] = 200; a[1][2] = 200; a[2][2] = -100;
a[0][3] = 100; a[1][3] = 200; a[2][3] = -100;

a[0][4] = 100; a[1][4] = 100; a[2][4] = -200;
a[0][5] = 200; a[1][5] = 100; a[2][5] = -200;

a[0][6] = 200; a[1][6] = 200; a[2][6] = -200;
a[0][7] = 100; a[1][7] = 200; a[2][7] = -200;

0 comments :

implement Oblique projection of 3D objects


#include<stdio.h>
#include<math.h>
#include<graphics.h>
main()
{
int x1,y1,x2,y2,gd,gm;
int ymax,a[4][8];
float par[4][4],b[4][8];
int i,j,k,m,n,p;
double L1,phi;

a[0][0] = 100; a[1][0] = 100; a[2][0] = 100;
a[0][1] = 200; a[1][1] = 100; a[2][1] = 100;

a[0][2] = 200; a[1][2] = 200; a[2][2] = 100;
a[0][3] = 100; a[1][3] = 200; a[2][3] = 100;

a[0][4] = 100; a[1][4] = 100; a[2][4] = 200;
a[0][5] = 200; a[1][5] = 100; a[2][5] = 200;

a[0][6] = 200; a[1][6] = 200; a[2][6] = 200;
a[0][7] = 100; a[1][7] = 200; a[2][7] = 200;

phi = (double) (3.14*45.0)/180 ;
L1 = 0.5;

0 comments :

implement Mid-Point Ellipse Drawing Algorithm


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <dos.h>

int main(void)
{
int gd=DETECT,gm;
int cenx,ceny;
float Pk,a,b,x,y;
clrscr();

printf("\n\n Enter 'a' and 'b': ");
scanf("%f%f",&a,&b);
initgraph(&gd,&gm,"c:\\tc\\bgi");
cenx=getmaxx()/2;
ceny=getmaxy()/2;

Pk=b*b-b*a*a+0.25*a*a;
x=0;
y=b;
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);

while (2*x*b*b <= 2*y*a*a)
{
if (Pk<0)
{
x=x+1;
y=y;
Pk=Pk+2*x*b*b+3*b*b;
}
else
{
x=x+1;
y=y-1;
Pk=Pk+2*x*b*b+3*b*b-2*y*a*a+2*a*a;
}
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);
delay(40);
}

1 comments :

implement Liang Barsky Line Clipping Algorithm


#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
int gd, gm ;
int x1 , y1 , x2 , y2 ;
int wxmin,wymin,wxmax, wymax ;
float u1 = 0.0,u2 = 1.0 ;
int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ;
float r1 , r2 , r3 , r4 ;
int x11 , y11 , x22 , y22 ;
clrscr();
printf("Enter the windows left xmin , top boundry ymin\n");
scanf("%d%d",&wxmin,&wymin);
printf("Enter the windows right xmax ,bottom boundry ymax\n");
scanf("%d%d",&wxmax,&wymax);
printf("Enter line x1 , y1 co-ordinate\n");
scanf("%d%d",&x1,&y1);
printf("Enter line x2 , y2 co-ordinate\n");
scanf("%d%d",&x2,&y2);
printf("liang barsky express these 4 inequalities using lpk<=qpk\n");
p1 = -(x2 - x1 ); q1 = x1 - wxmin ;
p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ;
p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;
p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;
printf("p1=0 line is parallel to left clipping\n");
printf("p2=0 line is parallel to right clipping\n");
printf("p3=0 line is parallel to bottom clipping\n");
printf("p4=0 line is parallel to top clipping\n");

0 comments :

Bresenham Circle Drawing algorithm


# include<stdio.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>

void main()
{
 int gd=DETECT,gm;
 int r,x,y,p,xc=320,yc=240;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();

printf("Enter the radius ");
scanf("%d",&r);

x=0;
y=r;
putpixel(xc+x,yc-y,1);
p=3-(2*r);
for(x=0;x<=y;x++)
{
if (p<0)
{
y=y;
p=(p+(4*x)+6);
}
else
{
y=y-1;
p=p+((4*(x-y)+10));
}

putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,8);

}
getch();
closegraph();
}



0 comments :