Find Out the summation Of the boundary elements Of the array

Find Out the summation Of the boundary elements Of the array


Problem 2.8:

Given a two-dimensional array, find out the summation of the boundary elements of the array. Here no element should be added twice.
Solution:
>>First, we have to identify the boundary elements.
  • In a two dimensional array, elements of first column and the last column and the first row and last row are the boundary elements as shown in the Figure-2.8.
>> Here the index, i represents the row number and j represents the column-number.
  • When i is 1, the row is the first row and when i is m (where m represents the number of rows in the array), the row is the last row.
  • Similarly, when j = 1, the column is the first column and the index of the last column is j = n. So, a number in a two-dimensional array is a boundary element if i = 1, i = m, j = 1 or j = n


The summation Of the boundary elements Of the array


  • We shall start from the first number of the array.
  • If it is a boundary element, the number will be added to the sum (which is a variable to store the result and initially it is set zero).
  • We shall check every number whether it is a boundary element or not, if the number is a boundary element it will be added to sum.
  • Otherwise, we shall proceed with the next number of the list (array) and continue the process to the end of the list.

Location of an element

Location of an element of a two-dimensional array
Row-major Order:
  • If Loc (A[i, j]) denotes the location in the memory of the element A[i][j] or Aij, then in row-major order –

Loc (A[i, j]) = Base (A) + (n (i - 1) + (j - 1)) * w;



  • Here Base (A) is starting or base address of the array A, n is the number of columns and w is the width of each cell, i.e, number bytes per cell.

Column-major Order:

In column-major order,

Loc (A[i, j]) = Base (A) + (m (j - 1) + (i - 1)) * w;

Here Base (A) is starting or base address of the array A, m is the number of rows and w is the cell width



Example:
Base address, Base (A) = 100, Size of the array = 5 × 6. If the type of array is integer then find Loc (A[4, 3]).
Solution:
(2 bytes for each integer cell in C/C++)
If the array is stored in row-major order:
Loc (A[4, 3]) = Base (A) + (n (i - 1) + (j - 1))* 2
= 100 + (6 × 3 + 2)* 2
= 100 + 40
= 140

If the array is stored in memory in column-major order:
Loc (A[4, 3]) = Base (A) + m (j - 1) + (i - 1)* 2
= 100 + (5 × 2 + 3)* 2
= 100 + 26
=126

Two dimensional array representation in memory

  • The elements of a two dimensional array are stored in computer’s memory row by row or column by column.
  • If the array is stored as row by row, it is called row-major order.
  • If the array is stored as column by column, it is called column-major order.
  • Suppose there is a two-dimensional array of size 5 × 6. That means, there are 5 rows and 6 columns in the array.
  • In row-major order, elements of a two dimensional array are ordered as –
  • A11, A12, A13, A14, A15, A16, A21, A22, A23, A24, A25, A26, A31, ............, A46, A51, A52, .......,A56.
  • and in column-major order, elements are ordered as –
  • A11, A21, A31, A41, A51, A12, A22, A32, A42, A52, A13, ............, A55, A16, A26, .......,A56.

Two Dimensional Array




Definition of two dimensional array

  • Two dimensional array is an array that has two dimensions, such as row and column.
  • Total number of elements in a two dimensional array can be calculated by multiplication of the number of rows and the number of columns.
  • If there are m rows and n columns, then the total number of elements is m × n, and m × n is called the size of the array.
  • Of course, the data elements of the array will be same type.
  • In mathematics, the two dimensional array is called a matrix and in business it is called table

A two dimensional array can be expressed as follows:




To store and retrieve values in and from array

Data can be stored in a two dimensional array using loop or directly as shown below:
i) storing data taken from keyboard

int B[7][3];
for (int i = 0; i < 7; ++ i)
{
for (int j = 0; j < 3; ++ j)
scanf (“%d”, &B[i][j]);
}


ii) Direct insertion of data in two dimensional array

int B[7][3] = {
{ 1, 2, 3},
{ 9, 10, 11},
… …. ….,
… …. ….,
… …. ….,
… …. ….,
{22, 25, 40}
};


Insert The Element Into The Array At A Given Position





