Professional Documents
Culture Documents
UNIT III Illustrative Programs
UNIT III Illustrative Programs
UNIT III Illustrative Programs
#include <stdio.h>
void main()
{
int m[5],i,sum=0,n;
float mean;
printf("enter number of students \n");
scanf("%d",&n);
printf("enter marks of students \n");
for(i=0;i<n;i++)
{
scanf("%d",&m[i]);
}
for(i=0;i<n;i++)
sum=sum+m[i];
mean=(float)sum/n;
printf("Mean=%f",mean);
}
Output:
enter number of students
5
enter marks of students
89
90
95
85
96
Mean=91.000000
C Program to find the Meadian
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,temp,n,a[20],sum=0;
float median;
printf("enter n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter %d number:",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
if(n%2==0)
{
median=((a[n/2]+a[n/2 -1])/2.0);
}
else
{
median=a[n/2];
}
printf("\n the median value is %f",median);
getch();
}
Output:
enter n:5
enter 1 number:11
enter 2 number:12
enter 3 number:13
enter 4 number:14
enter 5 number:15
The median value is 13.000000
#include<stdio.h>
void main()
{
int maxvalue = 0, maxCount = 0, i, j,a[20],n,count=0;
printf("Enter the N value:");
scanf("%d",&n);
for(i = 0; i < n;i++)
{
printf("\n Enter %d number:",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for (j = i+1; j < n;j++)
{
if(a[j] == a[i])
count++;
}
if (count > maxCount)
{
maxCount = count;
maxvalue = a[i];
printf("\nThe mode value is %d",maxvalue);
}
}
}
Output:
Enter the N value:5
Enter 1 Number:0
Enter 2 Number:6
Enter 3 Number:7
Enter 4 Number:2
Enter 5 Number:7
The mode value is 7
Program 1:
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
10 8 6
Program 2:
#include <stdio.h>
int main()
{
int a[2][2],b[2][2],c[2][2],i,j;
Output
ENTER VALUES FOR FIRST MATRIX :
1
2
3
4
ENTER VALUES FOR SECOND MATRIX :
1
2
3
4
THE ADDITION OF MATRIX IS :
2 4
6 8
Output
enter the number of row=3
enter the number of column=3
enter the first matrix element=
111
222
333
enter the second matrix element=
111
222
333
multiply of the matrix=
666
12 12 12
18 18 18
Output:
Enter rows and columns: 3
3
Entered matrix:
1 4 7
8 9 7
3 5 8
#include <stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d Numbers n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
a[i]=a[position];
a[position=swap;
}
}
printf("Sorted Array:n");
for(i = 0; i < n; i++)
printf("%dn", a[i]);
return 0;
}
#include<stdio.h>
void main()
{
int a[10],i,n,m,c=0,l,u,mid;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements in ascending order: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the number to be searched: ");
scanf("%d",&m);
l=0,u=n-1;
while(l<=u)
{
mid=(l+u)/2;
if(m==a[mid])
{
c=1;
break;
}
else if(m<a[mid])
{
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("The number is not found.");
else
printf("The number is found.");
}
Sample output:
Enter the size of an array: 5
Enter the elements in ascending order: 4 7 8 11 21
Enter the number to be search: 11
The number is found.
Example:
3 5 7 9 11
Search key=7
middle element=7
Searching element=middle element. So the element is found.
Search key=11
Middle element=7
Searching element>middle
So go to right half: 9 11.
Repeat steps until 11 is found or list ends.
Output:
Saranya
#include <stdio.h>
int main()
{
char first_string[20]; // declaration of char array variable
char second_string[20]; // declaration of char array variable
int i; // integer variable declaration
printf("Enter the first string");
scanf("%s",first_string);
printf("\nEnter the second string");
scanf("%s",second_string);
for(i=0;first_string[i]!='\0';i++);
for(int j=0;second_string[j]!='\0';j++)
{
first_string[i]=second_string[j];
i++;
}
first_string[i]='\0';
printf("After concatenation, the string would look like: %s", first_string);
return 0;
}
Output: