20_10

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 C program to find the first repeated element in an array.

Source code:

 //Write a C program to find the first repeated element in an array of size 5

#include <stdio.h>

int main()

{

    int arr[5];

    int i,j,n=5;

    int ind=-1,ele; /* to store index & element */

    /* read array elements */

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

    {

    printf("Enter the value for %d: ",i);

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

    }

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

    {

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

        {

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

            {

                ele=arr[j];

                ind=j;

                goto l;

                break;

            }

        }

    }

l: if(ind!=-1)

printf("%d repeated at index %d\n",ele,ind);

    else

        printf("There is no repeated element\n");

return 0;

}

Output:
C program to find the first repeated element in an array


Write a C program to calculate Sum and Product of all Elements in an array.

Source code:

 //C program to calculate Sum and Product of all Elements in an array

#include <stdio.h>

int main() 

    int arr[10]; 

    int sum,product,i;

    /*Read array elements*/

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

    {  

    printf("Enter the value for %d: ",i);

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

    }

sum=0;

product=1;

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

{

sum+=arr[i];

    product*=arr[i];

}

printf("Sum of array is:%d\n",sum); 

printf("Product of array is:%d",product);

return 0;

}

Output:
C program to calculate Sum and Product of all Elements in an array


Write a C Program to Calculate mean of 10 numbers.

Source code:

 //Write a C Program to Calculate mean of 10 numbers

#include <stdio.h>

int main()

{

int num[10];

    int  i, Number=10;

    int Mean, Sum=0;


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

    {

    printf("Enter the value for %d: ",i);

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

    }

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

    {

    Sum = Sum + num[i];

    }


    Mean = Sum/Number;

    printf("%d",Mean);

    return 0;

}

Output:

C Program to Calculate mean of 10 numbers


Write a C program to add and subtract of two one dimensional array elements and store it in a third and fourth one dimensional array and print it.

Source code:

 /* To add and subtract of two one dimensional array elements and store it in a third and fourth one dimensional array and print it */

#include <stdio.h>

int main()

{

    int arr1[5];

    int arr2[5];

    int arr3[5];

    int arr4[5];

    int i=0;

    /*Read array elements*/

    printf("For 1st array:-\n");

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

    {

    printf("Enter the value for %d: ",i);

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

    }

    printf("For 2nd array:-\n");

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

    {

    printf("Enter the value for %d: ",i);

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

}

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

{

arr3[i]=arr1[i]+arr2[i];

arr4[i]=arr1[i]-arr2[i];

}

printf("Sum of two array:-\n");

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

{

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

}

printf("Subtraction of two array:-\n");

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

    {

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

    }

return 0;

}

Output:
C program to add and subtract of two one dimensional array elements and store it in a third and fourth one dimensional array and print it


Write a C program to merge two one dimensional array elements in one dimensional third array.

Source code:

 //Merge two one dimensional array elements in one dimensional third array

#include <stdio.h>

int main()

{

    int arr1[5];

    int arr2[5];

    int arr3[10];

int i=0,j=0;

 /*Read array elements*/

  printf("For 1st array:-\n");

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

    {

    printf("Enter the value for %d: ",i);

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

    }

    printf("For 2nd array:-\n");

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

    {

    printf("Enter the value for %d: ",i);

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

}

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

{

  arr3[i]=arr1[i];

}

for(j=5,i=0;j<10,i<5;j++,i++)

{

  arr3[j]=arr2[i];

}

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

{

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

}

return (0);

}

Output:

Merge two one dimensional array elements in one dimensional third array


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