Search This Blog

Thursday, 31 October 2024

8. C Program to perform addition and subtraction of Matrices

 /*8. C Program to perform addition and subtraction of Matrices */

#include<stdio.h>

void main()

{

     int a[10][10],b[10][10],add[10][10],sub[10][10],i,j,m,n;

     clrscr();

     printf("Enter the order of matrix\n");

     scanf("%d%d",&m,&n);

     printf("Enter the elements of first matrix:\n");

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

     {

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

           {

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

           }

     }

     printf("Enter the elements of second matrix:\n");


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

     {

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

           {

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

           }

     }

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

     {

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

           {

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

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

           }

     }

     printf("Matrix Addition:\n");

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

     {

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

           {

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

 

           }

           printf("\n");

     }

     printf("Matrix Subtraction:\n");

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

     {

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

           {

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

           }

           printf("\n");

     }

     getch();

}

7. Write a C Program to remove Duplicate Element in a single dimensional Array

 /* 7. Write a C Program to remove Duplicate Element in a single dimensional Array */

#include <stdio.h>

void main()

{

    int arr[20];     // Declare an array of size 20

    int size;

    int i, j, k;      // Loop control variables

    clrscr();

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

    scanf("%d", &size);  // Read size of the array

    printf("Enter elements in array:\n");

    for(i=0; i<size; i++)  // Read array elements

    {

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

    }

     //Find duplicate elements in the array

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

    {

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

     {

         // Check for duplicate elements

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

         {

           // Delete the current duplicate element

                for(k=j; k < size - 1; k++)

                {

                    arr[k] = arr[k + 1];

                }

           // Decrement size after removing duplicate element

                size--;

                j--;

            }

        }

    }

     //Display array after deleting duplicate elements

    printf("\nArray elements after deleting duplicates: ");

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

    {

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

    }

     getch();    

}

6. Write a C program to read marks scored in 3 subjects by n students and find the average of marks and result (Demonstration of single dimensional array)

 /* 6. Write a C program to read marks scored in 3 subjects by n students and find the average of marks and result (Demonstration of single dimensional array) */

#include<stdio.h>

void main()

{

     int sub1[10],sub2[10],sub3[10],total[10],avg[10],n,i;

     printf("Enter no of students:");

     scanf("%d",&n);

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

     {

           printf("Enter the marks of Student-%d:\n",i+1);

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

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

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

           total[i]=sub1[i]+sub2[i]+sub3[i];

           avg[i]=total[i]/n;

     }

     printf("Results are as follows:\n");

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

     {

           printf("Student-%d is as follows:\n",i+1);

           if((total[i]*100/300)>=70)

                printf("Distinction\n");

           else if((total[i]*100/300)>=60)

                printf("First Class\n");

           else if((total[i]*100/300)>=50)

                printf("Second Class\n");

           else if((total[i]*100/300)>=40)


                printf("Pass\n");

           else

                printf("Fail\n");

     }

     getch();

}

5. Write a C Program to read percentage of marks and to display appropriate grade (using switch case)

 /*5. Write a C Program to read percentage of marks and to display appropriate grade (using switch case) */

#include<stdio.h>

void  main()

{

     int percent;

     clrscr();

     printf("Enter Marks in Percentage:");

     scanf("%d",&percent);

     switch(percent/10)

     {

           case 10:

           case  9:

           case  8:

           case  7:  printf("Distinction! Or Grade-A\n");

                     break;

           case  6:  printf("First Class! Or Grade-B\n");

                     break;

           case 5:   printf("Second Class! Or Grade-C\n");

                     break;

           case 4:   printf("Pass! Or Grade-P\n");

                     break;

          default:   printf("Fail! Or Grade-F\n"); break; 

     

     getch();

}

 

 

4. Write a C Program to read a number, find the sum of the digits, reverse the number and check it for palindrome.

 /* 4. Write a C Program to read a number, find the sum of the digits, reverse the number and check it for palindrome. */


#include<stdio.h>

void main()

{

     int num,temp,digit,sum=0,rev=0;

     clrscr();

     printf("Enter a number:");

     scanf("%d",&num);

     temp=num;

     while(num>0)


     {

          digit=num%10;

          num=num/10;

          sum=sum+digit;

          rev=rev*10+digit;

     }

     printf("\nSum of Digits=%d",sum);

     printf("\nReverse Number=%d",rev);

     if(rev==temp)

     {

          printf("\n%d is Palindrome.",temp);

     }

     else

          printf("\n%d is Not a Palindrome.",temp);

     getch();

}

FREE Hit Counters