Problem 2.5:
Given a list of integers stored in a linear array and a data element, insert the element into the array at a given position.
Algorithm 2.5:
Algorithm to insert an element into an array.
1. Input: An array A[1...n], the position of insertion m and the data x.
2. Increase the size of the array, A[1...n + 1]
3. for (i = m; i≤ n; i = i + 1)
A[i + 1] = A[i];
4. A[m] = x;
5. Output: The array, A with size n + 1.

Problem as assignment

Given, two linear arrays of integers, merge the two arrays into a single array.


This is the end
of
one dimensional array



Preview <<





Find out the summations of even and odd numbers




Algorithm 2.3:
Algorithm to find the summation of even and odd numbers

Input: A[1...n], sum_odd = 0, sum_even = 0;
//An array and variables to store the summation
2. for (i = 1; i ≤ n; i = i + 1)
{
if (A[i]%2 = = 0), sum_even = sum_even + A[i];
else sum_odd = sum_odd + A[i];
}
3. Output: Summation of odd numbers (print sum_odd) and summation of even numbers (print sum_even)


Find Out The Summations Of Numbers In Odd Index And Even Index Separately

Problem 2.4:
Given a list of integers stored in a linear array, find out the summations of numbers in odd index and even index separately.

Solution:
This problem is similar to the problem 2.3. Here the difference is that, we have to check whether the index is odd or even.

Summation Of Even And Odd Indexed Numbers
Algorithm 2.4:
Algorithm to find the summation of even and odd indexed numbers
Input: A[1...n], sum_odd = 0, sum_even = 0;
//An array and variables (to store the summation)
2. for (i = 1; i ≤ n; i = i + 1)
{
if (i%2 = = 0), sum_even = sum_even + A[i];
else sum_odd = sum_odd + A[i];
}
3. Output: Summation of numbers in odd indices (print sum_odd) and summation of numbers in even indices (print sum_even)




preview <<





To find out largest element



Problem 2.1:
Given a list of elements, write an algorithm to store the list of elements (numbers) in an array and find out the largest element of the list.
Algorithm 2.1: Algorithm to search the largest element of a list
1. Input: x[1 . . . n];
2. for (i = 1; i ≤ n; i = i + 1)
store data to x[i];
3. large = x[1];
4. for (i = 2; i ≤ n; i = i + 1)
if (x[i] > large), large = x[i]; // if any element larger
then the previous_upgrade large
5. Output: the largest number is, large


Find Out A Particular (Specific) Element


Problem 2.2
Given a linear array with data, find out a particular (specific) element of x from the array. We do not know the index (cell) number where the element has been stored.

Algorithm 2.2: Algorithm to search a particular element from a list
1. Input: a[1 . . . n], x;
//A set of data in array a, and variable x i.e., the target element
2. found = 0
3. for (i = 1; i ≤ n; i = i + 1)
{
if (a[i] = = x);
location = i, found = 1, break;
}
4. Output: if (found = = 1), print “FOUND” message and location.
else print “NOT FOUND” message

Find out the summations of odd numbers and even numbers separately


Problem 2.3:
Given a list of integers stored in a linear array. Find out the summations of odd numbers and even numbers separately.
Solution:
  • Given a list of integer, we have to find out the odd numbers and then we shall add those odd numbers.
  • Similarly, we shall find out the even numbers in the list and adding those numbers we shall get the summation of even .
  • To store the result, we require two variables; sum__even and sum__odd.
  • Initially, values of these variables will be zero (0) and every time we find an even number we shall add it to the sum__even and every time we find an odd number, we shall add it to the sum__odd.
  • If a number is divisible by 2 it is even, otherwise odd.
  • We have to start from the first number of the list.
  • If it is even, it will be added with the sum_even and if it is odd, it will be added with the sum_odd.
  • Similarly, we shall access whole list one by one and we shall add them with either sum_even (if a number is even) or sum_odd (if a number is odd).


preview <<


One Dimensional Array




An array that can be represented by only one dimension such as row or column and that holds finite number of same type of data items is called one dimensional (linear) array.
Here 1, 2, 3, … … …, 10 are index number, and
0, 10, 12, … … …, 39 are data items or elements of the array and B is the array name.
Symbolically an element of the array is expressed as Bi or B[i], which denotes ith element of the array, B.
Thus B[4], B[9] denotes respectively the 4th element and the 9th element of the array, B.
The name of the array usually is a name constituted by one or more characters.
Thus array name may be A, S, Stock, Array1 etc.
The element of an array may be number (integer or floating point number) or character



