Thursday 14 June 2012

Program to find simultaneous linear equations using Jordan Elimination method

/*
#include<stdio.h>
#include<stdlib.h>
main()
{
int a[10][10],n,i,j,k;
printf("\n\tEnter the no.of variables: ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n\tEQUATION %d\n\t", i+1);
for(j=0;j<n;j++)
{
printf("\n\tEnter the coefficients of x%d : ", j+1);
scanf("%d", &a[i][j]);
}
printf("\nEnter the constant: ");
scanf("%d", &a[i][n]);
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
l=a[i][k];
for(j=0;j<=n;j++)
{
if(i!=k)
a[i][j]=a[k][k]*a[i][j]-(l*a[k][j]);
}
}
}
printf("\n\tSolutions are: \n");
for(i=0;i<n;i++)
{
if(a[i][i]==0)
{
printf("\n\tNOn trivial solution");
exit(0);
}
printf("\n\tThe value of %d
is %.3f",i+1,(float)(a[i][n]/(float)a[i][i]));
}
}

/*
-----------------OUTPUT------------------------

Enter the no.of variables: 3

EQUATION 1

Enter the coefficients of x1 : 3

Enter the coefficients of x2 : 2

Enter the coefficients of x3 : 1

Enter the constant: 6

EQUATION 2

Enter the coefficients of x1 : 1

Enter the coefficients of x2 : 2

Enter the coefficients of x3 : 3

Enter the constant: 6

EQUATION 3

Enter the coefficients of x1 : 2

Enter the coefficients of x2 : 3

Enter the coefficients of x3 : 1

Enter the constant: 6

Solutions are:

The value of 1 is 1.000
The value of 2 is 1.000
The value of 3 is 1.000
-----------------------------------------------
*/

Using Taylor's series implement sin(x), cos(x), e^x

