Basic binary search tree routines

Back


//Basic binary search tree routines

#include stdio.h
#include stdlib.h

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

/* insert, swap, search value, search minimum and search maximum values */
struct tnode *tnode_insert(struct tnode *p, int value);
struct tnode *tnode_swap(struct tnode *p);
struct tnode *tnode_search(struct tnode *p, int key);
struct tnode *tnode_searchmin(struct tnode *p);
struct tnode *tnode_searchmax(struct tnode *p);

/* destroy, count tree nodes */
void tnode_destroy(struct tnode *p);
int tnode_count(struct tnode *p);

/* print binary tree inorder, preorder, postorder [recursive] */
void print_inorder(struct tnode *p);
void print_preorder(struct tnode *p);
void print_postorder(struct tnode *p);

int main(void) {
int demo_nr[] = {1, 3, 4, 7, 2, 9, 9, 0, 5, 6, 8, 7, 1, 2, 4};
struct tnode *root = NULL;
struct tnode *searchval = NULL;
int querry = 0;
int i = 0;

/* demo: insert some nr's into the binary tree */
for(i = 0; i < 15; i++)
root = tnode_insert(root, demo_nr[i]);

printf("=-=-=\n");
printf("Total number of tree nodes: %d\n", tnode_count(root));
printf("inorder : ");
print_inorder(root);
printf("\n");

printf("preorder : ");
print_preorder(root);
printf("\n");

printf("postorder: ");
print_postorder(root);
printf("\n");

printf("=-=-=\n");
printf("Enter integer, find: ");
scanf("%d", &querry);
searchval = tnode_search(root, querry);
if(searchval == NULL)
printf(" * %d Not! found in btree\n", querry);
else
printf(" * Found! %d in btree\n", searchval->data);

searchval = NULL;
printf("Searching for Minimum value\n");
searchval = tnode_searchmin(root);
if(searchval == NULL)
printf(" * Minimum Not! found in btree ?\n");
else
printf(" * Found! minimum value %d in btree\n", searchval->data);

searchval = NULL;
printf("Searching for Maximum value\n");
searchval = tnode_searchmax(root);
if(searchval == NULL)
printf(" * Maximum Not! found in btree ?\n");
else
printf(" * Found! Maximum value %d in btree\n", searchval->data);

printf("=-=-=\n");
printf("Exchanging all tree nodes: left <-> right\n");
root = tnode_swap(root);

printf("inorder : ");
print_inorder(root);
printf("\n");

printf("preorder : ");
print_preorder(root);
printf("\n");

printf("postorder: ");
print_postorder(root);
printf("\n");

printf("=-=-=\n");
printf("Destroying btree... bye!\n");
tnode_destroy(root);

return 0;
}

/* insert a tnode into the binary tree */
struct tnode *tnode_insert(struct tnode *p, int value) {
struct tnode *tmp_one = NULL;
struct tnode *tmp_two = NULL;

if(p == NULL) {
/* insert [new] tnode as root node */
p = (struct tnode *)malloc(sizeof(struct tnode));
p->data = value;
p->left = p->right = NULL;
} else {
tmp_one = p;
/* Traverse the tree to get a pointer to the specific tnode */
/* The child of this tnode will be the [new] tnode */
while(tmp_one != NULL) {
tmp_two = tmp_one;
if(tmp_one ->data > value)
tmp_one = tmp_one->left;
else
tmp_one = tmp_one->right;
}

if(tmp_two->data > value) {
/* insert [new] tnode as left child */
tmp_two->left = (struct tnode *)malloc(sizeof(struct tnode));
tmp_two = tmp_two->left;
tmp_two->data = value;
tmp_two->left = tmp_two->right = NULL;
} else {
/* insert [new] tnode as left child */
tmp_two->right = (struct tnode *)malloc(sizeof(struct tnode));
tmp_two = tmp_two->right;
tmp_two->data = value;
tmp_two->left = tmp_two->right = NULL;
}
}

return(p);
}

/* print binary tree inorder */
void print_inorder(struct tnode *p) {
if(p != NULL) {
print_inorder(p->left);
printf("%d ", p->data);
print_inorder(p->right);
}
}

/* print binary tree preorder */
void print_preorder(struct tnode *p) {
if(p != NULL) {
printf("%d ", p->data);
print_preorder(p->left);
print_preorder(p->right);
}
}

