HOT C PROGRAMS BLOGSPOT

HOT C PROGRAMS BLOGSPOT

Click On Related Results For More Information

Sunday 14 October 2012

Multiplication Table

 /*  Multiplication Multiply Table Tables  */

#include<stdio.h>
#include<conio.h>
void main()
{
 int num,i,a;
 clrscr();
 printf("Enter a number\t");
 scanf("%d",&num);
 for(i=1;i<=10;i++)
 {
  a=num*i;
  printf("    %d\n",a);
 }
 getch();
}




Saturday 13 October 2012

Graphics Ellipse C Program

 /*  Works on XP */

#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");  /*Sometimes it may be  "C:\\TC\\BGI"  , It depends machine to mac..*/
clrscr();
setcolor(5);

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


Pk=(x+0.5)*(x+0.5)*b*b+(y-1)*(y-1)*a*a-a*a*b*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 (y>0)
{
if (Pk>0)
{
x=x;
y=y-1;
Pk=Pk-2*y*a*a+3*a*a;
}
else
{
x=x+1;
y=y-1;
Pk=Pk-2*y*a*a+3*a*a+2*x*b*b+2*b*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);
delay(40);
}
gotoxy(1,25);
printf ("\npress any key to exit.");
getch();
closegraph();
return 0;
}




Networking Client Server C Program

/*  Networking Network Client Server C Program  */


/*  Posting more than one Program Of Each, Any one may run. Should Try all . Some Debugging may require. Program had run using RED HAT LINUX.  Dont know whether runs on any other OS or not*/

/*   Works on  RED HAT LINUX  */

Client Program No1.

#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<unistd.h>
#include<string.h>
#include<sys/time.h>

#define REMOTE_SERVER_PORT 1500

int main(int argc,char *argv[])
{ char msg[10];
 int sd,rc,i,client_len;
 struct sockaddr_in client_address,remote_server_address;
 if(argc<3) { printf("\n Number of arguments is less than <3 \n");exit(1);}
 remote_server_address.sin_family=AF_INET;
 remote_server_address.sin_addr.s_addr=inet_addr(argv[1]);
 remote_server_address.sin_port=htons(REMOTE_SERVER_PORT);
 sd=socket(AF_INET,SOCK_DGRAM,0);
 if(sd<0) { printf("\n Cannot open socket \n ");exit(1); }
 client_address.sin_family=AF_INET;
 client_address.sin_addr.s_addr=htonl(INADDR_ANY);
 client_address.sin_port=htons(0);
 rc=bind(sd,(struct sockaddr*)&client_address,sizeof(client_address));
if(rc<0) { printf("\n Cannot bind port \n"); exit(1);}

rc=sendto(sd,argv[2],strlen(argv[2]),0,(struct sockaddr*)&remote_server_address,         sizeof(remote_server_address));
if(rc<0) { printf("\n Cannot send data \n "); close(sd);exit(1);}

while(1)
{ memset(msg,0x0,10);
  client_len=sizeof(remote_server_address);
  rc=recvfrom(sd,msg,2,0,(struct sockaddr*)&remote_server_address,&client_len);
 
 if(rc<0) { printf("\n Cannto Receive data \n ");
 continue;
  }

printf("\n\n Result Received From Server % d \n ",msg[0]);
}
close(sd);
return 1;
}









Client Program No 2


#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<unistd.h>
#include<string.h>
#include<sys/time.h>

#define REMOTE_SERVER_PORT 1500

