C Programs Final
C Programs Final
Submitted By:-
Manoj Kumar
62/EC/06
Contents
1) Write a program for finding the inverse of a matrix A=Adj(A)/|A|
2) Write a program for finding any possible largest string in the text when the string
is taken as an input
3) Write a program for generating n*n matrix which have sum of rows, sum of
Columns and sum of diagonals equal
4) Write a program for finding English equivalent of a given input number(<9999)
5) To write a program for searching a value in the list using linear search and
binary search method and display the time taken in each case
6) Write a program to find the age of a person by taking input the present date and
the birth date keeping track of leap years
7) Write a program to store information related to a manager using array of structure
8) To write a program for storing information of a student using linked list
9) Write a program for generating a single link list in which insertion, deletion and
modification of a node is possible
10) Write a program for traversing a tree(Preorder, Post order, In order)
11) Write a program for traversing a graph
12) Write a program for storing, appending and retrieving information from a file
13) Write a program for sorting list of numbers using quick, selection, merge and
insertion sort techniques
14) Write a program to determine the day of a given date
EXP-1
AIM:- Write a program for finding the inverse of a matrix A=Adj(A)/|A|
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int order,i,j;
float matrix[10][10],det;
clrscr();
printf("Enter the order of the matrix:");
scanf("%d",&order);
printf("Enter the elements of matrix\n");
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
scanf("%f",&matrix[i][j]);
}
}
printf("\nyour entered matrix is\n");
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
printf("%4.1f ",matrix[i][j]);
}
printf("\n\n");
}
det = determinant(matrix,order);
if(det==0)
{
printf("Matrix is singular\nInverse not possible");
goto end;
}
printf("\nDeterminant is %f \n",det);
printf("\nThe inverse of the matrix is\n");
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
printf("%f ",pow(-1,i+j)*minor(matrix,order,j,i)/det);
}
printf("\n\n");
}
end:
getch();
}
OUTPUT
Enter the order of the matrix:5
Enter the elements of matrix
12896
26478
23698
14523
17895
Determinant is -323.000000
#include<conio.h>
#include<stdio.h>
//arr to store string
//arr1 to store position of spaces
//arr2 to store length of words b/w spaces
//max to store length of longest word or substring
//maxn to store position of longest word
//IMP user must give a space after end of input string before entering it
void main()
{ char arr[50];
int arr1[50],arr2[50];
int i,j,k,max,maxn;
clrscr();
printf("Enter String\n\n");
for(i=0;i<50;i++)
{
scanf("%c",&arr[i]);
if(arr[i]=='\n')
break;
}
k=1; arr1[0]=0;
for(i=0;;i++)
{ if((arr[i]==' '))
{ arr1[k]=i;
k++;
// printf("%d ",i);
}
if(arr[i]=='\n')
break;
}
for(i=0;i<k;i++)
{ arr2[i]=arr1[i+1]-arr1[i]; }
maxn=0;max=0;
for(i=0;i<k;i++)
{ if(arr2[i]>max)
{ max=arr2[i];
maxn=i;}
}
// printf("\n");
// printf("%d",maxn);
printf("\nLongrst word entered is : ");
for(i=arr1[maxn];i<arr1[maxn+1];i++)
printf("%c",arr[i]);
getch();
}
OUTPUT
Enter String
#include<iostream.h>
#include<conio.h>
int arr[9][9],n;
void display(void)
{ cout<<"\n\nMagic Square\n\n";
for(int i=0;i<n;i++)
{for(int j=0;j<n;j++)
cout<<arr[i][j]<<" ";
cout<<"\n\n";
}
}
void main()
{
clrscr();
int a,b,c;
cout<<"Enter order of matrix(Odd integer only)\n";
cin>>n;
arr[0][n/2]=1;
a=0;b=n/2;
//cout<<"\n"<<a<<" \n"<<b<<"\n";
for(int m=2;m<=n*n;m++)
{ a=a-1;
b=b+1;
if(a<0)
a=a+n;
if(b>n-1)
b=b-(n);
if(arr[a][b]==0)
arr[a][b]=m;
else
{ a=a+2;
b=b-1;
if(a>(n-1))
a=a-(n);
if(b<0)
b=b+n;
arr[a][b]=m;
}
}
display();
getch();
}
OUTPUT
Magic Square
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
EXP-4
AIM: - Write a program for finding English equivalent of a given input number(<9999)
# include<stdio.h>
# include<conio.h>
void main()
{ int p,n;
char a[30][10]={"one ","two ","three ","four ","five ","six ","seven ","eight ","nine
","ten ","eleven ","twelv e","thirteen ","fourteen ","fifteen ","sixteen ","seventeen
","eighteen ","nineteen ","twenty ","thirty ","forty ","fifty ","sixty ","seventy ","eighty
","ninety "};
clrscr();
while(1)
{
printf("enter a no.(0 to 9999):");
scanf("%d",&n);
if(n>=0 && n<=9999)
break;
}
if(n==0)
{
printf("zero");
getch();
exit(0);
}
p=n/1000;
if(p!=0)
{
printf("%s",a[p-1]);
printf("thousand ");
}
n=n-(p*1000);
p=n/100;
if(p!=0)
{
printf("%s",a[p-1]);
printf("hundred ");
}
n=n-(p*100);
if(n>=1 && n<=20)
{
printf("%s",a[n-1]);
getch();
exit(0);
}
p=n/10;
if(p!=0)
{
printf("%s",a[p+17]);
}
n=n-(p*10);
if(n!=0)
{
printf("%s",a[n-1]);
}
getch();
}
OUTPUT
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
struct dostime_t t1,t2,t3;
int a[10],i,d;
clrscr();
printf("Enter the elements of A:");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\nArray A:");
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n\nEnter the value to be searched:");
scanf("%d",&d);
_dos_gettime(&t1);
printf("\nBinary Searching");
binarysearch(a,10,d);
_dos_gettime(&t2);
printf("\nIn binarysearching Execution time=0.%02d second",(t2.hsecond-
t1.hsecond)/100);
printf("\n\nLinearsearching");
linearsearch(a,10,d);
_dos_gettime(&t3);
printf("\nIn linearsearching Execution time=0.%02d second",(t3.hsecond-
t2.hsecond)/100);
getch();
}
OUTPUT
Array A:5 9 32 35 39 45 49 87 89 99
Binary Searching
Element 45 is found at location 6
Linearsearching
Element 45 is found at location 6
#include<stdio.h>
#include<conio.h>
void main()
{
int d1,d2,m1,m2,y1,y2;
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int b[12]={31,29,31,30,31,30,31,31,30,31,30,31};
clrscr();
printf("Enter the today's date?\n");
scanf("%d%d%d",&d1,&m1,&y1);
OUTPUT
#include<stdio.h>
#include<conio.h>
struct man{
char name[25];
int age;
int id;
long int salary;
}ar[3];
void main()
{
int i,j;
char name[25],x,y;
clrscr();
for(i=0;i<3;i++)
{
printf("\nEnter the data of %d manager\n",i+1);
printf("\nEnter name : ");
gets(ar[i].name);
printf("\nEnter age : ");
scanf("%d",&ar[i].age);
printf("\nEnter ID : ");
scanf("%d",&ar[i].id);
printf("\nEnter Salary : ");
scanf("%ld",&ar[i].salary);
scanf("%c",&y);
clrscr();
}
clrscr();
for(i=0;i<3;i++)
{
printf("\nName : %s\nID : %d\nAge : %d\nSalary :
%ld\n",ar[i].name,ar[i].id,ar[i].age,ar[i].salary);
}
getch();
}
OUTPUT
Manager 1
Name:Raj
Age:45
Salary:Rs.362145
Manager 2
Name:Ravi
Age:42
Salary:Rs.563214
Manager 3
Name:Aryan
Age:56
Salary:Rs.563296
EXP-8
AIM :- To write a program for storing information of a student using linked list.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
struct node{
int id;
char name[15];
node *next;
}*start,*rear,*save,*ptr,*nwptr;
int no;
if(start==NULL)
{start=np;
rear=np;
}
else
{rear->next=np;
rear=np;
}
}
void main()
{ clrscr();
start=NULL;
int id;
char ch,nam[15];
no=0;
cout<<"Insert information\n\n";
do{
cout<<"Enter ID No.of student\n";
cin>>id;
cout<<"Enter name of student\n";
gets(nam);
nwptr=new_node(id,nam);
insert(nwptr);
display(start);
cout<<"Continue to enter information\n(Press y for yes)";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
getch();
}
OUTPUT
Insert information
2 988 Raj
#include<iostream.h>
#include<conio.h>
struct node{
int info;
node *next;
}*start,*rear,*save,*ptr,*nwptr;
int no;
node *new_node(int n)
{ no++; ptr=start;
ptr=new node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
if(pos<=1)
{
cout<<"Inserting node in begining\n";
if(start==NULL)
{
start=np;
rear=np;
}
else
{save=start;
start=np;
np->next=save;
}
}
else if(pos>=no)
{
cout<<"Inserting node in the end\n";
{if(start==NULL)
{start=np;
rear=np;
}
else
{rear->next=np;
rear=np;
}
}
}
else
{while(!(p==pos-2))
{ ptr=ptr->next;
p++;
}
save=ptr->next;
ptr->next=np;
np->next=save;
}
}
if(pos<=1)
{ save=start;
start=start->next;
no--;
delete save;
}
else if(pos>=no)
{ while(!(p==no-2))
{ptr=ptr->next;
p++;
}
save=ptr->next;
ptr->next=NULL;
rear=ptr;
no--;
delete save;
}
else
{ while(!(p==pos-2))
{ptr=ptr->next;
p++;
}
save=ptr->next;
ptr->next=save->next;
no--;
delete save;
}
}
void modify(int pos,int data)
{ if(no==0)
{cout<<"There is no node to modify\n";
return;
}
int p=0;
ptr=start;
if(pos<=1)
{ start->info=data;
}
else if(pos>=no)
{ rear->info=data;
}
else
{ while(!(p==pos-1))
{ ptr=ptr->next;
p++;
}
ptr->info=data;
}
}
void main()
{ clrscr();
start=NULL;
int inf,c,pos;
char ch;
no=0;
do{
cout<<"MENU\n\n";
cout<<"Enter your choice\n";
cout<<"1.Insert node\n2.Delete node\n3.Modify content of node\n";
cin>>c;
switch(c)
{
case 1: cout<<"Enter information for node\n";
cin>>inf;
nwptr=new_node(inf);
cout<<"Position for node\n";
cin>>pos;
insert(nwptr,pos);
break;
case 2: if(no==0)
{ cout<<"First create and insert some nodes\n";
break;}
cout<<"Enter position of node\n";
cin>>pos;
del(pos);
break;
case 3: if(no==0)
{ cout<<"First create and insert some nodes\n";
break;}
display(start);
cout<<"Continue\n";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
getch();
}
OUTPUT
MENU
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
struct node *right;
struct node *left;
int data;
};
/*For building a tree according to the data in array A,l & r.*/
struct node* buildtree(int n);
void inorder(struct node*);
void preorder(struct node*);
void postorder(struct node*);
void main()
{
struct node* tree;
clrscr();
tree=buildtree(0);
printf("\nInorder traversal: ");
inorder(tree);
printf("\n\nPreorder traversal: ");
preorder(tree);
printf("\n\nPostorder traversal: ");
postorder(tree);
getch();
}
/*Values of nodes*/
int A[]={20,17,6,8,10,7,18,13,12,5};
/*Address of left node pointed by corresponding node in A*/
int l[]={1,2,9,5,-1,-1,-1,8,-1,-1};
/*Address of right node pointed by corresponding node in A*/
int r[]={-1,6,3,4,7,-1,-1,-1,-1,-1};
/* -1 indicates that the node is pointing to NULL*/
if(n!=(-1))
{
tree=(struct node*)malloc(sizeof(struct node));
tree->left=buildtree(l[n]);
tree->data=A[n];
tree->right=buildtree(r[n]);
}
return(tree);
}
OUTPUT
Inorder traversal: 5 6 7 8 10 12 13 17 18 20
Preorder traversal: 20 17 6 5 8 7 10 13 12 18
Postorder traversal: 5 7 12 13 10 8 6 18 17 20
EXP-12
AIM: - Write a program for storing, appending and retrieving information from a file.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f;
char str[50],s[1],c;
int i,ch;
clrscr();
do{
printf("1.Storing\n2.Appending\n3.Retrieving\n");
printf("Enter your choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
f=fopen("abc.txt","w");
if(f==NULL)
{
printf("Cannot open file ");
exit(1);
}
printf("Enter string to be written to file ");
gets(str);
gets(str);
fputs(str,f);
fclose(f);
break;
case 2:
f=fopen("abc.txt","a");
if(f==NULL)
{
printf("Cannot open file ");
exit(1);
}
printf("Enter the string to be appended to file ");
gets(str);
gets(str);
fputs(str,f);
fclose(f);
break;
case 3:
f=fopen("abc.txt","r");
if(f==NULL)
{
printf("Cannot open file ");
exit(1);
}
fgets(str,50,f);
printf("%s",str);
fclose(f);
break;
default: break;
}
printf("\nContinue(Press'y') ");
scanf("%c",&c);
}while((c=='y')||(c=='Y'));
getch();
}
OUTPUT
1.Storing
2.Appending
3.Retrieving
Enter your choice 1
Enter string to be written to file Hello i am Manoj
Continue(Press'y') y
1.Storing
2.Appending
3.Retrieving
Enter your choice 2
Enter the string to be appended to file Who are you?
Continue(Press'y') y
1.Storing
2.Appending
3.Retrieving
Enter your choice 3
Hello i am Manoj Who are you?
Continue(Press'y')
EXP-13
AIM: - Write a program for sorting list of numbers using quick, selection, merge and
insertion sort techniques.
#include<stdio.h>
#include<conio.h>
void creation(int*,int);
void print(int*,int);
void selectionsort(int*,int);
void quicksort(int*,int,int);
int split(int*,int,int);
void mergesort(int*,int*,int*,int,int);
void insertionsort(int*,int);
void main()
{
int a[10],b[5],c[15],i;
//clrscr();
printf("\nEnter the elements of array A having 10 elements:");
creation(a,10);
printf("\nBefore sorting Array A:");
print(a,10);
printf("\nFor mergesorting press 1.\nFor quicksorting press 2.\n");
printf("For insertionsorting press 3.\nFor selectionsorting press 4.\n");
printf("Enter your choice:");
again:
scanf("%d",&i);
switch(i)
{
case 1:printf("\nEnter the elements of array B having 5 elements:");
creation(b,5);
printf("\nBefore sorting Array B:");
print(b,5);
mergesort(a,b,c,10,5);
break;
case 2:quicksort(a,0,9);
printf("After quicksorting array is:");
print(a,10);
break;
case 3:insertionsort(a,10);
break;
case 4:selectionsort(a,10);
break;
default:printf("Enter the correct choice:");
goto again;
}
getch();
}
OUTPUT
#include<stdio.h>
#include<conio.h>
void main()
{
long int a,b,d,i,e,c;
int s[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int s1[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
char
day[][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday
"};
d=0;
clrscr();
printf("Enter the date : ");
scanf("%ld%ld%ld",&a,&b,&c);
//printf("Enter the month : ");
//scanf("%ld",&b);
//printf("Enter the year : ");
//scanf("%ld",&c);
for(i=1;i<c;i++)
{
if((i%4)!=0)
d+=365;
else if((i%4)==0)
{
if((i%100)!=0)
d+=366;
else if((i%100)==0)
{
if((i%400)==0)
d+=366;
else
d+=365;
}
}
}
for(i=1;i<b;i++)
{ if((c%4)!=0)
d+=s[i];
else if((c%4)==0)
{
if((c%100)!=0)
d+=s1[i];
else if((c%100)==0)
{
if((c%400)==0)
d+=s1[i];
else
d+=s[i];
}
}
}
d+=a;
e=d%7;
// printf("%ld",d);
// printf("\n%ld",e);
// printf("\n");
puts(day[e]);
getch();
}
OUTPUT
Enter the date : 3 11 2008
Monday