/*
#include<stdio.h>
#include<math.h>
double fact(int num)
{
if(num==0)
return 1;
else
return (fact(num-1))*num;
}
main()
{
int ch,i,neg,c;
float val=1,x=0;
double t=0.0,sin=0.0,cos=1.0;
a: printf("Enter the value of x : ");
scanf("%d",&x);
t=(x*3.14)/180;
printf("\n\t----------Select your Choice------------\n");
printf("\n\t1. e^x");
printf("\n\t2. sin x");
printf("\n\t3. cos x");
printf("\n\t4. Exit");
printf("\n\n-----------------------------------------------------\n\n")
printf("\n\nEnter your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
for(i=1;i<100;i++)
val+=pow(x,i)/fact(i);
printf("\ne^%f = %.3f\n",x,val);
break;
case 2:
neg=-1;
for(i=1;i<=20;i++)
{
neg=neg*-1;
sin=sin+((pow(t,i)/fact(i))*neg);
}
printf("\nsin(%f) = %.3f\n",x,sin);
break;
case 3:
neg=1;
for(i=2;i<=20;i+=2)
{
neg=neg*-1;
cos=cos+((pow(t,i)/fact(i))*neg);
}
printf("\ncos(%f) = %.3f\n",x,cos);
break;
case 4:
printf("\nEXITED\n");
exit(0);
default:printf("\nINVALID CHOICE\n");
break;

}
printf("\nDO YOU WANT TO CONTINUE(Y/N) : \n");
printf("\npress 1 to continue......... : ");
scanf("%d",&c);
if(c==1)
goto a;
}
/*
---------------OUTPUT---------------

Enter the value of x : 0

----------Select your Choice------------

1. e^x
2. sin x
3. cos x
4. Exit

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

Enter your Choice : 1

e^0.000000 = 1.000

DO YOU WANT TO CONTINUE(Y/N) :

press 1 to continue......... : 1
Enter the value of x : 0

----------Select your Choice------------

1. e^x
2. sin x
3. cos x
4. Exit

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

Enter your Choice : 2

sin(0.000000) = 0.000

DO YOU WANT TO CONTINUE(Y/N) :

press 1 to continue......... : 1
Enter the value of x : 0

----------Select your Choice------------

1. e^x
2. sin x
3. cos x
4. Exit

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

Enter your Choice : 3

cos(0.000000) = 1.000

DO YOU WANT TO CONTINUE(Y/N) :

press 1 to continue......... : 1
Enter the value of x : 0

----------Select your Choice------------

1. e^x
2. sin x
3. cos x
4. Exit

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

Enter your Choice : 6

INVALID CHOICE

DO YOU WANT TO CONTINUE(Y/N) :

press 1 to continue......... : 1
Enter the value of x : 0
----------Select your Choice------------
1. e^x
2. sin x
3. cos x
4. Exit
-----------------------------------------------------
Enter your Choice : 4
EXITED

Conversion of Binary to Hexa Decimal, Decimal, Octal

/*
#include<stdio.h>
#include<math.h>
int basetodec(int n, int b)
{
int s=0,i=0,d;
while(n>0)
{
d=n%10;
if(d>0)
s=s+pow(b,i)*d;
i++;
n/=10;
}
return s;
}
int dectobase(int n,int b)
{
int i=1,s=0,d;
while(n>0)
{
d=n%b;
s=s+i*d;
n/=b;
i*=10;
}
return s;
}
main()
{
char a[15];
int ext,n,b,d,g,sum=0,c,i=0,x,f=0,ex;
a: printf("\n\n\t-------Select your Choice--------\n");
printf("\n\n\t1. Binary Number to Decimal Number");
printf("\n\t2. Binary Numberto Hexa Decimal Number");
printf("\n\t3. Binary Number to Octal Number");
printf("\n\t4. Decimal Number to Binary Number");
printf("\n\t5. Hexa Decimal Number to Binary Number");
printf("\n\t6. Octal Number to Binary Number");
printf("\n\t7. Exit");
printf("\n--------------------------------------------------\n");
b: printf("\nEnter your Choice : ");
scanf("%d",&c);
if(c<7)
printf("Enter the Number : ");
if(c==5)
{
scanf("%s",a);
while((a[i]!='\0')&&(f==0))
{
if((a[i]>=65&&a[i]<=70)||(a[i]>=45&&a[i]<=59))
f=0;
else
{
f=1;
printf("\nINVALID HEXA DECIMAL NUMBER\n");
goto b;
}
x=i;
i++;
}
}
else
{
scanf("%d",&n);
b=n;
if((c==1)||(c==2)||(c==3))
{
while(b>0)
{
d=b%10;
if((d==0)||(d==1))
b/=10;
else
{
b=0,f=1;
printf("\nINVALID BINARY NUMBER\n");
goto b;
}
}
}
else
{
if(c==6)
{
while(b>0)
{
d=b%10;
if(d>7)
{
f=1;
printf("\nINVALID HEXA DECIMAL
NUMBER\n");
goto b;
}
b/=10;
}
}
}
}
if(f==0)
{
switch(c)
{
case 1:
printf("\nThe Decimal Equivalent of the given Binary
Number is : %d",basetodec(n,2));
break;
case 2:
b=basetodec(n,2);
i=0;
while(b>0)
{
d=b%16;
if((d<17)&&(d>=0))
a[i]=d+48;
if((d<17)&&(d>=10))
a[i]=d+55;
b/=16;
i++;

}
x=0;
printf("\nThe Hexa Decimal Equivalent of the given
Binary Number is : ");
while(x<i)
{
printf("%c",a[x]);
x++;
}
break;
case 3:
printf("\nThe Octal Equivalent of the given Binary
Number is : %d",dectobase(basetodec(n,2),8));
break;
case 4:
printf("\nThe Binary Equivalent of the given Decimal
Number is : %d",dectobase(n,2));
break;
case 5:
i=0;
while(x>=0)
{
g=pow(16,i);
if((a[x]<=57)&&(a[x]>=48))
b=a[x]-48;
if((a[x]<71)&&(a[x]>=65))
b=a[x]-55;
sum=b*g+sum;
x--;
i++;
}
printf("\nThe Binary Equivalent of the given Hexa
Decimal Number is : %d",dectobase(sum,2));
break;
case 6:
printf("\nThe Binary Equivalent of the given Octal
Number is : %d",dectobase(basetodec(n,8),2));
break;
case 7:
printf("\n\nDO YOU WANT TO QUIT(Y/N) : \n");
printf("press 1 to continue..........: ");
scanf("%d",&ex);
if(ex==1)
goto a;
else
exit(0);
default:printf("\nYOUR CHOICE IS WRONG\n");
break;
}
printf("\n\nDO YOU WANT TO CONTINUE(Y/N) : \n");
printf(" press 1 to continue........... : ");
scanf("%d",&ext);
if(ext== 1)
goto a;
else
exit(1);

}
}
/*

--------------OUTPUT---------------


-------Select your Choice--------


1. Binary Number to Decimal Number
2. Binary Numberto Hexa Decimal Number
3. Binary Number to Octal Number
4. Decimal Number to Binary Number
5. Hexa Decimal Number to Binary Number
6. Octal Number to Binary Number
7. Exit
--------------------------------------------------

Enter your Choice : 1
Enter the Number : 323

INVALID BINARY NUMBER

-------Select your Choice--------


1. Binary Number to Decimal Number
2. Binary Numberto Hexa Decimal Number
3. Binary Number to Octal Number
4. Decimal Number to Binary Number
5. Hexa Decimal Number to Binary Number
6. Octal Number to Binary Number
7. Exit
--------------------------------------------------


Enter your Choice : 1
Enter the Number : 1111

The Decimal Equivalent of the given Binary Number is : 15

DO YOU WANT TO CONTINUE(Y/N) :
press 1 to continue........... : 1


-------Select your Choice--------


1. Binary Number to Decimal Number
2. Binary Numberto Hexa Decimal Number
3. Binary Number to Octal Number
4. Decimal Number to Binary Number
5. Hexa Decimal Number to Binary Number
6. Octal Number to Binary Number
7. Exit
--------------------------------------------------

Enter your Choice : 2
Enter the Number : 1111

The Hexa Decimal Equivalent of the given Binary Number is : F

DO YOU WANT TO CONTINUE(Y/N) :
press 1 to continue........... : 1


-------Select your Choice--------


1. Binary Number to Decimal Number
2. Binary Numberto Hexa Decimal Number
3. Binary Number to Octal Number
4. Decimal Number to Binary Number
5. Hexa Decimal Number to Binary Number
6. Octal Number to Binary Number
7. Exit
--------------------------------------------------

Enter your Choice : 3
Enter the Number : 1111

The Octal Equivalent of the given Binary Number is : 17

DO YOU WANT TO CONTINUE(Y/N) :
press 1 to continue........... : 1


-------Select your Choice--------


1. Binary Number to Decimal Number
2. Binary Numberto Hexa Decimal Number
3. Binary Number to Octal Number
4. Decimal Number to Binary Number
5. Hexa Decimal Number to Binary Number
6. Octal Number to Binary Number
7. Exit
--------------------------------------------------

Enter your Choice : 4
Enter the Number : 10

The Binary Equivalent of the given Decimal Number is : 1010

DO YOU WANT TO CONTINUE(Y/N) :
press 1 to continue........... : 1


-------Select your Choice--------


1. Binary Number to Decimal Number
2. Binary Numberto Hexa Decimal Number
3. Binary Number to Octal Number
4. Decimal Number to Binary Number
5. Hexa Decimal Number to Binary Number
6. Octal Number to Binary Number
7. Exit
--------------------------------------------------

Enter your Choice : 5
Enter the Number : 15

The Binary Equivalent of the given Hexa Decimal Number is : 10101

DO YOU WANT TO CONTINUE(Y/N) :
press 1 to continue........... : 1









-------Select your Choice--------


1. Binary Number to Decimal Number
2. Binary Numberto Hexa Decimal Number
3. Binary Number to Octal Number
4. Decimal Number to Binary Number
5. Hexa Decimal Number to Binary Number
6. Octal Number to Binary Number
7. Exit
--------------------------------------------------

Enter your Choice : 6
Enter the Number : 10

The Binary Equivalent of the given Octal Number is : 1000

DO YOU WANT TO CONTINUE(Y/N) :
press 1 to continue........... : 1


-------Select your Choice--------


1. Binary Number to Decimal Number
2. Binary Numberto Hexa Decimal Number
3. Binary Number to Octal Number
4. Decimal Number to Binary Number
5. Hexa Decimal Number to Binary Number
6. Octal Number to Binary Number
7. Exit
--------------------------------------------------

Enter your Choice : 9

YOUR CHOICE IS WRONG


DO YOU WANT TO CONTINUE(Y/N) :
press 1 to continue........... : 0


_______________________________________________________________________


*/



To create a file for insert,display,delete,update,sort records

#include<stdio.h>
struct student
{
int rollno;
char name[30];
float mark;
}stud;
//    FUNCTION TO INSERT RECORDS TO THE FILE
void insert()
{
FILE *fp;
fp = fopen("Record", "a");
printf("Enter the Roll no   :");
scanf("%d", &stud.rollno);
printf("Enter the Name      :");
scanf("%s", &stud.name);
printf("Enter the mark      :");
scanf("%f", &stud.mark);
fwrite(&stud, sizeof(stud), 1, fp);
fclose(fp);
}
//    FUNCTION TO DISPLAY RECORDS
void disp()
{
FILE *fp1;
fp1 = fopen("Record", "r");
printf("\nRoll Number\tName\tMark\n\n");
while (fread(&stud, sizeof(stud), 1, fp1))
printf("  %d\t\t%s\t%.2f\n", stud.rollno, stud.name, stud.mark);
fclose(fp1);
}
//    FUNCTION TO SEARCH THE GIVEN RECORD
void search()
{
FILE *fp2;
int r, s, avl;
printf("\nEnter the Roll no you want to search  :");
scanf("%d", &r);
avl = avlrollno(r);
if (avl == 0)
printf("Roll No %d is not available in the file\n",r);
else
{
fp2 = fopen("Record", "r");
while (fread(&stud, sizeof(stud), 1, fp2))
{
s = stud.rollno;
if (s == r)
{
printf("\nRoll no = %d", stud.rollno);
printf("\nName    = %s", stud.name);
printf("\nMark    = %.2f\n", stud.mark);
}
}
fclose(fp2);
}
}
//    FUNCTION TO DELETE A RECORD


