WELCOME IN THE SEA OF C

Here you find some good quality programs.
If any one want any type of help in C and C++ then you can mail me.
Google
Showing posts with label C Programing Beginner. Show all posts
Showing posts with label C Programing Beginner. Show all posts

Sunday, September 21, 2008

Linked List::

These function show different operation perform on Linked list.

'/' is used instead of '<' and '>'.

#include/stdio.h/
#include/conio.h/
#include/alloch.h/

struct
{
int data; /*defining structure*/
struct node *link;
};

/*add a node at the end of a linked list*/
void append(struct node **q,int num)
{
struct node *temp,*r;
if(*q==NULL)
{
temp=malloc(sizeof(struct node));
temp->data=num;
temp->link=NULL;
*q=temp;
}
else
{
temp=*q;
while(temp->link==NULL)
temp=temp->link;
r=malloc(sizeof(struct node))
r->data=num;
r->link=NULL;
temp->link=r;
}
}

/* add a new node at the beginning of the linked list */
void addatbeg(struct node **q,int num)
{
struct node *temp;
temp=malloc(sizeof(struct node));
temp->data=num;
temp->link=*q;
*q=temp;
}

/* add a new node after the specified numbers nodes */
void addafter(struct node *q,int loc,int num)
{
struct node *temp,*r;
int i;
temp=q;
for(i=0;ilink;
if(temp==NULL)
{
printf("there are %d element in the list",loc);
return;
}
}
r=malloc(sizeof(struct node));
r->data=num;
r->link=temp->link;
temp->link=r;
}

/* display the content of the linked list */
void display(struct node *q)
{
printf("\n");
while(q!=NULL)
{
printf("%d",q->data);
q=q->link;
}
}

/* counts the number of nodes present in the linked list */
int count(struct node *q)
{
int c=0;
while(q!=NULL)
{
q=q->link;
c++;
}
return c;
}

/* delete the specified node from the linked list */
void delete(struct node **q,int num)
{
struct node *old,*temp;
temp=*q;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==*q)
*q=temp->link;
else
old->link=temp->link;
free(temp);
return;
}
else
{
old=temp;
temp=temp->link;
}
}
printf("element %d not found",num);
}

Saturday, August 2, 2008

Different operation on LINK LIST:

'/' is used instead of '<' and '>'.

#include/stdio.h/
#include/conio.h/
#include/alloc.h/

struct node
{
int data;
struct node *link;
}*start;

void append(struct node **,int);
void addatbeg(struct node **,int);
void addafter(struct node *,int,int);
void reverse(struct node **);
void getdata();
void display(struct node *);

void main()
{
struct node *p;
clrscr();
p=NULL;

append(&p,2);
append(&p,25);
append(&p,8);

display(p);

addatbeg(&p,10);
addatbeg(&p,15);
display(p);

addafter(p,4,20);
display(p);

reverse(&p);
display(p);

getdata();

getch();
}

void append(struct node **q,int num)
{
struct node *temp,*r;
if(*q==NULL)
{
temp=malloc(sizeof(struct node));
temp->data=num;
temp->link=NULL;
*q=temp;
}
else
{
temp=*q;
while(temp->link!=NULL)
temp=temp->link;
r=malloc(sizeof(struct node));
r->data=num;
r->link=NULL;
temp->link=r;

}
}

void addatbeg(struct node **q,int num)
{
struct node *temp;
temp=malloc(sizeof(struct node));
temp->data=num;
temp->link=*q;
*q=temp;
}

void addafter(struct node *q,int loc,int num)
{
struct node *temp,*r;
int i;
temp=q;
for(i=0;ilink;
if(temp==NULL)
{
printf("\nthere are less than %d elements in list",loc);
return;
}
}
r=malloc(sizeof(struct node));
r->data=num;
r->link=temp->link;
temp->link=r;
}

void display(struct node *q)
{
printf("\n");
while(q!=NULL)
{
printf("%d\t",q->data);
q=q->link;
}
}

void reverse(struct node **q)
{
struct node *t,*r,*s;
t=*q;
r=NULL;
while(t!=NULL)
{
s=r;
r=t;
t=t->link;
r->link=s;
}
*q=r;
}

void getdata()
{
int val,n;
char ch;
struct node *new;
new=NULL;
do
{
printf("\nenter a value");
scanf("%d",&val);
append(&new,val);
display(new);
printf("any more nodes(Y/N)");
ch=getche();
}while(ch=='y' || ch=='Y');
start=new;
}

