0% found this document useful (0 votes)
53 views

Print C Programme

The document contains code to implement queue operations using an array. It defines a node structure with information and next pointer fields. Functions are defined to enqueue, dequeue and display elements. The main function contains a menu to choose insert, delete or display and calls the corresponding functions. Elements can be added to the rear and removed from the front of the queue in FIFO order.

Uploaded by

Naga Periyasamy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Print C Programme

The document contains code to implement queue operations using an array. It defines a node structure with information and next pointer fields. Functions are defined to enqueue, dequeue and display elements. The main function contains a menu to choose insert, delete or display and calls the corresponding functions. Elements can be added to the rear and removed from the front of the queue in FIFO order.

Uploaded by

Naga Periyasamy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

/*Find the roots of a quadratic equation using function (ax2+bx+c=0)*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,r1,r2,determinant,real,imag;
clrscr();
printf("Enter the co-efficients");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if(determinant>0)
{
r1=(-b+sqrt(determinant))/(2*a);
r2=(-b-sqrt(determinant))/(2*a);
printf("The roots are distinct\nr1=%.2f, r2=%.2f\n",r1,r2);
}
else if(determinant==0)
{
r1=r2=-b/(2*a);
printf("The roots are equal\n");
printf("r1=%.2f\nr2=%.2f\n",r1,r2);
}
else
{
real=-b/(2*a);
imag=sqrt(-determinant)/(2*a);
printf("The roots are complex\n");
printf("The roots are %.2f+%.2fi and %.2f-%.2fi\n",real,imag,real,imag);
}
getch();
}
Input/Output

Enter the co-efficients


1
2
5
The roots are complex
The roots are -1.00+2.00i and -1.00-2.00i

Enter the co-efficients


3
-6
3
The roots are equal
r1=1.00
r2=1.00

Enter the co-efficients


3
5
2
The roots are distinct
r1=-0.67, r2=-1.00
/*Matrix manipulation using function*/

#include<stdio.h>
#include<conio.h>
void read_mat(int m,int n,int a[5][5])
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("Enter element:");
scanf("%d",&a[i][j]);
}
}
}
void read_mat1(int p,int q,int b[5][5])
{
int i,j;
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("Enter element:");
scanf("%d",&b[i][j]);
}
}
}
void print_mat(int m,int n,int a[5][5])
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
}
void print_mat1(int p,int q,int b[5][5])
{
int i,j;
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("%d\t",b[i][j]);
printf("\n");
}
}
void add_mat(int m,int n,int a[5][5],int b[5][5],int s[5][5])
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
s[i][j]=a[i][j]+b[i][j];
}
}
}
void sub_mat(int m,int n,int a[5][5],int b[5][5],int sb[5][5])
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sb[i][j]=a[i][j]-b[i][j];
}
}
}
void mul_mat(int m,int n,int q,int a[5][5],int b[5][5],int mul[5][5])
{
int i,j,k;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
mul[i][j]=0;
for(k=0;k<q;k++)
{
mul[i][j]=mul[i][j]+a[i][k]*b[k][j];
}}
}}
void main()
{
int m,n,p,q,i,j,a[5][5],b[5][5],s[5][5],sb[5][5],mul[5][5];
clrscr();
printf("\nEnter order of first matrix:\n");
scanf("%d%d",&m,&n);
printf("\nOrder of second matrix:\n");
scanf("%d%d",&p,&q);
if((m!=p)&&(n!=q))
printf("\n Matrices are impatable for addition and substraction");
else
{
printf("\nRead the element of first matrix\n");
read_mat(m,n,a);
printf("\nRead the element of second matrix\n");
read_mat(p,q,b);
printf("\nFirst matrix is:\n");
print_mat(m,n,a);
printf("\nSecond matrix is:\n");
print_mat(p,q,b);
add_mat(m,n,a,b,s);
printf("\nAfter addition matrix is:\n");
print_mat(m,n,s);
sub_mat(m,n,a,b,sb);
printf("\nAfter substracion matrix is:\n");
print_mat(m,n,sb);
}
if(p==q)
{
mul_mat(m,n,q,a,b,mul);
printf("\nAfter multiplication matrix is:\n");
print_mat(m,q,mul);
}
else
printf("\nMatrices are incompatible for multiplication");
getch();
}
Input/Output

Enter order of first matrix:


2
2

Order of second matrix:


2
2

Read the element of first matrix


