You are on page 1of 20

SHAKTHIDHAR/ CP LAB/ CSE I

PROGRAM
/*program to find smallest and largest of n numbers*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, sm, a[50], la, l;
clrscr();
printf("\n SMALLEST AND LARGEST NUMBER \n\n");
printf("Enter the limit : ");
scanf("%d", &l);
printf("Enter the numbers \n");
for(i=0;i<l;i++)
{
scanf("%d", &a[i]);
}
sm = a[0];
la = a[0];
for(i=1;i<l;i++)
{
if(a[i]>la)
{
la = a[i];
}
if(a[i]<sm)
{
sm = a[i];
}
}
printf("\n Largest number : ");
printf("%d",la);
printf("\n Smallest number : ");
printf("%d", sm);
getch();
}

SHAKTHIDHAR/ CP LAB/ CSE I


OUTPUT
SMALLEST AND LARGEST NUMBER
Enter the limit : 5
Enter the numbers
4 6 9 3 34
Largest number : 34
Smallest number : 3

SHAKTHIDHAR/ CP LAB/ CSE I


PROGRAM
/*implementation of addition and subrataction of matrices*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10], b[10][10], c[10][10], i, j, n, m;
clrscr();
printf("ADDITION AND SUBTRACTION OF MATRICES\n\n");
printf("Enter the rows and columns of the matrix : ");
scanf("%d %d", &m, &n);
printf("\n Enter the elements of matrix A\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the elements of matrix B\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n The elements of the matrix A are \n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("\t%d",a[i][j]);
}
}
printf("\n The elements of the matrix B are \n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<m;j++)
{

SHAKTHIDHAR/ CP LAB/ CSE I


printf("\t%d",b[i][j]);
}
}
printf("\n Addition of two matrices is \n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
c[i][j] = a[i][j] + b[i][j];
printf("\t%d",c[i][j]);
}
}
printf("\n Subtraction of two matrices is \n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
c[i][j] = a[i][j] - b[i][j];
printf("\t%d",c[i][j]);
}
}
getch();
}

SHAKTHIDHAR/ CP LAB/ CSE I


OUTPUT
ADDITION AND SUBTRACTION OF MATRICES
Enter the rows and columns of the matrix : 3 3
Enter the elements of matrix A
1 2 3 4 5 6 7 8 89
Enter the elements of matrix B
987654321
The elements of the matrix A are
1

89

The elements of the matrix B are


9

Addition of two matrices is


10

10

10

10

10

10

10

10

90

Subtraction of two matrices is


-8

-6

-4

-2

88

SHAKTHIDHAR/ CP LAB/ CSE I


PROGRAM
/*implementation of multiplication of matrices */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10], b[10][10], c[10][10], i, j, n, m, k;
clrscr();
printf("MATRIX MULTIPLICATION\n\n");
printf("Enter the rows and columns of the matrix : ");
scanf("%d %d", &m, &n);
printf("\n Enter the elements of matrix A\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the elements of matrix B\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n The elements of the matrix A are \n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("\t%d",a[i][j]);

}
printf("\n The elements of the matrix B are \n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<m;j++)

SHAKTHIDHAR/ CP LAB/ CSE I


{
printf("\t%d",b[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
c[i][j] = 0;
for(k=0;k<m;k++)
{
c[i][j] = c[i][j] + (a[i][k] * b[k][j]);
}
}
}
printf("\n Resultant matrix is \n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("\t%d",c[i][j]);
}
}
getch();
}

SHAKTHIDHAR/ CP LAB/ CSE I

OUTPUT
MATRIX MULTIPLICATION
Enter the rows and columns of the matrix : 3 3
Enter the elements of matrix A
123456789
Enter the elements of matrix B 1 2 9 8 7 6 5 4 6
The elements of the matrix A are
1

The elements of the matrix B are

Resultant matrix is
32

28

39

74

67

102

116

106

165

SHAKTHIDHAR/ CP LAB/ CSE I


PROGRAM
//number of occurences in an array
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, a[50], l, c = 0;
clrscr();
printf("\n NUMBER OF OCCURENCES \n\n");
printf("Enter the limit : ");
scanf("%d", &l);
printf("Enter the numbers \n");
for(i=0;i<l;i++)
{
scanf("%d", &a[i]);
}
printf("\n Enter the element to be found : ");
scanf("%d",&n);
for(i=0;i<l;i++)
{
if(a[i]==n)
{
c = c + 1;
}
}
if(c>0)
{
printf("\n The number is found in %d times",c);
}
else
{
printf("\n The number is not present in this array\n ");
}
getch();
}

SHAKTHIDHAR/ CP LAB/ CSE I

OUTPUT
NUMBER OF OCCURENCES
Enter the limit : 5
Enter the numbers
34453

Enter the element to be found : 4

The number is found in 2 times

SHAKTHIDHAR/ CP LAB/ CSE I


PROGRAM

//finding the element and sorting an array


#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, a[50], l, c;
clrscr();
printf("\n FIND THE ELEMENT IN AN ARRAY \n\n");
printf("Enter the limit : ");
scanf("%d", &l);
printf("Enter the numbers \n");
for(i=0;i<l;i++)
{
scanf("%d", &a[i]);
}
printf("\n Enter the element to be found : ");
scanf("%d",&n);
for(i=0;i<l;i++)
{
if(a[i]==n)
{
c=1;
printf("The number is found in %d position\n",i+1);
}
}
if(c!=1)
printf("\n The number is not present in this array\n ");
getch();
}

SHAKTHIDHAR/ CP LAB/ CSE I


OUTPUT
FIND THE ELEMENT IN AN ARRAY

Enter the limit : 5


Enter the numbers
12345

Enter the element to be found : 2


The number is found in 2 position

SHAKTHIDHAR/ CP LAB/ CSE I


PROGRAM
//sorting an array in ascending and descending order
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, a[50], temp, l;
clrscr();
printf("\n ASCENDING AND DESCENDING ORDER \n\n");
printf("Enter the limit : ");
scanf("%d", &l);
printf("Enter the numbers \n");
for(i=0;i<l;i++)
{
scanf("%d", &a[i]);
}
for(i=0;i<l;i++)
{
for(j=i+1;j<l;j++)
{
if(a[i]>a[j])
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
printf("\n Ascending Order \n");
for(i=0;i<l;i++)
{
printf("%d\t",a[i]);
}
printf("\n Descending Order \n");
for(i=l-1;i>=0;i--)
{
printf("%d\t", a[i]);
}
getch();
}

SHAKTHIDHAR/ CP LAB/ CSE I


OUTPUT
ASCENDING AND DESCENDING ORDER

Enter the limit : 5


Enter the numbers
75621

Ascending Order
1

Descending Order
7

SHAKTHIDHAR/ CP LAB/ CSE I

SHAKTHIDHAR/ CP LAB/ CSE I

SHAKTHIDHAR/ CP LAB/ CSE I

SHAKTHIDHAR/ CP LAB/ CSE I

SHAKTHIDHAR/ CP LAB/ CSE I

SHAKTHIDHAR/ CP LAB/ CSE I

You might also like