Professional Documents
Culture Documents
#include <stdio.h>
int main() {
int n, i;
float num[100], sum = 0.0, avg;
avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}
Output:
Enter the numbers of elements: 6
1. Enter number: 45.3
2. Enter number: 67.5
3. Enter number: -45.6
4. Enter number: 20.34
5. Enter number: 33
6. Enter number: 45.6
Average = 27.69
__________________________________________
__________________
//2.C Program to Find Largest Element of an Array.
#include <stdio.h>
int main() {
int n;
double arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);
return 0;
}
Output:
Enter the number of elements (1 to 100): 5
Enter number1: 34.5
Enter number2: 2.4
Enter number3: -35.5
Enter number4: 38.7
Enter number5: 24.5
Largest element = 38.70
__________________________________________
____________________
//3.C Program to Calculate Standard Deviation.
// SD of a population
#include <math.h>
#include <stdio.h>
float calculateSD(float data[]);
int main() {
int i;
float data[10];
printf("Enter 10 elements: ");
for (i = 0; i < 10; ++i)
scanf("%f", &data[i]);
printf("\nStandard Deviation = %.6f",
calculateSD(data));
return 0;
}
float calculateSD(float data[]) {
float sum = 0.0, mean, SD = 0.0;
int i;
for (i = 0; i < 10; ++i) {
sum += data[i];
}
mean = sum / 10;
for (i = 0; i < 10; ++i) {
SD += pow(data[i] - mean, 2);
}
return sqrt(SD / 10);
}
Output:
Enter 10 elements: 1
2
3
4
5
6
7
8
9
10
Output:
Hexadecimal number is: 2D
__________________________________________
_________________________
int main()
{
long int binaryval, hexadecimalval = 0, i = 1,
remainder;
Output:
Enter the binary number: 10000
Equivalent hexadecimal value: 10
__________________________________________
__________________________
//C Program to Add Two Matrix Using Multi-
dimensional Arrays.
#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);
Output:
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100):
3
10 8 6
__________________________________________
__________________________
//11.C Program to Multiply to Matrix Using Multi-
dimensional Arrays.
#include <stdio.h>
printf("\nOutput Matrix:\n");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf("%d ", result[i][j]);
if (j == column - 1)
printf("\n");
}
}
}
int main() {
int first[10][10], second[10][10], result[10][10], r1,
c1, r2, c2;
printf("Enter rows and column for the first matrix:
");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for the second
matrix: ");
scanf("%d %d", &r2, &c2);
// Taking input until
// 1st matrix columns is not equal to 2nd matrix row
while (c1 != r2) {
printf("Error! Enter rows and columns again.\n");
printf("Enter rows and columns for the first
matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and columns for the second
matrix: ");
scanf("%d%d", &r2, &c2);
}
return 0;
}
Output:
Enter rows and column for the first matrix: 2
3
Enter rows and column for the second matrix: 3
2
Enter elements:
Enter a11: 2
Enter a12: -3
Enter a13: 4
Enter a21: 53
Enter a22: 3
Enter a23: 5
Enter elements:
Enter a11: 3
Enter a12: 3
Enter a21: 5
Enter a22: 0
Enter a31: -3
Enter a32: 4
Output Matrix:
-21 22
159 179
__________________________________________
_____________________
//12.C Program to Find Transpose of a Matrix.
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
Output:
Enter rows and columns: 2
3
int main()
{
int firstMatrix[10][10], secondMatrix[10][10],
mult[10][10], rowFirst, columnFirst, rowSecond,
columnSecond, i, j, k;
return 0;
}
Output:
6 25
__________________________________________
_________________________
//14.C Program to find the sum of all elements in an
array.
#include <stdio.h>
void main()
{
int a[100];
int i, n, sum=0;
Output:
Find sum of all elements of array:
--------------------------------------
Input the number of elements to be stored in the
array :3
Input 3 elements in the array :
element - 0 : 2
element - 1 : 5
element - 2 : 8
Sum of all elements stored in the array is : 15
__________________________________________
___________________________
//15.C program to read a sorted list of floating point
values then calculate and display
the median of the values.
#include<stdio.h>
#define N 10
main( ){
int i,j,n;
float median,a[N],t;
printf("Enter the number of items
");
scanf("%d", &n);
/* Reading items into array a */
printf("Input %d values
",n);
for (i = 1; i <= n ; i++)
scanf("%f", &a[i]);
/* Sorting begins */
for (i = 1 ; i <= n-1 ; i++){ /* Trip-i begins */
for (j = 1 ; j <= n-i ; j++) {
if (a[j] <= a[j+1]) { /* Interchanging values */
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
else
continue ;
}
} /* sorting ends */
/* calculation of median */
if ( n % 2 == 0)
median = (a[n/2] + a[n/2+1])/2.0 ;
else
median = a[n/2 + 1];
/* Printing */
for (i = 1 ; i <= n ; i++)
printf("%f ", a[i]);
printf("Median is %f", median);
}
Output:
Enter the number of items
5
Input 5 values
2.3
1.2
3.8
4.6
8.9
8.900000 4.600000 3.800000 2.300000 1.200000
Median is 3.800000
__________________________________________
___________________
//16.C Program to sort the given array using bubble
sort.
#include <stdio.h>
void bubble_sort(int arr[], int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Sorted array: 11 12 22 25 34 64 90
__________________________________________
___________________
//17.C Program to copy the contents of one array
into another in the reverse order.
// C program to copy the contents
// of one array into another
// in the reverse order
#include <stdio.h>
// Function to print the array
void printArray(int arr[], int len)
{
int i;
for (i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
}
// Driver code
int main()
{
int original_arr[] = {1, 2, 3, 4, 5};
int len = sizeof(original_arr)/sizeof(original_arr[0]);
int copied_arr[len], i, j;
// Copy the elements of the array
// in the copied_arr in Reverse Order
for (i = 0; i < len; i++) {
copied_arr[i] = original_arr[len - i - 1];
}
return 0;
}
Output:
Original array: 1 2 3 4 5
Resultant array: 5 4 3 2 1
__________________________________________
______________________
//18.Twenty five numbers are entered from the
keyboard into an array. Write a C
Program to find out how many of them are prime
numbers.
#include <stdio.h>
printf("\n");
return 0;
}
Output:
100 - Not Prime
200 - Not Prime
31 - Prime
13 - Prime
97 - Prime
10 - Not Prime
20 - Not Prime
11 - Prime
__________________________________________
______________________
//19.C program to find whether the given element is
present in an array or not
using Linear and Binary search.
#include <stdio.h>
int main()
{
int array[100], search, c, number;
printf("Enter the number of elements in array\n");
scanf("%d",&number);
printf("Enter %d numbers\n", number);
for ( c = 0 ; c < number ; c++ )
scanf("%d",&array[c]);
printf("Enter the number to search\n");
scanf("%d",&search);
for ( c = 0 ; c < number ; c++ )
{
if ( array[c] == search ) /* if required element
found */
{
printf("%d is present at location %d.\n",
search, c+1);
break;
}
}
if ( c == number )
printf("%d is not present in array.\n", search);
return 0;
}
Output:
Enter the number of elements in array
5
Enter 5 numbers
12
23
22
10
45
Enter the number to search
22
22 is present at location 3.
//Binary Search
//Program Name: BinarySearch.c #include<stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search,
middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the
list.\n", search);
return 0;
}
Output:
Enter number of elements
6
Enter 6 integers
10
30
20
15
56
100
Enter value to find
33
Not found! 33 is not present in the list.
__________________________________________
_______________________
//20.C program to insert an element in a sorted
array.
#include <stdio.h>
int main()
{
int arr1[100],i,n,p,inval;
printf("\n\nInsert New value in the sorted array
:\n");
printf("-----------------------------------------\n");
printf("Input the size of array : ");
scanf("%d", &n);
/* Stored values into the array*/
printf("Input %d elements in the array in
ascending order:\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
printf("Input the value to be inserted : ");
scanf("%d",&inval);
printf("The exist array list is :\n");
for(i=0;i<n;i++)
printf("% 5d",arr1[i]);
/* Determine the position where the new value will
be insert.*/
for(i=0;i<n;i++)
{
if(inval<arr1[i])
{
p = i;
break;
}
else
{
p=i+1;
}
}
/* move all data at right side of the array */
for(i=n+1;i>=p;i--)
arr1[i]= arr1[i-1];
/* insert value at the proper position */
arr1[p]=inval;
printf("\n\nAfter Insert the list is :\n");
for(i=0;i<=n;i++)
printf("% 5d",arr1[i]);
printf("\n");
}
Output:
Insert New value in the sorted array :
-----------------------------------------
Input the size of array : 6
Input 6 elements in the array in ascending order:
element - 0 : 2
element - 1 : 5
element - 2 : 7
element - 3 : 11
element - 4 : 9
element - 5 : 6
Input the value to be inserted : 8
The exist array list is :
2 5 7 11 9 6
After Insert the list is :
2 5 7 8 11 9 6
--------------------------------
Process exited after 33.18 seconds with return value
10
Press any key to continue . . .
__________________________________________
_____________________________
//21.C program to arrange the values of an array in
such a way that even numbers
precede the odd numbers.
#include <conio.h>
int main()
{
int a[10000],b[10000],i,n,j,k,temp,c=0;
printf("Enter size of the array : ");
scanf("%d", &n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
if(a[i]%2==1)
c++;
}
for(i=0; i<n-1; i++)
{
k=0;
j=n-c;
}
Output:
Enter size of the array : 10
Enter elements in array : 0 1 2 3 4 5 6 7 8 9
int i,j,visited[n];
return 0;
}
Output:
2 20 11
--------------------------------
__________________________________________
____________________________
//25.C program to find the sum of diagonal elements
in a given matrix.
int main()
{
int i, j, m = 3, n = 3, a = 0, sum = 0;
// input matrix
int matrix[3][3]
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Output:
The matrix is
123
456
789
int main() {
int A[3][3], B[3][3];
int row, col, isSym;
printf("\n");
}
} else {
printf("\n Matrix is not Symmetric.");
}
return 0;
}
Output:
Enter the elements in matrix of size 3x3:
2
3
6
3
4
5
6
5
9
Matrix is Symmetric.
236
345
659
//27.program to find the sum of upper triangular and
lower triangular of the square
matrix.
Output:
Output:
//29.C program to count the total number of non-zero
elements in a twodimensionalarray.
Output: