C program source codes

Posted by Unknown at 18:11

C program source codes
1.analog clock
2.shell Short

/*PROGRAM TO CONSTRUCT THE CLOCK*/
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<process.h>
#include<math.h>
#define PI 3.14
#include<stdio.h>
void main()
{

int gd=DETECT,gm;
int h,m,s,xs,ys,xm,ym,xh,yh;
initgraph(&gd,&gm,"c:\\tc\\bgi");

struct time t;
fflush(stdout);
cleardevice();
setfillstyle(6,10);
circle(319,239,239);
circle(319,239,219);
floodfill(556,239,15);

setcolor(9);
settextstyle(1,0,4);
outtextxy(301,37,"12");
outtextxy(480,218," 3");
outtextxy(297,400," 6");
outtextxy(113,218," 9");

gettime(&t);
s=t.ti_sec;
h=t.ti_hour;
m=t.ti_min;
int ang=30;


for(int i=0;i<60;i++)
line(320+210*(cos(PI/180*(90-6*i))),240-210*(sin(PI/180*(90-6*i))),320+200*(cos(PI/180*(90-6*i))),240-200*(sin(PI/180*(90-6*i))));
settextstyle(4,0,3);
outtextxy(263,80,"Neeraj Gulia");

while(1)
{
if (kbhit())
exit(0);
gotoxy(36,25);
printf("%02d::%02d::%02d",h,m,s);
setcolor(4);
xs=320+150*(cos(PI/180*(90-6*s)));
ys=240-150*(sin(PI/180*(90-6*s)));
line(320,240,xs,ys);

setcolor(14);
xm=320+120*(cos(PI/180*(90-6*m)));
ym=240-120*(sin(PI/180*(90-6*m)));
setlinestyle(0,0,3);
line(320,240,xm,ym);

if(s>59)
{
s=0;
m++;
}
setcolor(5);
xh=320+90*(cos(PI/180*(90-ang*h)));
yh=240-90*(sin(PI/180*(90-ang*h)));
line(320,240,xh,yh);

if(m>59)
{
m=0;
h++;
s=0;

}
delay(980);
sound(40);
delay(50);
nosound();

setcolor(0);
line(320,240,xh,yh);
line(320,240,xm,ym);
setlinestyle(0,0,0);
line(320,240,xs,ys);
s++;
}
}

shell sort

#include<iostream.h>
#include<conio.h>
void main()
{
int k,i,temp,n,a[20],swap,gap;
clrscr();
cout<<"enter the no. of elements: ";
cin>>n;
cout<<"enter elements: \n";
for(i=1;i<=n;i++)
cin>>a[i];
gap=n/2;
k=0;
do{
do{
swap=0;
k++;
for(i=1;i<=n-gap;i++)
if(a[i]>a[i+gap])
{
temp=a[i];
a[i]=a[i+gap];
a[i+gap]=temp;
swap=1;
}
}while(swap);
gap=gap/2;
}while(gap);
cout<<"sorted list is: \n";
for(i=1;i<=n;i++)
cout<<a[i]<<endl;
getch();
}


quick sort

#include<iostream.h>
#include<conio.h>
int loc=0;
void main()
{
int i,n,a[20],lower[20],upper[20],top,beg,end;
void quick(int *,int,int,int,int);
clrscr();
cout<<"enter the no. of elements : ";
cin>>n;
cout<<"enter elements: \n";
for(i=1;i<=n;i++)
cin>>a[i];
top=0;
if(n>1)
{
top=top+1;
lower[1]=1;
upper[1]=n;
}
while(top!=0)
{
beg=lower[top];
end=upper[top];
top--;
quick(a,n,beg,end,loc);
if(beg<loc-1)
{
top++;
lower[top]=beg;
upper[top]=loc-1;
}
if(loc+1<end)
{
top++;
lower[top]=loc+1;
upper[top]=end;
}
}
cout<<"sorted list is: \n";
for(i=1;i<=n;i++)
cout<<a[i]<<endl;
}

