Showing posts with label C Programs. Show all posts
Showing posts with label C Programs. Show all posts

C program to calculate sum of Upper Triangular Elements in C

#include< stdio.h>
#include< conio.h>

void main()
{
int i,j,a[10][10],sum,m,n;

/* m - Number of rows 
   n - Number of Columns */

printf("nEnter the number of Rows : ");
scanf ("%d",&m);

printf("nEnter the number of Columns : ");
scanf ("%d",&n);

/* Accept the Elements in m x n Matrix */

for( i = 0 ; i < m ; i++ )
       for( j = 0 ; j < n ; j++ )
       {
       printf("Enter the Element a[%d][%d] : ", i , j);
       scanf("%d",&a[i][j]);
       }

/* Addition of all Diagonal Elements */

sum = 0;

for( i = 0 ; i < m ; i++ )
       for( j = 0 ; j < n ; j++ )
       {
        if ( i < j )             // Condition for Upper Triangle
        sum = sum + a[i][j];
       }

/* Print out the Result */

printf("nThe Addition of Upper Triangle Elements : %d",sum);

getch();

}

Output
Enter the number of Rows : 3

Enter the number of Columns : 3

Enter the Element a[0][0] : 1
Enter the Element a[0][1] : 2
Enter the Element a[0][2] : 3
Enter the Element a[1][0] : 2
Enter the Element a[1][1] : 1
Enter the Element a[1][2] : 1
Enter the Element a[2][0] : 1
Enter the Element a[2][1] : 2
Enter the Element a[2][2] : 1

The Addition of Upper Triangle Elements : 6
Explanation :

Considering above 3×3 matrix -
  1. By Observing , it is clear that when i < j Condition is true then and then only we have to add the elements

C Program to find addition of Lower Triangular Elements in C Programming

#include<stdio.h>
#include<conio.h>

void main()
{
int i,j,a[10][10],sum,m,n;

/* m - Number of rows 
   n - Number of Columns */

printf("nEnter the number of Rows : ");
scanf ("%d",&m);

printf("nEnter the number of Columns : ");
scanf ("%d",&n);

/* Accept the Elements in m x n Matrix */

for( i = 0 ; i < m ; i++ )
       for( j = 0 ; j < n ; j++ )
       {
       printf("Enter the Element a[%d][%d] : ", i , j);
       scanf("%d",&a[i][j]);
       }

/* Addition of all Diagonal Elements */

sum = 0;

for( i = 0 ; i < m ; i++ )
       for( j = 0 ; j < n ; j++ )
       {
        if ( i > j )             // Condition for Lower Triangle
        sum = sum + a[i][j];
       }

/* Print out the Result */

printf("nThe Addition of Lower Triangle Elements : %d",sum);

getch();

}

Output :
Enter the number of Rows : 3

Enter the number of Columns : 3

Enter the Element a[0][0] : 1
Enter the Element a[0][1] : 2
Enter the Element a[0][2] : 3
Enter the Element a[1][0] : 2
Enter the Element a[1][1] : 1
Enter the Element a[1][2] : 1
Enter the Element a[2][0] : 1
Enter the Element a[2][1] : 2
Enter the Element a[2][2] : 1

The Addition of Lower Triangle Elements : 5
Explanation : 

Considering above 3×3 matrix  -
  1. By Observing , it is clear that when i > j Condition is true then and then only we have to add the elements

Program to find Transpose of Given Square Matrix

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[10][10],m,i,j,temp;
 /* actual size of matrix is m*n
 i,j  - for scanning of array
 temp - for interchanging of a[i][j] and a[j][i] */

 printf("n Enter the size of matrix :");
 scanf("%d",&m);

 /* Reading elements of matrix */

 printf("n Enter the values a:");

 for(i=0;i<m;i++)
  for(j=0;j<m;j++)
     scanf("%d",&a[i][j]);

 //to print original square matrix

 printf("nGiven square matrix is");

 for(i=0;i<m;i++)
  {
  printf("n");
     for(j=0 ; j 〈 m ; j++)
          printf("%dt",a[i][j]);
 }
 /* Find transpose */

 for(i=1;i<m;i++)
  for(j=0;j<i;j++)
   {
   temp=a[i][j];
   a[i][j]=a[j][i];
   a[j][i]=temp;
   }
 /* printing of all elements of final matrix */

 printf("nTranspose matrix is :");

 for(i=0;i 〈 m;i++)
 {
  printf("n");
  for(j=0;j<m;j++)
      printf("%dt",a[i][j]);
 }
 getch();
}

