Program to create a linked list. If the inserted value is an even number, the number is inserted at the beginning. If the inserted number is odd, it i

Program to create a linked list. If the inserted value is an even number, the number is inserted at the beginning. If the inserted number is odd, it is inserted at the end.


#include
#include

typedef struct listnode
{
struct listnode * next;
int value;
}node;

Node * Node_construct(int v)
{
Node * n = malloc(sizeof(node));
n -> value = v;
n -> next = 0;
return n;
}

void Node_destruct(Node * n)
{
free(n);
}

void List_instert(Node ** h, int v)
{
Node * n = Node_construct(v);
n -> next = h;
return n;
}
\


void List_delete(Node ** h, int v)
{
/*delete the first occurrence of the value v*/

if ((*h) == 0)
{return;}
Node *p = *h;

if((p -> value) == v)
{
/* if it is v, move the head to the next Node and delete the original head*/

return;
}
/*Otherwise, find the first Node whose value is v */

/* If no Node whose value is v, do nothing. If a Node is found, delete it from the list */

/* If a Node is found, destroy the Node and release memory */
}

void List_destruct(Node ** h)
{
Node *p = *h;
node *q;
while (p != 0)
{
q = p -> next;
Node_destruct(p);
p=q;
}
}

void Node_print(Node *n)
{ printf("%d", n -> value);}
void List_print(Node *n)
{
Node *p = n;
while (p != 0)
{
Node_print(p);
p = p -> next;
}
printf("\n\n");
}
int main(int argc, char * argv[])
{
int x[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 7, 4, 2, 0};
int n = sizeof(x) / sizeof(int);
int i;
Node * head = 0;
for (i = 0; i < n =" Node_construct(v);" h ="="" h=" n;}" 2 ="="" head =" n;}"> next != NULL)
{n = n -> next;}
/* make the new Node the last Node in the list */
n -> next = n;
}
}

Related Links :

How can i decide which sorting method to use in c ( Ex : Bubble Sort, Merge Sort)

Well Friends, to deside which method shout be used you should ask Some important questions you should ask yourself are:

- How expensive are comparisons?
- Are comparisons monolithic, or is there a useful notion of partial comparison?
- How many items are there to sort?
- Are the items basically in random order?
- How limited is memory?
- How bad are cache misses?

To use the answers to those questions, you'll need to be familiar with sorting methods. I'm not going to teach you; there's lots of accessible literature on the subject. But some quick examples of how these criteria might influence your choice of algorithm:

- If there are very few items, then the fast algorithms don't pay for the overhead. Insertion sort is probably the way to go.
- If the cost of comparisons dwarfs everything else, then you may want to minimize the number of comparisons. It's hard to do better than something like insertion sort with binary search to find the insertion location.
- But if you can do partial comparisons, like with strings, then radix methods are an obvious candidate.
- If the problem has no outstanding characteristics, then quicksort is a good first choice, but it has some gotchas. You can read about that in a textbook.

Related Links :

Program showing use of Pointers and functions in C



#include
int* do_balagan(int* a, int* b);

int main(void)
{
int a,b;
int* c;
a=5;
b=7;
c = do_balagan(&a, &b);
printf("In main:\n");
printf("a=%d b=%d *c=%d\n", a, b, *c);
return 0;
}

int* do_balagan(int* a, int* b)
{
int* c;
c = b;
b = a;
a = c;
*a = *a + 5;
*b = *a + *b;
printf("In balagan:\n");
printf("*a=%d *b=%d *c=%d\n", *a, *b, *c);
return c;
}

Related Links :

Program to show Dynamic memory allocation getline.


#include
#include
int getline(char line[], int max)
{
int nch = 0, c;
max = max - 1; /* leave room for '\0' */

while ((c = getchar()) != EOF)
{
if (c == '\n') break;
if (nch < max)
{
line[nch] = c;
nch++;
}
}

if (c == EOF && nch == 0)
return EOF;

line[nch] = '\0';
return nch;
}

int main()
{
char * line;
int linelen;
printf("Specify the sentence length:\n");
scanf("%d", &linelen);
if (linelen <= 0)
{
printf("ERROR: line length must be positive!\n");
return 1;
}
line = (char *) calloc(linelen, sizeof(char));
//line = (char *) malloc(linelen);
if (line == NULL)
{
printf("ERROR: out of memory!\n");
return 1;
}
flushall();
printf("\nPlease type your sentence:\n");
getline(line, linelen);
printf("\nYou typed:\n");
printf("%s\n", line);
return 0;
}

Related Links :

List of Input/Output Functions in C

printf ()
Formatted printing

scanf ()
Formatted input analysis

getchar()
Get one character from stdin file buffer

putchar()
Put one charcter in stdout file buffer

gets ()
Get a string from stdin

puts ()
Put a string in stdout

fprintf()
Formatted printing to general files

fscanf()
Formatted input from general files

fgets()
Get a string from a file

fputs()
Put a string in a file

fopen()
Open/create a file for high level access

fclose()
Close a file opened by fopen()

getc()
Get one character from a file (macro?)

ungetc();
Undo last get operation

putc()
Put a character to a file (macro?)

fgetc()
Get a character from a file (function)

fputc()
Put a character from a file (function)

feof()
End of file . returns true or false

fread()
Read a block of characters

fwrite()
Write a block of characters

ftell()
Returns file position

fseek()
Finds a file position

rewind()
Moves file position to the start of file

fflush()
Empties file buffers

open()
Open a file for low level use

close()
Close a file opened with open()

creat()
Create a new file

read()
Read a block of untranslated bytes

write()
Write a block of untranslated bytes

rename()
Rename a file

unlink()
Delete a file

remove()
Delete a file

lseek()
Find file position

Related Links :

List of scanf conversion specifers (%d,%x,%if)

  1. d=Denary integer
  2. ld=Long int
  3. x=Hexadecimal integer
  4. o=Octal integer
  5. h=Short integer
  6. f=Float type
  7. lf=Long float or double
  8. e=Float type
  9. le=Double
  10. c=Single character
  11. s=Character string

Related Links :

List of Function in Maths Library of C, math.h