Tuesday, February 26, 2008

Coloured Rectangle's

This is the simple example of drawing and filling of different images.

"/" is used instead of "<" and ">".

#include/stdio.h/
#include/conio.h/
#include/graphics.h/
void main()
{
int gd=DETECT,gm,maxx,maxy,x=40,y=40,fst;
char str[40];
char *pattern[]={"EMPTY_FILL","SOLID_FILL",
"LINE_FILL","LTSLASH_FILL","SLASH_FILL","BKSLASH_FILL","LTBKSLASH_FILL",
"HATCH_FILL","XHATCH_FILL","INTERLEAVE_FILL","WIDE_DOT_FILL",
"CLOSE_DOT_FILL","USER_FILL"};
initgraph(&gd,&gm,"c:\\tc\\bgi");
maxx=getmaxx();
maxy=getmaxy();
rectangle(0,10,maxx,maxy);

setcolor(WHITE);
outtextxy(175,0,"pre-defined Fill style");

for(fst=0;fst<12;fst++)
{
setfillstyle(fst,MAGENTA);
bar(x,y,x+20,y+20);
rectangle(x,y,x+80,y+80);
setfillstyle(fst,BLUE);
bar(x+40,y,x+60,y+20);
rectangle(x,y,x+80,y+80);
setfillstyle(fst,RED);
bar(x+20,y+20,x+40,y+40);
rectangle(x,y,x+80,y+80);
setfillstyle(fst,GREEN);
bar(x+60,y+20,x+80,y+40);
rectangle(x,y,x+80,y+80);
setfillstyle(fst,MAGENTA);
bar(x,y+40,x+20,y+60);
rectangle(x,y,x+80,y+80);
setfillstyle(fst,BLUE);
bar(x+40,y+40,x+60,y+60);
rectangle(x,y,x+80,y+80);
setfillstyle(fst,RED);
bar(x+20,y+60,x+40,y+80);
rectangle(x,y,x+80,y+80);
setfillstyle(fst,GREEN);
bar(x+60,y+60,x+80,y+80);
rectangle(x,y,x+80,y+80);
itoa(fst,str,10);
outtextxy(x,y+100,str);
outtextxy(x,y+110,pattern[fst]);
x=x+150;
if(x>490)
{
y=y+150;
x=40;
}
}
getch();
closegraph();
restorecrtmode();
}

Wednesday, February 20, 2008

A Funny Loop:

This will make you laugh....its a joke

"/" is used instead of "<" and ">".

#include/stdio.h/
#include/conio.h/
int main(void)
{
int c;
for(c=1;c<=100,c++)
printf("my name is...");
return 0;
getch();
}

Monday, February 11, 2008

Decimal To Binary

This program convert a decimal number into binary number.

"/" is used instead of "<" and ">".
#include/stdio.h/
#include/conio.h/
#include/alloc.h/
struct node
{
int data;
struct node *link;
};
void atbeg(struct node**,int);
void display(struct node*);
int count(struct node*);
void main()
{
int n,i;
struct node *p;
p=NULL;
clrscr();
printf("enter the decimal number:");
scanf("%d",&n);
while(n!=0)
{
i=n%2;
atbeg(&p,i);
n=n/2;
}
display(p);
printf("\nnumber of elements=%d",count(p));
getch();
}
void atbeg(struct node **q,int i)
{
struct node *temp;
temp=malloc(sizeof(struct node));
temp->data=i;
temp->link=*q;
*q=temp;
}
void display(struct node *q)
{
printf("\n");
while(q!=NULL)
{
printf("%d",q->data);
q=q->link;
}
}
int count(struct node *q)
{
int c=0;
while(q!=NULL)
{
q=q->link;
c++;
}
return c;
}

Wednesday, January 30, 2008

Number having special property:

Square of 12 is 144.21,which is reverse of 12 has a square 441,which is reverse of 144.This program search foe such numbers in the range 10 to 100.


"/" is used instead of "<" and ">".
#include/stdio.h/
#include/conio.h/
void main()
{
unsigned long num,rnum,square,rsquare,n2,num2;
int d1,d2,d3,d4,d5;
for(num=10;num<=100;num++)
{ num2=num;
d1=num2%10;
num2=num2/10;
d2=num2%10;
num2=num2/10;
d3=num2%10;
num2=num2/10;
d4=num2%10;
num2=num2/10;
d5=num2;
rnum=d5+d4*10+d3*100+d2*1000L+d1*10000L;
while(rnum%10==0)
rnum=rnum/10;
square=num*num; r
square=rnum*rnum;
d1=square%10;
square=square/10;
d2=square%10;
square=square/10;
d3=square%10;
square=square/10;
d4=square%10;
square=square/10;
d5=square;
n2=d5+d4*10+d3*100+d2*1000L+d1*10000L;
while(n2%10==0)
n2=n2/10;
if(rsquare==n2)
printf("\n%lu",num);
} }