Enter element:1
Enter element:2
Enter element:3
Enter element:4
Read the element of second matrix
Enter element:4
Enter element:3
Enter element:2
Enter element:1

First matrix is:


1 2
3 4

Second matrix is:


4 3
2 1

After addition matrix is:


5 5
5 5

After substracion matrix is:


-3 -1
1 3

After multiplication matrix is:


8 5
20 13
/*Employee Salary Bill*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,ov=0,hr=0,ovl,rp,ra=0,r;
float gp;
clrscr();
printf("Enter the no of hours worked per week\n");
scanf("%d",&n);
if(n>=45)
{
hr=45*15;
}
ovl=n-45;
if(ovl>=25)
{
ov=25*1.5*15;
}
r=n-45-25;
if(r>0)
{
ra=r*2*15;
}
gp=hr+ov+ra;
printf("The Gross weekly wage=%f",gp);
getch();
}
Input/Output

Enter the no of hours worked per week


100
The Gross weekly wage=2137.000000
/*Find the factorial of a given number using recusrsion*/

#include<stdio.h>
#include<conio.h>
int fact(int n);
void main()
{
int n;
clrscr();
printf("\nEnter an integer:");
scanf("%d",&n);
printf("\nThe factorial of %d is=%d",n,fact(n));
getch();
}
int fact(int n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}
Input/Output

Enter an integer:6

The factorial of 6 is = 720


/*Find a word is Palindrome or Not*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],b[10];
clrscr();
printf("Enter the string\n");
scanf("%s",&a);
strcpy(b,a);
strrev(a);
if(strcmp(a,b)==0)
printf("Given string is palindrome\n");
else
printf("Given string is not palindrome\n");
getch();
}
Input/Output

Enter the string


malayalam
Given string is palindrome

Enter the string


gowri
Given string is not palindrome
/*To read 10 values to an array variable using Pointers*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],*p,i;
clrscr();
printf("Enter 10 values\n");
for(i=1;i<=10;i++)
{
scanf("%d",&a[i]);
}
p=a;
printf("Display 10 values using pointers\n");
for(i=1;i<=10;i++)
{
p++;
printf("p[%d]=%d\n",i,*p);
}
getch();
}
Input/Output

Enter 10 values
98
87
76
65
54
43
32
21
10
09
Display 10 values using pointers
p[1]=98
p[2]=87
p[3]=76
p[4]=65
p[5]=54
p[6]=43
p[7]=32
p[8]=21
p[9]=10
p[10]=9
/*To copy the contents of one file into another file*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(int arg,char *arr[])
{
FILE *fs,*ft;
char ch;
clrscr();
if(arg!=3)
{
printf("Argument Missing!Press key to exit.");
getch();
exit(0);
}
fs=fopen(arr[1],"r");
if(fs==NULL)
{
printf("Cannot open source file! Press key to exit.");
getch();
exit(0);
}
ft=fopen(arr[2],"w");
if(ft==NULL)
{
printf("Cannot copy file! Press key to exit.");
fclose(fs);
getch();
exit(0);
}
while(1)
{
ch=getc(fs);
if(ch==EOF)
{
break;
}
else
putc(ch,ft);
}
printf("File copied successfully!");
fclose(fs);
fclose(ft);
}
Input/Output

E:\BACKUP\TURBOC3>copy hai.txt hello.txt


1 file(s) copied.

E:\BACKUP\TURBOC3>type hello.txt
Welcome to all

E:\BACKUP\TURBOC3>edit hello.txt

E:\BACKUP\TURBOC3>exit
/*To implement push and pop operations on stack*/