void deletefile()
{
FILE *fpo;
FILE *fpt;
int r, s;
printf("Enter the Roll no you want to delete :");
scanf("%d", &r);
if (avlrollno(r) == 0)
printf("Roll no %d is not available in the file\n", r);
else
{
fpo = fopen("Record", "r");
fpt = fopen("TempFile", "w");
while (fread(&stud, sizeof(stud), 1, fpo))
{
s = stud.rollno;
if (s != r)
fwrite(&stud, sizeof(stud), 1, fpt);
}
fclose(fpo);
fclose(fpt);
fpo = fopen("Record", "w");
fpt = fopen("TempFile", "r");
while (fread(&stud, sizeof(stud), 1, fpt))
fwrite(&stud, sizeof(stud), 1, fpo);
printf("\nRECORD DELETED\n");
fclose(fpo);
fclose(fpt);
}

}
//    FUNCTION TO UPDATE THE RECORD
void update()
{
int avl;
FILE *fpt;
FILE *fpo;
int s, r, ch;
printf("Enter roll number to update:");
scanf("%d", &r);
avl = avlrollno(r);
if (avl == 0)
{
printf("Roll number %d is not Available in the file", r);
}
else
{
fpo = fopen("Record", "r");
fpt = fopen("TempFile", "w");
while (fread(&stud, sizeof(stud), 1, fpo))
{
s = stud.rollno;
if (s != r)
fwrite(&stud, sizeof(stud), 1, fpt);
else
{
printf("\n\t1. Update Name of Roll Number %d", r);
printf("\n\t2. Update Mark of Roll Number %d", r);
printf("\n\t3. Update both Name and Mark of Roll Number %d", r);
printf("\nEnter your choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter Name:");
scanf("%s", &stud.name);
break;
case 2:
printf("Enter Mark : ");
scanf("%f", &stud.mark);
break;
case 3:
printf("Enter Name: ");
scanf("%s", &stud.name);
printf("Enter Mark: ");
scanf("%f", &stud.mark);
break;
default:
printf("Invalid Selection");
break;
}
fwrite(&stud, sizeof(stud), 1, fpt);
}
}
fclose(fpo);
fclose(fpt);
fpo = fopen("Record", "w");
fpt = fopen("TempFile", "r");
while (fread(&stud, sizeof(stud), 1, fpt))
{
fwrite(&stud, sizeof(stud), 1, fpo);
}
fclose(fpo);
fclose(fpt);
printf("RECORD UPDATED");
}
}
/* FUNCTION TO SORT THE RECORD */
void sort()
{
int a[20], count = 0, i, j, t, c;
FILE *fpo;
fpo = fopen("Record", "r");
while (fread(&stud, sizeof(stud), 1, fpo))
{
a[count] = stud.rollno;
count++;
}
c = count;
for (i = 0; i<count - 1; i++)
{
for (j = i + 1; j<count; j++)
{
if (a[i]>a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
printf("Roll No.\tName\t\tMark\n\n");
count = c;
for (i = 0; i<count; i++)
{
rewind(fpo);
while (fread(&stud, sizeof(stud), 1, fpo))
{
if (a[i] == stud.rollno)
printf("\n %d\t\t %s \t\t %2f",stud.rollno, stud.name, stud.mark);
}

}
}
//    FUNCTION TO CHECK GIVEN ROLL NO IS AVAILABLE //
int avlrollno(int rno)
{
FILE *fp;
int c = 0;
fp = fopen("Record", "r");
while (!feof(fp))
{
fread(&stud, sizeof(stud), 1, fp);

if (rno == stud.rollno)
{
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}
//FUNCTION TO CHECK THE FILE IS EMPTY OR NOT
int empty()
{
int c = 0;
FILE *fp;
fp = fopen("Record", "r");
while (fread(&stud, sizeof(stud), 1, fp))
c = 1;
fclose(fp);
return c;
}
// MAIN PROGRAM
void main()
{
int c, emp;
do
{
printf("\n\t---Select your choice---------\n");
printf("\n\t1. INSERT\n\t2. DISPLAY\n\t3. SEARCH");
printf("\n\t4. DELETE\n\t5. UPDATE\n\t6. SORT");
printf("\n\t7. EXIT");
printf("\n\n------------------------------------------\n");
printf("\nEnter your choice:");
scanf("%d", &c);
printf("\n");
switch (c)
{
case 1:
insert();
break;
case 2:
emp = empty();
if (emp == 0)
printf("\nThe file is EMPTY\n");
else
disp();
break;
case 3:
search();
break;
case 4:
deletefile();
break;
case 5:
update();
break;
case 6:
emp = empty();
if (emp == 0)
printf("\n The file is EMPTY\n");
else
sort();
break;
case 7:
exit(1);
break;
default:
printf("\nYour choice is wrong\nPlease try again...\n");
break;

}
} while (c != 7);


}

Out put