// Finding Largest Number and Smallest Number in a list of integers.
#include <stdio.h>
void main()
{
int i,n,largest = 0,smallest = 30000;
int a[100];
printf("\n Enter how many integers : ");
scanf("%d",&n);
for(i = 0 ; i < n ; i++)
{
printf("\n Enter the integer %d : ",i+1);
scanf("%d",&a[i]);
}
for(i = 0 ; i < n ; i++)
{
if(largest < a[i])
largest = a[i];
if(smallest > a[i])
smallest = a[i];
}
printf("\n Largest Number in the list is %d \n",largest);
printf("\n Smallest Number in the list is %d \n",smallest);
}
// Matrix Addition.
#include <stdio.h>
void main()
{
int i,j,m, n, p, q, a[100][100], b[100][100], c[100][100];
printf("Enter the number of rows and columns of matrix A :\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of matrix A\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);
printf("Enter the number of rows and columns of matrix B :\n");
scanf("%d%d", &p, &q);
printf("Enter the elements of matrix B : \n");
for (i = 0; i < p; i++)
for (j = 0 ; j < q; j++)
scanf("%d", &b[i][j]);
if(m != p || n != q)
printf("\n Matrix Addition is not Possible. \n");
else
{
for (i = 0; i < m; i++)
{
for (j = 0 ; j < n; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
printf("Resultant Matrix is :-\n");
for (i = 0; i < m; i++)
{
for (j = 0 ; j < n; j++)
{
printf("%d \t", c[i][j]);
}
printf("\n");
}
}
}
// Matrix Multiplication.
#include <stdio.h>
void main()
{
int i,j,k,m, n, p, q, a[100][100], b[100][100], c[100][100];
printf("Enter the number of rows and columns of matrix A :\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of matrix A\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);
printf("Enter the number of rows and columns of matrix B :\n");
scanf("%d%d", &p, &q);
printf("Enter the elements of matrix B : \n");
for (i = 0; i < p; i++)
for (j = 0 ; j < q; j++)
scanf("%d", &b[i][j]);
if(n != p)
printf("\n Matrix Addition is not Possible. \n");
else
{
for (i = 0; i < m; i++)
{
for (j = 0 ; j < q; j++)
{
for(k=0 ; k < p; k++)
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
printf("Resultant Matrix is :-\n");
for (i = 0; i < m; i++)
{
for (j = 0 ; j < q; j++)
{
printf("%d \t", c[i][j]);
}
printf("\n");
}
}
}
// String Operations.
#include <stdio.h>
#include <string.h>
void main()
{
char str1[30],str2[30];
int choice,result=0;
int len1=0,len2=0;
printf("\n Enter First String : ");
scanf("%[^\n]",str1);
printf("\n Enter Second String : ");
scanf("%s",str2);
printf(" %s \n %s",str1,str2);
printf("\n Enter Your Choice : \n");
printf("1. Copy Strings. \n");
printf("2. Compare Strings. \n");
printf("3. Concatenate Strings. \n");
printf("4. Find Lengths. \n");
printf("\n Enter Your Choice :");
scanf("%d",&choice);
switch(choice)
{
case 1 :
{
strcpy(str2,str1);
printf("\n %s %s \n",str1,str2);
break;
}
case 2 :
{
result = strcmp(str2,str1);
if(result == 0)
printf("\n Both Strings are Same. ");
else
printf("\n Both are different strings.");
break;
}
case 3 :
{
strcat(str2,str1);
printf("\n %s %s \n",str1,str2);
break;
}
case 4 :
{
len1 = strlen(str1);
printf("\n Legth of First String is %d \n",len1);
len2 = strlen(str2);
printf("\n Legth of Second String is %d \n",len2);
break;
}
default : printf("\n Wrong Choice .");
}
}
// Searching of given number in a given list.
#include <stdio.h>
void main()
{
int i,n,ele,pos;
int a[100];
printf("\n Enter how many integers : ");
scanf("%d",&n);
for(i = 0 ; i < n ; i++)
{
printf("\n Enter the integer %d : ",i+1);
scanf("%d",&a[i]);
}
printf("\n Enter element to be searched : ");
scanf("%d",&ele);
pos = -1;
for(i = 0 ; i < n ; i++)
{
if(ele == a[i])
{
pos = i + 1;
}
}
if(pos == -1)
printf("\n Search element not found in the list.\n");
else
printf("\n Element found in the list at %d position \n",pos);
}
// Bubble Sort.
#include <stdio.h>
void main()
{
int i,j,n,temp;
int a[100];
printf("\n Enter how many integers : ");
scanf("%d",&n);
for(i = 0 ; i < n ; i++)
{
printf("\n Enter the integer %d : ",i+1);
scanf("%d",&a[i]);
}
for(i = 0 ; i < n-1 ; i++)
{
for(j = 0; j < n - i - 1; j++)
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
printf("\n The given list of integers in ascending order is :\n");
for(i = 0 ; i < n ; i++)
{
printf("%d\n",a[i]);
}
}
// Binary Search.
#include<stdio.h>
void main()
{
int arr[50],i,n,x,flag=0,first,last,mid;
printf("Enter size of array:");
scanf("%d",&n);
printf("\nEnter array element(ascending order)\n");
for(i=0;i<n;++i)
scanf("%d",&arr[i]);
printf("\nEnter the element to search:");
scanf("%d",&x);
first=0;
last=n-1;
while(first<=last)
{
mid=(first+last)/2;
if(x==arr[mid]){
flag=1;
break;
}
else
if(x>arr[mid])
first=mid+1;
else
last=mid-1;
}
if(flag==1)
printf("\nElement found at position %d",mid+1);
else
printf("\nElement not found");
}
#include <stdio.h>
void main()
{
int i,n,largest = 0,smallest = 30000;
int a[100];
printf("\n Enter how many integers : ");
scanf("%d",&n);
for(i = 0 ; i < n ; i++)
{
printf("\n Enter the integer %d : ",i+1);
scanf("%d",&a[i]);
}
for(i = 0 ; i < n ; i++)
{
if(largest < a[i])
largest = a[i];
if(smallest > a[i])
smallest = a[i];
}
printf("\n Largest Number in the list is %d \n",largest);
printf("\n Smallest Number in the list is %d \n",smallest);
}
// Matrix Addition.
#include <stdio.h>
void main()
{
int i,j,m, n, p, q, a[100][100], b[100][100], c[100][100];
printf("Enter the number of rows and columns of matrix A :\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of matrix A\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);
printf("Enter the number of rows and columns of matrix B :\n");
scanf("%d%d", &p, &q);
printf("Enter the elements of matrix B : \n");
for (i = 0; i < p; i++)
for (j = 0 ; j < q; j++)
scanf("%d", &b[i][j]);
if(m != p || n != q)
printf("\n Matrix Addition is not Possible. \n");
else
{
for (i = 0; i < m; i++)
{
for (j = 0 ; j < n; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
printf("Resultant Matrix is :-\n");
for (i = 0; i < m; i++)
{
for (j = 0 ; j < n; j++)
{
printf("%d \t", c[i][j]);
}
printf("\n");
}
}
}
// Matrix Multiplication.
#include <stdio.h>
void main()
{
int i,j,k,m, n, p, q, a[100][100], b[100][100], c[100][100];
printf("Enter the number of rows and columns of matrix A :\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of matrix A\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);
printf("Enter the number of rows and columns of matrix B :\n");
scanf("%d%d", &p, &q);
printf("Enter the elements of matrix B : \n");
for (i = 0; i < p; i++)
for (j = 0 ; j < q; j++)
scanf("%d", &b[i][j]);
if(n != p)
printf("\n Matrix Addition is not Possible. \n");
else
{
for (i = 0; i < m; i++)
{
for (j = 0 ; j < q; j++)
{
for(k=0 ; k < p; k++)
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
printf("Resultant Matrix is :-\n");
for (i = 0; i < m; i++)
{
for (j = 0 ; j < q; j++)
{
printf("%d \t", c[i][j]);
}
printf("\n");
}
}
}
// String Operations.
#include <stdio.h>
#include <string.h>
void main()
{
char str1[30],str2[30];
int choice,result=0;
int len1=0,len2=0;
printf("\n Enter First String : ");
scanf("%[^\n]",str1);
printf("\n Enter Second String : ");
scanf("%s",str2);
printf(" %s \n %s",str1,str2);
printf("\n Enter Your Choice : \n");
printf("1. Copy Strings. \n");
printf("2. Compare Strings. \n");
printf("3. Concatenate Strings. \n");
printf("4. Find Lengths. \n");
printf("\n Enter Your Choice :");
scanf("%d",&choice);
switch(choice)
{
case 1 :
{
strcpy(str2,str1);
printf("\n %s %s \n",str1,str2);
break;
}
case 2 :
{
result = strcmp(str2,str1);
if(result == 0)
printf("\n Both Strings are Same. ");
else
printf("\n Both are different strings.");
break;
}
case 3 :
{
strcat(str2,str1);
printf("\n %s %s \n",str1,str2);
break;
}
case 4 :
{
len1 = strlen(str1);
printf("\n Legth of First String is %d \n",len1);
len2 = strlen(str2);
printf("\n Legth of Second String is %d \n",len2);
break;
}
default : printf("\n Wrong Choice .");
}
}
// Searching of given number in a given list.
#include <stdio.h>
void main()
{
int i,n,ele,pos;
int a[100];
printf("\n Enter how many integers : ");
scanf("%d",&n);
for(i = 0 ; i < n ; i++)
{
printf("\n Enter the integer %d : ",i+1);
scanf("%d",&a[i]);
}
printf("\n Enter element to be searched : ");
scanf("%d",&ele);
pos = -1;
for(i = 0 ; i < n ; i++)
{
if(ele == a[i])
{
pos = i + 1;
}
}
if(pos == -1)
printf("\n Search element not found in the list.\n");
else
printf("\n Element found in the list at %d position \n",pos);
}
// Bubble Sort.
#include <stdio.h>
void main()
{
int i,j,n,temp;
int a[100];
printf("\n Enter how many integers : ");
scanf("%d",&n);
for(i = 0 ; i < n ; i++)
{
printf("\n Enter the integer %d : ",i+1);
scanf("%d",&a[i]);
}
for(i = 0 ; i < n-1 ; i++)
{
for(j = 0; j < n - i - 1; j++)
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
printf("\n The given list of integers in ascending order is :\n");
for(i = 0 ; i < n ; i++)
{
printf("%d\n",a[i]);
}
}
// Binary Search.
#include<stdio.h>
void main()
{
int arr[50],i,n,x,flag=0,first,last,mid;
printf("Enter size of array:");
scanf("%d",&n);
printf("\nEnter array element(ascending order)\n");
for(i=0;i<n;++i)
scanf("%d",&arr[i]);
printf("\nEnter the element to search:");
scanf("%d",&x);
first=0;
last=n-1;
while(first<=last)
{
mid=(first+last)/2;
if(x==arr[mid]){
flag=1;
break;
}
else
if(x>arr[mid])
first=mid+1;
else
last=mid-1;
}
if(flag==1)
printf("\nElement found at position %d",mid+1);
else
printf("\nElement not found");
}