void quick(int a[],int n,int beg, int end, int loc)
{
int left,right,temp;
left=beg;
right=end;
loc=beg;
a:
while((a[loc]<=a[right]) && (loc!=right))
right=right-1;
if (loc==right)
return;
if(a[loc]>a[right])
{
temp=a[loc];
a[loc]=a[right];
a[right]=temp;
loc=right;
}
while((a[left]<=a[loc]) && (left!=loc))
left=left+1;
if(loc==left)
return;
if(a[left]>a[loc])
{
temp=a[left];
a[left]=a[loc];
a[loc]=temp;
loc=left;
goto a;
}
}

to print like in calculator, i.e. from right to left

//program to enter the name of the user
#include<conio.h>
#include<stdio.h>
#define row 41
#define col 2

void main()
{
char *name;
int i;
clrscr();
printf("enter your name: "); // 18
for(i=0;;i++)
{
gotoxy(row,col);
name[i]=getch();
if(name[i]=='\r')
break;
gotoxy(row-i,col);
printf("%s",name);

}
}

#include<stdio.h>

int fib_rec(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return (fib_rec(n - 1) + fib_rec(n - 2));
}

int fib_loop(int n) {
int a, b, sum;
a = b = sum = 0;
b = 1;
if (n <= 0)
return 0;
else if(n == 1 '' n == 2)
return 1;
while(n > 1) {
sum = a + b;
a = b;
b = sum;
n--;
}
return sum;
}

int main() {
int i, n = 37;
printf("the fib upto %d using recursion is: \n",n);
for(i = 0; i < n; i++)
printf("%d, ",fib_loop(i));

printf("\nthe fib upto %d using loop is: \n",n);
for(i = 0; i < n; i++)
printf("%d, ",fib_rec(i));

puts("");
return 0;
}

C calendar from 1752

#include<stdio.h>
#define Y 1752
// 1/1/1752Saturday
/* the The calendar as we know it only came into effect (in England) in 1752,
replacing the Julian calendar. Changes included cutting 11 or more days out
of the calendar and changing the first day of the year from march 21st to January 1st,
and so this calculation method should not be used for dates before this changeover.
*/
#define BASE 5