int main(int argc,char *argv[])
{ char msg[100];
 int sd,rc,i,client_len;
 struct sockaddr_in client_address,remote_server_address;
 if(argc<3) { printf("\n Number of arguments is less than <3 \n");exit(1);}
 remote_server_address.sin_family=AF_INET;
 remote_server_address.sin_addr.s_addr=inet_addr(argv[1]);
 remote_server_address.sin_port=htons(REMOTE_SERVER_PORT);
 sd=socket(AF_INET,SOCK_DGRAM,0);
 if(sd<0) { printf("\n Cannot open socket \n ");exit(1); }
 client_address.sin_family=AF_INET;
 client_address.sin_addr.s_addr=htonl(INADDR_ANY);
 client_address.sin_port=htons(0);
 rc=bind(sd,(struct sockaddr*)&client_address,sizeof(client_address));
if(rc<0) { printf("\n Cannot bind port \n"); exit(1);}

rc=sendto(sd,argv[2],strlen(argv[2]),0,(struct sockaddr*)&remote_server_address,         sizeof(remote_server_address));
if(rc<0) { printf("\n Cannot send data \n "); close(sd);exit(1);}

while(1)
{ memset(msg,0x0,100);
  client_len=sizeof(remote_server_address);
  rc=recvfrom(sd,msg,2,0,(struct sockaddr*)&remote_server_address,&client_len);
 
 if(rc<0) { printf("\n Cannto Receive data \n ");
 continue;
  }
//printf("\n Rc = %d",rc);
if(rc==2)
  { printf("\n\n Result Received From Server % d \n ",msg[0]); break;}
}
close(sd);
return 1;
}




























Server Program No. 1


#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>

#define LOCAL_SERVER_PORT 1500

int main(int argc, char *argv[])
 { char msg[10],x[2];
   int sd,rc,i,client_len,a,b,c;
   struct sockaddr_in client_address,server_address;
   sd=socket(AF_INET,SOCK_DGRAM,0);
   if(sd<0)
     { printf("\n Can not open Socket \n ");
       exit(1);
     }
   server_address.sin_family=AF_INET;
   server_address.sin_addr.s_addr=htonl(INADDR_ANY);
   server_address.sin_port=htons(LOCAL_SERVER_PORT);
   rc=bind(sd,(struct sockaddr *) &server_address,sizeof(server_address));
   if(rc<0)
          { printf("\n Can not bind port number \n ");
              exit(1);
            }
   while(1)
    { memset(msg,0x0,10);
      client_len=sizeof(client_address);
      rc=recvfrom(sd,msg,10,0,(struct sockaddr *)&client_address,&client_len);
      if(rc<0) { printf("\n Cannot receive data \n "); continue;}
      printf("\n Received from Client %d  %d  %d \n",msg[0],msg[1],msg[2]);
      a=msg[0];b=msg[2];
      a=a-48;b=b-48;c=a+b;x[0]=c;
      printf("\n In the Server the Result of Computation is %d \n\n",x[0]);
      break;
    }
  rc=sendto(sd,x,2,0,(struct sockaddr*)&client_address,sizeof(client_address));
  if(rc<0) { printf("\n Can not send data \n ");
                close(sd); exit(1);}
  return 1;
}



Server Program No. 2


#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>

#define LOCAL_SERVER_PORT 1500

int main(int argc, char *argv[])
 { char msg[10],x[2];
   int sd,rc,i,client_len,a,b,c;
   struct sockaddr_in client_address,server_address;
   sd=socket(AF_INET,SOCK_DGRAM,0);
   if(sd<0)
     { printf("\n Can not open Socket \n ");
       exit(1);
     }
   server_address.sin_family=AF_INET;
   server_address.sin_addr.s_addr=htonl(INADDR_ANY);
   server_address.sin_port=htons(LOCAL_SERVER_PORT);
   rc=bind(sd,(struct sockaddr *) &server_address,sizeof(server_address));
   if(rc<0)
          { printf("\n Can not bind port number \n ");
              exit(1);
            }
   while(1)
    { memset(msg,0x0,10);
      client_len=sizeof(client_address);
      rc=recvfrom(sd,msg,10,0,(struct sockaddr *)&client_address,&client_len);
      if(rc<0) { printf("\n Cannot receive data \n "); continue;}
      printf("\n Received from Client %d  %d  %d \n",msg[0],msg[1],msg[2]);
      a=msg[0];b=msg[2];
       a=a-48;b=b-48;c=a+b;x[0]=c;
       if(msg[1]=='+') c=a+b;
         else if(msg[1]=='-') c=a-b;
           else if(msg[1]=='*') c=a*b;
            else if(msg[1]=='/') c=a/b;
             else if(msg[1]=='%') c=a%b;
             x[0]=c;
      printf("\n In the Server the Result of Computation is %d \n\n",x[0]);
      break;
    }
  rc=sendto(sd,x,2,0,(struct sockaddr*)&client_address,sizeof(client_address));
  if(rc<0) { printf("\n Can not send data \n ");
                close(sd); exit(1);}
  return 1;
}

