Program to find the numbers co prime to each other.



#include
#include

int exteuclid(int a,int b);

void main()
{
int a[20],M=1,x[20],i,b[20],c[20],A[20],e,T,j,p=0;
// M[0]=1;
clrscr();
printf("Enter the no of m's which must be co prime to each other\n");
scanf("%d",&e);
printf("Enter the values of m's\n");
for(i=0;i scanf("%d",&a[i]); //taking input small m's....

printf("Enter the values of A's\n");
for(i=0;i scanf("%d",&A[i]); //taking input A's......



for(i=0;i {
M=M*a[i]; //calculating M....
}
printf("M=%d\n",M);
// T=M[i];
for(i=0;i {
b[i]=M/a[i]; //calculating M1,M2,M3.....and so on..

}
for(i=0;i printf("M%d=%d\t",i+1,b[i]);


for(i=0;i {
c[i]=exteuclid(a[i],b[i]); //calculating multiplicative inverse....

}
for(i=0;i printf("MI%d=%d\t",i+1,c[i]);

for(i=0;i {
x[i]=A[i]*b[i]*c[i];
}
for(i=0;i {
p=p+x[i];

}
p=p%M;



printf("our unique value x=%d",p);






getch();
}


int exteuclid(int a,int b)
{
int A2, A1, B2, B1, A3, B3,T1,T2,T3,q, r = 1, t, x;

A1 = B2 = 1;
A2 = B1 = 0;
A3 = a;
B3= b;


do
{
q = (A3/B3);
r = (A3% B3);
//printf("%d\t %d\t %d\t %d\t %d\t %d\t %d\t %d\n",
// A1, A2, B1, B2, c, d, q, r);

T1=A1-q*B1;
T2=A2-q*B2;
T3=A3-q*B3;
A1=B1;
A2=B2;
A3=B3;
B1=T1;
B2=T2;
B3=T3;

} while(r !=1);
return(B2);
}


Related Links :

characters read from file and save in new file


for(int c = 0; (c = fgetc (file)) = EOF;)
(
if(c> = 10 & & c <= 300)
fputc (c, outfile);
)

Related Links :

Given String is palindrome or Not.



# include
# include
# include
void main(void)
{
clrscr();
char st[20];
int i,l,s,mid;
printf(" Enter the string \n");
gets(st);
l=strlen(st);
mid=l/2;
for(i=0;i<=mid;i++)
if (st[i]!=st[l-1])
goto XX;
else
l=l-1;
printf("the given string is palindrome \n");
goto YY;
XX:
printf("The given string is not palindrome\n");
YY:;
getch();
}



Related Links :

Matrix Multiplication in C




# include
# include
void main (void)

{
int A[3][3],B[3,3],C[3][3];
clrscr();
printf(" Read matrix A \n");
for (i=1;i<=3;i++)
for(j=1;j<=3;j++)
scanf("%d"&A[i][j]);

printf(" Read matrix B \n");
for (i=1;i<=3;i++)
for(j=1;j<=3;j++)
scanf("%d"&B[i][j]);

for(i=1;i<3;i++)
for(j=1;j<3;j++)
{
for(k=1;k C[i][j]=C[i]+A[i][k] * B[K][J];
}
printf(" The product of A&B matrix is C \n");
for (i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
printf("%d"C[i][j]);
printf('/n');
}
getch();
}


Related Links :

linked list in C


typedef struct
{
char fname[30];
char lname[30];
char MI;
}stud_name;

typedef struct
{
unsigned long idnum;
stud_name name;
char course [10];
int yr_level;
}studtype;

typedef struct
{
int grpnum;
char subj[24];
int time_sked;
int day;
int units;
char rmnum;
}
class_sked;

Related Links :

decimal number into binary number system


#include
#include
#include

void dec_bin(long int num) // Function Definition
{
long int rem[50],i=0,length=0;
while(num>0)
{
rem[i]=num%2;
num=num/2;
i++;
length++;
}
printf("\nBinary number : ");
for(i=length-1;i>=0;i--)
printf("%ld",rem[i]);
}
//================================================
void main()
{
long int num;
clrscr();

printf("Enter the decimal number : ");
scanf("%ld",&num);

dec_bin(num); // Calling function

getch();
}


Related Links :

Heapsort algorithm Program in C



#include
#include


#define uint unsigned int

typedef int (*compare_func)(int, int);


void heap_sort(int This[], compare_func func_pointer, uint len)
{
/* heap sort */

uint half;
uint parents;

if (len <= 1)
return;

half = len >> 1;
for (parents = half; parents >= 1; --parents)
{
int tmp;
int level = 0;
uint child;

child = parents;
/* bottom-up downheap */

/* leaf-search for largest child path */
while (child <= half)
{
++level;
child += child;
if ((child < len) &&
((*func_pointer)(This[child], This[child - 1]) > 0))
++child;
}
/* bottom-up-search for rotation point */
tmp = This[parents - 1];
for (;;)
{
if (parents == child)
break;
if ((*func_pointer)(tmp, This[child - 1]) <= 0)
break;
child >>= 1;
--level;
}
/* rotate nodes from parents to rotation point */
for (;level > 0; --level)
{
This[(child >> level) - 1] =
This[(child >> (level - 1)) - 1];
}
This[child - 1] = tmp;
}

--len;
do
{
int tmp;
int level = 0;
uint child;

/* move max element to back of array */
tmp = This[len];
This[len] = This[0];
This[0] = tmp;

child = parents = 1;
half = len >> 1;

/* bottom-up downheap */

/* leaf-search for largest child path */
while (child <= half)
{
++level;
child += child;
if ((child < len) &&
((*func_pointer)(This[child], This[child - 1]) > 0))
++child;
}
/* bottom-up-search for rotation point */
for (;;)
{
if (parents == child)
break;
if ((*func_pointer)(tmp, This[child - 1]) <= 0)
break;
child >>= 1;
--level;
}
/* rotate nodes from parents to rotation point */
for (;level > 0; --level)
{
This[(child >> level) - 1] =
This[(child >> (level - 1)) - 1];
}
This[child - 1] = tmp;
} while (--len >= 1);
}


#define ARRAY_SIZE 250000

int my_array[ARRAY_SIZE];

void init()
{
int indx;

for (indx=0; indx < ARRAY_SIZE; ++indx)
{
my_array[indx] = rand();
}
}

int cmpfun(int a, int b)
{
if (a > b)
return 1;
else if (a < b)
return -1;
else
return 0;
}

int main()
{
int indx;

init();

heap_sort(my_array, cmpfun, ARRAY_SIZE);

for (indx=1; indx < ARRAY_SIZE; ++indx)
{
if (my_array[indx - 1] > my_array[indx])
{
printf("bad sort\n");
return(1);
}
}

return(0);
}

Related Links :

Program to Calculate Edit Distance between Two Strings in C


#include

#define MAXLEN 80

int findMin(int d1, int d2, int d3)
{
/*
* return min of d1, d2 and d3.
*/
if(d1 < d2 && d1 < d3)
return d1;
else if(d1 < d3)
return d2;
else if(d2 < d3)
return d2;
else
return d3;
}

int findEditDistance(char *s1, char *s2) {
/*
* returns edit distance between s1 and s2.
*/
int d1, d2, d3;

if(*s1 == 0)
return strlen(s2);
if(*s2 == 0)
return strlen(s1);
if(*s1 == *s2)
d1 = findEditDistance(s1+1, s2+1);
else
d1 = 1 + findEditDistance(s1+1, s2+1); // update.
d2 = 1+findEditDistance(s1, s2+1); // insert.
d3 = 1+findEditDistance(s1+1, s2); // delete.

return findMin(d1, d2, d3);
}

int main() {
char s1[MAXLEN], s2[MAXLEN];

printf("Enter string 1: ");
gets(s1);

while(*s1) {
printf("Enter string 2: ");
gets(s2);
printf("Edit distance(%s, %s) = %d.\n", s1, s2, findEditDistance(s1, s2));
printf("Enter string 1(enter to end): ");
gets(s1);
}

return 0;
}

Related Links :

Link Between Array and Pointer


#include

void main()
{

const int size = 5;

int list[size] = {2,1,3,7,8};

int* plist = list;

// print memory address of array elements
for(int i = 0; i < size;i++)
{
printf("list[%d] is in %d\n",i,&list[i]);

}

// accessing array elements using pointer
for(i = 0; i < size;i++)
{
printf("list[%d] = %d\n",i,*plist);

/* increase memory address of pointer so it go to the next
element of the array */
plist++;
}

}

Related Links :

Program to show Concept of Pointers In C




int c = 10;
int c2 = 20;

/* define a pointer and points to an constant integer.
pc can point to another integer but you cannot change the
content of it */

const int *pc = &c;

/* pc++; */ /* cause error */

printf("value of pc is %d\n",pc);

pc = &c2;

printf("value of pc is %d\n",pc);

/* define a constant pointer and points to an integer.
py only can point to y and its memory address cannot be changed
you can change its content */

int y = 10;
int y2 = 20;

int const *py = &y;

*py++;/* it is ok */
printf("value of y is %d\n",y);

/* py = &y2; */ /* cause error */


Related Links :

Program to show Example of recursive function in C


# include

int factorial(unsigned int number)
{
if(number <= 1)
return 1;
return number * factorial(number - 1);
}
void main()
{
int x = 5;
printf("factorial of %d is %d",x,factorial(x));
}

Related Links :

Program to show various way to pass arguments to function in C



#include

/* Program to show various way to pass arguments to function in C */

/* demonstrate pass by pointer */
void swap(int *x, int *y);

/* demonstrate pass by value */
void swap(int x, int y);

/* demonstrate pass an array to the function */
void bubble_sort(int a[], int size);

void print_array(int a[],int size);

void main()
{
int x = 10;
int y = 20;


printf("x,y before swapping\n");
printf("x = %d\n",x);
printf("y = %d\n",y);

// pass by value
swap(x,y);

printf("x,y after swapping using pass by value\n");
printf("x = %d\n",x);
printf("y = %d\n",y);

// pass by pointer
swap(&x,&y);

printf("x,y after swapping using pass by pointer\n");
printf("x = %d\n",x);
printf("y = %d\n",y);

// declare an array
const int size = 5;
int a[size] = {1,3,2,5,4};

printf("array before sorting\n");
print_array(a,size);

bubble_sort(a,size);

printf("array after sorting\n");
print_array(a,size);


}


/* functions implementation */

void swap(int *x, int *y){
int temp = *x;
*x = *y;
*y = temp;
}

void swap(int x, int y){
int temp = x;
x = y;
y = temp;
}

void bubble_sort(int a[], int size)
{
int i,j;
for(i=0;i<(size-1);i++)
for(j=0;j<(size-(i+1));j++)
if(a[j] > a[j+1])
swap(&a[j],&a[j+1]);
}

void print_array(int a[],int size)
{

for(int i = 0;i < size; i++)
{
printf("%d\t",a[i]);
printf("\n");
}
}

Related Links :

a program demonstrates assignment operator in C



#include
void main()
{
int x = 10;

/* demonstrate = operator */
int y = x;
printf("y = %d\n",y);

/* demonstrate += operator */
y += 10;
printf("y += 10;y = %d\n",y);
/* demonstrate -= operator */
y -=5;
printf("y -=5;y = %d\n",y);

/* demonstrate *= operator */
y *=4;
printf("y *=4;y = %d\n",y);

/* demonstrate /= operator */
y /=2;
printf("y /=2;y = %d\n",y);

}

Related Links :

ompanion fgetline function which reads from an arbitrary file pointer in C



#include

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

while((c = getc(fp)) != EOF)
{
if(c == '\n')
break;

if(nch < max)
{
line[nch] = c;
nch = nch + 1;
}
}

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

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


Now we could read one line from ifp by calling

char line[MAXLINE];
...
fgetline(ifp, line, MAXLINE);

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