#include<stdio.h>
#define MAX 5
#include<conio.h>
void push();
void pop();
void display();
int top=-1;
int stack_arr[MAX];
void main()
{
int choice;
while(1)
{
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}
}
}
void push()
{
int pushed_item;
if(top==(MAX-1))
printf("Stack Overflow\n");
else
{
printf("Enter the item to be pushed in stack:");
scanf("%d",&pushed_item);
top=top+1;
stack_arr[top]=pushed_item;
}
}
void pop()
{
if(top==-1)
printf("Stack Underflow\n");
else
{
printf("Popped element is:%d\n",stack_arr[top]);
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
printf("Stack is empty\n");
else
{
printf("Stack elements:\n");
for(i=top;i>=0;i--)
printf("%d\n",stack_arr[i]);
}
}
Input/Output

1.Push
2.Pop
3.Display
4.Quit
Enter your choice: 1
Enter the item to be pushed in stack:12
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:1
Enter the item to be pushed in stack:23
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:3
Stack elements:
23
12
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:2
Popped element is:23
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:3
Stack elements:12
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:4
/*Evaluate the given mathematical expresion using stack*/

#include<stdio.h>
#include<stdlib.h>
struct list
{
int num;
struct list *next;
};
struct list *push(int i,struct list *node)
{
struct list *new1;
new1=(struct list*)malloc(sizeof(struct list));
new1->num=i;
new1->next=node;
node=new1;
return node;
}
struct list *pop(struct list *node)
{
struct list *p;
p=node->next;
free(node);
node=p;
return node;
}
void main()
{
struct list *start=NULL;
char c;
int x,y,result;
clrscr();
printf("\nEnter the expression:");
while((c=getchar())!='\n')
{
if(isdigit(c)!=0)
start=push(c-48,start);
else
{
y=start->num;
start=pop(start);
x=start->num;
start=pop(start);
switch(c)
{
case'+':
{
result=(x+y);
start=push(result,start);
break;
}
case'-':
{
result=(x-y);
start=push(result,start);
break;
}
case'*':
{
result=(x*y);
start=push(result,start);
break;
}
case'/':
{
result=(x/y);
start=push(result,start);
break;
}
}
}}
printf("Result of the expression is:%d",start->num);
getch();
}
Input/Output

Enter the expression:888-88*


Result of the expression is:64
/*Insert and delete operations on queue using array*/

#include<stdio.h>
#include<malloc.h>
struct node
{
int info;
struct node *next;
}
*front, *rear;
void enqueue(int elt);
int dequeue();
void display();
void main()
{
int ch,elt;
rear=NULL;
front=NULL;
while(1)
{
printf("\nMENU");
printf("\nEnter:\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the element value\n");
scanf("%d",&elt);
enqueue(elt);
break;
case 2:
elt=dequeue();
printf("The deleted element=%d\n",elt);
break;
case 3:
display();
break;
default:
printf("Exit");
getch();
exit(0);
break;
}
}
}
void enqueue(int elt)
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
p->info=elt;
p->next=NULL;
if(rear==NULL||front==NULL)
front=p;
else
rear->next=p;
rear=p;
}
int dequeue()
{
struct node *p;
int elt;
if(front==NULL||rear==NULL)
{
printf("\nUnder Flow");
getch();
exit(0);
}
else
{
p=front;
elt=p->info;
front=front->next;
free(p);
}
return(elt);
}
void display()
{
struct node *t;
t=front;
while(front==NULL||rear==NULL)
{
printf("\nQueue is empty");
getch();
exit(0);
}
while(t!=NULL)
{
printf("->%d",t->info);
t=t->next;
}
}
Input/Output

MENU
Enter:
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the element value
98
MENU
Enter:
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the element value
87
MENU
Enter:
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:3
->98->87
MENU
Enter:
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:2
The deleted element=98
MENU
Enter:
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:3
->87
MENU
Enter:
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:4
Exit
/*Linked list implementation of Queue operations*/

#include<stdio.h>
#include<stdlib.h>
struct link
{
char info[20];
struct link *next;
};
struct link *p,*node,*new1,*start=NULL;
struct link *insert(struct link *nn)
{
node=nn;
p=NULL;
new1=(struct link*)malloc(sizeof(struct link));
while(node)
{
node=node->next;
p=p->next;
}
p->next=new1;
new1->next=node;
fflush(stdin);
printf("\nInput the node value:");
gets(new1->info);
node=start->next;
return node;
}
void display(struct link *node)
{
while(node)
{
printf("->%s",node->info);
node=node->next;
}
}
struct link *del(struct link *nn)
{
node=nn;
if(node==NULL)
printf("\nDeletion not possible");
else
{
p=node->next;
free(node);
node=p;
}
return node;
}
void main()
{
struct link *start=NULL;
int k=0;
char choice;
clrscr();
do
{
printf("\n\nInsert->i\nDelete->d\nQuit->q:");
printf("\nInput the choice:");
do
{
choice=getchar();
choice=tolower(choice);
}
while(strchr("idq",choice)==NULL);
printf("Your choice is:%c",choice);
switch(choice)
{
case'i':
{
start=insert(start);
printf("\nQueue after inserting:");
display(start);
break;
}
case'd':
{
start=del(start);
printf("\nQueue content after deletion is as follows:");
display(start);
break;
}
case'q':
{
k=1;
}
}
}
while(!k);
getch();
}
Input/Output