Expression of one dimensional array in C/C++:
For integer array:
int a[10];
For character array:
char b[30];
For floating point array:
float c[10];






Store an element into an array
B[4] = 19; it means 19 will be stored in the cell number 4 of the array of B
If there is any (previous) value that will be overwritten.

Read (retrieve) a value (element) from an array
x = B[6]; it means the value of x will be 20, since the cell number 6 of the array, B contains 20

Code in C/C++ for storing data in an array
int x[10];
for (i = 0; i < style="font-weight: bold;">Code in C/C++ for accessing data from an array and the data will be displayed on the monitor’s screen:
int AC[20];
for (i =0; i < style="font-weight: bold;">Here array name is AC and the size of the array is 20.

Preview <<





Definition of an array



  • An array is a finite set of same type of data items. In other words, it is a collection of homogeneous data items (elements).
  • The elements of an array are stored in successive memory locations.
  • Any element of an array is referred by array name and index number (subscript).
  • There may have many dimensional arrays. But usually two types of array are widely used; such as
one dimensional (linear) array and
two dimensional array.





Space complexity



This complexity is related to space (memory) needs in the main memory for the data used to implement the algorithm for solving any problem. That means if there n data items used in an algorithm, the space complexity of the algorithm will be proportional to n.

The complexity of an algorithm (either time complexity or space complexity) is represented using asymptotic notations.
One of the asymptotic notations is O (big-oh) notation.
Big-oh (O) notation is also called upper bound of the complexity.
If we get the total number of element comparisons is ½ n2 – ½ n, then we can write it as O (n2).
Since (½ n2 – ½ n) <>

Preview <<



This is the end Of Chapter 1


Time complexity

This complexity is related to execution time of the algorithm.
It depends on the number of element (item) comparisons and number of element movement (movement of data from one place to another).
However, the complexity of the most of the algorithms described here related to the number of element comparisons.
That means, the complexity of the algorithm is computed with respect to the total number of element (item) comparisons needed for the algorithm

Preview <<<<<<

Importance of data structure

  • Computer science as well as computer engineering deals with two jargons which are software and hardware.
  • Without software, hardware (electrical, mechanical, electronic parts of computer that we see and touch) is useless.
  • So, study of software is very important in computer science, and software consists of programs which use different types of data.
  • In a program we not only use elementary data items but also use different types of organized data. That means we use data structure in a program. As we know we write programs to solve problems. That is, to solve problems we have to use data structures. The different data structures give us different types of facilities.
  • If we need to store data in such a way that we have to retrieve data directly irrespective of their storage location. We can get this facility using one type of data structure such as array gives us such facility.
Preview <<

program

Program
  • Sequence of instructions of any programming language that can be followed to perform a particular task.
  • For a particular problem, at first we may write an algorithm then the algorithm may be converted into a program.
  • In a program usually we use a large amount of data. Most of the cases these data are not elementary items, where exists structural relationship between elementary data items.
    That means the programs uses data structure(s).

Preview <<

>> Next

Algorithm

Set or sequence of instructions (steps) that can be followed to perform a task (problem).
Do not strictly follow grammar of any particular programming language.
However its language may be near to a programming language.


q Each and every algorithm can be divided into three sections.

v First section is input section, where we show which data elements are to be given.

v The second section is very important one, which is operational or processing section. Here we have to do all necessary operations, such as computation, taking decision, calling other procedure (algorithm) etc.

v The third section is output, where we display the result found from the second section


preview <<

BackgRound2

Example of Data Structures:
Array, Linked List, Stack, Queue, Tree, Graph, Hash
Table etc.
Types of elementary data item:
Character, Integer, Floating point numbers etc.

Expressions of elementary data in C/C++
Elementary data item - Expression in C/C++
Character - char
Integer - int
Floating point number - float

preview <<

BackgRound

Elementary items constitute a unit and that unit may be considered as a structure.
.>> structure may be treated as a frame or proforma where we organize some elementary items in different ways.
Data structure is a structure or unit where we organize elementary data items in different ways.
>>That means, a data structure is a means of structural relationships of elementary data items for storing and retrieving data in computer’s memory.
Usually elementary data items are the elements of a data structure.
However, a data structure may be an element of another data structure. That means a data structure may contain another data structure

Data Structures Fundamentals


,

facebook





CPluspuls

C++

chat






Identify data structure

Data structure

  • Elementary items constitute a unit and that unit may be considered as a structure.
A structure may be treated as a frame or proforma where we organize some elementary items in different ways.
  • Data structure is a structure or unit where we organize elementary data items in different ways.
That means, a data structure is a means of structural relationships of elementary data items for storing and retrieving data in computer’s memory.
  • Usually elementary data items are the elements of a data structure.
  • However, a data structure may be an element of another data structure. That means a data structure may contain another data structure.

Table of contents



  1. BackgRound
  2. Array
  3. Record
  4. Linked List
  5. Stack
  6. Queue
  7. Tree
  8. Graph
  9. Searching and sorting
  10. Hashing



icon




logo




pre-paid card system

d card system

#include"stdio.h"

#include"conio.h"

int main(void)


{


int n,pcn=12345;


printf("Enter your pre-paid card number : ");


scanf("%d",&n);


if(n==pcn)


{


printf("your balance is : 300 BDT\n\n");


}


else if(n!=pcn)


{


printf("Enter your pre-paid card number Again : ");


scanf("%d",&n);
if(n==pcn)
{
printf("your balance is : 300 BDT\n\n");


}


if(n!=pcn)


{
printf("Enter your pre-paid card number Again : ");


scanf("%d",&n);
if(n==pcn)
{
printf("your balance is : 300 BDT\n\n");


}


else
{
printf("\nyour card number is invalid!!!!\n\nYou can insert card number After 6 Hours\n\n");


}


}


}


return 0;


}




student class tests

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

void main()
{
int marks[40][4];
float avg_mrk[4];
int sum,min_mrk;

printf("Enter class test marks: \n");

for(int i=0;i<40;++i)
{
printf("serial No %d :\n",i+1);
for(int j=0;j<4;j++)
{
scanf("%d",&marks[i][j]);
}
sum=0;
min_mrk=marks[i][0];
for(j=0;j<4;j++)
{
sum=sum+marks[i][j];
if(min_mrk>marks[i][j])
min_mrk=marks[i][j];
}
avg_mrk[i]=float(sum-min_mrk)/3;
}
printf("Average marks : \n");
for(i=0;i<40;i++)
{
printf("serial No %d :\t",i+1);
printf("%.2f: \n ",avg_mrk[i]);
}


}

/* OUTPUT
Enter class test marks:
serial No 1 :
17 18 19 17
serial No 2 :
18 18 19 17
serial No 3 :
18 16 15 13
serial No 4 :
10 08 19 20
..........
serial No 40 :
18 18 19 17
Average marks :
serial No 1 : 18.00:
serial No 2 : 18.33:
serial No 3 : 16.33:
serial No 4 : 16.33:
..................
serial No 40 : 18.33:




*/

Arrange in link list

#include"stdio.h"
#include"iostream.h>
#include"conio.h"
struct node
{
int data;
node *next;
};
class linklist
{
node *list,*nptr,*tptr;
public:
linklist()
{
list=NULL;
}
void newnode(int item);
void link();
void showdata();
void sort(int item);
};
void linklist::newnode(int item)
{
nptr =new(node);
nptr->data=item;
nptr->next=NULL;
//link();
}
void linklist::link()
{
if(list==NULL)
{
list=nptr;
tptr=nptr;
}
else
{
tptr->next=nptr;
tptr=nptr;
}
}

void linklist::showdata()
{
node *curptr;
curptr=list;
while(curptr !=NULL)
{
cout<<" "<data;
curptr=curptr->next;
}
//cout<<" "<data;
}
void linklist::sort(int item)
{
node *pptr,*fptr;
pptr=list;
while(pptr !=NULL)
{
fptr=pptr->next;
while(fptr !=NULL)
{
if(pptr->data>fptr->data)
{
int temp;
temp=pptr->data;
pptr->data=fptr->data;
fptr->data=temp;
}
fptr=fptr->next;
}
pptr=pptr->next;

}
}

int main()
{
int n,d;
linklist mylist;
cout<<"\n How many nodes you have ?\t";
cin>>n;
cout<<"\n Enter data for nodes :";
for(int i=0;i {
cin>>d;
mylist.newnode(d);
mylist.link();
}
cout<<"data in the list :";
mylist.showdata();
//mylist.sort();
//char ans;
/* cout<<"\nDo you want to sort the list ?(y for yes )\t";
cin>>ans;
if(ans=='y'|| ans=='Y')
{
//int x;
//cout<<"\n\n Enter the node value to be deleted: ";
//cin>>x;*/
mylist.sort(d);

cout<<"\n\nsort Data in the list: \t";
mylist.showdata();
cout<<"\n";




return 0;
}

double_link_list

#include"stdio.h"
#include"iostream.h"
#include"conio.h"
struct node
{
node *pre;
int data;
node *next;
};
class linklist
{
node *list,*nptr,*tptr;
public:
linklist()
{
list=NULL;
}
void newnode (int item);
void link(); //constructed
void showdata();
};
void linklist::newnode(int item)
{
nptr=new(node);
nptr->pre=NULL;
nptr->data=item;
nptr->next=NULL;
link();
}
void linklist::link()
{
if(list==NULL)
{
list=nptr;
tptr=nptr;
}
else
{
tptr->next=nptr;
nptr->pre=tptr;
tptr=nptr;
}
}
void linklist::showdata()
{
node *curptr;
curptr =list;
while(curptr->next !=NULL)
{
cout<<" " <data;
curptr=curptr->next;
}
cout<<" " <data;
//curptr=curptr-next
}


int main()
{
int n,d;


linklist mylist;

cout<<"how many node do you have?\t";
cin>>n;
cout<<"Enter data:"<<"\n";
for(int i=0;i {
cin>>d;
mylist.newnode(d);
}
cout<<"data in the list:";
mylist.showdata();


return 0;


}

deleting in link list

#include 'stdio.h'
#include 'iostream.h'
#include 'conio.h"
struct node
{
int data;
node *next;
};
class linklist
{
node *list,*nptr,*tptr;
public:
linklist()
{
list=NULL;
}
void newnode(int item);
void link();
void showdata();
void deletion(int item);
};
void linklist::newnode(int item)
{
nptr =new(node);
nptr->data=item;
nptr->next=NULL;
//link();
}
void linklist::link()
{
if(list==NULL)
{
list=nptr;
tptr=nptr;
}
else
{
tptr->next=nptr;
tptr=nptr;
}
}

void linklist::showdata()
{
node *curptr;
curptr=list;
while(curptr !=NULL)
{
cout<<" "<data;
curptr=curptr->next;
}
//cout<<" "<data;
}
void linklist::deletion(int item)
{
node *pptr;
tptr=list;
if(list->data!=item)
{
while(tptr->data!=item)
{
if(tptr==NULL)
{
cout<<"\nItem is not found in the list\n";
break;
}
pptr=tptr;
tptr=tptr->next;
}
pptr->next=tptr->next;
delete(tptr);
}
else
{
list =list->next;
delete(tptr);
}

}

int main()
{
int n,d;
linklist mylist;
cout<<"\n How many nodes you have ?\t";
cin>>n;
cout<<"\n Enter data for nodes :";
for(int i=0;i {
cin>>d;
mylist.newnode(d);
mylist.link();
}
cout<<"data in the list :";
mylist.showdata();

char ans;
cout<<"\nDo you want to delete a node ?(y for yes )\t";
cin>>ans;
if(ans=='y'|| ans=='Y')
{
int x;
cout<<"\n\n Enter the node value to be deleted: ";
cin>>x;
mylist.deletion(x);
cout< cout<<"\n\nUpdated Data in the list: \t";
mylist.showdata();
cout<<"\n";
}



return 0;
}

search link list

#include"stdio.h"
#include"iostream.h"
#include"conio.h"
struct node
{
int data;
node *next;
};
class linklist
{
node *list,*nptr,*tptr;
public:
linklist()
{
list=NULL;
}
void newnode(int item);
void link();
void showdata();
void search(int item);
};
void linklist::newnode(int item)
{
nptr =new(node);
nptr->data=item;
nptr->next=NULL;
//link();
}
void linklist::link()
{
if(list==NULL)
{
list=nptr;
tptr=nptr;
}
else
{
tptr->next=nptr;
tptr=nptr;
}
}
void linklist::search(int item)
{
tptr=list;
while(tptr !=NULL)
{
if(tptr->data==item)
{
cout<<"Data Found !\n"; break; } tptr=tptr->next;
if(tptr==NULL)
cout<<"Data is not in the list\n"; } } void linklist::showdata() { node *curptr; curptr=list; while(curptr !=NULL) { cout<<" "<data;
curptr=curptr->next;
}
//cout<<" "<data;
}

int main()
{
int n,d;
linklist mylist;
cout<<"\n How many nodes you have ?\t"; cin>>n;
cout<<"\n Enter data for nodes :"; for(int i=0;i>d;
mylist.newnode(d);
mylist.link();
}
cout<<"data in the list :"; mylist.showdata(); int item; cout<<"\n Enter the data to be found: "; cin>>item;
cout<<"\n Search Result: ";
mylist.search(item);



return 0;
}
http://www.google.com.bd/search?q=search+link+list&pov=100366784536684831572&usg=__l-bqafYvVrd4O1wVprEd-7Upvu0=&hl=en

Link list creation

#include"stdio.h"
#include"iostream.h"
#include"conio.h"
struct node
{
int data;
node *next;
};
class linklist
{
node *list,*nptr,*tptr;
public:
linklist()
{
list=NULL;
}
void newnode(int item);
void link();
void showdata();
};
void linklist::newnode(int item)
{
nptr =new(node);
nptr->data=item;
nptr->next=NULL;
//link();
}
void linklist::link()
{
if(list==NULL)
{
list=nptr;
tptr=nptr;
}
else
{
tptr->next=nptr;
tptr=nptr;
}
}
void linklist::showdata()
{
node *curptr;
curptr=list;
while(curptr !=NULL)
{
cout<<" "<data;
curptr=curptr->next;
}
//cout<<" "<data;
}

int main()
{
int n,d;
linklist mylist;
cout<<"\n How many nodes you have ?\t";
cin>>n;
cout<<"\n Enter data for nodes :";
for(int i=0;i {
cin>>d;
mylist.newnode(d);
mylist.link();
}
cout<<"data in the list : ";
mylist.showdata();
cout<<"\n";

// getch();
return 0;
}

largest Element in the array

Back
#include "stdio.h"
#include "conio.h"
void main()
{
int a[15],lar,I,n,odd,even;

printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements of the array\n");
for(I=0;Ilar)

lar=a[I];

}
printf("\n%d is largest Element in the array\n\n",lar);

for(I=0;I
{
if(a[I]%2!=0)
{
odd=a[I];
printf("%d : ",odd);
}
}
printf("are odd number \n\n");

for(I=0;I
{
if(a[I]%2==0)
{
even=a[I];
printf("%d : ",I);
}
}
printf("Even number position \n\n");

for(I=0;I
{
if(a[I]%2==0)
{
even=a[I];
printf("%d : ",even);
}
}
printf("are even number \n\n");


}



Array

Data Structure

leap year in c

include stdio.h
int main(void)
{
int n;
printf("enter year (like this 2005) :");
scanf("%d",&n);
if(n%4==0 && n%100 !=0 || n%400==0)
printf("%d is leap year \n",n);
else
printf("%d is not leap year\n ",n);
return 0;
}

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

Factorial with recursion

Back

//Factorial with recursion

#include stdio.h
#include conio.h
long int find_factorial(long int n)
{
if(n<=1)
return 1;
else
return (n* find_factorial(n-1));
}
int main(void)
{
long int x,n;
scanf("%ld",&n);
x=find_factorial(n);
printf("factorial is : %ld\n",x);
return 0;
}

C++

Under construction

C





pre-paid card system



Mathematics sample source codes


Structures sample source codes

Followers

About Me

Dhaka, Dhaka, Bangladesh
..