20_10

Write a C program to find average using findavg() function.

Source code:

 /*Consider the function find average findavg() , which takes an array as an argument along with another argument and based on the passed arguments and it returns the average of the numbers passed through the array. Consider an array of size 10. It reads 5 marks and find the average.  */


#include<stdio.h>

int findavg(int marks[]);

int findavg(int marks[])

{

    int i, sum = 0;

    int avg;

    for (i = 0; i <= 4; i++) {

        sum += marks[i];

    }

    avg = (sum / 5);

    return avg;

}

int main()

{

    int avg; int marks[10];int i;int n=5;

    /* Read Marks*/

    printf("Enter marks of 5 subject: ");

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

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

    avg = findavg(marks);

    printf("Average=%d", avg);

    return 0;

}

Output:

C program to find average using findavg() function


Write a C program to find out the Sum of the diagonal elements of a matrix.

 Source code:

/*Write a C program to find out the Sum of the diagonal elements of a matrix. Consider a matrix as below:

1 2 3

4 5 6

7 8 9

The Sum of the diagonal elements of the above matrix is 15. */


#include<stdio.h>

int main(){

  int a[3][3],i,j,sum=0,m=3,n=3;


  /* Read the elements of the Matrix a */

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

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

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

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

  

for(i=0;i<m;i++){

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

          if(i==j)

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

      }

  }

printf("Sum of the Diagonal elements of the matrix=%d",sum);

  return 0;

}

Output:

C program to find out the Sum of the diagonal elements of a matrix


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

 Source code:

 /*Write a C program to find the transpose of a given matrix. The transpose of a matrix is a new matrix whose rows are the columns of the original. This makes the columns of the new matrix the rows of the original. Consider a matrix of size 3x3. */

 

#include<stdio.h>

int main(){

  int a[3][3],b[3][3],i,j,k=0,m=3,n=3;


  /* Read an Array a*/

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

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

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

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

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

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

          b[i][j]=0;

  for(i=0;i<m;i++){

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

            b[i][j]=a[j][i];

      }

  }

printf("Transpose of a Matrix");

  for(i=0;i<m;i++){

      printf("\n");

      for(j=0;j<m;j++){

            printf("%d ",b[i][j]);

      }

  }

  return 0;

}

Output:

C program to find the transpose of a given matrix


Write a C program to perform Multiplication of two matrixes.

 Source code:

/*Multiplication of two matrixes. It is only possible if the first matrix of size i x j and other matrix of size j x k , where I, j and k are any positive integer. Three two dimensional matrices a, b and c each of size [3x3] will be considered here. Read the values of matrix a and matrix b, multiply and put it matrix c. Finally print the value of matrix c. */


#include<stdio.h>

int main()

{

  int a[3][3],b[3][3],c[3][3],i,j,k,sum=0,m=3,n=3,o=3,p=3;

printf("Enter 1st matrix: \n");

/* Read Matrix a */

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

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

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


printf("Enter 2nd matrix: \n");

/* Read Matrix b */

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

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

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

           

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

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

            c[i][j]=0;

      for(i=0;i<m;i++){ /* row of first matrix */

      for(j=0;j<p;j++){  /* column of second matrix */

            sum=0;

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

                sum=sum+a[i][k]*b[k][j];

            c[i][j]=sum;

      }

      }

printf("The multiplication of two matrix is");

  for(i=0;i<m;i++){

      printf("\n");

      for(j=0;j<p;j++){

          printf("%d ",c[i][j]);

      }

  }

  return 0;

}

Output:

C program to perform Multiplication of two matrixes


Write a C program which find the student name who obtain the highest marks.

Source code:

//Consider the following program statement:

/*One needs to first input a set of N number of ALPHABETIC Strings each representing a name of a student in an array studname [N] . Assume each string can be Max. 40 Character Long. Subsequently , one needs to input Marks obtained by those students in another array marks [N] . Assume that studname [I] i.e. ith student in the list of student names has obtained Marks [I] in the Marks List.  You need to find out and print the Max Marks obtained by a student and also print the name of the student who has obtained this marks. Considering here both the arrays of size 5. */


#include <stdio.h>

#include <string.h>

typedef char st[10];

int main ()

{

    st studname[10];

    int marks[10];

    int i; int n=5; int imaxpos=1;

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

    {

    printf("Enter name of %d : ",i);

        scanf("%s", studname[i]);

    }

        

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

    {

    printf("Enter marks of %d : ",i);

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

    }


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

    {

        if (marks [i]  > marks [imaxpos] )

        {

            imaxpos = i;

        }

    }

printf ("The largest marks=%d\nObtained by student : %d Name: %s\n", marks[imaxpos] , imaxpos, studname[imaxpos]);

    return(0);

}

Output:

C program which find the student name who obtain the highest marks


Write a C program to sort an array using Bubble sort.

 Source code:

/*Bubble sort is a sorting algorithm that works by repeatedly stepping through lists that need to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. This passing procedure is repeated until no swaps are required, indicating that the list is sorted. Bubble sort gets its name because smaller elements bubble toward the top of the list. Consider an array of size 10. It will be filled it by reading 10 integers. The final output will be sorted output in Ascending Order. */


#include <stdio.h>

int main()

{

  int array[10], n=10, i, j, swap;


 /* Read array elements */

printf("Enter the array: ");

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

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

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

  {

    for (j = 0 ; j < n - i - 1; j++)

    {

      if (array[j] > array[j+1]) /* For decreasing order use < */

      {

        swap       = array[j];

        array[j]   = array[j+1];

        array[j+1] = swap;

      }

    }

  }

printf("Sorted list in ascending order:\n");


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

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

  return 0;

}

Output:
C program to sort an array using Bubble sort


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...