/* print binary tree postorder */
void print_postorder(struct tnode *p) {
if(p != NULL) {
print_postorder(p->left);
print_postorder(p->right);
printf("%d ", p->data);
}
}

/* returns the total number of tree nodes */
int tnode_count(struct tnode *p) {
if(p == NULL)
return 0;
else {
if(p->left == NULL && p->right == NULL)
return 1;
else
return(1 + (tnode_count(p->left) + tnode_count(p->right)));
}
}

/* exchange all left and right tnodes */
struct tnode *tnode_swap(struct tnode *p) {
struct tnode *tmp_one = NULL;
struct tnode *tmp_two = NULL;

if(p != NULL) {
tmp_one = tnode_swap(p->left);
tmp_two = tnode_swap(p->right);
p->right = tmp_one;
p->left = tmp_two;
}

return(p);
}

/* locate a value in the btree */
struct tnode *tnode_search(struct tnode *p, int key) {
struct tnode *temp;
temp = p;

while(temp != NULL) {
if(temp->data == key)
return temp;
else if(temp->data > key)
temp = temp->left;
else
temp = temp->right;
}

return NULL;
}

/* locate a minimum value in the btree */
struct tnode *tnode_searchmin(struct tnode *p) {
if(p == NULL)
return NULL;
else
if(p->left == NULL)
return p;
else
return tnode_searchmin(p->left);
}

/* locate a maximum value in the btree */
struct tnode *tnode_searchmax(struct tnode *p) {
if(p != NULL)
while(p->right != NULL)
p = p->right;

return p;
}

/* destroy the binary tree */
void tnode_destroy(struct tnode *p) {
if(p != NULL) {
tnode_destroy(p->left);
tnode_destroy(p->right);

free(p);
}
}

Add two long positive intergers

Back

//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();
}

Recursive prime number

Back

#include stdio.h
#include conio.h
#include assert.h

int is_prime(int n);

void main(void)
{
int n=0;
// clrscr();
printf("An integer ");
scanf("%d",&n);
assert(n > 1);
n=is_prime(n);
if (n==1)
printf("\nThe number is prime");
else
printf("\nThe number is not prime");
//getch();
}

int is_prime(int n)
{
int i;
for(i=2;i
if (n%i)
continue;
else return 0;
}
return 1;
}

Recursive prime number

Back

#include stdio.h
#include conio.h
#include assert.h

int is_prime(int n);

void main(void)
{
int n=0;
// clrscr();
printf("An integer ");
scanf("%d",&n);
assert(n > 1);
n=is_prime(n);
if (n==1)
printf("\nThe number is prime");
else
printf("\nThe number is not prime");
//getch();
}

int is_prime(int n)
{
int i;
for(i=2;i if (n%i)
continue;
else return 0;
}
return 1;
}

Prime Numbers Series




Back
#include "stdio.h"

main()
{
int n,i=1,j,c;
//clrscr();
printf("Enter Number Of Terms: \n");
scanf("%d",&n);
printf("Prime Numbers Are Follwing\n");

while(i<=n)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0) c++;
}
if(c==2) printf("%d\t",i);
i++;
}
return 0;
}

Fibonacci Number

#include


int main (int argc, const char * argv[])

{

int n, i;
double num1, num2, ans;

num1 = 0;
num2 = 1;

printf("How many numbers would you like to print?\t");

scanf("%d", &n);

printf("\n2\t%.0lf\n",num2);

for(i = 0; i < (n-2); ++i)
{

ans = (num1 + num2);

printf("%d\t%.0lf\n", i+3, ans);

num1 = num2;
num2 = ans;
}


return 0;
}

Volumes & Areas

//Volumes & Areas

/PROGRAM TO CALCULATE AREA,VOLUME,PERIMETER OF A PARTICULAR
GEOMETRIC SHAPE/

#include stdio.h
#include conio.h
#include math.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("WWW");

}
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);
}

Square Root of a number by using simple calculations

Square Root of a number by using simple calculations

#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();
}

Program to find your Day of Birth given Date of Birth

//Program to find your Day of Birth given Date of Birth

#include
#include
#include

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;
}

Program to calculate Area of a Polygon

//Program to calculate Area of a Polygon

