Professional Documents
Culture Documents
UNIT-II
1)C program to print the address of each element in an array.
Program:
#include<stdio.h>
void main()
{
int a[5]={1,2,3,4,5},i;
for (i=0;i<5;i++)
printf("the address of element of index %d is %d\n",i,&a[i]);
}
output:
the address of element of index 0 is 606155904
the address of element of index 1 is 606155908
the address of element of index 2 is 606155912
the address of element of index 3 is 606155916
the address of element of index 4 is 606155920
output:
enter no of elements in array : 5
enter a number:4
enter a number:2
enter a number:5
enter a number:2
enter a number:4
4,2,5,2,4,
output:
enter no of elements in array : 5
enter a number:3
enter a number:6
enter a number:1
enter a number:3
enter a number:4
enter index to delete:3
3,6,1,4,
for (i=3;i>=b;i--)
a[i+1]=a[i];
a[b]=n;
for (i=0;i<5;i++)
printf("%d,",a[i]);
}
output:
enter no of elements in array : 5
enter a number:3
enter a number:5
enter a number:2
enter a number:3
enter a number:4
enter a number to insert:5
enter the index to input:1
3,5,5,2,3,
UNIT-III
1)C program to add two matrices :
program:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i,j,A[20][20],B[20][20],C[20][20],m,n,o,p;
printf("enter first matrix rows and columns:");
scanf("%d %d",&m,&n);
printf("enter second matrix rows and columns:");
scanf("%d %d",&o,&p);
if ((m!=o)||(n!=p))
{
printf("matrix addition is not possible.\n");
exit(0);
}
printf("enter first matrix elements :\n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf("\ta[%d][%d] : ",i,j);
scanf("%d",&A[i][j]);
}
}
printf("enter second array elements:\n");
for (i=0;i<o;i++)
{
for (j=0;j<p;j++)
{
printf("\tb[%d][%d] : ",i,j);
scanf("%d",&B[i][j]);
}
}
printf("\nadded matrix elements:\n");
printf("C=(A+B)=\n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
C[i][j]=A[i][j]+B[i][j];
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",C[i][j]);
printf("\n\n");
}
}
output:
enter first matrix rows and columns:2
2
enter second matrix rows and columns:2
2
enter first matrix elements :
a[0][0] : 4
a[0][1] : 6
a[1][0] : 7
a[1][1] : 4
enter second array elements:
b[0][0] : 6
b[0][1] : 3
b[1][0] : 8
b[1][1] : 7
added matrix elements:
C=(A+B)=
10
9
15
11
2.C program to substract a matrix from another matrix.
Program:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i,j,A[20][20],B[20][20],C[20][20],m,n,o,p;
printf("enter first matrix rows and columns:");
scanf("%d %d",&m,&n);
printf("enter second matrix rows and columns:");
scanf("%d %d",&o,&p);
if ((m!=o)||(n!=p))
{
printf("matrix addition is not possible.\n");
exit(0);
}
printf("enter first matrix elements :\n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf("\ta[%d][%d] : ",i,j);
scanf("%d",&A[i][j]);
}
}
printf("enter second array elements:\n");
for (i=0;i<o;i++)
{
for (j=0;j<p;j++)
{
printf("\tb[%d][%d] : ",i,j);scanf("%d",&B[i][j]);
}
}
printf("\nsubtracted matrix elements:\n");
printf("C=(A-B)=\n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
C[i][j]=A[i][j]-B[i][j];
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",C[i][j]);
printf("\n\n");
}
}
output:
enter first matrix rows and columns:2
2
enter second matrix rows and columns:2
2
enter first matrix elements :
a[0][0] : 4
a[0][1] : 6
a[1][0] : 8
a[1][1] : 5
enter second array elements:
b[0][0] : 1
b[0][1] : 4
b[1][0] : 2
b[1][1] : 3
subtracted matrix elements:
C=(A-B)=
3
2
6
2
3)c program to a multiply a matrix with another matrix.
Program:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i,j,k,A[20][20],B[20][20],C[20][20],m,n,o,p;
printf("enter first matrix rows and columns:");
scanf("%d %d",&m,&n);
printf("enter second matrix rows and columns:");
scanf("%d %d",&o,&p);
if (n!=o)
{
printf("matrix product is not possible.\n");
exit(0);
}
printf("enter first matrix elements :\n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf("\tA[%d][%d] : ",i,j);
scanf("%d",&A[i][j]);
}
}
printf("enter second array elements:\n");
for (i=0;i<o;i++)
{
for (j=0;j<p;j++)
{
printf("\tB[%d][%d] : ",i,j);
scanf("%d",&B[i][j]);
}
}
printf("\nmultiplied matrix elements:\n");
printf("C=(AB)=\n");
for (i=0;i<m;i++)
{
for (j=0;j<p;j++)
{
C[i][j]=0;
for (k=0;k<n;k++)
C[i][j]+=A[i][k]*B[k][j];
}
}
for(i=0;i<p;i++)
{
for(j=0;j<p;j++)
printf("\t%d",C[i][j]);
printf("\n\n");
}
}
output:
enter first matrix rows and columns:2
3
enter second matrix rows and columns:3
2
enter first matrix elements :
A[0][0] : 5
A[0][1] : 3
A[0][2] : 7
A[1][0] : 1
A[1][1] : 4
A[1][2] : 8
enter second array elements:
B[0][0] : 4
B[0][1] : 6
B[1][0] : 2
B[1][1] : 7
B[2][0] : 5
B[2][1] : 3
52 58
4)C program to print transpose of given matrix.
Program:
#include<stdio.h>
void main()
{
int i,j,a[20][20],b[20][20],r,c;
printf("enter no of rows :");
scanf("%d",&r);
printf("enter no of columns:");
scanf("%d",&c);
for (i=0;i<c;i++)
{
for (j=0;j<c;j++)
{
printf("enter a value:");
scanf("%d",&a[i][j]);
}
}
printf("a transpose=\n");
for (j=0;j<r;j++)
{
for (i=0;i<c;i++)
{
b[j][i]=a[i][j];
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf("\t%d",b[i][j]);
printf("\n");
}
}
output:
enter no of rows :2
enter no of columns:3
enter a value:2
enter a value:3
enter a value:45
enter a value:2
enter a value:4
enter a value:5
a transpose=
2 2
3 4
45 5
5)bubble sort:
program:
#include<stdio.h>
void main()
{
int a[50],i,j,temp,v=1,n;
printf("enter no of elements need to sort:");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("enter a number:");
scanf("%d",&a[i]);
}
for (i=0;(i<n-1);i++)
{
v=0;
for (j=0;j<n-1-i;j++)
{
if (a[j]>a[j+1])
{
v=1;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
if (v==0)
break;
}
printf("elements after getting sorted:");
for (i=0;i<n;i++)
printf("%d,",a[i]);
printf("\n");
}
output:
enter no of elements need to sort:5
enter a number:3
enter a number:6
enter a number:3
enter a number:8
enter a number:1
elements after getting sorted:1,3,3,6,8,
6)selection sort:
program:
#include<stdio.h>
void main()
{
int a[50],i,j,temp,n,min;
printf("enter no of elements in the list:");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("a[%d] = ",i);
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
min=i;
for (j=i+1;j<n;j++)
{
if (a[j]<a[min])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
printf("\nsorted list=");
for (i=0;i<n;i++)
printf("\t%d",a[i]);
printf("\n");
}
output:
enter no of elements in the list:5
a[0] = 4
a[1] = 6
a[2] = 2
a[3] = 8
a[4] = 4
sorted list= 2 4 4 6 8
7)insertion sort
program:
#include<stdio.h>
void main()
{
int a[50],i,j,temp,n;
printf("enter no of elements needed to sort:");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("a[%d] = ",i);
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(temp<a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
printf("\nsorted list=");
for (i=0;i<n;i++)
printf("\t%d",a[i]);
printf("\n");
}
output:
enter no of elements needed to sort:5
a[0] = 7
a[1] = 3
a[2] = 5
a[3] = 1
a[4] = 8
sorted list= 1 3 5 7 8
8)binary search:
program:
#include<stdio.h>
void main()
{
int a[50],i,j,temp,n,key,low,k=0,high,mid;
printf("enter no of elements in the list:");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("a[%d] = ",i);
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i-1;temp<a[j];j--)
a[j+1]=a[j];
a[j+1]=temp;
}
printf("enter element needed to search:");
scanf("%d",&key);
low=0;
high=n-1;
while (low<=high)
{
mid=(low+high)/2;
if (a[mid]==key)
{
printf("enter value is founded at the index of %d in sorted list \
n",mid);
k=1;
break;
}
else if (key<a[mid])
high=mid-1;
else
low=mid+1;
}
if (k==0)
printf("entered element is not found.\n");
}
output:
enter no of elements in the list:5
a[0] = 3
a[1] = 7
a[2] = 2
a[3] = 5
a[4] = 3
enter element needed to search:7
enter value is founded at the index of 4 in sorted list
STRING FUNCTIONS
1)strlen()
program:
#include<stdio.h>
#include<string.h>
void main()
{
int i;
char str1[1000];
printf("enter a string:");
scanf("%[^\n]",str1);
printf(“string length is %d.\n”,strlen(str1));
}
output:
enter a string:google
string length is 6.
2)strcpy()
program:
#include<stdio.h>
#include<string.h>
void main()
{
int i;
char str1[1000],str2[100],ch;
printf("enter a string:");
scanf("%[^\n]",str1);
printf("strings before copy are str1=%s and str2=%s",str1,str2);
strcpy(str2,str1);
printf("\nstrings after copy are str1=%s and str2=%s",str1,str2);
printf("\n");
}
output:
enter a string:Keep calm
strings before copy are str1=Keep calm and str2=
strings after copy are str1=Keep calm and str2=Keep calm
3)strcat()
program:
#include<stdio.h>
#include<string.h>
void main()
{
int i;
char str1[1000],str2[100]="hi",ch;
printf("enter a string:");
scanf("%[^\n]",str1);
printf("strings before catenation are str1=%s and str2=%s",str1,str2);
strcat(str2,str1);
printf("\nstrings after catenation are str1=%s and str2=%s",str1,str2);
printf("\n");
}
output:
enter a string:go
strings before catenation are str1=go and str2=hi
strings after catenation are str1=go and str2=higo
4)strcmp()
program:
#include<stdio.h>
#include<string.h>
void main()
{
char a[100],b[100];
printf("enter two strings:");
gets(a);
gets(b);
if(strcmp(a,b)==0)
printf("both strings are identical");
else if(strcmp(a,b)>0)
printf("string %s comes first in lexilogical order",b);
else
printf("string %s comes first in lexicogical order",a);
printf("\n");
}
output:
enter two strings:hi
hello
string hello comes first in lexicographical order
UNIT-IV
1)C program to check whether given number is prime or not using user defined function.
Program:
#include<stdio.h>
int prime(int);
void main()
{
int a;
printf("enter a number:");
scanf("%d",&a);
if (prime(a)!=0)
printf("%d is a prime number\n",a);
else
printf("%d is not a prime number\n",a);
}
int prime(int a)
{
int i,fact=0;
for (i=1;i<=a;i++)
{
if (a%i==0)
{
fact=fact+1;
}
}
if(fact==2)
return 1;
return 0;
}
output:
enter a number:5
5 is a prime number
2)C program to swap two integers using call by value and call by reference.
i.call by value:
Program:
#include<stdio.h>
void swap();
int a=2,b=3;
void main()
{
printf("before swap :\na = %d \nb = %d\n.",a,b);
swap();
printf("after swap :\na = %d \nb = %d\n.",a,b);
}
void swap()
{
int temp;
temp=a;
a=b;
b=temp;
}
output:
before swap :
a=2
b=3
.after swap :
a=3
b=2
ii.call by reference:
Program:
#include<stdio.h>
void swap(int *a,int *b);
void main()
{
int a,b;
printf("enter a value :");
scanf("%d",&a);
printf("enter b value:");
scanf("%d",&b);
printf("before swap :\na = %d \nb = %d.\n.",a,b);
swap(&a,&b);
printf("after swap:\na = %d \nb = %d.\n",a,b);
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
output:
enter a value:4
enter b value:6
before swap :
a=4
b = 6.
.after swap:
a=6
b = 4.
5)C program to sort elements in lexicographical order using inbuilt string functions.
Program:
#include<stdio.h>
#include<string.h>
void main()
{
char a[50][1000],temp[1000];
int i,j,n;
printf("enter no of strings needed to sort:");
scanf("%d",&n);
printf("enter strings:");
for (i=0;i<=n;i++)
gets(a[i]);
for(i=2;i<=n;i++)
{
strcpy(temp,a[i]);
j=i-1;
while(strcmp(a[j],temp)>0)
{
strcpy(a[j+1],a[j]);
j--;
}
strcpy(a[j+1],temp);
}
printf("the elements in lexicographical order are :");
for (i=1;i<=n;i++)
printf(" %s ,",a[i]);
printf("\n");
}
output:
enter no of strings needed to sort:6
enter strings:focus
on
what
you
left
with
the elements in lexicographical order are : focus , left , on , what , with , you ,