PRACTICAL 5
PRACTICAL PROGRAMS:
Program 1: Write a C program to find out the Maximum and Minimum number from given 10 numbers.
#include<stdio.h>
int main( )
{
int array[10], i, max, min;
printf("Enter value of array elements: \n");
for( i = 0 ; i < 10 ; i++)
{
printf("Enter element value %d= ",i+1);
scanf("%d", &array[i]);
}
max=array[0];
min=array[0];
for( i = 0 ; i < 10 ; i++)
{
if(max < array[i])
{
max = array[i];
}
if(min > array[i])
{
min = array[i];
}
}
printf("Max=%d, Min=%d", max, min);
return 0;
}
Output:
Enter value of array elements:
Enter element value 1= 1
Enter element value 2= 6
Enter element value 3= 4
Enter element value 4= 8
Enter element value 5= 9
Enter element value 6= 2
Enter element value 7= 3
Enter element value 8= 4
Enter element value 9= 5
Enter element value 10= 7
Max=9, Min=1
Program 2: Write a program to sort given array in ascending order.
#include<stdio.h>
int main( )
{
int array[10] i, n, temp;
printf("Enter total array elements (1-10) = ");
scanf("%d", &n);
printf("Enter value of array elements: \n");
for( i=0 ; i<n ; i++)
{
printf("Enter array element %d = ",i+1);
scanf("%d", &array[i]);
}
for( i=0 ; i<n ; i++)
{
for( j=0 ; j<n ; j++)
{
if(array[i] > array[j])
{
temp=array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
printf("Array after sorting : \n");
for(i=0 ; i<n ; i++)
{
printf("%d\n", array[i]);
}
return 0;
}
Output:
Enter total array elements (1-10) = 5
Enter value of array elements:
Enter element value 1 = 40
Enter element value 2 = 20
Enter element value 3 = 50
Enter element value 4 = 30
Enter element value 5 = 10
Array after sorting :
10
20
30
40
50
Program 3: Read five persons height and weight and count the number of person having height greater than 170 and weight less than 50.
#include<stdio.h>
int main( )
{
int height[5], weight[5], i, n=0;
printf("Enter value of array elements: \n");
for( i=0 ; i<5 ; i++)
{
printf("Enter height of person %d = ", i+1 );
scanf("%d", &height[i]);
printf("Enter weight of person %d = ", i+1 );
scanf("%d", &weight[i]);
}
for( i=0 ; i<5 ; i++)
{
if(height[i] > 170 && weight[i] < 50)
{
n++;
}
}
printf("number of person having height greater than 170 and weight less than 50 = %d", n);
return 0;
}
Output:
Enter value of array elements:
Enter height of person 1 = 171
Enter weight of person 1 = 45
Enter height of person 2 = 168
Enter weight of person 2 = 49
Enter height of person 3 = 175
Enter weight of person 3 = 52
Enter height of person 4 = 160
Enter weight of person 4 = 40
Enter height of person 5 = 130
Enter weight of person 5 = 50
number of person having height greater than 170 and weight less than 50 = 1
Program 4: Write a program to calculate average and total of 5 students for 3 subjects (use nested for loops)
#include<stdio.h>
int main( )
{
int marks[5][3] , i , j , sum=0;
float avg = 0;
printf("Enter value of array elements: \n");
for( i=0 ; i<5 ; i++ )
{
printf("Enter students %d marks : ", i+1);
for( j=0 ; j<3 ; j++ )
{
printf("Enter subject %d marks : ", j+1 );
scanf("%d", &marks[i][j]);
}
}
for( i=0 ; i<5 ; i++ )
{
sum=0;
for( j=0 ; j<3 ; j++ )
{
sum = sum + marks[i][j];
}
avg = (float) sum/3;
printf(“Students %d marks : %d “, i+1, sum);
printf(“Students %d average : %f “,i+1, avg);
}
return 0;
}
Output:
Enter value of array elements:
Enter students 1 marks :
Enter subject 1 marks : 60
Enter subject 2 marks : 60
Enter subject 3 marks : 60
Enter students 2 marks :
Enter subject 1 marks : 70
Enter subject 2 marks : 70
Enter subject 3 marks : 70
Students 1 marks : 180
Students 1 average : 60.000000
Students 2 marks : 210
Students 2 average : 70.000000
Program 5: Write a c program to multiply two metrics A and B of dimension p*q and q*r respectively, and store result in third matrix C.
#include<stdio.h>
int main( )
{
int a[3][3],b[3][3],c[3][3], i, j, sum=0, k;
int r1,c1,r2,c2;
printf("enter details of array a\n");
printf("enter number of rows : ");
scanf("%d", &r1);
printf("enter number of column : ");
scanf("%d", &c1);
for( i=0 ; i<r1 ; i++ )
{
for( j=0 ; j<c1 ; j++ )
{
printf("Enter array element a[%d] [%d] = ", i, j );
scanf("%d", &a[i][j]);
}
}
printf("enter details of array b\n");
printf("enter number of rows : ");
scanf("%d",&r2);
printf("enter number of column : ");
scanf("%d",&c2);
for( i=0 ; i<r2 ; i++ )
{
for( j=0 ; j<c2 ; j++ )
{
printf("Enter array element b[%d] [%d] = ", i, j );
scanf("%d", &b[i][j]);
}
}
for( i=0 ; i<r1 ; i++ )
{
for( j=0 ; j<c2 ; j++ )
{
sum=0;
for(k=0;k<c1;k++)
{
sum=sum + a[i][k] * b[k][j];
}
c[i][j]=sum;
printf("%d\t", c[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter details of array a
Enter number of rows : 2
Enter number of column : 3
Enter array element a[0][0] = 1
Enter array element a[0][1] = 1
Enter array element a[0][2] = 1
Enter array element a[1][0] = 2
Enter array element a[1][1] = 2
Enter array element a[1][2] = 2
Enter details of array b
Enter number of rows : 3
Enter number of column : 2
Enter array element b[0][0] = 1
Enter array element b[0][1] = 1
Enter array element b[1][0] = 2
Enter array element b[1][1] = 2
Enter array element b[2][0] = 3
Enter array element b[2][1] = 3
6 6
12 12
PRACTICE PROGRAM:
Practice Program 1: Write a C program to read and store the roll no and marks of 20 students using array.
#include<stdio.h>
int main( )
{
int rollno[20], marks[20], i;
for( i = 0 ; i < 20 ; i++)
{
printf("Enter Roll of Student [%d] ",i+1);
scanf("%d", &rollno[i]);
printf("Enter Mark of Student [%d] ",i+1);
scanf("%d", &marks[i]);
}
for( i = 0 ; i < 20 ; i++)
{
printf("\nRoll No : %d Marks : %d", rollno[i], marks[i]);
}
return 0;
}
Output:
Enter Roll of Student [1] 1
Enter Mark of Student [1] 60
Enter Roll of Student [2] 2
Enter Mark of Student [2] 55
Enter Roll of Student [3] 3
Enter Mark of Student [3] 80
Roll No : 1 Marks : 60
Roll No : 2 Marks : 55
Roll No : 3 Marks : 80
Practice Program 2: Write a program to find out which number is even or odd from list of 10 numbers using array.
#include<stdio.h>
int main( )
{
int num[10], i, odd=0, even=0, n;
printf("Enter total array elements (1-10) = ");
scanf("%d", &n);
printf("Enter value of array elements: \n");
for( i=0 ; i<n ; i++)
{
printf("Enter element value %d = ", i+1 );
scanf("%d", &num[i]);
}
printf(“Odd numbers : ”);
for( i=0 ; i<n ; i++)
{
if(num[i] %2 ==1)
{
odd=odd+1;
printf("%d\n", num[i]);
}
}
printf(“Total Odd Numbers in Array = %d”, odd);
printf(“Even numbers : ”);
for( i=0 ; i<n ; i++)
{
if(num[i] %2 ==0)
{
even=even+1;
printf("%d\n", num[i]);
}
}
printf(“Total Even Numbers in Array = %d”, even);
return 0;
}
Output:
Enter total array elements (1-10) = 5
Enter value of array elements:
Enter element value 1 = 34
Enter element value 2 = 89
Enter element value 3 = 45
Enter element value 4 = 22
Enter element value 5 = 18
Odd numbers : 89 45
Total Odd Numbers in Array = 2
Even numbers : 34 22 18
Total Even Numbers in Array = 3
Practice Program 3: Write a C program to calculate the average, geometric and harmonic mean of n elements in an array.
#include<stdio.h>
int main( )
{
int i, n,;
float a[50], sum=0, sum1=0, sum2=1;
printf("How many numbers you want to enter : ");
scanf("%d", &n);
for( i=0 ; i<n ; i++)
{
printf("Enter Value at Position [%d] : “, i+1);
scanf("%f", &a[i]);
sum=sum+a[i];
sum1=sum1+(1.0/a[i]);
sum2=sum2*a[i];
}
printf("\nAverage = %f", sum/n);
printf("\nGeometric Mean = %f", pow(sum2,(1.0/n)));
printf("\nHarmonic Mean = %f",n*pow(sum1,-1));
return 0;
}
Output:
How many numbers you want to enter :5
Enter Value at Position [1] : 10
Enter Value at Position [2] : 20
Enter Value at Position [3] : 30
Enter Value at Position [4] : 40
Enter Value at Position [5] : 50
Average = 30.000000
Geometric Mean = 26.051711
Harmonic Mean = 21.897810