ABS(x)
Return absolute (unsigned) value. (macro)
fabs(x)
Return absolute (unsigned) value. (Function)
ceil(x)
Rounds up a "double" variable
floor(x)
Rounds down (truncates) a "double" variable.
exp(x)
Find exponent
log(x)
Find natural logarithm
log10(x)
Find logarithm to base 10
pow(x,y)
Raise x to the power y
sqrt(x)
Square root
sin(x)
Sine of (x in radians)
cos(x)
Cosine of (x in radians)
tan(x)
Tangent of (x in radians)
asin(x)
Inverse sine of x in radians
acos(x)
Inverse cosine of x in radians
atan(x)
Inverse tangent of x in radians
atan2(x,y)
Inverse tangent of x/y in radians
sinh(x)
Hyperbolic sine
cosh(x)
Hyperbolic cosine
tanh(x)
Hyperbolic tangent

Related Links :

List of reserved words in C

     auto d                       if
break int d
case long d
char d register d
continue return
default short d
do sizeof
double d static d
else struct
entry switch
extern d typedef d
float d union d
for unsigned d
goto while

also in modern implementations:

enum d
void d

const d
signed d
volatile d

Related Links :

Comparesion between C, Pascal, BASIC

   C                   Pascal              BASIC

= := =

== = =

*,/ *,/ *,/

/,% div, mod DIV, MOD

printf (".."); writeln ('..'); PRINT ".."
write ('..');

scanf ("..",a); readln (a); INPUT a
read (a);

for (x = ..;...;) for x := ...to FOR x = ...
{ begin

} end; NEXT x

while (..) while ...do N/A
{ begin
} end;

do N/A N/A
{
}
while (..);

N/A repeat REPEAT
until (..) UNTIL ..

if (..) ..; if ... then ... IF .. THEN..
else ...; else ....; ELSE

switch (..) case .. of N/A
{
case :
} end;

/* .... */ { ..... } REM .....

* ^ ? ! $

struct record N/A

union N/A N/A

Related Links :

lists of the decimal, octal, and hexadecimal numbers for characters

ecimal  Octal  Hexadecimal  Character

