implementation of LN command using system calls

int main(int argc,char* argv[])
{
int i;
struct stat s;
if (argc<3)>4)
{
perror("ERROR:Too Many Arguments");
exit(1);
}
if(argc==3)
i=0;
else
i=1;

if(i && !(strcmp(argv[1],"-s")==0)) {
perror("ERROR:Invalid Syntax");
exit(1);
}

if(access(argv[i+1],F_OK))
{
perror("ERROR:File name not Found");
exit(1);
}
if(!access(argv[i+2],F_OK))
{
perror("ERROR:File Name already exist");
exit(1);
}
if(stat(argv[i+1],&s)<0)
{
perror("ERROR:Unable to reterive stat information");
exit(1);
}
if(!S_ISREG(s.st_mode))
{
perror("ERROR:Not a Regular File");
exit(1); }
if(argc==3)
if(link(argv[i+1],argv[i+2])<0)
{
perror("ERROR:Unable to create the Link");
exit(1);
}
if(argc==4)
if(symlink(argv[i+1],argv[i+2])<0)
{
perror("ERROR:Unable to create the Link");
exit(1);
}
}

Related Links :

implementing who am i using system calls

#include
#include
int main()
{
char *s,*c;
struct utmp *u;
int i;
c=getlogin();
setutent();
u=getutent();
while(u!=NULL)
{
if(u->ut_type==7 && strcmp(u->ut_user,c)==0)
{
printf("%-12s",u->ut_user);
printf("%-9s",u->ut_line);
s=ctime(&u->ut_time);
for(i=4;i<16;i++)
printf("%c",s[i]);
printf("(%s",u->ut_host);
printf(")
");
}
u=getutent();
}
}

Related Links :

Output of one program is input of another program Using Pipes

#include "unistd.h"
#include "process.h"

/* Pipe the output of program to the input of another. */

int main()
{
int pipe_fds[2];
int stdin_save, stdout_save;

if (pipe(pipe_fds) < 0)
return -1;

/* Duplicate stdin and stdout so we can restore them later. */
stdin_save = dup(STDIN_FILENO);
stdout_save = dup(STDOUT_FILENO);

/* Make the write end of the pipe stdout. */
dup2(pipe_fds[1], STDOUT_FILENO);

/* Run the program. Its output will be written to the pipe. */
spawnl(P_WAIT, "/dev/env/DJDIR/bin/ls.exe", "ls.exe", NULL);

/* Close the write end of the pipe. */
close(pipe_fds[1]);

/* Restore stdout. */
dup2(stdout_save, STDOUT_FILENO);

/* Make the read end of the pipe stdin. */
dup2(pipe_fds[0], STDIN_FILENO);

/* Run another program. Its input will come from the output of the
first program. */
spawnl(P_WAIT, "/dev/env/DJDIR/bin/less.exe", "less.exe", "-E", NULL);

/* Close the read end of the pipe. */
close(pipe_fds[0]);

/* Restore stdin. */
dup2(stdin_save, STDIN_FILENO);

return 0;
}

Related Links :

Program that shows how fork system call works

#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "sys/types.h"

int main(void)
{
pid_t pid;
char cwdir_cld[100];
char cwdir_prt[100];

if ((pid = fork()) < 0)
{
perror("fork error");
exit(1);
}

if (pid == 0)
{
/*
* Now we are in the childs thread
* let us issue a chdir syscall :)
*/
if(chdir("/tmp") < 0) {
perror("chdir error");
}

/*
* Let us now print the current working directory of the child
*/
if (getcwd(cwdir_cld, 100) != NULL)
{
printf("Current working directory of the child is : %s", cwdir_cld);
}
else
{

perror("getcwd error");
}

/* end of the child */
} else
{
/*
* Now we are in the parent thread
* Let is check the current working directory
*/
if (getcwd(cwdir_prt, 100) != NULL)
{
printf("Current working directory of the parent is : %s", cwdir_prt);
} else
{
perror("getcwd error");
}
}

printf(" ");
return(0);
}

Related Links :

Program to catch Ctrl+C.

#include "stdio.h"
#include "unistd.h"
#include "stdlib.h"
#include "signal.h"

void sigfun(int sig)
{
printf("You have presses Ctrl-C , please press again to exit");
(void) signal(SIGINT, SIG_DFL);
}

int main()
{
(void) signal(SIGINT, sigfun);

while(1) {
printf("Hello World!");
sleep(1);
}

return(0);
}

Related Links :

program to implement who command

#include "stdio.h"
#include"sys/utsname.h"
#include "utmp.h"
int main(void)
{
struct utmp *n;
char *a;
int i;
setutent();
n=getutent();
while(n!=NULL)
{
if(n->ut_type==7)
{
printf("%-9s",n->ut_user);
printf("%-12s",n->ut_line);
a=ctime(&n->ut_time);
printf(" ");
for(i=4;i<16;i++)
printf("%c",a[i]);
printf(" (");
printf("%s",n->ut_host);
printf(")");
}
n=getutent();
}
}

Related Links :

Program to print its Process ID, Parent Process ID and Group ID

#include "stdio.h"
#include "unistd.h"
#include "stdlib.h"
#include "sys/types.h"

int main()
{
pid_t pid, ppid;
gid_t gid;

/* get the process id */
if ((pid = getpid()) < 0)
{
perror("unable to get pid");
}
else {
printf("The process id is %d", pid);
}

/* get the parent process id */
if ((ppid = getppid()) < 0)
{
perror("unable to get the ppid");
} else
{
printf("The parent process id is %d", ppid);
}

/* get the group process id */
if ((gid = getgid()) < 0) {
perror("unable to get the group id");
} else
{
printf("The group id is %d", gid);
}

return(0);
}

Related Links :

Program to work on Child Processes

# include "stdio.h"
# include "stdlib.h"
# include "unistd.h"
int main()
{
long limit;
/* GET THE LIMIT OF THE ARGUMENT */
if((limit = sysconf(_SC_ARG_MAX)) < 0)
{
perror("ARG MAXIMUM LIMIT ERROR");
}
else
{
printf("ARGUMENT LIMIT IS %ld ",limit);
}
/* GET THE LIMIT OF THE CHILD PROCESS */
if((limit = sysconf(_SC_CHILD_MAX)) < 0)
{
perror("CHILD MAXIMUM LIMIT ERROR ");
}
else
{
printf("MAXIMUM CHILD PROCESS %ld ",limit);
}
printf(" ");
return(0);
}

Related Links :

Program to work on Semapores

# include "unistd.h"
# include "stdlib.h"
# include "stdio.h"
# include "sys/types.h"
# include "sys/ipc.h"
# include "sys/sem.h"

#ifndef _SEMUN_H
#define _SEMUN_H
union semun
{
int val;
struct semid_ds * buf;
unsigned short int * array;
struct seminfo *__buf;
};
# endif

static int set_semvalue(void);
static void del_semvalue(void);
static int semaphore_p(void);
static int semaphore_v(void);
static int sem_id;

int main(int argc, char *argv[])
{
int i;
int pause_time;
char op_char = 'O';
srand((unsigned int) getpid());
sem_id = semget((key_t)1234,1,0666| IPC_CREAT);
if ( argc > 1 )
{
if (!set_semvalue())
{
fprintf(stderr, "Failed to initialize semapore ");
exit(EXIT_FAILURE);
}
op_char = 'X';
sleep(2);
}

for ( i=0; i<10;i++)
{
if (!semaphore_p()) exit(EXIT_FAILURE);
printf("%c", op_char);fflush(stdout);
pause_time = rand() % 3;
sleep(pause_time);
printf("%c", op_char);fflush(stdout);
if (!semaphore_p()) exit(EXIT_FAILURE);
pause_time = rand() % 2;
sleep(pause_time);
}
printf(" %d - finished", getpid());
if (argc >1 )
{
sleep(10);
del_semvalue();
}
exit(EXIT_SUCCESS);
}

static int set_semvalue(void)
{
union semun sem_union;
sem_union.val = 1;
if (semctl(sem_id,0,SETVAL, sem_union) == -1) return(0);
return(1);
}

static void del_semvalue(void)
{
union semun sem_union;
if (semctl(sem_id,0, IPC_RMID, sem_union) == -1)
fprintf(stderr, "Failed to delete semaphore ");
}

static int semaphore_p(void)
{
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = -1;
sem_b.sem_flg = SEM_UNDO;
if (semop(sem_id, &sem_b,1) == -1)
{
fprintf(stderr, "semaphore_p failed ");
return(0);
}
return(1);
}

Related Links :

String manipulation using message Queue

#include "stdio.h"
#include "errno.h"
#include "sys/ipc.h"
#include "sys/msg.h"
struct queue
{
long type;
char msg[50];
};

Queue.c
-------

#include "queue.h"
int main()
{
struct queue q;
int mid;
if((mid=msgget(ftok("string",0x99),0644|IPC_CREAT))<0)
{
if(mid!=-1)
{
printf("Message Queue Error
");
exit(1);
}
else
{
printf("Message Already Exist");
exit(1);
}
}
printf("Enter the Message Type:");
scanf("%d",&q.type);
while(q.type>0)
{
printf("Enter the Message:");
getchar();
gets(q.msg);
if(msgsnd(mid,&q,sizeof(q),IPC_NOWAIT)<0)
{
printf("Message send Error");
exit(1);
}
printf("Enter the Message Type:");
scanf("%d",&q.type);
}
return 0;
}


Word.c
------

#include "queue.h"
#include
void word(char* str)
{
int i,n,w=1;
n=strlen(str);
for(i=0;i if(str[i]==' ' && str[i-1]!=' ')
w++;
printf("Number of Words in "%s" :%d",str,w);
}

int main()
{
struct queue q;
int mid;
long type;
if((mid=msgget(ftok("string",0x99),0644 ))<0)
{
printf("Message Queue Error");
exit(1);
}
printf("
Enter the Message Type to Reterive:");
scanf("%d",&type);
while(msgrcv(mid,&q,sizeof(q),type,IPC_NOWAIT) && errno!=ENOMSG)
{
word(q.msg);
}
msgctl(mid,IPC_RMID,NULL);
}

Reverse.c
---------

#include "queue.h"
#include "string.h"
void rev(char* str)
{
char s[30];
int i,j,n;
n=strlen(str);
printf("Reverse for "%s" :",str);
for(i=n;i>=0;i--)
putchar(str[i]);
printf("
");
}
int main()
{
struct queue q;
int mid;
long type;
if((mid=msgget(ftok("string",0x99),0644 ))<0)
{
printf("Message Queue Error");
exit(1);
}
printf("Enter the Message Type to Reterive:");
scanf("%d",&type);
while(msgrcv(mid,&q,sizeof(q),type,IPC_NOWAIT) && errno!=ENOMSG)
{
rev(q.msg);
}
}

Related Links :

vowels counting using vfork

#include"stdio.h"
#include "types.h"
int main()
{
int j,n,a,i,e,o,u;
char str[50];
a=e=i=o=u=0;
pid_t pid;
if((pid=vfork())<0)
{
perror("FORK ERROR");
exit(1);
}
if(pid==0)
{
printf("
Counting Number of Vowels using VFORK");
printf("-------- ------ -- ------ ----- -----");
printf("Enter the String:");
gets(str);
_exit(1);
}
else
{
n=strlen(str);
for(j=0;j
{
if(str[j]=='a' || str[j]=='A')
a++;
else if(str[j]=='e' || str[j]=='E')
e++;
else if(str[j]=='i' || str[j]=='I')
i++;
else if(str[j]=='o' || str[j]=='O')
o++;
else if(str[j]=='u' || str[j]=='U')
u++;
}
printf("Vowels Counting");
printf("------ --------");
printf("Number of A : %d",a);
printf("Number of E : %d",e);
printf("Number of I : %d",i);
printf("Number of O : %d",o);
printf("Number of U : %d",u);
printf("Total vowels : %d",a+e+i+o+u);
exit(1);
}
}

Related Links :

Write a program to print stuff to a file reading the number of time

#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"

void writefile(char *);

int main()
{
int N;
FILE *inf;
int i;

inf = fopen("infile.txt", "r");

/* Now let us read the number from a file */
fscanf(inf, "%d", &N);

for(i = 0; i <>
writefile("WWW");

printf("
");
fclose(inf);
return(0);
}

void writefile(char *str)
{
FILE *outf;
int i;

outf = fopen("outfile.txt", "a");

/* Now let us write to the file */
fprintf(outf, "
%s", str);

fclose(outf);
}

Related Links :

Program. to find & replace any desired character from the input text.

#include
# include
char c1,c2,a[80];
void main()
{
clrscr();
find_rep();
getch();

}

void find_rep(void)
/* Function to find & replace any text */
{
char c1,c2;
char a[80];
int i,j,k;
printf("
Enter a line of text below:-");
printf("
Line will be terminated by pressing ENTER.");
printf("TEXT:- ");
gets(a);
printf("Enter the replaceable & replacing letter respectively:- ");
scanf("%c%c%c",&c1,' ',&c2);
for (j=0;j<80;j++)
{
if (a[j]==c1)
a[j]=c2;
}
puts(a);
printf("Here all %c are replaced by %c.",c1,c2);
return;
}

Related Links :

Program to calculate frequency of vowels in a string.

#include
#include
void main()
{
int a=0,e=0,i=0,o=0,u=0,sum=0;
char c;
clrscr();
printf("Enter string:- ");
printf("String will be terminated if you press Ctrl-Z & then ENTER.");
printf("STRING:- ");
while ((c=getchar())!=EOF)
{
if (c=='a'||c=='A')
a=a+1;
if (c=='e'||c=='E')
e=e+1;
if (c=='i'||c=='I')
i=i+1;
if (c=='o'||c=='O')
o=o+1;
if (c=='u'||c=='U')
u=u+1;
}
sum=a+e+i+o+u;
printf("Frequency of vowel 'a' is %d.",a);
printf("Frequency of vowel 'e' is %d.",e);
printf("Frequency of vowel 'i' is %d.",i);
printf("Frequency of vowel 'o' is %d.",o);
printf("Frequency of vowel 'u' is %d.",u);
printf("Total no. of vowels in the text is %d.",sum);
printf("HAVE A NICE DAY! BYE. Take Care :)");
getch();
}

Related Links :

PROGRAM TO FIND WHETHER A GIVEN YEAR IS LEAP YEAR OR NOT.

#include"stdio.h"
#include "conio.h"
void main()
{
int year,leap;
clrscr();
printf("enter the year:- ");
scanf("%d",&year);
if ((year%100)==0)
leap=(year/400)*400;
else
leap=(year/4)*4;
if (leap==year)
printf("
%d is a leap year.",year);
else
printf("
%d is not a leap year.",year);
getch();
}

Related Links :

Reversal of a singly linklist by recursion

#include "stdio.h"
#include "conio.h"
#include "alloc.h"
struct node
{
int data;
struct node*next;
};
void insert(struct node**p,int num) /*Function for inserting an
element into a list */
{
if(*p==NULL)
{
(*p)=(struct node*)malloc(sizeof(struct node));
(*p)->next=NULL;
(*p)->data=num;
}
else
{
insert(&((*p)->next),num);
}
}
void display(struct node*p) /*Function for displaying the list*/
{
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
void reverse(struct node**p) /*Function for reversing the list by
recursion */
{
struct node*q,*r,*x;
int d;
q=(*p); /*stores the address of the first element */
x=q; /*also stores the element of the first element for
counter pourpose */
d=q->data; /*stores the data of the first element*/
r=q->next; /*stores the address of the second element in the list
*/
free(q); /*deletes the first element of the list*/
if(x==NULL)
return ;
else
{
reverse(&(r));/*Recursive call*/
insert(p,d); /*This function is put in the stack so the first
will be taken as last element for the new list */
}
}
void main()
{
clrscr();
struct node*p=NULL;
int n,d,i=0;
printf("How many...? ");
scanf("%d",&n);
while(i++!=n)
{
scanf("%d",&d);
insert(&p,d);
}
display(p);
reverse(&p);
printf("
The reversed list is...");
display(p);
getch();
}

Related Links :

Rat in a Maze Game implemented in C

#include "conio.h"
#include "stdio.h"
#define SIZE 15
#include "stdlib.h"
void main()
{
int maze[SIZE][SIZE],mark[SIZE][SIZE],stack[SIZE][3];
static int
move[8][2]={-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};

int i,j,m,n,top,mov,g,h;
clrscr();
printf("enter size");
scanf("%d%d",&m,&n);
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&maze[i][j]);
}
}
for(i=0;i<=n+1;i++)
maze[0][i]=1;
for(i=0;i<=n+1;i++)
maze[m+1][i]=1;
for(i=0;i<=m+1;i++)
maze[i][0]=1;
for(i=0;i<=m+1;i++)
maze[i][n+1]=1;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
mark[i][j]=0;
}
}
mark[1][1]=1;
stack[0][0]=1;
stack[0][1]=1;
stack[0][2]=2;
top=1;
while(top!=0)
{
i=stack[0][0];
j=stack[0][1];
mov=stack[0][2];
top=top-1;
while(mov<=7)
{
g=i+move[mov][0];
h=j+move[mov][1];

if(mark[g][h]==0&&maze[g][h]==0)
{
mark[g][h]=1;
top++;
stack[top][0]=i;
stack[top][1]=j;
mov=-1;
i=g;j=h;
}
mov=mov+1;
if(g==m&&h==n)
{
printf("
path made by the rat is");
for(i=1;i<=top;i++)
printf("
%d %d",stack[i][0],stack[i][1]);
printf("
%d %d",m,n);
getch();
exit(0);
}
}
}
}




Related Links :

Program to implement Stack as Linked List


#include "stdio.h"
#include "conio.h"
# include "malloc.h"
struct node
{
int data;
struct node *link;
} ;
struct node *top;

void main()
{
void push(int);
void display();
int wish, num,will,a;
wish = 1;
top = NULL;
clrscr();
printf("Program for Stack as Linked List demo..");
while(wish == 1)
{
printf("Main Menu
1.Enter data in stack
2.Delete from stack");
scanf("%d",&will);
switch(will)
{
case 1:
printf("Enter the data");
scanf("%d",&num);
push(num);
display();
break;
case 2:
a=pop();
printf("Value returned from top of the stack is %d",a);
break;
default:
printf("Invalid choice");
}
printf("Do you want to continue, press 1");
scanf("%d",&wish);
}
}


void push(int y)
{
struct node *x;
x=malloc(sizeof(struct node));
printf(" Address of newly created node x is %d",x);
x->data = y;
x->link = top;
top = x;
}
void display()
{
int i =0;
struct node * temp;
temp = top;

while(temp!=NULL)
{
printf("Item No. %d : Data %d Link %d ",i++,temp->data,temp->link);
temp=temp->link;
}
}

/// THIS FUNCTION REMOVES TOP NODE FROM THE STACK AND RETURNS ITS VALUE///

int pop()
{
int a;
if(top==NULL)
{
printf("STACK EMPTY...");
return 0;
}
else
{
a=top->data;
printf("The value returned is %d ",a);
free(top);
top=top->link;
return (a);
}
}

Related Links :

Program for Stack implementation through Array

#include "stdio.h"
#include "ctype.h"
# define MAXSIZE 200
int stack[MAXSIZE];
int top; //index pointing to the top of stack
void main()
{
void push(int);
int pop();
int will=1,i,num;
clrscr();
while(will ==1)
{
printf(" MAIN MENU:
1.Add element to stack
2.Delete element from the stack
");
scanf("%d",&will);

switch(will)
{
case 1:
printf("Enter the data... ");
scanf("%d",&num);
push(num);
break;
case 2: i=pop();
printf("Value returned from pop function is %d ",i);
break;
default: printf("Invalid Choice . ");
}

printf("Do you want to do more operations on Stack ( 1 for yes, any other key to exit) ");
scanf("%d" , &will);
} //end of outer while
} //end of main


void push(int y)
{

if(top>MAXSIZE)
{
printf("STACK FULL");
return;
}
else
{
top++;
stack[top]=y;
}
}

int pop()
{
int a;
if(top<=0) { printf("STACK EMPTY"); return 0; } else { a=stack[top]; top--; } return(a); }

Related Links :

Heap sort Program

#include "stdio.h"
#include "conio.h"
#include "alloc.h"

void main()
{
int *x,i,n;
int temp;
void heap(int *,int);
clrscr();
fflush(stdin);
printf("
Heap Sort
");
printf("
Enter How many Numbers : ");
scanf("%d",&n);
x = (int *)malloc(n * sizeof(int));
for(i=0;i=1;i--)
{
temp = x[i];
x[i] = x[0];
x[0] = temp;
heap(x,i-1);
}

printf("
Resultant Array
");
for(i=0;i=0;i--)
{
if(a[(2*i)+1] < temp =" a[(2*i)+1];"> a[i] && (2*i+1)<=n && i<=n)
{
temp = a[(2*i)+1];
a[(2*i)+1] = a[i];
a[i] = temp;
}
}
}

Related Links :

Dijkstra Algorithm

#include "stdio.h"
#include "conio.h"
#include "graphics.h"
#include "stdlib.h"
void fillcol(int ltx,int lty ,int col)
{
int i,j;
lty=lty+1;
ltx=ltx+1;
for(j=0;j<69;j++)
{
for(i=0;i<69;i++)
{
putpixel(ltx+i,lty,col);
}
lty=lty+1;
}
}
void draw(int *var,int len)
{
int gm,gd=DETECT,i,j,x=10,y=10,temp=10,k,data[17][2],l,count=1,tt;
initgraph(&gd,&gm,"c:\tc\bgi");
for(i=0;i<4;i++)
{
temp=temp+70;
y=temp;
x=10;
for(j=0;j<4;j++)
{
setcolor(3);
data[count][0]=x;
data[count][1]=y;
count++;
rectangle(x,y,x+70,y+70);
fillcol(x,y,4);
x=x+70;
}
}
for(i=0;i<=len;i++)
{
tt = var[i];
rectangle(data[tt][0],data[tt][1],data[tt][0]+70,data[tt][1]+70);
fillcol(data[tt][0],data[tt][1],0);
}
setcolor(8);
outtextxy(35,100,"1");
outtextxy(105,100,"2");
outtextxy(175,100,"3");
outtextxy(235,100,"4");
outtextxy(35,170,"5");
outtextxy(105,170,"6");
outtextxy(175,170,"7");
outtextxy(235,170,"8");
outtextxy(35,240,"9");
outtextxy(105,240,"10");
outtextxy(175,240,"11");
outtextxy(235,240,"12");
outtextxy(35,310,"13");
outtextxy(105,310,"14");
outtextxy(175,310,"15");
outtextxy(235,310,"16");
setcolor(13);
settextstyle(6, HORIZ_DIR, 4);
outtextxy(250,10,"INTELLIGENT DRIVING");
}
void main()
{
int cost[17][17],dist[17],s[17],prev[17];
int sp ,ep,i,j,k,t=0,path[20],dec,temp,min=32767,u;
clrscr();
for(i=0;i<20;i++)
{
path[i]=0;
}
for(i=0;i<17;i++)
{
for(j=0;j<17;j++)
{
if(i==j)
{
cost[i][j]=0;
}
if( abs(j-i)==4)
{
cost[i][j]=cost[j][i]=1;
}
if( abs(i-j)==1)
{
cost[i][j]=cost[j][i]=1;
}
if(i!=j && abs(j-i)!=4 && abs(i-j)!=1)
{
cost[i][j]=cost[j][i]=32767;
}
}
}
printf("enter the source name
");
scanf("%d",&sp);
printf("enter the destination name
");
scanf("%d",&ep);
for(i=0;i<17;i++)
{
prev[i]=sp;
cost[0][i]=0;
cost[i][0]=0;
}
for(i=0;i<17;i++)
{
dist[i]=0;
s[i]=0;
}
for(i=1;i<=16;i++)
{
dist[i]=cost[sp][i];
}
s[sp]=1;
dist[sp]=0;
for(i=2;i<=16;i++)
{
for(k=1;k<=16;k++)
{
if(min > dist[k] && s[k]!=1)
{
min=dist[k];
u=k;
break;
}
min=32767;
}
s[u]=1;
for(j=1;j<=16;j++)
{
if(cost[u][j]==1)
{
if(s[j]==0)
{
if(dist[j] > (dist[u]+ cost[u][j]))
{
dist[j]=dist[u]+cost[u][j];
prev[j]=u;
}
}
}
}
}
clrscr();
t=ep;
dec=dist[ep];
path[dec]=t;
for(i=0;i {
dec--;
t=prev[t];
path[dec]=t;
}
draw(&path[0],dist[ep]);
getch();
closegraph();
}


Related Links :

Circular Linked List in C

#include:"stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "alloc.h"
#define null 0
struct node
{
int info;
struct node *link;
}*start;
void main()
{
int ch,n,m,position,i;
last=null;
while(1)
{
printf("1.create
2.addat
3.addbt
4.del
5.disp
6.exit
");
printf("er ur ch");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("er no of itc");
scanf("%d",&n);
for(i=0;i{
printf("er the element");
scanf("%d",&m);
create(m);
}break;
case 2:
printf("er the element");
scanf("%d",&m);
addat(m);
break;
case 3:
printf("er the element");
scanf("%d",&m);

printf("er the position");
scanf("%d",&position);
addbt(m,position);
break;
case 4:
if(last==null)
{
printf("list is empty");
continue;
}
printf("er the element for delete");
scanf("%d",&m);
del(m);
break;
case 5:
disp();
break;
case 6:
exit(0);
break;
default:
printf("wrong choice");
}
}
}
create(int data)
{
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=null;
if(last==null)
{
last=tmp;
tmp->link=last;
}
else
{
tmp->link=last->link;
last->link=tmp;
last=tmp;
}}


addat(int data)
{

struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=last->link;
last->link=tmp;
}
addbt(int data,int pos)
{
struct node *tmp,*q;
int i;
q=last->link;;
for(i=0;i{
q=q->link;
if(q==last->link)
{
printf("there r lessthan %d elements",pos);
return;
}
}
tmp=(struct node *)malloc(sizeof(struct node));
tmp->link=q->link;
tmp->info=data;
q->link=tmp;
if(q==last)
last=tmp;
}
del(int data)
{
struct node *tmp,*q;
if(last->link==last&&last->info==data)
{
tmp=last;
last=null;
free(tmp);
return;
}
q=last->link;

if(q->info==data)
{
tmp=q;
last->link=q->link;
free(tmp);
return;
}
while(q->link!=last)
{

if(q->link->info==data)
{
tmp=q->link;
q->link=tmp->link;
free(tmp);

printf("element %d is deleted",data);
}
if(q->link->info=data)
{
tmp=q->link;
q->link=last->link;
free(tmp);
last=q;
return;}
printf("element%d is not found",data);
}
disp()
{
struct node *q;
if(last==null)
{
printf("list isdempty");
return;
}q=last->link;
while(q!=last)
{
printf("%d",q->info);
q=q->link;
}
printf("%d",last->info);
}

Related Links :

A Calculator in C Using Graphics & Mouse Operations

#include"graphics.h"
#include"dos.h"
#include"stdio.h"
#include"math.h"
union REGS i,o;
char *text[]={ "7","8","9","*",
"4","5","6","/",
"1","2","3","+",
"0","00",".","-",
"M","M+","M-","+/-",
"MR","MC","x^2","sr",
"OFF","AC","CE","="};

int k=0,pass,op,prop,newnum=1,bt,memo=1,d=0,sq;
long double num=0,accum,m;


void normalbutton(int ,int ,int ,int,char**);

void main()

{
int gd=DETECT,gm,x1,x2,y1,y2,i,j,maxx,maxy,x,y,button;
char *text1[]={""","T","o"," ","K","n","o","w"," ","a","b","o",
"u","t"," ","m","e"," ","l","o","g","o","n"," ",":"};
char *text2[]={"w","w","w",".","g","e","o","c","i","t","i","e","s",
".","c","o","m","/","t","a","l","k","d","e","e","p",
"e","s","h"};

initgraph(&gd,&gm,"");

if(initmouse()==0)
{
closegraph();
restorecrtmode();
printf("
Mouse driver not loded");
exit(1);
}
showmouseptr();
// x=y=50;
movemouseptr(&x,&y);

setbkcolor(11);
setcolor(1);
rectangle(198,140,417,163);
rectangle(199,141,418,164);
rectangle(197,139,416,162);
rectangle(185,130,430,450);
rectangle(184,129,431,451);
rectangle(182,127,433,454);
rectangle(181,126,434,453);


//setfillstyle(SOLID_FILL,3);
//bar(200,142,415,161);

outtextxy(200,50,"A Calculator in C By Deepesh Jain");
outtextxy(200,100,"Press OFF button to exit....");
y1=140;
y2=160;

for(j=0;j<7;j++) x1="200;" x2="235;" i="0;i<4;i++)" y1="140;" y2="160;">400&&x<450)>400&&y<420) j="0;j<7;j++)" x1="200;" x2="235;" i="0;i<4;i++)">x1)&&(yy1))
{
if((button & 1)==1)
{ gotoxy(28,10);
// printf("%d",ch=*text[j*4+i]);
// printf("char is %c",ch);
bt=j*4+i;
// printf("char is %d",j*4+i);
setcolor(11);
outtextxy(x1+12,y1+7,text[j*4+i]);

if(num>pow(10.0,18))
exit();
sound(500);delay(10);nosound();
delay(250);
sound(400);delay(10);
nosound();
switch (bt)
{
case 8 :
addnum(1);
break;
case 9 :
addnum(2);
break;
case 10 :
addnum(3);
break;
case 4 :
addnum(4);
break;
case 5 :
addnum(5);
break;
case 6 :
addnum(6);
break;
case 0 :
addnum(7);
break;
case 1 :
addnum(8);
break;
case 2 :
addnum(9);
break;
case 12 :
addnum(0);
break;
case 11 :
// plus
operation(1);
break;

case 15 :
// minus
operation(2);
break;
case 3 :
// multiplication
operation(3);
break;
case 7 :
// division
operation(4);
break;
case 13:
doublezero();
break;
case 14 :
decimal();
break;

case 16:
mem();
break;
case 20:
recallmem();
break;
case 19:
plusminus();
break;
case 17:
plusm();
break;
case 18:
minusm();
break;
case 21:
clearm();
break;
case 22 :
square();
break;
case 23:

sqroot();
break;
case 24:
// OFF
hidemouseptr();
setcolor(1);
for(j=0;j<20;j++)
{
for(i=75;i<481;i+=20)

line(0,0+i+j,640,j+0+i);

delay(100);
}
setcolor(14);
outtextxy(225,200,"Thanks for using it !");
delay(2000);
setcolor(13);
for(j=0;j<20;j++)
{
for(i=0;i<640;i+=20)

line(0+i+j,0,j+0+i,640);

delay(100);
}
setcolor(1);

for(i=0;i<25;i++)
{
outtextxy(75+10*i,200,text1[i]);
sound(3000);
delay(50);
nosound();
}
for(i=0;i<29;i++)
{
outtextxy(125+10*i,225,text2[i]);
sound(3000);
delay(50);
nosound();
}
// outtextxy(200,225,"www.geocities.com/talkdeepesh ");

delay(2500);
sound(5000);
delay(10);
exit();
break;
case 25:
allclear();
break;

case 26:
clear();
break;

case 27:
// equalto
operation(5);
break;



}
setcolor(1);
outtextxy(x1+12,y1+7,text[j*4+i]);

}
}
x1+=60;
x2+=60;

}

}

}



}

void normalbutton(int x1,int x2,int y1,int y2,char **text)
{
setcolor(15);
rectangle(x1-2,y1-2,x2+1,y2+1);
rectangle(x1-1,y1-1,x2+2,y2+2);
setcolor(7);
rectangle(x1,y1,x2+2,y2+2);
rectangle(x1,y1,x2+1,y2+1);

setfillstyle(SOLID_FILL,14);
bar(x1,y1,x2,y2);
setcolor(1);
outtextxy(x1+12,y1+7,text[k]);
k++;
}
/* initmouse */
initmouse()
{
i.x.ax=0;
int86 (0x33,&i,&o);
return(o.x.ax);
}
hidemouseptr()
{
i.x.ax=2;
int86(0x33,&i,&o);
}

/* displays mouse pointer */
showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
return 0;
}

/*gets mouse coordinates and button status*/

getmousepos(int *button,int *x,int *y)
{
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
return 0;
}
/* Move mouse ptr to x,y */
movemouseptr(int *x,int *y)
{
i.x.ax=4;
int86(0x33,&i,&o);
o.x.cx=*x;
o.x.dx=*y;
return 0;
}

addnum(int pass)
{ if(sq)
newnum=1;

if(newnum)
{ if(d)
{
num=pass/(pow(10.0,d));
d++;
newnum=0;
}
else
{ num=pass;
newnum=0;
}
}
else
{
/* if(num==0)
{ if(d)
{
num=num+pass/(pow(10.0,d));
d++;
}
else
num=pass;
}
*/
// else
{ if(d)
{
if(num<0)
num=num-pass/(pow(10.0,d));
else
num=num+pass/(pow(10.0,d));
d++;
}
else
{
num=num*10+pass;
}
}
}
printf("%25.5Lf",num);
}

operation(int opr)
{ long double pnum;
pnum=num;

if(newnum && (prop != 5) && memo)
{
}
else
{ newnum=1;
d=0;
sq=0;
switch(prop)
{
case 1:
accum=accum+pnum;
break;
case 2:
accum=accum-pnum;
break;
case 3:
accum=accum*pnum;
break;
case 4:
accum=accum/pnum;
break;
default:
accum=pnum;
}
}
prop=opr;
num=accum;
printf("%25.5Lf",num);
}

allclear()
{
sq=0;
accum=0;
num=0;
d=0;
newnum=1;
printf("%25.5Lf",num);
}

mem()
{
m=num;
}
recallmem()
{
memo=0;
printf("%25.5Lf",m);
num=m;
}
plusminus()
{ if(num!=0)
{ num*=-1;
printf("%25.5Lf",num);
}
}


plusm()
{
m+=num;
}
minusm()
{
m-=num;
}
clearm()
{
m=0;
}
decimal()
{
if(!d)
{d=1;
if(newnum==1)
{
num=0;
}
printf("%25.5Lf",num);
}
}

square()
{ sq=1;
num*=num;
printf("%25.5Lf",num);
// newnum=1;
}
sqroot()
{ sq=1;
num=pow(num,0.5);
printf("%25.5Lf",num);
// newnum=1;
}
doublezero()
{
if(d)
{
// num=num+pass/(pow(100.0,d));
d++;
d++;
}
else
num*=100;
printf("%25.5Lf",num);
}

clear()
{
num=0;
printf("%25.5Lf",num);
}

Related Links :

Add two long positive intergers

#include "stdio.h"
#include "alloc.h"
#include "conio.h"
#include "ctype.h"
struct node
{
int data;
struct node*next;
};
void insert(struct node**p,int num)
{
struct node*temp;
if(*p==NULL)
{
(*p)=(struct node*)malloc(sizeof(struct node));
(*p)->next=NULL;
(*p)->data=num;
}
else
{
temp=(struct node*)malloc(sizeof(struct node));
temp->next=(*p);
(*p)=temp;
(*p)->data=num;
}
}


void add_in(struct node*a,struct node*b,struct node**c)
{
int d,carry;
carry=0;
struct node*t;
while(a!=NULL&&b!=NULL)
{
d=(a->data+b->data+carry)%10;
insert(c,d);
if( (a->data+b->data+carry) >= 10)
{
carry=1;
}
else carry=0;
a=a->next;
b=b->next;
}
if(a==NULL&&b==NULL)
{
return;
}
else
{
if(a!=NULL&&b==NULL)
{
t=a;
}
else
{
t=b;
}
while(t!=NULL)
{
d=(carry+t->data)%10;
if((carry+t->data)>=10)
carry=1;
else
carry=0;
insert(c,d);
t=t->next;
}
if(carry==1)
insert(c,carry);
}
}
void numin(struct node**p)
{
*p=NULL;char c='c';
while(c!='n')
{
c=getch();
if(!isdigit(c))
return;
else
{
putch(c);
insert(p,c-'0');
}
}
}
void disp(struct node*p)
{
if(p==NULL)
return;
else
{
printf("%d",p->data);
disp(p->next);
}

}
void main()
{
struct node *a,*b,*c;
clrscr();
a=b=c=NULL;
printf("Enter the first number....
");
numin(&a);
printf("
Enter the second number....
");
numin(&b);
printf("
The added result is...
");
add_in(a,b,&c);
disp(c);
getch();
}

Related Links :

Program calculates details of a Triangle given the lengths of its sides

#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "math.h"

main()
{
clrscr();
float a,b,c,S,D,A,B,C,Area,R;
printf("Enter the lengths of the three sides of the triangle :");
scanf("%f%f%f",&a,&b,&c);

S = (a+b+c)/2.0; // S is the semiperimeter of the triangle
D = S*(S-a)*(S-b)*(S-c);//D is the square of the area of the triangle
if(D<=0)
{ printf("The triangle cannot be formed");
getch();
exit(0);
} if((a==b || b==c || c==a) && !(a==b && b==c && c==a)) // this complex logic is to eliminate interpretting a triangle with all three // sides equal as both isosceles and equilateral.
printf("The triangle is ISOSCELES");
if(a==b && b==c && c==a)
printf("The triangle is EQUILATERAL");
if(a!=b && b!=c && c!=a)
printf("The triangle is SCALENE");
Area = sqrt(D); R = (a*b*c)/(4.0*Area);
printf("PERIMETER = %.2f units ",(2.0*S));
printf("AREA = %.2f sq.units ",Area);
printf("CIRCUM RADIUS = %.2f units",R); // using sine rule,we get... A = (180.0/3.1415926)*asin(a/(2.0*R));// value of pi should be upto 7 B = (180.0/3.1415926)*asin(b/(2.0*R));// decimal places of accuracy and also C = (180.0/3.1415926)*asin(c/(2.0*R));// note that the 7th decimal place is // 6 and not 7 as it had to be if were if(A==90.0 || B==90.0 || C==90.0) // approximated to 7 decimal places
printf(" The triangle is RIGHT ANGLED ");
if(A<90.0>
printf("The triangle is ACUTE ANGLED");
if(A>90.0 || B>90.0 || C>90.0)
printf("The triangle is OBTUSE ANGLED");
printf("The angles are as follows :");
printf("A = %.2f degrees",A);
printf("B = %.2f degrees",B);
printf("C = %.2f degrees",C);
printf("Where A,B,C stand for angles opposite to sides
%.2f,%.2f,%.2f",a,b,c);
printf(" respectively");

getch();
return 0;
}

Related Links :

Program to calculate Area of a Polygon

#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#define MAX_VERT 50
enum {x, y};
typedef struct triangle
{
double v1[2];
double v2[2];
double v3[2];
} triangle;
double area(triangle a);
double perimeter(double *vertices, int size);
double side(double *p1, double *p2);

int main(void)
{
int n, idx;
int triangles;
int index;
int xycount;
double xy;
double triangle_area;
double polygon_area;
double perim;
double polygon_vertices[MAX_VERT] = {0.0};
triangle a;
FILE *data;

xycount = 0;
polygon_area = 0;
if((data = fopen("pvert.txt", "r")) == NULL)
{
fprintf(stderr, "can't open data file
");
exit(EXIT_FAILURE);
}

/* Read x-y coordinates of the vertices
of the polygon from a file. */
while(fscanf(data, "%lf", &xy) == 1)
polygon_vertices[xycount++] = xy;
fclose(data);
idx = 0;
/* triangles in polygon = vertices - 2 */
triangles = (xycount / 2) - 2;
putchar('
');

for(index = 2, idx = 0; idx <>
{
/* Load vertices of a triangle into struct.
1st vertex of the polygon will be the 1st
vertex of each triangle. index holds the
starting index of each consecutive set of
triangle vertices after the 1st. */
a.v1[x] = polygon_vertices[0];
a.v1[y] = polygon_vertices[1];
a.v2[x] = polygon_vertices[index+0];
a.v2[y] = polygon_vertices[index+1];
a.v3[x] = polygon_vertices[index+2];
a.v3[y] = polygon_vertices[index+3];

/* calculate the area of the triangle */
triangle_area = area(a);
printf("area of triangle = %.2f", triangle_area);

/* add triangle area to polygon area */
polygon_area += triangle_area;
}
printf("area of polygon = %.2f", polygon_area);

/* calculate the perimeter of the polygon */
perim = perimeter(polygon_vertices, xycount);
printf("perimeter of polygon = %.2f
", perim);

return 0;
}

/* calculate triangle area with Heron's formula */
double area(triangle a)
{
double s1, s2, s3, S, area;

s1 = side(a.v1, a.v2);
s2 = side(a.v2, a.v3);
s3 = side(a.v3, a.v1);
S = (s1 + s2 + s3) / 2;
area = sqrt(S*(S - s1)*(S - s2)*(S - s3));

return area;
}

/* calculate polygon perimeter */
double perimeter(double *vertices, int size)
{
int idx, jdx;
double p1[2], p2[2], pfirst[2], plast[2];
double perimeter;

perimeter = 0.0;
/* 1st vertex of the polygon */
pfirst[x] = vertices[0];
pfirst[y] = vertices[1];
/* last vertex of polygon */
plast[x] = vertices[size-2];
plast[y] = vertices[size-1];
/* calculate perimeter minus last side */
for(idx = 0; idx <= size-3; idx += 2)
{
for(jdx = 0; jdx <>
{
p1[x] = vertices[idx];
p1[y] = vertices[idx+1];
p2[x] = vertices[idx+2];
p2[y] = vertices[idx+3];
}
perimeter += side(p1, p2);
}
/* add last side */
perimeter += side(plast, pfirst);

return perimeter;
}

/* calculate length of side */
double side(double *p1, double *p2)
{
double s1, s2, s3;

s1 = (p1[x] - p2[x]);
s2 = (p1[y] - p2[y]);
s3 = (s1 * s1) + (s2 * s2);

return sqrt(s3);
}

Related Links :

Program to find your Day of Birth given Date of Birth

#include
#include "stdlib.h"
#include "conio.h"

main()
{
clrscr();
int d,m,y,year,month,day,i,n;
printf("Enter how many times you want to run this program : ");
scanf("%d",&n);
for(i=1;i<=n;i++) { printf(" Enter the date : "); scanf("%d%d%d",&d,&m,&y); if( d>31 || m>12 || (y<1900>=2000) )
{
printf("INVALID INPUT");
getch();
exit(0);
}
year = y-1900;
year = year/4;
year = year+y-1900;
switch(m)
{
case 1:
case 10:
month = 1;
break;
case 2:
case 3:
case 11:
month = 4;
break;
case 7:
case 4:
month = 0;
break;
case 5:
month = 2;
break;
case 6:
month = 5;
break;
case 8:
month = 3;
break;
case 9:
case 12:
month = 6;
break;
}
year = year+month;
year = year+d;
day = year%7;
switch(day)
{
case 0:
printf("Day is SATURDAY");
break;
case 1:
printf("Day is SUNDAY");
break;
case 2:
printf("Day is MONDAY");
break;
case 3:
printf("Day is TUESDAY");
break;
case 4:
printf("Day is WEDNESDAY");
break;
case 5:
printf("Day is THURSDAY");
break;
case 6:
printf("Day is FRIDAY");
break;
}
}
getch();
return 0;
}

Related Links :

Roots of Quadratic EQUATION

#include "stdio.h"
#include "conio.h"
#include "math.h"
void main()
{
clrscr();
int a,b,c;
float d,r1,r2;
printf("ENTER THE VALUE OF A,B,C TO FORM THE QUADRATIC EQUATION AX^2+BX+C:");
printf("A=");
scanf("%d",&a);
printf("
B=");
scanf("%d",&b);
printf("
C=");
scanf("%d",&c);
d=(b*b)-4*a*c;
if(sqrt(d)<0)
printf("NO REAL ROOT EXISTS:");
else
{
printf("
REAL ROOTS EXIST:");
r1=((-b)+sqrt(d))/(2*a);
r2=((-b)-sqrt(d))/(2*a);
printf("THE REAL ROOTS OF THE EQUATION ");
printf("%d",a);
printf("x^2+");
printf("%d",b);
printf("x+");
printf("%d",c);
printf(" ARE :%f",r1);
printf(" AND %f",r2);
}
getch();
}

Related Links :

PROGRAM TO FIND SQUARE ROOT OF A NUMBER

#include "stdio.h"
#include "conio.h"
main()
{
float a,b,e=0.00001,p,k;
clrscr();
textcolor(GREEN);
do {
printf(" ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ
");
printf(" xDB PROGRAM TO FIND SQUARE ROOT OF A NUMBERxDB
");
printf(" ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ
");
cprintf("
ENTER A NUMBER(-1 to Quit) :");
scanf("%f",&k);

a=k;p=a*a;
while(p-k>=e)
{
b=(a+(k/a))/2;
a=b;
p=a*a;
}
printf("SQUARE ROOT IS = %f",a);
getch();
clrscr();
}while(k!=-1);
getch();
}

Related Links :

To generate cos series

# include "stdio.h"
# include "conio.h"
main()
{
int i,p,n;
clrscr();
printf("enter the number in term of series");
scanf("%d",&n);
printf("xcos(x)=1");
for(i=1;i<=n;i++)
{
p=2*i;
if(i%2==0)
printf("+");
else
printf("-");
printf(x^%d/%d!",p,p);
getch();
}
}

Related Links :

Transportation Cost Estimate

#include "stdio.h"
#include "conio.h"

main()
{
int flag=0,flag1=0;
int s[10],d[10],sn,eop=1,dm,a[10][10];
int i,j,sum=0,min,x[10][10],k,fa,fb;

clrscr();
/* Getting The Input For the Problem*/

printf("Enter the number of Supply
");
scanf("%d",&sn);
printf("Enter the number of Demand
");
scanf("%d",&dm);
printf("Enter the Supply Values
");
for(i=0;i=d[j]) //Check the supply greater than equal to
demand
{
x[i][j]=a[i][j]*d[j]; // Calculate amount * demand
s[i]=s[i]-d[j]; // Calculate supply - demand
j++; // Increment j for the deletion of the row
or
column
}

}
/* The Cost Matrix is Estimated here */
printf("Given Cost Matrix is :
");
for(fa=0;fa
{
for(fb=0;fb
{
printf("%d ",a[fa][fb]);
}
printf("
");
}
/* The Allocated Cost Matrix is */

printf("Allocated Cost Matrix is
");
for(fa=0;fa
{
for(fb=0;fb
{
printf("%d ",x[fa][fb]);
sum=sum+x[fa][fb];
}
printf("
");
}
/* Transportation Cost Estimated and Sum is Printed*/
printf("The Transportation cost:%d
",sum);
getch();
}


Related Links :

Volume & area of shapes

#include "maths.h"
#include "stdio.h"
#include "conio.h"
#define PI 3.14159
char ch;
main()
{

clrscr();

textcolor(4);
intro();
getch();
textcolor(7);
clrscr();
do
{
ch=menu();
switch(ch)
{
case 'a':
case 'A':
clrscr();
square();
getch();
break;
case 'b':
case 'B':
clrscr();
rect();
getch();
break;
case 'c':
case 'C':
clrscr();
circl();
getch();
break;
case 'd':
case 'D':
clrscr();
tri();
getch();
break;
case 'e':
case 'E':
clrscr();
rom();
getch();
break;
case 'f':
case 'F':
clrscr();
para();
getch();
break;

case 'g':
case 'G':
clrscr();
tra();
getch();
break;
case 'h':
case 'H':
clrscr();
qua();
getch();
break;
case 'i':
case 'I':
clrscr();
semicir();
getch();
break;
case 'j':
case 'J':
clrscr();
msector();
getch();
break;

case 'k':
case 'K':
clrscr();
sphere();
getch();
break;
case 'l':
case 'L':
clrscr();
cone();
getch();
break;
case 'm':
case 'M':
clrscr();
cyll();
getch();
break;

case 'n':
case 'N':
clrscr();
cube();
getch();
break;
case 'o':
case 'O':
clrscr();
cuboid();
getch();
break;
case 'p':
case 'P':
clrscr();
hemisphe();
getch();
break;

case 'q':
case 'Q':
exit(1);
}
} while(ch!='Q'||ch!='q');
getch();
}
intro()
{
int i;
clrscr();
printf("



");
textcolor(2);


cprintf("#################################################################
###############");
textcolor(4);
printf("



PROGRAM TO CALCULATE AREAS , VOLUMES ,
CIRCUMFERENCES ");
printf("

=====================================================
");
printf("
OF VARIOUS GEOMETRIC SHAPES");
printf("
===========================

");
textcolor(2);

cprintf("#################################################################
###############");
getch();

printf("





Program developed and designed
by...

");
printf("KALYAN SRINIVAS . PABOLU ");

}
menu()
{
clrscr();
textcolor(7);
printf(" MENU
Two Dimensional Shapes.

-----------------------

A.SQUARE
B.RECTANGLE

C.CIRCLE
D.TRIANGLE

E.RHOMBUS
F.PARALLELOGRAM

G.TRAPEZIUM
H.QUADRILATERAL.

I.SEMICERCLE
J.SECTOR
");
printf("
Three Dimensional Shapes.

-------------------------

K.SPHERE
L.CONE
M.CYLLINDER

N.CUBE
O.CUBOID
P.HEMISPHERE

Q.QUIT
Enter Your Choice :");
scanf("%c",&ch);
return(ch);
}

/***** SUB FUNCTIONS *****/
/***** 2 D SHAPES *****/

square()
{
float s,a,p;int i,j;
printf("
Enter side of square:");
scanf("%f",&s);
a=s*s;
p=4*s;
printf("
Perimeter of square : %.3f units",p);
printf("
Area of square : %.3f sq.units",a);
printf("
Square is ...
");
for(i=1;i<=s;i++)
{
textcolor(10);
for(j=1;j<=s;j++)
cprintf("ÛÛ");
printf("
");
}
return(0);
}
rect()
{
float a,p,l,b; int i,j;
printf("
Enter length and breadth of rectangle:
Length:");
scanf("%f",&l);
printf("
Breadth:");
scanf("%f",&b);
a=l*b;
p=2*(l+b);
printf("
Perimeter of rectangle : %.3f units",p);
printf("
Area of rectangle : %.3f sq.units",a);
printf("
Rectangle is...
");
for(i=1;i<=b;i++)
{
textcolor(4);
for(j=1;j<=l;j++)
cprintf("ÛÛ");
printf("
");
}
return(0);
}
tri()
{
float area,p;
float a,b,c,s;
printf("
Enter three sides of triangle:");
scanf( "%f%f%f",&a,&b,&c);
p=a+b+c;
s=p/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("
Perimeter of triangle : %.3f units",p);
printf("
Area of a triangle : %.3f sq.units",area);
}
rom()
{
float s,d1,d2,a,p;
printf("
Enter side and diagonals of a rhombus:
Side:");
scanf("%f",&s);
printf("
Diagonal :");scanf("%f",&d1);
printf("
Diagonal :");scanf("%f",&d2);
a=0.5*d1*d2;
p=4*s;
printf("
Perimeter of rhombus :%.3f units",p);
printf("
Area of rhombus :%.3f sq.units",a);
}
circl()
{
float r,a,p;
printf("Enter radius of circle:");
scanf("%f",&r);
a=PI * r * r;
p=2 * PI * r;
printf("
Circumference of circle : %.3f units",p);
printf("
Area of circle : %.3f sq.units",a);
}
para()
{
float a,p,base,h,l,b;
printf("Enter height,length,breadth of parallalogram :
" );
printf("
Height :"); scanf("%f",&h);
printf("
Base or Length :"); scanf("%f",&l);
printf("
Breadth :"); scanf("%f",&b);
base=l;
a=base*h;
p=2 * ( l + b );
printf("
Perimeter of parallalogram :%.3f units",p);
printf("
Area of parallogram :%.3f sq.units",a);

}


tra()
{
float a,b,d,are;
printf("Enter height and lengths of two parallel sides:
Height :");
scanf("%f",&d);
printf("Side:"); scanf("%f",&a);
printf("Side:"); scanf("%f",&b);
are=0.5 * d * (a+b);
printf("
Area of trapezium : %.3f sq.units",are);
}
qua()
{
float a,b,area,d;
printf("Enter diagonal and perpendicular distances from opposite
vertices:
");
printf("Diagonal :"); scanf("%f",&d);
printf("
Distance :"); scanf("%f",&a);
printf("
Distance :");scanf("%f",&b);
area= 0.5 * d * (a + b);
printf("
Area of quadrilateral : %.3f sq.units", area);
}
semicir()
{
float a,p,r;
printf("Enter radius of semicircle:");
scanf("%f",&r);
a=0.5* PI * r * r;
p= (PI * r ) + (2 * r);
printf("
Circumference of semicircle : %.3f units",p);
printf("
Area of semicircle : %.3f sq.units",a);
}

msector()
{
float x,r,temp,a,p;
printf("Enter radius and angle of sector:");
printf("
Radius :");
scanf("%f",&r);
printf("
Angle(in degrees) :");
scanf("%f",&x);
temp= x/360;
a= temp * (PI * r * r);
p= temp * (2 * PI * r);
printf("
Circumference of sector : %.3f units",p);
printf("
Area of sector : %.3f sq.units",a);
}

/******** 3 DIMENSIONAL SHAPES *********/

sphere()
{
float lsa,tsa,v,r;
printf("Enter radius of sphere :");
scanf("%f",&r);
tsa=4*PI*r*r;
v=(4.0/3.0)*PI*r*r*r;
printf("
Total surface area of sphere :%.3f sq.units",tsa);
printf("
Volume of sphere :%.3f cu.units",v);
}
cone()
{
float h,r,s ,v,tsa,lsa;
printf("Enter base radius ,height, slant height of cone :");
printf("
Radius :"); scanf("%f",&r);
printf("
Height :"); scanf("%f",&h);
printf("
Slant height :"); scanf("%f",&s);
tsa=PI * r *(s+r);
lsa=PI * r * s;
v=(PI * r * r * h)/3;
printf("
Total surface area of cone :%.3f sq.units",tsa);
printf("
Lateral surface area of cone :%.3f sq.units",lsa);
printf("
Volume of cone :%.3f cu.units",v);
}
cyll()
{
float lsa,tsa,v,r,h;
printf("Enter height and radius of cyllinder");
printf("Height :"); scanf("%f",&h);
printf("Radius :"); scanf("%f",&r);
lsa=2*PI*r*h;
tsa=2*PI*r*(h+r);
v=PI*r*r*h;
printf("
Total surface area of cyllinder :%.3f sq.units",tsa);
printf("
Curved surface area of cyllinder :%.3f sq.units",lsa);
printf("
Volume of cyllinder :%.3f cu.units",v);
}
cube()
{
float lsa,tsa,v,s,d;
printf("Enter side of cube :");
scanf("%f",&s);
d=s*sqrt(3);
lsa=4 * s * s;
tsa=6 * s * s;
v= s * s * s;
printf("
Diagonal of cube :%.3f units",d);
printf("
Total surface area of cube :%.3f sq.units",tsa);
printf("
Lateral surface area of cube :%.3f sq.units",lsa);
printf("
Volume of cube :%.3f cu.units",v);
}
cuboid()
{
float lsa,tsa,v,l,b,d,h;
printf("Enter length,breadth,height of cuboid :");
printf("
Length :"); scanf("%f",&l);
printf("
Breadth :"); scanf("%f",&b);
printf("
Height :"); scanf("%f",&h);
d=sqrt(l*l + b*b + h*h );
lsa =2 * h *( l+b );
tsa = lsa + 2 * l * b;
v=l*b*h;
printf("
Diagonal of cuboid :%.3f units",d);
printf("
Total surface area of cuboid :%.3f sq.units",tsa);
printf("
Lateral surface area of cuboid :%.3f sq.units",lsa);
printf("
Volume of cuboid :%.3f cu.units",v);
}
hemisphe()
{
float lsa,tsa,v,r;
printf("Enter radius of hemisphere :");
scanf("%f",&r);
tsa=3*PI*r*r;
lsa=2*PI*r*r;
v=(2.0/3.0)*PI*r*r*r;
printf("
Total surface area of hemisphere :%.3f sq.units",tsa);
printf("
Lateral surface area of hemisphere :%.3f sq.units",lsa);
printf("
Volume of hemisphere :%.3f cu.units",v);
}

Related Links :


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!