You are on page 1of 32

1

Programe to add 3X3 matrix


#include<stdio.h>

#include<conio.h>

main()

int a[3][3], b[3][3], c[3][3], i,j,k,s;

printf("\n\n\t\t =====PROGRAM TO ADD 3X3 MATRIX=====\n\n");

printf("\n\n Enter the values of first 3 X 3 matrix : " );

for (i=0; i<=2; i++)

for (j=0; j<=2; j++)

scanf("%d", &a[i][j]);

printf("\n\n Enter the values of second 3 x 3 matrix : " );

for (i=0; i<=2; i++)

for (j=0; j<=2; j++)

scanf("%d", &b[i][j]);

getch();

printf("\n\n");

printf("\n First matrix entered by user is :\n\n");

for (i=0; i<=2; i++)

for (j=0; j<=2; j++)

printf(" %d",a[i][j]);
2

printf("\n\n");

printf("Second matrix entered by user is :\n\n");

for (i=0; i<=2; i++)

for (j=0; j<=2; j++)

printf(" %d",b[i][j]);

printf("\n\n");

for (i=0; i<=2; i++)

for (j=0; j<=2; j++)

c[i][j]=a[i][j]+b[i][j];

printf("Resulted matrix is :\n\n ");

for (i=0; i<=2; i++)

for (j=0; j<=2; j++)

printf(" %d", c[i][j]);

getch();

}
3

Output of the program :-


4

Programé to subtract 3X3 matrix


#include<stdio.h>

#include<conio.h>

main()

int a[3][3], b[3][3], c[3][3], i,j,k,s;

printf("\n\n\t\t =====PROGRAM TO ADD 3X3 MATRIX=====\n\n");

printf("\n\n Enter the values of first 3 X 3 matrix : " );

for (i=0; i<=2; i++)

for (j=0; j<=2; j++)

scanf("%d", &a[i][j]);

printf("\n\n Enter the values of second 3 x 3 matrix : " );

for (i=0; i<=2; i++)

for (j=0; j<=2; j++)

scanf("%d", &b[i][j]);

getch();

printf("\n\n");

printf("\n First matrix entered by user is :\n\n");

for (i=0; i<=2; i++)

for (j=0; j<=2; j++)

{
printf(" %d",a[i][j]);

printf("\n\n");

printf("Second matrix entered by user is :\n\n");

for (i=0; i<=2; i++)

for (j=0; j<=2; j++)

printf(" %d",b[i][j]);

printf("\n\n");

for (i=0; i<=2; i++)

for (j=0; j<=2; j++)

c[i][j]=a[i][j]-b[i][j];

printf("Resulted matrix is :\n\n ");

for (i=0; i<=2; i++)

for (j=0; j<=2; j++)

printf(" %d", c[i][j]);

getch();
}

Your output is:-


7

Linear search

#include<stdio.h>

#include<conio.h>

void main()

int a[10],i,n,data,j;

clrscr();

printf("\n enter no of ele's");

scanf("%d",&n);

printf("\n enter %d,ele's", n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("\n enter the to be searched");

scanf("%d",&data);

j=linear(a,n,data);

if(j==0)

printf("\n data not found \n");

else

printf("\n %d is found at %d",data,j);

getch();

linear(int a[],int n,int data)

{
8

int i;

for(i=0;i<n;i++)

if(data==a[i])

return i+1;

return NULL;

You output is:-


9

BUBBLE SORT-

#include<stdio.h>

#include<conio.h>

void main()

int a[40],i,n;

clrscr();

printf("\n enter no's of ele of sort");

scanf("%d",&n);

printf("enter %d ele",n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);

bubble(a,n);

printf("sorted list is:");

for(i=0;i<n;i++)

printf("\n %d \n",a[i]);

getch();

bubble(int a[],int n)

int i,j,temp;

for(i=0;i<n;i++)

for(j=0;j<n-1;j++)
{

10

if(a[i]>a[j+1]);

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}}}

return 0;

YOUR OUTPUT IS
11

SELECTION SORT

#include<stdio.h>

#include<conio.h>

void main()

int a[40],i,n;

clrscr();

printf("\n no of ele's to sort");

scanf("%d",&n);

printf("enter %d ele ",n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);

selection(a,n);

printf("sorted list is ");

for(i=0;i<n;i++)

printf("\n %d \n",a[i]);

getch();

selection(int a[],int n)

int i,j,temp;

for(i=0;i<n-1;i++)

for(j=i+1;j<n;j++)
{

12

if(a[i]>a[j])

temp=a[i];

a[i]=a[j];

a[j]=temp;

}}}

return 0;

Yor output is:-


13

LINKED LIST HAVING STRING TYPE

#include<stdio.h>

#include<conio.h>

struct list

char data[35];

struct list *next;

};

typedef struct list linked;

void main()

linked *start,node1,node2,node3,node4;

int i;

clrscr();

start=(struct list*)malloc(sizeof(linked));

start=&node1;

gets(node1.data);

node1.next=&node2;

gets(node2.data);

node2.next=&node3;

gets(node3.data);

node3.next=&node4;

gets(node4.data);

node4.next='\0';
printf("\n output is\n");

14

for(i=0;i<4;i++)

printf("\n %s",start->data);

start=start->next;

printf("\npress a key");

getch();

YOUR OUTPUT IS
15

Structure of link list

#include<stdio.h>

#include<conio.h>

struct link_list

int data;

struct link_list*next;

};

typedef struct link_list node;

node*start=NULL;

int num;

void create(node*);

void display(node*);

void main()

start=(node*)malloc(sizeof (node));

create (start);

printf("/n created list is as followes:\n");

display (start);

getch();

void create(node*ptr)