Tuesday, January 22, 2008

Prime Factor Generator

Generates the prime factors of a given number

"/" is used instead of "<" and ">."
#include/iostream.h/
#include/iomanip.h/
#include/conio.h/
#include/math.h/
void main()
{ clrscr();
unsigned long int x;
char c='y';
do
{
cout<<"enter the no....."< cin>>x;
cout<<"Factors are...";
do
{
for(unsigned long i=2;i<=x;i++)
{
long f=x%i;
if(f==0)
{
cout<<<" ";
x=x/i;
break;
} } }
while(x!=1);
getch();
cout<<"continue...(y/n)....";
cin>>c;
cout<}
while(c=='y');
}

Monday, January 21, 2008

Program to Count CAPITAL Alphabets

This program will count the number of capital alphabets in the given string.
"/" is used instead of "<" and ">'.
#include/stdio.h/
#include/conio.h/
#include/string.h/
void main()
{
static char *str[]={Enter any string};
printf("No. of capitals=%d",capcount(s,n)); /*n=no of string*/
}
capcount(s,x)
char **s;
int c;
{
int i,cap=0;
char *t;
for(i=0;i=65 && *t<=90) cap++; t++; } } return(cap); }

Sunday, January 20, 2008

Smiling Face Program

/* Program to fill the entire screen with smiling face */
"/" is used instead of "<" and ">".
#include/stdio.h/
#include/conio.h/
main()
{
int r,c;
clrscr();

for(r=0;r<=24;r++) /*fills rows 0to 24*/ for(c=0;c<=79;c++) /*fills colums 0 to 79*/ printf("%c",1); printf("\n\n\nPress any key to exit"); getch(); }

Typical Pattern Program

/* Program to produce a typical pattern */

"/" is used instead of "<" and ">".
#include/stdio.h/
#include/conio.h/
main()
{
int i=1,x=71,blanks=0,j,val,k;
clrscr();
while(i<=7) { j=65; /*ASCII value of A*/ val=x; while(j<=val) { printf("%c",j); j++; } if(i==1) val--; k=1; while(k<=blanks) { printf(" "); k++; } blanks=2*i-1; while(val>=65)
{
printf("%c",val);
val--;
}
printf("\n");
x--;
i++;
}
printf("\n\n\nPress any key to exit");
getch();
}

ARMSTRONG NUMBER

/* Generate all ARMSTRONG number between 1 t0 500 */
An Armstrong number is that whose sum of cube of each digit is equal to the number.
"/" is used instead of "<" and ">".
#include/stdio.h/
#include/conio.h/
main()
{
int i=1,a,b,c;
clrscr();
printf("Armstrong number are\n");
while(i<=500) { a=i%10; /*extract last digit*/ b=i%100; b=(b-a)/10; /*extract second digit*/ c=i/100; /*extract first digit*/ if((a*a*a)+(b*b*b)+(c*c*c)==i) printf("%d",i); i++; } printf("\n\n\nPress any key to exit"); getch(); }

Saturday, January 19, 2008

This Program Will Show You The Day on 1st January Of Any Year

Just Enter Year and you will get the day on 1st January of that year
"/" is used instead of "<" and ">".
#include/stdio.h/
#include/conio.h/
main()
{
int leapdays,firstday,yr;
long int normaldays,totaldays;
clrscr();
printf("enter year");
scanf("%ld",&yr);
normaldays=(yr-1)*365L;
leapdays=(yr-1)/4-(yr-1)/100+(yr-1)/400;
totaldays=normaldays+leapdays;
firstday=totaldays%7;
if(firstday==0)
printf("\nMonday");
if(firstday==1)
printf("\nTuesday");
if(firstday==2)
printf("\nWedneshday");
if(firstday==3)
printf("\nThursday");
if(firstday==4)
printf("\nFriday");
if(firstday==5)
printf("\nSaturday");
if(firstday==6)
printf("\nSunday");
printf("\n\n\nPress any key to exit");
getch();
}