Thursday, 26 May 2011

Chat Program in C


#include <stdio.h>


#include<conio.h>


#include<process.h>


#include<string.h>






char c[30],ch[70];


FILE *stream,*stream1;






void read(void)


{ stream1 = fopen("//computer21/s/read.txt","r");


       fread(ch,1000,1,stream1);


 printf("%s",ch);


fclose(stream1); }






void clearhist()


{ stream=fopen("//computer21/s/write1.txt","w");


     fprintf(stream," ");


     fclose(stream); }










void main ()


{  int i,d=0;


   char temp,name[10];


   clrscr ();


   gotoxy(10,12);


   printf("Enter Name:");


   scanf("%s",name);


   clrscr ();


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


      { clrscr ();


read();


gotoxy(1,21);


printf("--------------Press ENTER Without Message to Check For New
Message--------------");


gotoxy(1,23);


printf("Enter the Message(30 chars max) : ");


gets(c);


//checking message


if((c[0]=='e')&&(c[1]=='x')&&(c[2]=='i')&&(c[3]=='t'))


  {  clearhist ();


     exit(0);  }


 
if(c[0]!='    //checking message for "enter"

Create a Calender


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


unsigned long days=0;
//stores the days elapsed since 01.01.1899
void display(int n)
//contains the number of days to display
{
int i, column, k, flag=0, j;
printf("Sun   Mon   Tues  Wed   Thur  Fri  Sat
");
for(i=1; i<=n; i++)
{
k=days%7;//remainder gives the starting day of each month
if(flag==0)
{
for(j=1; j<=k; j++)//controls tabs of first week
printf(" ");
flag=1;//ensures that block is only executed once
column=k;
}
printf("%d ", i);
column++;
if(column%7==0)//prints new line at the end of each week
printf("
");
}
printf("
Press any key to continue


");
getch();
}


void calculate(int year)
//function calculates no. of days elapsed since 1899
{
int i, month;
for(i=1899; i<year; i++)
        //1899 chosen because Jan 1, 1899 is a Sunday
{
if((i%400==0)?1:((i%100==0)?0:((i%4==0)?1:0)))


/*This is because a leap year does not strictly fall on every
fourth year.  If a year is divisible by 4, then it is a leap
year, but if that year is divisible by 100, then it is
not a leap year.  However, if the year is also divisible by
400, then it is a leap year.  Eg: 1900 is not a leap year*/


days+=366;
else
days+=365;
}
for(month=1; month<=12; month++)
{
printf("




");
switch(month)
        /*switch case used to display each month and
increment no. of days*/
{
case 1: printf(" JANUARAY %d


", year);
display(31);
days+=31;
break;
case 2: printf(" FEBURARY %d


", year);
if((year%400==0)?1:((year%100==0)?0:((year%4==0)?1:0)))
{
display(29);
days+=29;
}
else
{
display(28);
days+=28;
}
break;
case 3: printf(" MARCH %d


", year);
display(31);
days+=31;
break;
case 4: printf(" APRIL %d


", year);
display(30);
days+=30;
break;
case 5: printf(" MAY %d


", year);
display(31);
days+=31;
break;
case 6: printf(" JUNE %d


", year);
display(30);
days+=30;
break;
case 7: printf(" JULY %d


", year);
display(31);
days+=31;
break;
case 8: printf(" AUGUST %d


", year);
display(31);
days+=31;
break;
case 9: printf(" SEPTEMBER %d


", year);
display(30);
days+=30;
break;
case 10: printf(" OCTOBER %d


", year);
display(31);
days+=31;
break;
case 11: printf(" NOVEMBER %d


", year);
display(30);
days+=30;
break;
case 12: printf(" DECEMBER %d


", year);
display(31);
days+=31;
break;
}
}
}


void main()
{
char ch[10];
int i, year, choice;
do
{
clrscr();
days=0;
printf("Enter the year in 'yyyy' format:
");
scanf("%s", ch);//stores input first as a string
for(i=0; i<strlen(ch); i++)
if(ch[i]<'0' || ch[i]>'9')//checks for invalid inputs
{
printf("


Invalid Year!");
printf("


END OF PROGRAM");
getch();
exit(0);
}
year = atoi(ch);
        //converts the year from string to integer datatype
clrscr();
printf("


Calender for Year %d", year);
printf("
**********************


");
calculate(year);
        //calls function to calculate no. of days elapsed

        printf("
*******************************************
");
printf("


Press 1 to continue, 2 to exit
");
scanf("%d", &choice);
}while(choice==1);
clrscr();
printf("


END OF PROGRAM");
getch();
}

Analog and Digital Clock


# include <graphics.h>
# include <stdio.h>
# include <dos.h>
# include <string.h>
# include <math.h>
# define pi 3.141592654
int x;
float x30,x60,y30,y60;
void paint1()
{
setcolor(GREEN);
outtextxy(297,60," ");
outtextxy(297,332," ");
outtextxy(435,198," ");
outtextxy(160,198," ");
setcolor(RED);
outtextxy(375,85,"è");
outtextxy(420,260,"è");
outtextxy(170,135,"è");
outtextxy(220,310,"è");
setcolor(LIGHTBLUE);
outtextxy(420,135,"ì");
outtextxy(375,310,"ì");
outtextxy(220,83,"ì");
outtextxy(175,260,"ì");
}
void paint2()
{
setcolor(RED);
outtextxy(297,60,"è");
outtextxy(297,332,"è");
outtextxy(435,198,"è");
outtextxy(160,198,"è");
setcolor(LIGHTBLUE);
outtextxy(375,85,"ì");
outtextxy(420,260,"ì");
outtextxy(170,135,"ì");
outtextxy(220,310,"ì");
setcolor(GREEN);
outtextxy(420,135," ");
outtextxy(375,310," ");
 outtextxy(220,83," ");
outtextxy(175,260," ");
}
void paint3()
{
setcolor(LIGHTBLUE);
outtextxy(297,60,"ì");
outtextxy(297,332,"ì");
outtextxy(435,198,"ì");
outtextxy(160,198,"ì");
setcolor(GREEN);
outtextxy(375,85," ");
outtextxy(420,260," ");
outtextxy(170,135," ");
outtextxy(220,310," ");
setcolor(RED);
outtextxy(420,135,"è");
outtextxy(375,310,"è");
outtextxy(220,83,"è");
outtextxy(175,260,"è");
}
void graph()
{
cleardevice();
setcolor((++x)%15+1);
if(x%2)
outtextxy(180,10,"X");
else
outtextxy(130,20,"X");
setcolor(GREEN);
circle (300,200,150);
circle(300,200,2);
setcolor(14);
circle (300,200,100);
setcolor(3);
circle (300,200,120);
setcolor(RED);
circle(300,50,2);
circle(450,200,2);
 circle(150,200,2);
circle(300,350,2);
setcolor(BLUE);
circle(300+x30,200+y30,2);//1
circle(300-x30,200+y30,2);//11
circle(300+x30,200-y30,2);//5
circle(300-x30,200-y30,2);//7


circle(300+x60,200+y60,2);//2
circle(300+x60,200-y60,2);//4
circle(300-x60,200-y60,2);//8
circle(300-x60,200+y60,2);//10
if(x%2)
{
setcolor(x);
outtextxy(300+x30*1.1,200+y30*1.1,"1");//1
outtextxy(300-x30*1.1,200+y30*1.1,"11");//11
outtextxy(300+x30*1.1,200-y30*1.1,"5");//5
outtextxy(300-x30*1.1,200-y30*1.1,"7");//7


outtextxy(300+x60*1.1,200+y60*1.1,"2");//2
outtextxy(300+x60*1.1,200-y60*1.1,"4");//4
outtextxy(300-x60*1.1,200-y60*1.1,"8");//8
outtextxy(300-x60*1.15,200+y60*1.1,"10");//10
outtextxy(460,200,"3");
outtextxy(300,360,"6");
outtextxy(140,200,"9");
outtextxy(290,40,"12");
}
}


void plot(int h , int m , int s)
{
char a[2],b[2],c[2];
setcolor( LIGHTCYAN );
itoa(h,a,10);
outtextxy(450,100,a);
outtextxy(470,100,":");


itoa(m,a,10);
outtextxy(490,100,a);
outtextxy(510,100,":");


itoa(s,a,10);
outtextxy(530,100,a);
setcolor( LIGHTGREEN );
if( h<13 )
outtextxy( 290 , 250 , "AM");
else
outtextxy( 290 , 250, "PM");
h=h%12;
setlinestyle(SOLID_LINE , 1 , 3);
setcolor( RED );
line(300,200,300+floor(100*cos(((h*5+m/10.0)*6-90)*pi/180)),
200+floor(100*sin(((h*5+m/10.0)*6-90)*pi/180)));
setcolor(BLUE);
setlinestyle(SOLID_LINE,1,3);
line(300,200,300+floor(120*cos((m*6-90)*pi/180)),
200+floor(120*sin((m*6-90)*pi/180)));
setcolor(MAGENTA);
setlinestyle(SOLID_LINE,1,1);
line(300,200,300+floor(120*cos((s*6-90)*pi/180)),
200+floor(120*sin((s*6-90)*pi/180)));
}


void timexy()
{
int h,m,s;
struct time t;
while(1)
{
//clrscr();
  cleardevice();
graph();
gettime(&t);
//printf("




The current time is:
%2d:%02d:%02d.%02d
",
//       t.ti_hour, t.ti_min, t.ti_sec,t.ti_hund);
h=t.ti_hour;
m=t.ti_min;
s=t.ti_sec;
if(s%3==0)
paint1();
else if(s%3==1)
     paint3();
     else paint2();
plot(h,m,s);
delay(995);
}
}
main()
{
int i=DETECT,m;
char a[100],b[10]="jalpari";
x30=floor(150*cos((30-90)*pi/180));
y30=floor(150*sin((30-90)*pi/180));
x60=floor(150*cos((60-90)*pi/180));
y60=floor(150*sin((60-90)*pi/180));
initgraph(&i,&m,"d:\tc\bgi");
i=0;
printf("
PASSWORD==>");
do
{
a[i]=getch();
if(!(a[i]==(char)13))
{
printf("vasim");
i++;
}
else {a[i]='

The Switch Statement

The switch statement is similar to the else-if construct but has more flexibility and a clearer format. Let's create a four function calculator using switch statement:


included files
void main()
{
 float num1=1.0,num2=1.0;
 char operator;
 while(!(num1==0.0 && num2==0.0))
 {
  printf("Type number, operator, number\n");
  scanf("%f %c %f",&num1,&operator,&num2);
  switch(operator)
   {
    case'+':
    printf(" = %f",num1 + num2);
    break;
    case'-':
    printf(" = %f",num1 - num2);
    break;
    case'*':
    printf(" = %f",num1 * num2);
    break;
    case'/':
    printf(" = %f"num1 / num2);
    break;
    default:
     printf("Unknown Operator!");
   } 
 printf("\n\n")
 }
getch();
}


In the above code, the statement 'case' is working as 'if', while 'default' is working as 'else' statement...


If you have any question, you can ask me freely!

Wednesday, 25 May 2011

if-else Statement

It is a conditional expression and easy to use.


#include<stdio.h> 
#include<conio.h>
void main()
{
 char ch;
 clrscr();
 ch=getche();
 if(ch=='y')
 printf("\nYou typed y");
 else
 printf("\nYou didn't type y");
 getch();
}


In the above code when you type 'y', the output will be "You typed y" other it will print "You didn't type y".


If you have any question, you can ask me freely!

Program to count words and characters in a sentence

#include<stdio.h>
#include<conio.h>
void main()
{
 int wordcnt=0;
 int charcnt=0;
 char ch;
 clrscr();
 printf("Type a sentence: ");
 while((getche()) != '\r')
  {
   charcnt++;
   if(ch==' ')
   wordcnt++;
  }
 printf("\nTotal characters are: %d",charcnt);
 printf("\nTotal words are: %d",wordcnt+1);
 getch(); 


}

Tuesday, 24 May 2011

The do-while Loop / How to restart the program?

#include<stdio.h>
#include<conio.h>
void main()
{
 int num;
 char ch;
 clrscr();
 do{
 printf("Enter a number");
 scanf("%d",&num);
 printf("\n\n");
 printf("You entered %d",num);
 printf("Press 'y' to restart again?");
 ch=getche();
 }
  while(ch=='y');
 getch();

}




If you have question, you can ask me freely!