{
char ch;

16

num=0;

printf("\n enter e for exit and any other key to continue:");

ch=getchar();

if (ch== 'e')

free (ptr);

exit (0);

do

printf("\n input the values of the node :%d:",num+1);

scanf("%d", & ptr-> data);

fflush(stdin);

printf("\n want to continue (y/n):");

ch=getchar();

if (ch=='y')

ptr-> next=(node*)malloc(sizeof (node));

ptr=ptr->next;

num++;

while (ch=='y');

ptr->next=NULL;
printf("\n total nodes =%d",num);

17

void display (node*ptr)

while (ptr!=NULL)

printf("%d",ptr->data);

ptr=ptr-> next;

Your output is:-


18

Linked of nodes

#include<stdio.h>

#include<conio.h>

struct list

char data;

struct list*next;

};

typedef struct list linked;

void main()

linked*start,node1,node2,node3,node4;

int i;

start=(struct list*)malloc(size of (linked));

start=& node1;

node1.data='A';

node1.next=& node2;

node2.data='B';

node2.next=& node3;

node3.data='C';

node3.next=& node4;

node4.data='D';

for(i=0;i<4;i++)

{
19

printf("\n%c",start->data);

start=start->next;

printf("\n press a key");

getch();

Your output is:-


20

Traverse array

#include<stdio.h>

#include<conio.h>

void traverse_arr(int linear_arr[],int l_b,int u_b)

int counter;

for(counter=l_b;counter<=u_b;counter++)

printf("\n element at position %d is %d",counter,linear_arr[counter]);

void input (int array[],int l_b,int u_b)

int counter;

for (counter=l_b;counter<=u_b;counter++)

printf("\n input value for the %d:",counter);

scanf("%d",& array[counter]);

void main()

int a[100];

int lb,ub;
21

printf("\n enter lower limit of the array:");

scanf("%d",&lb);

printf("\n enter upper limit");

scanf("%d",&ub);

input(a,lb,ub);

traverse_arr(a,lb,ub);

getch();

Your output is:-


22

Insert element in array

#include<stdio.h>

#include<conio.h>

int insert_arr(char array[],int num,int pos,char ele)

int temp=num;

while(temp>=pos)

array[temp+1]=array[temp];

temp--;

array[pos]=ele;

num=num+1;

return(num);

void input(char array[],int num)

int i;

for(i=0;i<num;i++)

fflush(stdin);

printf("\n input val for %d:",i);

scanf("%c",&array[i]);

}
}

23

void display(char array[],int num)

int i;

for(i=0;i<num;i++)

printf("\n value at position %d:%c",i,array[i]);

void main()

int num;

char array[100];

int pos;

char ele;

clrscr();

fflush(stdin);

printf("input the num of ele's into the list:");

scanf("%d",& num);

fflush(stdin);

input (array,num);

printf("\n entered list is as follows");

fflush(stdin);

display(array,num);

fflush(stdin);
printf("\n input the pos where u want to enter data");

24

scanf("%d",&pos);

fflush(stdin);

printf("input the value for the pos");

scanf("%c",& ele);

num=insert_arr(array,num,pos,ele);

fflush(stdin);

display(array,num);

getch();

Your output is:-


25

Delete element of array

#include<stdio.h>

#include<conio.h>

void delete(int array[],int len,int num);

void main()

int num,m,len,array[100];

clrscr();

printf("enter num of digits");

scanf("%d",& len);

printf("\n enter digits in the array");

for(m=0;m<len;m++)

scanf("%d",&array[m]);

printf("enter integer to be deleted");

scanf("%d",&num);

delete(array,len,num);

getch();

void delete(int array[],int len,int num)

int m,p=0, n;

for(m=0;m<len;m++)
if(num==array[m])

26

p=1;

break;

if(p==0)

printf("%d does not exist in the array\n",num);

for(n=m;n<len;n++)

array[n]=array[n+1];

len--;

printf("revised array");

for(m=0;m<len;m++)

printf("\n%d",array[m]);

Your output is:-


27

Binary search

#include<stdio.h>

#include<conio.h>

void main()

int a[10],i,n,data,j;

clrscr();

printf("\n enter no. of ele's:");

scanf("%d",&n);

printf("\n enter %d ele's:",n);

for(i=1;i<=n;i++)

scanf("%d",&a[i]);

printf("\n enter the data to be searched");

scanf("%d",& data);

j=binary(a,1,n,data);

if(j==0)

printf("\n data not found\n");

else

printf("\n %d is found at pos %d",data,j);

getch();

binary(int a[],int low,int high,int data)

int mid;
mid=(low+high)/2;

28

while(low<mid && mid<high)

if(a[mid]==data)

return mid;

if(a[mid]>data)

high=mid;

else

low=mid;

mid=(low+high)/2;

if(a[low]==data)

return low;

if(a[high]==data)

return high;

return NULL;

Your output is:-


29

Sorted array elements

#include<stdio.h>

#include<conio.h>

void main()

int a[5],b[5],c[10],i,j,k=0,temp;

clrscr();

puts("enter ele's of 1st array:\n");

for(i=0;i<5;i++)

scanf("%d",&a[i]);

puts("enter ele' of 2nd array:\n");

for(i=0;i<5;i++)

scanf("%d",&b[i]);

for(i=0;i<5;i++)

c[k++]=a[i];

for(i=0;i<5;i++)

c[k++]=b[i];
}

30

for(i=0;i<10;i++)

for(j=i;j<10;j++)

if(c[i]>c[j+1])

temp=c[j+1];

c[j+1]=c[i];

c[i]=temp;

printf("contents of sorted array:\n");

for(i=0;i<10;i++)

printf("%d\n",c[i]);

getch();

}
31

Your output is:-

You might also like