/* Given the coordinates of the vertices of a convex polygon,
calculate its area and perimeter. Subdivide it into triangles
and calculate the area of each triangle with Heron's formula.
Requires data file pvert.txt containing coordinates of each vertex.
Example of data for a polygon with 5 vertices:

3 7 6 4 3 -2 -6 1 -6 7
*/
#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, "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 < 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 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 < 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);
}

Program for Decimal to Roman Number conversion

//Program for Decimal to Roman Number conversion

#include

main()
{
int a,b,c,d,e;
clrscr();
printf("Input a number (between 1-3000):");
scanf("%d",&e);
while (e==0||e>3000)
{
printf ("ERROR: Invalid Input!");
printf ("Enter the number again:");
scanf ("%d",&e);
}
if (e>3000)
printf("Invalid");
a = (e/1000)*1000;
b = ((e/100)%10)*100;
c = ((e/10)%10)*10;
d = ((e/1)%10)*1;

if (a ==1000)
printf("M");
else if (a ==2000)
printf("MM");
else if (a ==3000)
printf("MMM");

if (b == 100)
printf("C");
else if (b == 200)
printf("CC");
else if (b == 300)
printf("CCC");
else if (b == 400)
printf("CD");
else if (b ==500)
printf("D");
else if (b == 600)
printf("DC");
else if (b == 700)
printf("DCC");
else if (b ==800)
printf("DCCC");
else if (b == 900)
printf("CM");


if (c == 10)
printf("X");
else if (c == 20)
printf("XX");
else if (c == 30)
printf("XXX");
else if (c == 40)
printf("XL");
else if (c ==50)
printf("L");
else if (c == 60)
printf("LX");
else if (c == 70)
printf("LXX");
else if (c ==80)
printf("LXXX");
else if (c == 90)
printf("XC");

if (d == 1)
printf("I");
else if (d == 2)
printf("II");
else if (d == 3)
printf("III");
else if (d == 4)
printf("IV");
else if (d ==5)
printf("V");
else if (d == 6)
printf("VI");
else if (d == 7)
printf("VII");
else if (d ==8)
printf("VIII");
else if (d == 9)
printf("IX");
getch();
}

Program that gives all details of a Triangle given the lengths of its sides

//Program that gives all details of a Triangle given the lengths of its sides

#include
#include
#include
#include

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 && B<90.0 && C<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;
}

Circle Through Three Points

//Circle Through Three Points

#include
#include
#include

int main()
{
clrscr();
double f,g,m,x1,x2,x3,y1,y2,y3;
double c,d,h,e,k,r,s;
for(;;)
{
if(scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3)==EOF)
//checking for input
break;

f = x3*x3-x3*x2-x1*x3+x1*x2+y3*y3-y3*y2-y1*y3+y1*y2; //formula
g = x3*y1-x3*y2+x1*y2-x1*y3+x2*y3-x2*y1;

if(g==0)
m = 0;
else
m = (f/g);

c = (m*y2)-x2-x1-(m*y1); //formula
d = (m*x1)-y1-y2-(x2*m);
e = (x1*x2)+(y1*y2)-(m*x1*y2)+(m*x2*y1);

h = (c/2); //formula
k = (d/2);
s = (((h)*(h))+((k)*(k))-e);
r = pow(s,.5);

printf("(x");

if(h>=0)
printf(" + ");
else if(h<0) h="-h;">=0)
printf(" + ");
else if(k<0) k="-k;" 2 =" %.3lf^2">=0) printf(" + ");
else if(c<0) c="-c;">=0) printf(" + ");
else if(d<0) d="-d;">=0) printf(" + ");
else if(e<0) printf(" - ");

if(e<0) e=-e;
printf("%.3lf = 0",e);
printf("

");
}

getch();
return 0;
}

Factorial series

//Factorial series

#include stdio
#include conio
#include math
long int factorial(int n);
void main()
{
int x,i;
float s,r;
char c;
clrscr();
printf("You have this series:-1+x/1! + x^2/2! + x^3/3! + x^4/4!..x^x/x!");
printf("To which term you want its sum? ");
scanf("%d",&x);
s=0;
for (i=1;i<=x;i++) { s=s+((float)pow(x,i)/(float)factorial(i)); }
printf("The sum of %d terms is %f",x,1+s);
fflush(stdin);
getch();
}
long int factorial(int n)
{ if (n<=1) return(1);
else n=n*factorial(n-1);
return(n);
}
There was an error in this gadget

Followers

About Me

Dhaka, Dhaka, Bangladesh
..