void day(int dd, int mm, int yy);
void display(int, int);
int _MONTH[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char *days[] = {"sunday",
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday"};

int main() {
char ch = 'y';
int mm,yy,dd;
do {
printf("\nEnter dd ");
scanf("%ld",&dd);
printf("\nEnter mm [1-12] ");
scanf("%ld",&mm);
printf("Enter yyyy ");
scanf("%ld",&yy);

if( (yy%4==0) && ( (yy%100!=0) '' (yy%400==0) ) )
_MONTH[2]=29;

day(dd,mm,yy);
printf("\n\n\n\nPress 'x' to EXIT\n");
fflush(stdin);
ch = getchar();
}while(ch != 'x');
return 0;
}

void day(int dd,int mm, int yy) {
int i,md=0,leap=0,track, temp;
unsigned int d,yrd;

for(i=Y;i<yy;i++) {
if( (i%4==0) && ((i%100!=0) '' (i%400==0)))
leap++;
}

for(i=1; i < mm; i++)
md = md + _MONTH[i];

yrd = (yy-Y) * 365;
d=yrd+leap+md+BASE;
temp = d + dd;
temp = temp % 7;
printf("day is : %s",days[temp]);
track=d%7;
display(track,mm);
}

void display(int track,int m) {
int dt,loop;
printf("\n\n\n\t\t");
printf("MON\tTUES\tWED\tTHURS\tFRI\tSAT\tSUN\n\n");

for(loop=0; loop < track + 2; loop++) /*t+2 due to two additional \t*/
printf("\t");

for(dt=1; dt <= _MONTH[m]; dt++) {
if(track % 7 == 0 && track != 0)
printf("\n\n\t\t");
printf("%d",dt);
printf("\t");
track++;
}
}DIGITAL CLOCK in C

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

#define PI 3.14

void getTime(int *h, int *m, int *s ){
struct time t;
gettime(&t);
gotoxy(36,18);
printf("%2d:%02d:%02d.%02d\n",
t.ti_hour,t.ti_min,t.ti_sec,t.ti_hund);
*h = t.ti_hour;
*m = t.ti_min;
*s = t.ti_sec;
}

void main(){
int gd=DETECT, gm;
initgraph(&gd, &gm, "\\tc");
int xs, ys, xm, ym, xh, yh, h, m, s;
while(!kbhit()){
cleardevice();
getTime(&h, &m, &s);
settextstyle(1,0,0);
setcolor(WHITE);
outtextxy(300,15,"12");
outtextxy(315,425,"6");
outtextxy(105,220,"9");
outtextxy(520,220,"3");
settextstyle(5,0,0);
setcolor(GREEN);
outtextxy(275,300,"CLOCK");
settextstyle(2 ,0,0);
setcolor(LIGHTRED);
outtextxy(310,295,"Mukesh");
xh = cos((h*30 + m / 2) * PI / 180 - PI / 2) * 150 + 320;
yh = sin((h*30 + m / 2) * PI / 180 - PI / 2) * 150 + 240;
xm = cos(m * PI / 30 - PI / 2) * 180 + 320;
ym = sin(m * PI / 30 - PI / 2) * 180 + 240;
xs = cos(s * PI / 30 - PI / 2) * 210 + 320;
ys = sin(s * PI / 30 - PI / 2) * 210 + 240;
setcolor(LIGHTBLUE);
circle(320,240,220);
setcolor(LIGHTRED);
line(320,240,xh,yh);
setcolor(LIGHTGREEN);
line(320,240,xm,ym);
setcolor(YELLOW);
line(320,240,xs,ys);
sleep(1);
}
}

Draw a hut in graphics

#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");

cleardevice();
fflush(stdout);
line(200,150,350,150);
line(140,200,200,150);
line(140,330,140,200);
line(250,200,140,200);
line(200,150,250,200);
circle(196,180,8);
setfillstyle(2,4);
floodfill(196,180,15);
setfillstyle(1,2);
line(350,150,400,200);
floodfill(210,180,15);
line(400,200,400,330);
line(140,330,400,330);
line(250,200,250,330);
line(250,200,400,200); //hut

setfillstyle(5,7);
floodfill(260,180,15);
line(170,260,170,330);
line(170,260,210,260);
setfillstyle(10,9);
floodfill(180,250,15);
line(210,260,210,330);
setfillstyle(9,9);
floodfill(210,250,15);

line(290,110,290,150);
line(310,110,310,150);
ellipse(300,110,0,360,10,3); //chimney
setfillstyle(6,8);
floodfill(300,120,15);

line(300,250,350,250);
line(300,280,350,280);
line(350,250,350,280);
line(300,280,300,250);
setfillstyle(9,9);
floodfill(252,300,15);
setfillstyle(8,9);
floodfill(342,270,15);

setcolor(4);
settextstyle(7,0,5);
outtextxy(100,390,"Home Sweet Home");

getch();
closegraph();
}

palindrome

int main()
{
int n;
char a[20];
cout<<"enter how many digit string u want 2 enter";
cin>>n;
cout<<"enter the string";
cin>>a;
for(inti=0;i<=n;i++)
{
for(int j=n;j>=0;j--)
if (a[i]==a[j])
s=0;
else
s=1;
}
if(s==0)
cout<<"no.is palindrome ";
else
cout<<"no. is not palindrome";

getch()
return 0;
}

to convert a string of upper case to lower case

 #include <stdio.h>
#include <string.h>

int main(void)
{
char *string = "Borland International";

printf("string prior to strlwr: %s\n", string);
strlwr(string);
printf("string after strlwr: %s\n", string);
return 0;
}

compare two compare 2 strings

 #include <string.h>
#include <stdio.h>

int main(void)
{
char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
int ptr;

ptr = strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
else
printf("buffer 2 is less than buffer 1\n");

ptr = strcmp(buf2, buf3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3\n");
else
printf("buffer 2 is less than buffer 3\n");

return 0;
}


If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!


Kindly Bookmark and Share it:

YOUR ADSENSE CODE GOES HERE

0 comments:

Have any question? Feel Free To Post Below:

 

© 2011. All Rights Reserved | Interview Questions | Template by Blogger Widgets

Home | About | Top