C Program to Check whether Matrix is Magic Square or Not ?

C Program to Check whether Matrix is Magic Square or Not ?

C Program to Check whether entered matrix is magic square or not ?

What is Magic Square :

  1. A magic square is a simple mathematical game developed during the 1500.
  2. Square is divided into equal number of rows and columns.
  3. Start filling each square with the number from 1 to num ( where num = No of Rows X No of Columns )
  4. You can only use a number once.
  5. Fill each square so that the sum of each row is the same as the sum of each column.
  6. In the example shown here, the sum of each row is 15, and the sum of each column is also 15.
  7. In this Example : The numbers from 1 through 9 is used only once. This is called a magic square.
Magic Square C Program
#include<stdio.h>
#include<conio.h>

void main()
{
int size,a[4][4];
int i,j=0;
int sum,sum1,sum2;
int flag=0;
clrscr();

printf("Enter matrix : ");
for(i=0;i<4;i++)
    {
    for(j=0;j<4;j++)
    scanf("%d",&a[i][j]);
    }

printf("Entered matrix is : nn");
for(i=0;i<4;i++)
{
    printf("n");
        for(j=0;j<4;j++)
        {
        printf("t%d",a[i][j]);
        }
}

//------for diagondal elements---------
sum=0;
for(i=0;i<4;i++)
    for(j=0;j<4;j++)
    {
    if(i==j)
    sum=sum+a[i][j];
    }
//-------------for rows--------------

for(i=0;i<4;i++)
{
    sum1=0;
    {
    for(j=0;j<4;j++)
    sum1=sum1+a[i][j];
    }
    if(sum==sum1)
        flag=1;
    else
        {
        flag=0;
        break;
        }
}
//-------------for colomns----------------
for(i=0;i<4;i++)
{
    sum2=0;
    for(j=0;j<4;j++)
    {
    sum2=sum2+a[j][i];
    }
    if(sum==sum2)
        flag=1;
    else
        {
        flag=0;
        break;
        }
}
//----------------------------------------
if(flag==1)
    printf("nntMagic square");
else
    printf("nntNo magic square");
//-----------------------------------------
getch();
}

Note :

Sum of Row1 = Sum of Row2 [Sum of All Rows must be Same]
Sum of Col1 = Sum of Col2 [Sum of All Cols must be Same]
Sum of Left Diagonal = Sum of Right Diagonal
C Program to Print Square of Each Element of 2D Array Matrix

C Program to Print Square of Each Element of 2D Array Matrix

C Program : C Program to Print Square of Each Element of 2D Matrix

#include<stdio.h>
#include<conio.h>

#define MAX_ROWS 3
#define MAX_COLS 4

void print_square(int [ ] );

void main (void)
{
 int row;
 int num [MAX_ROWS][MAX_COLS] = {
                                {0,1,2,3},
                {4,5,6,7},
                {8,9,10,11}
                };

    for(row=0; row<MAX_ROWS; row++)
            print_square(num[row]);
}
void print_square(int x[ ])
{
    int col;
    for (col = 0; col<MAX_COLS; col++)
        printf ("%dt", x[col] * x[col]);
    printf("n");
}
Output :
0       1       4       9
16      25      36      49
64      81      100     121

Explanation :

Note 1 :
  • Wherever a macro name occurs in Program the Preprocessor Substitutes the code of the macro at that position.
  • Whenever we use variable name instead of Macro it will throw error.
int row=3,column=3;

int arr[row][column];
C Program to Read Array Elements

C Program to Read Array Elements

#include<stdio.h>
#include<conio.h>
void main()
{
 int i,a[50],sum,n;
 printf("n Enter no of elements :");
 scanf("%d",&n);

 /* Reading values into Array */

 printf("n Enter the values :");
 for(i=0;i < n;i++)
 scanf("%d",&a[i]);

 /* computation of total */

 sum=0;
 for(i=0;i < n;i++)
 sum=sum+a[i];

 /* printing of all elements of array */

 for(i=0;i < n;i++)
 printf("n a[%d]=%d",i,a[i]);

 /* printing of total */

 printf("n sum=%d",sum);

getch();
}