0 0 0 CTRL-@
1 1 1 CTRL-A
2 2 2 CTRL-B
3 3 3 CTRL-C
4 4 4 CTRL-D
5 5 5 CTRL-E
6 6 6 CTRL-F
7 7 7 CTRL-G
8 10 8 CTRL-H
9 11 9 CTRL-I
10 12 A CTRL-J
11 13 B CTRL-K
12 14 C CTRL-L
13 15 D CTRL-M
14 16 E CTRL-N
15 17 F CTRL-O
16 20 10 CTRL-P
17 21 11 CTRL-Q
18 22 12 CTRL-R
19 23 13 CTRL-S
20 24 14 CTRL-T
21 25 15 CTRL-U
22 26 16 CTRL-V
23 27 17 CTRL-W
24 30 18 CTRL-X
25 31 19 CTRL-Y
26 32 1A CTRL-Z
27 33 1B CTRL-[
28 34 1C CTRL-\
29 35 1D CTRL-]
30 36 1E CTRL-^
31 37 1F CTRL-_
32 40 20
33 41 21 !
34 42 22 "
35 43 23 #
36 44 24 $
37 45 25 %
38 46 26 &
39 47 27 '
40 50 28 (
41 51 29 )
42 52 2A *
43 53 2B +
44 54 2C ,
45 55 2D -
46 56 2E .
47 57 2F /
48 60 30 0
49 61 31 1
50 62 32 2
51 63 33 3
52 64 34 4
53 65 35 5
54 66 36 6
55 67 37 7
56 70 38 8
57 71 39 9
58 72 3A :
59 73 3B ;
60 74 3C < 3d =" 62">
63 77 3F ?
64 100 40 @
65 101 41 A
66 102 42 B
67 103 43 C
68 104 44 D
69 105 45 E
70 106 46 F
71 107 47 G
72 110 48 H
73 111 49 I
74 112 4A J
75 113 4B K
76 114 4C L
77 115 4D M
78 116 4E N
79 117 4F O
80 120 50 P
81 121 51 Q
82 122 52 R
83 123 53 S
84 124 54 T
85 125 55 U
86 126 56 V
87 127 57 W
88 130 58 X
89 131 59 Y
90 132 5A Z
91 133 5B [
92 134 5C \
93 135 5D ]
94 136 5E ^
95 137 5F _
96 140 60 `
97 141 61 a
98 142 62 b
99 143 63 c
100 144 64 d
101 145 65 e
102 146 66 f
103 147 67 g
104 150 68 h
105 151 69 i
106 152 6A j
107 153 6B k
108 154 6C l
109 155 6D m
110 156 6E n
111 157 6F o
112 160 70 p
113 161 71 q
114 162 72 r
115 163 73 s
116 164 74 t
117 165 75 u
118 166 76 v
119 167 77 w
120 170 78 x
121 171 79 y
122 172 7A z
123 173 7B {
124 174 7C |
125 175 7D }
126 176 7E ~
127 177 7F DEL

Related Links :

Program to show Bubble Sorting Algorithm in C




#include
#include

void bubblesort(int *, const int, char);
void swap(int *, int *);



int main()
{
const int i = 10;
int a[11];
char b;
printf("Enter a number:
");
int c;
for (c=0;c<10;c++)
{
printf("%d: ",c+1);
scanf("%d",&a[c]);
}

printf("Data Items in Original Order:");

for (c=0;c {
printf("%d",a[c]);
}

printf("Which order would you like?: ");
scanf("%s",&b);
if (b=='a')
{
bubblesort(a,i,'a');
printf("Data Items in Ascending Order:");
for (c=0;c {
printf("%d",a[c]);
}
}
else if(b=='d')
{
bubblesort(a,i,'d');
printf("Data Items in Decending Order:
");
for (c=0;c {
printf("%d",a[c]);
}
}
printf("Bubble Sorting By Pankaj A.H.World INDIA ");
return 0;
}

void bubblesort(int * array, const int size, char order)
{

order=tolower(order);
for (int pass=1;pass {
for (int j=0;j {
if (order=='a')
{
if (array[j]>array[j+1])
{
printf("%d : ",array[j]);
printf("%d
",array[j+1]);
swap(&array[j],&array[j+1]);
}
}
if (order=='d')
{
if (array[j] {
printf("%d : ",array[j]);
printf("%d
",array[j+1]);
swap(&array[j],&array[j+1]);
}
}
}
}
}



void swap(int *e1, int *e2)
{
int swap=*e1;
*e1=*e2;
*e2=swap;
}


Related Links :

Program to reverse a number by recursion Function




#include
long int rev(long int);
void main()
{
long int n;
clrscr();
printf("Enter number to reverse : ");
scanf("%ld",&n);
printf("Reversed Number is : %ld",rev(n));
getch();
}

long int rev(long int n)
{
long int i=1,x;
if(n==0){return 0;
}
else
{
while(i<=n)
{
i=i*10;
}
i=i/10;x=n%10;
return(x*i+rev(n/10));
}
}

Related Links :

A PROGRAM FOR MONETRING SYSTEM Blinking Graphics in C++




#include
#include
#include

main()
{
clrscr();

int port=0x378;
int i,n,j,t;

textcolor(14);
cout<<"
";
cprintf(" A PROGRAM FOR MONETRING SYSTEM ");
cout<<"

";
cprintf("
Enter the number of cycles -> ");
cin>>n;

cprintf("
Enter the time delay between each display -> ");
cin>>t;

j=t*100;

for(i=1;i<=n;i++)
{

outportb(port,1);
delay(j);
outportb(port,2);
delay(j);
outportb(port,4);
delay(j);
outportb(port,8);
delay(j);
outportb(port,16);
delay(j);
outportb(port,32);
delay(j);
outportb(port,64);
delay(j);
outportb(port,128);
delay(j);
}
outportb(port,0);
cprintf("

Request Completed... ");
cprintf("








Program developed by :");
textcolor(YELLOW+BLINK);
cprintf("

Pankaj A.H>");
getch();
}

Related Links :

PROGRAM TO CALCULATE AREA,VOLUME,PERIMETER OF DIFFERENT GEOMETRIC SHAPE (cirle, triangle, rombus, rectangle, parallelogram,quadrilatral,semicirc) IN C



#include
#include
#include
#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 OF SHAPES ");
printf("

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

");
textcolor(2);

cprintf("********************************************************************************************");
getch();

}
menu()
{
clrscr();
textcolor(7);
printf(" OPTION 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 Menu.

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

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 :

Program to calculate Area of a Polygon in C




/* This Program is used to calculate Area of a Polygon in C */
#include
#include
#include
#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, "Unable to 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 < triangles; index += 2, ++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 Details of Polygon*/
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 < 4; ++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 solve a 3 Variable Linear Equation in c.


#include
#include
#include
main()
{
clrscr();
float a,b,c,d,l,m,n,k,p,D,q,r,s,x,y,z;
printf("PROGRAM TO SOLVE THREE VARIABLE LINEAR SIMULTANEOUS
EQUATIONS");
printf("The equations are of theform:
ax+by+cz+d=0
lx+my+nz+k=0
px+qy+rz+s=0
");
printf("Enter the coefficients in the order a,b,c,d,l,m,n,k,p,q,r,s");
scanf("%f%f%f%f%f%f%f%f%f%f%f%f",&a,&b,&c,&d,&l,&m,&n,&k,&p,&q,&r,&s);
printf("The equations you have input are:");
printf(" %.2f*x + %.2f*y + %.2f*z + %.2f = 0",a,b,c,d);
printf(" %.2f*x + %.2f*y + %.2f*z + %.2f = 0",l,m,n,k);
printf(" %.2f*x + %.2f*y + %.2f*z + %.2f = 0",p,q,r,s);

D = (a*m*r+b*p*n+c*l*q)-(a*n*q+b*l*r+c*m*p);
x = ((b*r*k+c*m*s+d*n*q)-(b*n*s+c*q*k+d*m*r))/D;
y = ((a*n*s+c*p*k+d*l*r)-(a*r*k+c*l*s+d*n*p))/D;
z = ((a*q*k+b*l*s+d*m*p)-(a*m*s+b*p*k+d*l*q))/D;

printf("The solutions to the above three equations are :");
printf(" x = %5.2f
y = %5.2f
z = %5.2f
",x,y,z);
getch();
return 0;
}

Related Links :

Progam to gives all details of a Triangle when given the lengths of its sides.

#include
#include
#include
main()
{
clrscr();
float a,b,c,d,l,m,n,

Related Links :

how can you get a list all of the predefined identifiers?

Well Friends first thing is that there's no standard way, although it is a common need. gcc provides a -dM option which works with -E, and other compilers may provide something similar. If the compiler documentation is unhelpful, the most expedient way is probably to extract printable strings from the compiler or preprocessor executable with something like the Unix strings utility. Beware that many traditional system-specific predefined identifiers (e.g. ``unix'') are non-Standard (because they clash with the user's namespace) and are being removed or renamed. (In any case, as a general rule, it's considered wise to keep conditional compilation to a minimum.)

Related Links :

how to create a multi-statement macro in C?

MACRO(arg1, arg2);

This means that the ``caller'' will be supplying the final semicolon, so the macro body should not. The macro body cannot therefore be a simple brace-enclosed compound statement, because of the possibility that the macro could be used as the if branch of an if/else statement with an explicit else clause:


if(cond)
MACRO(arg1, arg2);
else /* some other code */

If the macro expanded to a simple compound statement, the final, caller-supplied semicolon would be a syntax error:

if(cond)
{stmt1; stmt2;};
else /* some other code */

The traditional solution, therefore, is to use

#define MACRO(arg1, arg2) do {
/* declarations */
stmt1;
stmt2;
/* ... */
} while(0) /* (no trailing ; ) */

When the caller appends a semicolon, this expansion becomes a single statement regardless of context. (An optimizing compiler will remove any ``dead'' tests or branches on the constant condition 0, although lint may complain.)

(Another possibility might be

#define MACRO(arg1, arg2) if(1) {
stmt1;
stmt2;
}
else


but it is inferior, since it quietly breaks the surrounding code if the caller happens to forget to append the semicolon upon invocation.)

If all of the statements in the intended macro are simple expressions, with no declarations or loops, another technique is to write a single, parenthesized expression using one or more comma operators:

#define FUNC(arg1, arg2) (expr1, expr2, expr3)

Related Links :

Program to print the source code of Program itself as output in C



# include
main()
{
FILE *fp1;
char ch;
fp1=fopen("program.c","r");
while((ch=fgetc(fp1))!=EOF)
printf("%c",ch);
fclose(fp1);
}

Related Links :

Program in C to write even and odd integers into different files



#include
#include
void main()
{
FILE *all,*even,*odd;
int number,i,records;
printf("Enter the Number of Records...");
scanf("%d",& records);
all=fopen("numdata","w");
for(i=1;i<=records;i++)
{
scanf("%d",&number);
if(number==-1)break;
putw(number,all);
}
fclose(all);
all=fopen("numdata","r");
even=fopen("EVENNUMBER","w");
odd=fopen("ODDNUMBER","w");
while((number=getw(all))!=EOF)
{
if(number%2==0)
putw(number,even);
else
putw(number,odd);
}
fclose(all);
fclose(even);
fclose(odd);


even=fopen("EVENNUMBER","r");
odd=fopen("ODDNUMBER","r");
printf("THE EVEN NUMBERS ARE");
while((number=getw(even))!=EOF)
printf(" %4d",number);
printf(" THE ODD NUMBERS ARE");
while((number=getw(odd))!=EOF)
printf(" %4d",number);

fclose(even);
fclose(odd);
}

Related Links :

What is a macro in C? how to use macro?

A macro is a preprocessor directive that provides a mechanism for token replacement in your source code. Macros are created by using the #define statement.


example of a macro:

Macros can also utilize special operators such as the stringizing operator (#) and the concatenation operator (##).The stringizing operator can be used to convert macro parameters to quoted strings, as in the following example:

#define DEBUG_VALUE(v) printf(#v ? is equal to %d.n?, v)

In your program, you can check the value of a variable by invoking the DEBUG_VALUE macro:

...
int x = 20;
DEBUG_VALUE(x);
...

The preceding code prints ?x is equal to 20.? on-screen. This example shows that the stringizing operator used with macros can be a very handy debugging tool.

Related Links :

What is a pointer variable in C?

A pointer variable is a variable that may contain the address of another variable or any valid address in the memory.
Pointer variable Can store the address of other variables, so it tells compiler that value is stored at where & it access the value from that Address.

Related Links :

What is a static function in C?

Well Friends A static function is a function whose scope is limited to the current source file(working File). Scope refers to the visibility of a function or variable. If the function or variable is visible outside of the current source file, it is said to have global, or external, scope. If the function or variable is not visible outside of the current source file, it is said to have local, or static, scope.

Related Links :

Difference between storage class and storage variable ?

A storage class is an attribute that changes the behavior of a variable. It controls the lifetime, scope and linkage.

A storage class is an attribute that tells the scope of accessability and also changes the behaviour of variable.

There are five types of storage classes

1) auto

2) static

3) extern

4) register

5) typedef

there are five types of storage classes in C-programming.
1)auto(local)
2)extern(global)
3)static
4)registr(faster memory)
5)typedef(user defined data type).

Related Links :

Is it Possible to declare or define a variable in a C header?

Variables should not be defined in header files, because the header file can be included in multiple source files, which would cause multiple definitions of the variable. The ANSI C standard will allow multiple external definitions, provided that there is only one initialization. But because there?s really no advantage to using this feature, it?s probably best to avoid it and maintain a higher level of portability.

A global variable that must be accessed from more than one file can and should be declared in a header file. In addition, such a variable must be defined in one source file.

Related Links :

Program to make initialization of DMA Mode Through C



# include
# include
# include
# include
# include

void main()
{
clrscr();
union REGS regs;
int ans;
char arr[1000];

outp(0x3f2,0x1c); //Motor On
delay(200);
outp(0x3f5,0x0f); //Command Code
delay(200);
outp(0x3f5,0x00); //Command Code
delay(200);
outp(0x3f5,0x0b); //Cylinder no.
delay(200);
outp(0x3f5,0x08); //Sense Interrupt Command
delay(200);

ans=inp(0x3f5); //Reading ST0 in data register
// cout<
delay(100);
ans=inp(0x3f5); //pcn

cout<
outportb(0x12,0); /*initialization of DMA Mode*/
outportb(0x11,10); /*supplying Mode Byte*/
clrscr();
int ar=FP_OFF(arr);
int ar1=FP_SEG(arr);

printf(" %x",ar1);

regs.h.ch = (ar1)&(0x0f00);
printf(" %x",(regs.h.ch));
regs.x.ax = regs.h.ah+ar;
(regs.h.ch)++;
printf(" %x",regs.x.ax);
int z = (regs.x.ax) & (0x0f00);
cout<<""< getch();
outportb(0x04,regs.h.al);
outportb(0x04,regs.h.ah);
outportb(0x81,regs.h.ch);
outportb(0x05,1);
// dma end

outp(0x3f2,0x0c); //Motor off
return;

}

Related Links :

Program To Implement Recalibrate Command in C Starts FDD motor , check its status & turn off Motor



#include
#include
#include

void main()
{
int show;
clrscr();
//Put on the motor
outp(0x3f2,28);

// Check whether the FDC is ready
show=inp(0x3f4); //Read the status of MAIN STATUS REGISTER
show=(show&128);

if(show==128)//Check whether FDC is ready
{

//Input the command parameters
outp(0x3f5,7);//Enter command parameters
delay(200);
outp(0x3f5,0);//Enter Drive No.
delay(300);
}
// Check the status of data register
show=inp(0x3f5);
if(show==0)
printf("Succesfully executed Recalibrate command");
getch();
//Put off the motor
outp(0x3f2,0);

}

Related Links :

Program to show Tree Operations like INSERTION, INORDER, PREORDER, POSTORDER,TRAVERSAL




# include
# include
# include

struct node
{
struct node *left;
int data;
struct node *right;
} ;

void main()
{
void insert(struct node **,int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
struct node *ptr;
int will,i,num;
ptr = NULL;
ptr->data=NULL;
clrscr();
printf("Enter the number of Nodes you want to add to the tree.");
scanf("%d",&will);

/* Getting Input */
for(i=0;i {
printf("Enter the item");
scanf("%d",&num);
insert(&ptr,num);
}

getch();
printf("INORDER TRAVERSAL");
inorder(ptr);
getch();
printf("PREORDER TRAVERSAL");
preorder(ptr);
getch();
printf("POSTORDER TRAVERSAL");
postorder(ptr);
getch();
}



void insert(struct node **p,int num)
{


if((*p)==NULL)
{ printf("Leaf node created.");
(*p)=malloc(sizeof(struct node));
(*p)->left = NULL;
(*p)->right = NULL;
(*p)->data = num;
return;
}
else
{ if(num==(*p)->data)
{
printf("
REPEATED ENTRY ERROR
VALUE REJECTED");
return;
}
if(num<(*p)->data)
{
printf("Directed to left link.");
insert(&((*p)->left),num);
}
else
{
printf("Directed to right link.");
insert(&((*p)->right),num);
}
}
return;
}


/* Tree Traversal Functions*/
////////////// INORDER (LDR) ////////////

void inorder(struct node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("Data :%d",p->data);
inorder(p->right);
}
else
return;
}

////////////// PREORDER (DLR) ////////////

void preorder(struct node *p)
{
if(p!=NULL)
{
printf("Data :%d",p->data);
preorder(p->left);
preorder(p->right);
}
else
return;
}


////////////// POSTORDER (LRD) ////////////

void postorder(struct node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("Data :%d",p->data);
}
else
return;
}

Related Links :

Program to show Circular Queue implementation through Array in C



#include
#include
# define MAXSIZE 200

int cq[MAXSIZE];
int front,rear;

void main()
{
void add(int,int [],int,int,int);
int del(int [],int ,int ,int );
int will=1,i,num;
front = 1;
rear = 1;

clrscr();
printf("Program for Circular Queue through array in C");
while(will ==1)
{
printf("MAIN MENU:
1.Add element to Circular Queue
2.Delete element from the Circular Queue");
scanf("%d",&will);

switch(will)
{
case 1:
printf("Enter the Number... ");
scanf("%d",&num);
add(num,cq,MAXSIZE,front,rear);
break;
case 2: i=del(cq,MAXSIZE,front,rear);
printf("Value returned from delete function is %d ",i);
break;
default: printf("Invalid Choice . ");
}

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

void add(int item,int q[],int MAX,int front,int rear)
{
rear++;
rear= (rear%MAX);
if(front ==rear)
{
printf("CIRCULAR QUEUE FULL");
return;
}
else
{
cq[rear]=item;
printf("Rear = %d Front = %d ",rear,front);
}
}
int del(int q[],int MAX,int front,int rear)
{
int a;
if(front == rear)
{
printf("CIRCULAR STACK EMPTY");
return (0);
}
else
{
front++;
front = front%MAX;
a=cq[front];
return(a);
printf("Rear = %d Front = %d ",rear,front);
}
}

Related Links :

Program to create Queue implementation through Linked List in C.




#include
#include
struct node
{
int data;
struct node *link;
};
struct node *front, *rear;
void main()
{

int wish,will,a,num;
void add(int);

wish=1;
clrscr();
front=rear=NULL;

printf("Program to create Queue implementation through Linked List in C.");
while(wish == 1)
{
printf("Main Menu
1.Enter data in queue
2.Delete from queue
");
scanf("%d",&will);
switch(will)
{
case 1:
printf("Enter the Numbers");
scanf("%d",&num);
add(num);
//display();
break;
case 2:
a=del();
printf("Value returned from front of the queue is %d",a);
break;
default:
printf("Invalid choice");
}
printf("Do you want to continue, press 1");
scanf("%d",&wish);
}
getch();
}

void add(int y)
{
struct node *ptr;
ptr=malloc(sizeof(struct node));
ptr->data=y;
ptr->link=NULL;
if(front ==NULL)
{
front = rear= ptr;
}
else
{
rear->link=ptr;
rear=ptr;
}
}


int del()
{
int num;
if(front==NULL)
{
printf("QUEUE EMPTY");
return(0);
}
else
{
num=front->data;
front = front->link;
printf(" Value returned by delete function is %d ",num);
return(num);
}
}

Related Links :

Program to compare two input strings in C



#include
#include
#include
#include
char s1[80],s2[80];
int a,b;
int strcmp(char *s,char *t);
void main()
{
int c;
clrscr();
printf("Enter string 1 :- ");
gets(s1);
printf("Enter string 2 :- ");
gets(s2);
c=strcmp(s1,s2);
if (c==0)
printf("Strings are similar.");
else if (a>b)
{
printf("Strings are not similar.");
printf("First string is lenthier than the second string.");
}
else if (b>a)
{
printf("Strings are not similar.");
printf("First string is shorter than the second string.");
}
else
printf("Strings are not similar though they are equal in length.");
getch();
}

int strcmp(char s[],char t[])
{
int i;
a=strlen(s1);
b=strlen(s2);
if (a>b)
{
for(i=0;*s!=')
}

Related Links :

Program to create a pyramid of an input numbers in C.


main()
{
int n,row=1,col=40,i=0,j,k=0,count=1;
int a[10];
clrscr();
i=n-1;
printf("Pyramid of how many numbers? ");
scanf("%d",&n);
for (j=0;j {
printf("Enter no.:- ");
scanf("%d",&a[j]);
}
clrscr();
for (row=n;row>=1;row--)
{
k=0;
k=40-(4*(row-1));
i=row-1;
for (col=40;col>=k;col=(col-4))
{
gotoxy(col,row);
printf("%d",a[i]);
--i;
}
}

for (count=n;count>=1;count--)
{
k=0;
k=40+(4*(count-1));
i=count-1;
for (col=40;col<=k;col=(col+4))
{
gotoxy(col,count);
printf("%d",a[i]);
--i;
}
}
getch();
}

Related Links :

Program to Sort the Given numbers in Ascending or descending order In C



void sort(void);
int c,a[20],l;
void main()
{
clrscr();
printf("Enter no. of elements in the list:- ");
scanf ("%d",&l);
printf("CHOICE:-");
printf("(1) Sort in ascending order.");
printf("(2) Sort in descending order.");
printf("CHOICE:- ");
scanf("%d",&c);
if (c!=1 && c!=2)
{
printf("ERROR");
getch();
exit(0);
}
sort();
getch();
}


void sort(void)
{
int n,i,j,temp=0,min,k;
for (i=1;i<=l;i++)
{
printf("Enter no.:- ");
scanf("%d",&a[i]);
}
for (i=1;i<=(l-1);i++)
{
min=a[i];
k=i;
for (j=(i+1);j<=l;j++)
{
if (a[j] {
min=a[j];
k=j;
}
}
temp=a[k];
a[k]=a[i];
a[i]=temp;
}

switch(c)
{
case 1:
printf("Elements in ascending order are:-");
for (i=1;i<=l;i++)
printf("%d",a[i]);
break;
case 2:
printf("Elements in descending order are:-");
for (i=l;i>=1;i--)
printf("%d",a[i]);
break;
default:
printf("ERROR");
}
return;
}

Related Links :

Program to calculate number of vowels in a given 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("www.lernc.blogspot.com");
getch();
}

Related Links :

Program to print ASCII values of 0 to 127 characters



int main(void)
{
char i;
for(i=0;i<=127;i++)
printf("char: %c , ASCII value: %d",i,i);
return 0;
}

Related Links :

Program to show Memory Allocation by variables in Program & Arrays use in Program.




#include
#include
#define MAXROW 3
#define MAXCOL 4
void main() {
int (*p)[MAXCOL];
printf("\n%d",sizeof(*p));
//p=(int (*)[MAXCOL])
p=(int *)malloc(MAXROW * sizeof(*p));
printf("\n%d",sizeof(*p));
}

Related Links :

Programm to Read Garbage values & Set Values from RAM




#include
main() {
char *s;
char *fun();
s=fun();
clrscr();
printf("\nString is :%s",s);

/* puts(s); Prints Garbage */
}

char *fun() {
char buffer[30];
strcpy(buffer,"ROM - Read Only Memory");
printf("\nbuffer: %s",buffer);
return(buffer);
}

Related Links :

Program to read and write absolute disk sectors

(these will work with all versions of DOS 2-7).



#include
#include
#include "snpdskio.h"

int CDECL absdisk(unsigned char function,
unsigned short drive, /* 0 = A:, etc. */
size_t number_of_sectors,
size_t starting_sector,
void * sector_buffer);

int AbsDiskRead(unsigned short drive,
size_t num_of_sectors,
size_t sector,
void *ptr)
{
return absdisk(0x25, drive, num_of_sectors, (unsigned)sector, ptr);
}

int AbsDiskWrite(unsigned short drive,
size_t num_of_sectors,
size_t sector,
void *ptr)
{
return absdisk(0x26, drive, num_of_sectors, (unsigned)sector, ptr);
}




CODE FOR SNPDSKIO.H







#ifndef SNPDKSIO__H
#define SNPDKSIO__H

#include
#include
#include "extkword.h"

/*
** File: ABSDISKC.C and ABSDISK.ASM
*/

int CDECL absdisk(unsigned char function,
unsigned short drive, /* 0 = A:, etc. */
size_t number_of_sectors,
size_t starting_sector,
void * sector_buffer);

int AbsDiskRead(unsigned short drive,
size_t num_of_sectors,
size_t sector,
void *ptr);

int AbsDiskWrite(unsigned short drive,
size_t num_of_sectors,
size_t sector,
void *ptr);

/*
** File: HUGEREAD.C
*/

long hugeread(int fh, unsigned char FAR *buf, long size);
long hugewrite(int fh, unsigned char FAR *buf, long size);
long hugefread(FILE *fp, char FAR *buf, long size);
long hugefwrite(FILE *fp, char FAR *buf, long size);


#endif /* SNPDKSIO__H */



IMP : Save This Second File In your include folder...

Related Links :

Program to Convert ASCII to EBCDIC & EBCDIC to ASCII conversion by using Function



#include "a2e.h"

static unsigned char a2e[256] = {
0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 37, 11, 12, 13, 14, 15,
16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,
64, 79,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97,
240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111,
124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
215,216,217,226,227,228,229,230,231,232,233, 74,224, 90, 95,109,
121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
151,152,153,162,163,164,165,166,167,168,169,192,106,208,161, 7,
32, 33, 34, 35, 36, 21, 6, 23, 40, 41, 42, 43, 44, 9, 10, 27,
48, 49, 26, 51, 52, 53, 54, 8, 56, 57, 58, 59, 4, 20, 62,225,
65, 66, 67, 68, 69, 70, 71, 72, 73, 81, 82, 83, 84, 85, 86, 87,
88, 89, 98, 99,100,101,102,103,104,105,112,113,114,115,116,117,
118,119,120,128,138,139,140,141,142,143,144,154,155,156,157,158,
159,160,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
184,185,186,187,188,189,190,191,202,203,204,205,206,207,218,219,
220,221,222,223,234,235,236,237,238,239,250,251,252,253,254,255
};

static unsigned char e2a[256] = {
0, 1, 2, 3,156, 9,134,127,151,141,142, 11, 12, 13, 14, 15,
16, 17, 18, 19,157,133, 8,135, 24, 25,146,143, 28, 29, 30, 31,
128,129,130,131,132, 10, 23, 27,136,137,138,139,140, 5, 6, 7,
144,145, 22,147,148,149,150, 4,152,153,154,155, 20, 21,158, 26,
32,160,161,162,163,164,165,166,167,168, 91, 46, 60, 40, 43, 33,
38,169,170,171,172,173,174,175,176,177, 93, 36, 42, 41, 59, 94,
45, 47,178,179,180,181,182,183,184,185,124, 44, 37, 95, 62, 63,
186,187,188,189,190,191,192,193,194, 96, 58, 35, 64, 39, 61, 34,
195, 97, 98, 99,100,101,102,103,104,105,196,197,198,199,200,201,
202,106,107,108,109,110,111,112,113,114,203,204,205,206,207,208,
209,126,115,116,117,118,119,120,121,122,210,211,212,213,214,215,
216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,
123, 65, 66, 67, 68, 69, 70, 71, 72, 73,232,233,234,235,236,237,
125, 74, 75, 76, 77, 78, 79, 80, 81, 82,238,239,240,241,242,243,
92,159, 83, 84, 85, 86, 87, 88, 89, 90,244,245,246,247,248,249,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57,250,251,252,253,254,255
};

unsigned char ASCIItoEBCDIC(const unsigned char c)
{
return a2e[c];
}

unsigned char EBCDICtoASCII(const unsigned char c)
{
return e2a[c];
}






****************************************************************

you have to creatr a file a2e.h & save it in INCLUDE folder of your C Installed Directory.

Now Code for Header file a2e.h





#ifndef A2E__H
#define A2E__H

unsigned char ASCIItoEBCDIC(const unsigned char c); /* A2E.C */
unsigned char EBCDICtoASCII(const unsigned char c); /* A2E.C */

extern int ascii2ebcdic[256]; /* Toascii.C */
extern int ebcdic2ascii[256]; /* Toascii.C */

#endif /* A2E__H */

Related Links :

Program showing Recursion function Example in C




#include
/* Contains prototype for printf */
void count_dn(int count);
/* Prototype for count_dn */

int main()
{
int index;

index = 8;
count_dn(index);

return 0;
}


void count_dn(int count)
{
count--;
printf("The value of the count is %d\n", count);

if (count > 0)
count_dn(count);

printf("Now the count is %d\n", count);
}



/* Output of Program.

The value of the count is 7
The value of the count is 6
The value of the count is 5
The value of the count is 4
The value of the count is 3
The value of the count is 2
The value of the count is 1
The value of the count is 0
Now the count is 0
Now the count is 1
Now the count is 2
Now the count is 3
Now the count is 4
Now the count is 5
Now the count is 6
Now the count is 7

*/

Related Links :

Program to print Square of numbers by using user define function.




#include

int main() /* This is the main program */
{
int x, y;

for(x = 0 ; x < 8 ; x++)
{
y = squ(x); /* go get the value of x*x */
printf("The square of %d is %d\n", x, y);
}

for (x = 0 ; x < 8 ; ++x)
printf("The square of %d is %d\n", x, squ(x));

return 0;
}

squ(input) /* function to get the value of "input" squared */
int input;
{
int square;

square = input * input;
return(square); /* This sets squ() = square */
}



/* Output of Program

The square of 0 is 0
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
The square of 6 is 36
The square of 7 is 49
The square of 0 is 0
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
The square of 6 is 36
The square of 7 is 49

*/

Related Links :

Program showing use of strcpy, strcat, strcmp function



#include
#include

int main()
{
char name1[12], name2[12], mixed[25];
char title[20];

strcpy(name1, "Rosalinda");
strcpy(name2, "Zeke");
strcpy(title, "This is the title.");

printf(" %s\n\n", title);
printf("Name 1 is %s\n", name1);
printf("Name 2 is %s\n", name2);

if(strcmp(name1, name2) > 0) /* returns 1 if name1 > name2 */
strcpy(mixed, name1);
else
strcpy(mixed, name2);

printf("The biggest name alphabetically is %s\n", mixed);

strcpy(mixed, name1);
strcat(mixed, " ");
strcat(mixed, name2);
printf("Both names are %s\n", mixed);

return 0;
}



/* Output of Program.

This is the title.

Name1 is Rosalinda
Name2 is Zeke
The biggest name alphabetically is Zeke
Both names are Rosalinda Zeke

*/

Related Links :

Program to show Basic use of structures



#include

struct
{
char initial;
int age;
int grade;
}
kids[12], *point, extra;


int main()
{
int index;

for (index = 0 ; index < 12 ; index++)
{
point = kids + index;
point->initial = 'A' + index;
point->age = 16;
point->grade = 84;
}

kids[3].age = kids[5].age = 17;
kids[2].grade = kids[6].grade = 92;
kids[4].grade = 57;

for (index = 0 ; index < 12 ; index++)
{
point = kids + index;
printf("%c is %d years old and got a grade of %d\n",

(*point).initial, kids[index].age, point->grade);
}
extra = kids[2]; /* Structure assignment */
extra = *point; /* Structure assignment */

return 0;
}



/* Out Pur Of Program

A is 16 years old and got a grade of 84
B is 16 years old and got a grade of 84
C is 16 years old and got a grade of 92
D is 17 years old and got a grade of 84
E is 16 years old and got a grade of 57
F is 17 years old and got a grade of 84
G is 16 years old and got a grade of 92
H is 16 years old and got a grade of 84
I is 16 years old and got a grade of 84
J is 16 years old and got a grade of 84
K is 16 years old and got a grade of 84
L is 16 years old and got a grade of 84

*/

Related Links :

Basic example of a while loop




#include

int main()
{
int count;

count = 0;
while (count < 6)
{
printf("The value of count is %d\n", count);
count = count + 1;
}

return 0;
}



/* Result of execution

The value of count is 0
The value of count is 1
The value of count is 2
The value of count is 3
The value of count is 4
The value of count is 5

*/

Related Links :

Program to read the name from given set from given array & check condition using character array in c



int main()
{
char name[5]; /* define a string of characters */

name[0] = 'D';
name[1] = 'a';
name[2] = 'v';
name[3] = 'e';
name[4] = 0; /* Null character - end of text */

printf("The name is %s\n", name);
printf("One letter is %c\n", name[2]);
printf("Part of the name is %s\n", &name[1]);

return 0;
}



/* OUTPUT of PROGRAM

The name is Dave
One letter is v
Part of the name is ave

*/

Related Links :

Program showing break & continue loop



#include

int main()
{
int xx;

for(xx = 5 ; xx < 15 ; xx = xx + 1)
{
if (xx == 8)
break;
printf("In the break loop, xx is now %d\n", xx);
}

for(xx = 5 ; xx < 15 ; xx = xx + 1)
{
if (xx == 8)
continue;
printf("In the continue loop, xx is now %d\n", xx);
}

return 0;
}



/* Result of execution

In the break loop, xx is now 5
In the break loop, xx is now 6
In the break loop, xx is now 7
In the continue loop, xx is now 5
In the continue loop, xx is now 6
In the continue loop, xx is now 7
In the continue loop, xx is now 9
In the continue loop, xx is now 10
In the continue loop, xx is now 11
In the continue loop, xx is now 12
In the continue loop, xx is now 13
In the continue loop, xx is now 14

*/

Related Links :

Program to masking the given set of numbers.




#include
int main()
{
char mask;
char number[6];
char and, or, xor, inv, index;
number[0] = 0X00;
number[1] = 0X11;
number[2] = 0X22;
number[3] = 0X44;
number[4] = 0X88;
number[5] = 0XFF;

printf(" nmbr mask and or xor inv\n");
mask = 0X0F;
for (index = 0 ; index <= 5 ; index++)
{
and = mask & number[index];
or = mask | number[index];
xor = mask ^ number[index];
inv = ~number[index];
printf("%5x %5x %5x %5x %5x %5x\n",
number[index], mask, and, or, xor, inv);
}

printf("\n");
mask = 0X22;
for (index = 0 ; index <= 5 ; index++)
{
and = mask & number[index];
or = mask | number[index];
xor = mask ^ number[index];
inv = ~number[index];
printf("%5x %5x %5x %5x %5x %5x\n",
number[index], mask, and, or, xor, inv);
}

return 0;
}



/* OUTPUT of Program

nmbr mask and or xor inv
0 f 0 f f ffff
11 f 1 1f 1e ffee
22 f 2 2f 2d ffdd
44 f 4 4f 4b ffbb
ff88 f 8 ff8f ff87 77
ffff f f ffff fff0 0

0 22 0 22 22 ffff
11 22 0 33 33 ffee
22 22 22 22 0 ffdd
44 22 0 66 66 ffbb
ff88 22 0 ffaa ffaa 77
ffff 22 22 ffff ffdd 0

*/

Related Links :

Program to create dynamic structures using malloc



#include
#include
#include

struct animal
{
char name[25];
char breed[25];
int age;
} *pet[12], *point; /* this defines 13 pointers, no variables */


int main()
{
int index;

/* first, fill the dynamic structures with nonsense */
for (index = 0 ; index < 12 ; index++)
{
pet[index] = (struct animal *)malloc(sizeof(struct animal));
if (pet[index] == NULL)
{
printf("Memory allocation failed\n");
exit (EXIT_FAILURE);
}

strcpy(pet[index]->name, "General");
strcpy(pet[index]->breed, "Mixed Breed");
pet[index]->age = 4;
}

pet[4]->age = 12; /* these lines are simply to */
pet[5]->age = 15; /* put some nonsense data into */
pet[6]->age = 10; /* a few of the fields. */

/* now print out the data described above */

for (index = 0 ; index < 12 ; index++)
{
point = pet[index];
printf("%s is a %s, and is %d years old.\n",
point->name, point->breed, point->age);
}

/* good programming practice dictates that we free up the */
/* dynamically allocated space before we quit. */

for (index = 0 ; index < 12 ; index++)
free(pet[index]);

return EXIT_SUCCESS;
}



/* OUTPUT

General is a Mixed Breed, and is 4 years old.
General is a Mixed Breed, and is 4 years old.
General is a Mixed Breed, and is 4 years old.
General is a Mixed Breed, and is 4 years old.
General is a Mixed Breed, and is 12 years old.
General is a Mixed Breed, and is 15 years old.
General is a Mixed Breed, and is 10 years old.
General is a Mixed Breed, and is 4 years old.
General is a Mixed Breed, and is 4 years old.
General is a Mixed Breed, and is 4 years old.
General is a Mixed Breed, and is 4 years old.
General is a Mixed Breed, and is 4 years old.

*/

Related Links :

Program showing example of malloc & structures



#include
#include

struct child
{
char initial;
int age;
int grade;
} *kids[12];

int main()
{
int index;

for (index = 0 ; index < 12 ; index++)
{
kids[index] = (struct child *)malloc(sizeof(struct child));
kids[index]->initial = 'A' + index;
kids[index]->age = 16;
kids[index]->grade = 84;
}

kids[3]->age = kids[5]->age = 17;
kids[2]->grade = kids[6]->grade = 92;
kids[4]->grade = 57;

*kids[10] = *kids[4]; /* Structure assignment */

for (index = 0 ; index < 12 ; index++)
printf("%c is %d years old and got a grade of %d\n",
kids[index]->initial, kids[index]->age,
kids[index]->grade);

return 0;
}

Related Links :

Program to read a data from given file name.



#include
int main()
{
FILE *fp1;
char oneword[100], filename[25];
char *c;
printf("Enter filename -> ");
scanf("%s", filename); /* read the desired filename */
fp1 = fopen(filename, "r");
if (fp1 == NULL)
{
printf("File doesn't exist\n");
exit (1);
}
else
{
do
{
c = fgets(oneword, 100, fp1); /* get a line from the file */
if (c != NULL)
printf("%s", oneword); /* display it on the monitor */
} while (c != NULL); /* repeat until NULL */
}
fclose(fp1);

return 0;
}

Related Links :

program to Print the File through printer & Save a file in C



#include

int main()
{
FILE *infile, *outfile, *printer;
char infilename[25], outfilename[25];
int c;

printf("Enter input file name ----> ");
scanf("%s", infilename);
infile = fopen(infilename, "r");

printf("Enter output file name ---> ");
scanf("%s", outfilename);
outfile = fopen(outfilename, "w");

printer = fopen("PRN", "w");

do
{
c = getc(infile);
if (c != EOF)
{
putchar(c);
putc(c, outfile);
putc(c, printer);
}
} while (c != EOF);

fclose(printer);
fclose(infile);
fclose(outfile);

return 0;
}

Related Links :

program which terminate with X letter





#include
#include

char storage[80];

int main()
{
char c;
int index = 0;

printf("Enter any characters, terminate program with X\n");

do
{
c = _getch(); /* get a character */
if (index < 79) /* limit it to 79 characters */
{
storage[index] = c;
index++;
}
putchar(c); /* display the hit key */
} while (c != 'X');
storage[index] = 0;
printf("%s\n", storage);
printf("\nEnd of program.\n");

return 0;
}

Related Links :

Program to show Centigrade to Farenheit temperature table



#include "stdio.h"

int tempcalc(itn centtemp);


int main()
{
int count; /* a loop control variable */
int farenheit; /* the temperature in farenheit degrees */
int centigrade; /* the temperature in centigrade degrees */

printf("Centigrade to Farenheit temperature table\n\n");

for(count = -2 ; count <= 12 ; count = count + 1)
{
centigrade = 10 * count;
farenheit = tempcalc(centigrade);
printf(" C =%4d F =%4d ", centigrade, farenheit);
if (centigrade == 0)
printf(" Freezing point of water");
if (centigrade == 100)
printf(" Boiling point of water");
printf("\n");
}
/* end of for loop */
return 0;

}

int tempcalc(int centtemp)
{
int faren;

faren = 32 + (centtemp * 9)/5;
return (faren);
}





Centigrade to Farenheit temperature table

C = -20 F = -4
C = -10 F = 14
C = 0 F = 32 Freezing point of water
C = 10 F = 50
C = 20 F = 68
C = 30 F = 86
C = 40 F = 104
C = 50 F = 122
C = 60 F = 140
C = 70 F = 158
C = 80 F = 176
C = 90 F = 194
C = 100 F = 212 Boiling point of water
C = 110 F = 230
C = 120 F = 248

Related Links :

Program to Print Square of 1 to 10 Numbers



#include

int main()
{
int index, square;

for(index = 1 ; index <=10 ; index = index + 1)
{
square = index * index;
printf("%5d%5d\n", index, square);
}

return 0;
}



/* OutPut of Program

1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100

*/








Related Links :

Basic While Loop Program to print Name.



#include

int main()
{
int index;

index = 0;
while (index < index =" index">

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!!!