Graphics Line Rotation C Program

 /*   .......Works on Win XP      */
 /*   Graphics Line Rotation Rotate C Program  */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define pi 3.1456
void rotation(int,int,float);
int x1,y1,x2,y2,x3,y3,xf,yf,a;
float b;
void rotation(int xf,int yf,float b)
{
setcolor(GREEN);
line((x1*cos(b)-y1*sin(b)-xf*cos(b)+yf*sin(b)+xf),(x1*sin(b)+y1*cos(b)-xf*sin(b)-yf*cos(b)+yf),(x2*cos(b)-y2*sin(b)-xf*cos(b)+yf*sin(b)+xf),(x2*sin(b)+y2*cos(b)-xf*sin(b)-yf*cos(b)+yf));
line((x2*cos(b)-y2*sin(b)-xf*cos(b)+yf*sin(b)+xf),(x2*sin(b)+y2*cos(b)-xf*sin(b)-yf*cos(b)+yf),(x3*cos(b)-y3*sin(b)-xf*cos(b)+yf*sin(b)+xf),(x3*sin(b)+y3*cos(b)-xf*sin(b)-yf*cos(b)+yf));
line((x1*cos(b)-y1*sin(b)-xf*cos(b)+yf*sin(b)+xf),(x1*sin(b)+y1*cos(b)-xf*sin(b)-yf*cos(b)+yf),(x3*cos(b)-y3*sin(b)-xf*cos(b)+yf*sin(b)+xf),(x3*sin(b)+y3*cos(b)-xf*sin(b)-yf*cos(b)+yf));
}
void main(void)
{
int gd=DETECT,gm;
clrscr();
printf("enter 1st point=");
scanf("%d %d",&x1,&y1);
printf("\n enter 2nd point=");
scanf("%d %d",&x2,&y2);
printf("\n enter3rd point=");
scanf("%d %d",&x3,&y3);
printf("enter reference point=");
scanf("%d %d",&xf,&yf);
printf("enter the angle=");
scanf("%d",&a);
b=(pi*a)/180.0;
initgraph(&gd,&gm,"c:\\tc\\bgi");   /*Sometimes it may be  "C:\\TC\\BGI"  , It depends machine to mac..*/
setcolor(WHITE);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3) ;
line(x1,y1,x3,y3);
rotation(xf,yf,b);
getch();
closegraph();
}





Graphics Reverse Diagonal Line C Program Figure

 /*   .......Works on Win XP      */


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main(void)
{
   int gd=DETECT,gm,x=400,y=30;
   clrscr();
   initgraph(&gd,&gm,"c:\\tc\\bgi");   /*Sometimes it may be  "C:\\TC\\BGI"  , It depends machine to mac..*/
   for(y=90;y<400;y++)
   {
      putpixel(x,y,RED);
      x--;
   }
   getch();
   closegraph();
   }





Thursday 11 October 2012

Graphics Point Clipping C Program

/* .......Works on Win XP */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int tlx,tly,brx,bry,px,py;
void point_clip()
{ int wxmin,wymin,wxmax,wymax;
wxmin=tlx;
wxmax=brx;
wymin=tly;
wymax=bry;
if(px>=wxmin&&px<=wxmax)
if(py>=wymin&&py<=wymax)
putpixel(px,py,RED);
getch();
closegraph();
}
void main(void)
{ int gd=DETECT,gm,xc,yc,r;
clrscr();
printf("Enter the top left coordinate");
scanf("%d%d",&tlx,&tly);
printf("Enter the bottom right coordinate");
scanf("%d%d",&brx,&bry);
printf("\n Enter the point");
scanf("%d%d",&px,&py);
 initgraph(&gd,&gm,"c:\\tc\\bgi");  /*Sometimes it may be "C:\\TC\\BGI" , It depends machine to mac..*/
setbkcolor(YELLOW);
setcolor(RED);
rectangle(tlx,tly,brx,bry);
point_clip();
}






Graphics MidPoint Mid Point Circle C Program

