C Programming : Array (BIM-TU)

By Suraj Chaudhary

  1. WAP to input numbers in an array of size ‘N’ and find the sum of even and odd numbers separately.
#include<stdio.h>
int main()
{
    int a[100],i,b=0,c=0,n;
    printf("Enter the value of n: ");
    scanf("%d",&n);
    printf("\nEnter %d numbers :",n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]%2==0)
        b=b+a[i];
        else
        c=c+a[i];
    }
    printf("\nSum of even numbers : %d",b);
    printf("\nSum of odd numbers : %d",c);
    return 0;
}
Enter the value of n: 5
Enter 5 numbers :1 2 3 4 5
Sum of even numbers : 6
Sum of odd numbers : 9

2. WAP to input numbers in an array and find the greatest and smallest number.

#include<stdio.h>
int main()
{
    int a[100],i,b,c,n;
    printf("Enter the value of n:");
    scanf("%d",&n);
    printf("Enter %d numbers :",n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    b=a[0];
    for(i=0;i<n;i++)
    {
        if(a[i]>b)
        b=a[i];
    }
    c=a[0];
    for(i=0;i<n;i++)
    {
        if(a[i]<c)
        c=a[i];
    }
    printf("Greatest Number : %d",b);
    printf("\nSmallest Number : %d",c);
    return 0;
}
Enter the value of n:5
Enter 5 numbers :1 5 2 3 4
Greatest Number : 5
Smallest Number : 1

3. WAP to sort the elements of an array in descending order.

#include<stdio.h>
int main()
{
    int a[100],i,j,temp,n;
    printf("Enter the value of n: ");
    scanf("%d",&n);
    printf("Enter %d numbers: ",n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(a[i]<a[j])
            {
                temp=a[j];
                a[j]=a[i];
                a[i]=temp;
            }
        }
    }
    printf("Array after sorting in descending order : \n");
    for(i=0;i<n;i++)
    {
        printf("%d ",a[i]);
    }
    return 0;
}
Enter the value of n: 5
Enter 5 numbers: 1 2 3 4 5
Array after sorting in descending order : 
5 4 3 2 1

4. WAP to find sum of two matrices.

#include<stdio.h>
int main()
{
    int a[2][2],b[2][2],c[2][2],i,j;
    printf("Enter the first matrix:\n");
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    printf("Enter the second matrix:");
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            scanf("%d",&b[i][j]);
        }
    }
    printf("The resultant matrix is : \n");
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            c[i][j]=a[i][j]+b[i][j];
            printf("%d ",c[i][j]);
        }
        printf("\n");
    }
    return 0;
}
Enter the first matrix:
1 2
3 4
Enter the second matrix:
2 3
4 5
The resultant matrix is : 
3 5 
7 9

5. WAP to multiply two matices.

#include<stdio.h>
int main()
{
    int a[2][2],b[2][2],c[2][2],i,j,k;
    printf("Enter the first matrix : \n");
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    printf("Enter the second matrix: \n");
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            scanf("%d",&b[i][j]);
        }
    }
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            c[i][j]=0; 
            for(k=0;k<2;k++)
            {
                c[i][j]+=a[i][k]*b[k][j];
            }
        }
    }

    printf("The resultant matrix is : \n");
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            printf("%d ",c[i][j]);
        }
        printf("\n");
    }
    return 0;
}
Enter the first matrix : 
7 5
6 3
Enter the second matrix: 
2 1
5 1
The resultant matrix is : 
39 12 
27  9

Suraj Chaudhary is a writer, developer, founder, and a constant learner. He shares lessons and resource to living a fuller life every week. On this blog, he shares helpful guides and helpful articles that help his 70,000+ monthly readers find answers, solve problems, and meet their curious needs.

Leave a Comment

Slide to prove you're not a bot/spammer *