Insert->i
Delete->d
Quit->q:
Input the choice:i
Your choice is:i
Input the node value:10

Queue after inserting:->10

Insert->i
Delete->d
Quit->q:
Input the choice:i
Your choice is:i
Input the node value:20

Queue after inserting:->10->20

Insert->i
Delete->d
Quit->q:
Input the choice:d
Your choice is:d
Queue content after deletion is as follows:->20

Insert->i
Delete->d
Quit->q:
Input the choice:q
Your choice is:q
/*Ascending order with naming of variable and the value
before and after sorting*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,t;
clrscr();
printf("Enter the 10 elements\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("Before sorting\n");
for(i=0;i<10;i++)
{
printf("a[%d]=%d\n",i,a[i]);
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("After sorting\n");
for(i=0;i<10;i++)
{
printf("a[%d]=%d\n",i,a[i]);
}
getch();
}
Input/Output

Enter the 10 elements


99
77
55
33
11
88
66
44
22
10
Before sorting
a[0]=99
a[1]=77
a[2]=55
a[3]=33
a[4]=11
a[5]=88
a[6]=66
a[7]=44
a[8]=22
a[9]=10
After sorting
a[0]=10
a[1]=11
a[2]=22
a[3]=33
a[4]=44
a[5]=55
a[6]=66
a[7]=77
a[8]=88
a[9]=99
/*To sort a set of elements using selection sort*/

#include<stdio.h>
#include<conio.h>
void main()
{
int array[100],n,c,d,position,swap;
clrscr();
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n",n);
for(c=0;c<n;c++)
scanf("%d",&array[c]);
for(c=0;c<(n-1);c++)
{
position=c;
for(d=c+1;d<n;d++)
{
if(array[position]>array[d])
position=d;
}
if(position!=c)
{
swap=array[c];
array[c]=array[position];
array[position]=swap;
}
}
printf("Sorted list in ascending order:\n");
for(c=0;c<n;c++)
printf("%d\n",array[c]);
getch();
}
Input/Output

Enter number of elements


5
Enter 5 integers
69
58
47
36
25
Sorted list in ascending order:
25
36
47
58
69
*To sort a set of elements using Insertion sort*/

#include<stdio.h>
void main()
{
int a[20],n,temp,i,j;
clrscr();
printf("Enter the number of terms");
scanf("%d",&n);
printf("Enter the elements of the array");
for(i=0;i<n;i++)
{
gotoxy(5,3+i);
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(temp<a[j]&&j>=0)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("The ascending order list is \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
Input/Output

Enter the number of terms5


Enter the elements of the array
96
85
74
63
52
The ascending order list is
52
63
74
85
96
/*Menu driven program to find an element using Linear
and Binary search methods*/

#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20],a,n,i,item,start,end,middle,f=0;
clrscr();
printf("1.Linear\n");
printf("2.Binary");
scanf("%d",&a);
switch(a)
{
case 1:
printf("How many elements you want to enter in the array:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter element %d:",i);
scanf("%d",&arr[i]);
}
printf("Enter the element to be searched:");
scanf("%d",&item);
for(i=1;i<=n;i++)
{
if(item==arr[i])
{
f=1;
}
}
if(f==1)
printf("%d element found\n",item);
else
printf("not found");
break;
case 2:
{
printf("How many elements you want to enter in the array");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter elements%d:",i);
scanf("%d",&arr[i]);
}
printf("Enter the element to be searched:");
scanf("%d",&item);
start=1;
end=n;
middle=(start+end)/2;
while(item!=arr[middle]&&start<=end)
{
if(item>arr[middle])
start=middle+1;
else
end=middle-1;
middle=(start+end)/2;
}
if(item==arr[middle])
printf("%dfound at position %d\n",item,middle);
if(start>end)
printf("%d not found in array\n",item);
break;
default:
printf("Invalid");
}
}
getch();
}
Input/Output

1.Linear
2.Binary
1
How many elements you want to enter in the array:5
Enter element 1:14
Enter element 2:25
Enter element 3:36
Enter element 4:47
Enter element 5:58
Enter the element to be searched:47
47 element found

1.Linear
2.Binary
2
How many elements you want to enter in the array5
Enter elements1:69
Enter elements2:58
Enter elements3:47
Enter elements4:63
Enter elements5:52
Enter the element to be searched:63
63found at position 4

You might also like