/* .......Works on Win XP */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int x1,y1;
void pt(int x,int y)
{putpixel(x+x1,y+y1,RED);
putpixel(-x+x1,y+y1,RED);
putpixel(-y+x1,x+y1,RED);
putpixel(-y+x1,-x+y1,RED);
putpixel(-x+x1,-y+y1,RED);
putpixel(x+x1,-y+y1,RED);
putpixel(y+x1,-x+y1,RED);
putpixel(y+x1,x+y1,RED);
}
void main()
{
int r,gd=DETECT,gm,x,y,d;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");   /*Sometimes it may be "c:\\tc\\bgi" , It depends machine to mac..*/
printf("enter radius");
scanf("%d",&r);
printf("enter center of the circle-");
scanf("%d%d",&x1,&y1);
x=0;
y=r;
d=1-r;
while(x<=y)
{
pt(x,y);
if(d<0)
d=d+2*x+3;
else
{d=d+2*(x-y)+5;
y--;

}
x++;
}
getch();
closegraph();
}

Graphics Flood Fill C Program

 /* .......Works on Win XP */


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void flood_fill(int x,int y,int f,int o)
{ int current;
current=getpixel(x,y);
delay(2);
if(current==o)
{putpixel(x,y,f);
flood_fill(x,y+1,f,o);
flood_fill(x,y-1,f,o);
flood_fill(x+1,y,f,o);
flood_fill(x-1,y,f,o);;
}
}
void main()
{int gd=DETECT,gm,x,y,r,xc,yc,f,o;
clrscr();
printf("\nEnter the seed point");
scanf("%d%d",&x,&y);
printf("\nEnter the center & radius of the circle");
scanf("%d%d%d",&xc,&yc,&r);
printf("\nEnter the fill and old color");
scanf("%d%d",&f,&o);
initgraph(&gd,&gm,"C:\\TC\\BGI");   /*Sometimes it may be "c:\\tc\\bgi" , It depends machine to mac..*/
setcolor(4);
circle(xc,yc,r);
flood_fill(x,y,f,o);
getch();
closegraph();
}


 /* Seed Pt and Center must be same,  Circle radius should be  less or equal to 30 otherwise machine may hang,  old colour be 0 always */










Graphics DAA Line Drawing C Program

/* .......Works on Win XP */

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

float abs(float);
float round(float);
int main() {
int gd=DETECT,gm,steps,k,dx,dy,x1,y1,x2,y2;
float xincr,yincr,x,y;
clrscr();
printf("Enter the co-ordinates of the starting point : ");
scanf("%d%d",&x1,&y1);
printf("Enter the co-ordinates of the end point : ");
scanf("%d%d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
x=x1;
y=y1;
if(abs(dx) > abs(dy)) {
steps=abs(dx);
}
else {
steps=abs(dy);
}
xincr=(float)dx/steps;
yincr=(float)dy/steps;
 initgraph(&gd,&gm, "C:\\TC\\BGI");   /*Sometimes it may be "c:\\tc\\bgi" , It depends machine to mac..*/
putpixel(round(x),round(y),RED);
for(k=0;k<steps;k++) {
x=x+xincr;
y=y+yincr;
putpixel(round(x),round(y),RED);
}
getch();
closegraph();
return 0;
}

float abs(float x) {
if(x>0) {
return x;
}
else {
return (-1*x);
}
}

float round(float x) {
int y;
y=(int)x;
if(x<(y+0.5)) {
return y;
}
else {
return (y+1);
}
}

Graphics Bresenham Line Drawing C Program

/* .......Works on Win XP */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x,y,d,x1,x2,y1,y2,dx,dy,dt,ds;
clrscr();
 initgraph(&gd,&gm,"C:\\TC\\BGI");  /*Sometimes it may be "c:\\tc\\bgi" , It depends machine to mac..*/
printf("Enter start and end coordinates x1 y1 & x2 y2 ");
scanf("%d%d%d%d",&x1,&y1,x2,y2);

dx=x2-x1;
dy=y2-y1;
dt=2*(dy-dx);
ds=2*dy;
d=2*dy-dx;
putpixel(x1,y1,YELLOW);
while(x1<x2)
{
x1++;
if(d<0)
d=d+ds;
else
{
y1++;
d=d+dt;
}
putpixel(x1,y1,YELLOW);
}
getch();
closegraph();
}