20_10

Write a C program to find the transpose of a matrix.

Source code:

 //Write a C program to find the transpose of a matrix 

#include <stdio.h>

int main()

{

int array1[10][10], array2[10][10];

int i,j,row,col;

printf("Enter the number of row:");

scanf("%d",&row); 

printf("Enter the number of column:");

scanf("%d",&col); 

  printf("Enter the matrix:\n");

    for(i=0;i<row;i++)

    {

        for(j=0;j<col;j++)

        {

            scanf("%d",&array1[i][j]); 

        }

    }

 /*Transpose a matrix */

    for(i=0;i<col;i++)

    {

        for(j=0;j<row;j++)

        {

            array2[i][j] = array1[j][i];    /*exchange elements*/

        }

    } 

for(i=0;i<col;i++)

    {

        for(j=0;j<row;j++)

        {

            printf("%d\t",array2[i][j]); 

        }

printf("\n");

    }

    return 0;

}

Output:
C program to find the transpose of a matrix


Write a C program to find number of blank space in a sentence(Do not use the strlen() function).

Source code:

 //Write a C program to find number of blank space in a sentence

#include <stdio.h>

int main()

{

    char s[1000], ch;

    int i = 0, count=0; /*i is array index and count is to count blank */

printf("Enter your sentence:");

    while(ch != '\n')    /* terminates if user hit enter */

    {

        ch = getchar();

        s[i] = ch;

        i++;

    }

    s[i-1] = '\0';       /* inserting null character at end */

for(i = 0; s[i] != '\0'; ++i)

    {

    if (s[i] == ' ')

    count += 1; 

    }   

    

printf("No. of blank space = %d", count);

return 0;

}

Output:

C program to find number of blank space in a sentence


Write a C program to count frequency of each element of an array.

 Source code:

/*Write a C program to count frequency of each element of an array. Frequency of a particular element will be printed once.*/

#include <stdio.h>

int main()

{

    int arr[50], freq[50]; /* Array arr[50] stores the elements and freq[50] stores the no. of times the element occurs */

    int size, i, j, count;

printf("Enter the size of array:");

    scanf("%d", &size);


    printf("Enter the values of the array :\n");

    for(i=0; i<size; i++)

    {

        scanf("%d", &arr[i]);

        freq[i] = -1; 

/* all frequencies are initialized to -1 which will be changed to specific value after counting no. of occurrence */

/* for the element which is already counted the corresponding frequency will be made 0 */ 

    }

for(i=0; i<size; i++)

    {

        count = 1;

        for(j=i+1; j<size; j++)

        {

            /* If duplicate element is found */

            if(arr[i]==arr[j])

            {

                count++;

                /* Make sure not to count frequency of same element again */

                freq[j] = 0;

            }

        }


        /* If frequency of current element is not counted */

        if(freq[i] != 0)

        {

            freq[i] = count;

        }

    }


   /* Print frequency of each element */


    for(i=0; i<size; i++)

    {

        if(freq[i] != 0) /* The element with corresponding frequency 0 means duplicate occurrence of the element */

        {

            printf("%d occurs %d times\n", arr[i], freq[i]);

        }

    }

return 0;

}

Output:
C program to count frequency of each element of an array


Write a C program which reads a square matrix a[n][n] and print only the elements that falls in the diagonal starting from a[0][0] to a[n][n] and all other elements printed as 0 (zero).

 Source code:

/*Write a C program which reads a square matrix a[n][n] and print only the elements that falls in the diagonal starting from a[0][0] to a[n][n] and all other elements printed as 0 (zero).*/

#include<stdio.h>

int main()

{

int n, i, j;

int a[10][10];

printf("Enter the number of row = column:");

scanf("%d", &n); /*Number of Rows = Column = n */

printf("Enter the matrix:\n");

for (i=0; i<n; ++i)

{

for (j=0;j<n; ++j)

scanf("%d",&a[i][j]);

}

for (i=0; i<n; ++i)

{

for (j=0;j<n; ++j)

{

if(i!=j)

a[i][j]=0;

}

}

/* printing the matrix in matrix format*/

printf("The diagonal matrix is:\n");

for (i=0; i<n; ++i)

{

for (j=0;j<n; ++j)

printf("%d\t",a[i][j]); /* print the row elements with one tab space */

printf("\n");

}

return 0;

}

 Output:

C program to make a diagonal matrix


Write a C code which computes and prints the sum of each rows of a 2D Matrix.

 Source code:

//Write a C code which computes and prints the sum of each rows of a 2D Matrix. 

#include <stdio.h>

int main()

{

    int matrix[10][10];

    int i,j,r,c; 

    int sum[10]; /* Sum of each row is kept in this array */

printf("Enter the number of row:\n");

    scanf("%d",&r); /* Input no. of rows */

printf("Enter the number of column:\n");

    scanf("%d",&c); /* Input no. of columns */

printf("Enter the matrix:\n");

for(i=0;i<r;i++)

    {

        for(j=0;j<c;j++)

        {

            scanf("%d",&matrix[i][j]);

        }

    }

    /*sum of all rows*/

    for(i=0;i<r;i++)

    {

    sum[i]=0;

        for(j=0;j<c;j++)

        {

            sum[i] = sum[i] +  matrix[i][j];

        }

    }

for(i=0;i<r;i++)

printf("%d\n",sum[i]);

return 0;

}

Output:

C code which computes and prints the sum of each rows of a 2D Matrix


Write a C code to count the number of negative elements in a 2D matrix.

Source code:

 //Write a C code to count the number of negative elements in a 2D matrix.

#include<stdio.h>

int main()

{

int row, col, i, j, count=0;

int a[10][10];

printf("Enter the number of row & column:");

scanf("%d %d", &row, &col); 

printf("Enter the matrix:\n");

for (i=0; i<row; ++i)

{

for (j=0;j<col; ++j)

scanf("%d",&a[i][j]);

}

for (i=0; i<row; ++i)

{

for (j=0;j<col; ++j)

{

if(a[i][j]<0)

count = count + 1;

}

}

printf("No. of Negative Elements = %d",count);

return 0;

}

Output:

C code to count the number of negative elements in a 2D matrix


Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...