Facebook
Banner
XMPP JavaScript Library READ MORE

Program to check if the given matrix is magic square or not

Program to check if the given matrix is magic square or not

1 /*Program to takes the the data of a 3x3 matrix and check if the given matrix is magic square or not*/
2 #include <stdio.h>
3 #include <conio.h>
4 int num[3][3];
5 int sum[8];
6 int r,c,i;
7
8 /*Function to take the value in matrix from user*/
9 void read_matrix()
10 {
11 for(r=0;r<3; r++)
12 {
13 for(c=0;c<3; c++)
14 {
15 printf("Enter value for %d %d::",r,c);
16 scanf("%d",&num[r][c]);
17 }
18 }
19 }
20
21 /*Function to print matrix and get the sum of rows,cols and diaglons*/
22 void print_calc_rows_cols()
23 {
24 int sum_row,sum_col,ctr=0;
25
26 /*Print the Matrix and get the sum of rows*/
27 for(r=0;r<3; r++)
28 {
29 sum_row=0;
30 for(c=0;c<3; c++)
31 {
32 printf("\t%d",num[r][c]);
33 sum_row=sum_row+num[r][c];
34 }
35 sum[ctr]=sum_row;
36 ctr++;
37 printf(":: %d",sum_row);
38 printf("\n");
39 }
40 printf("\t::\t::\t::\n");
41
42 /*Get the sum of columns*/
43 for(c=0;c<3;c++)
44 {
45 sum_col=0;
46 for(r=0;r<3;r++)
47 sum_col=sum_col+num[r][c];
48
49 sum[ctr]=sum_col;
50 ctr++;
51 printf("\t%d",sum_col);
52 }
53 printf("\n");
54
55 /*Get the sum of diagnols*/
56 sum[6]=num[0][0]+num[1][1]+num[2][2];
57 sum[7]=num[0][2]+num[1][1]+num[2][0];
58
59 }
60
61 /*Function to Check the sum of rows,cols and diagnols*/
62 char check_matrix()
63 {
64 char c;
65 for(i=0;i<3; i++)
66 {
67 if(sum[i]==sum[i+1])
68 c='y';
69 else
70 {
71 c='n';
72 break;
73 }
74 }
75 return c;
76 }
77
78 void main()
79 {
80 char c;
81 clrscr();
82 read_matrix();
83 print_calc_rows_cols();
84 c=check_matrix();
85
86
87 /*Print the result*/
88 if(c=='y')
89 printf("\nThis Matrix is a Magic Square");
90 else
91 printf("\nThis Matrix is Not a Magic Square");
92
93 getch();
94 }
Add Your Comment
   
    Yes! I want to receive all comments by email

  by marikani on 15-Dec-2015 03:50 pm
very bad.....how length it is......
  • Reply
  •  1 Like
  •  1 Dislike
  • Report
  by Dhananjay Shukla on 01-Oct-2015 12:02 am
Thaank U Soo Much
  • Reply
  •  1 Like
  •  1 Dislike
  • Report
  by charan on 28-Feb-2015 01:27 pm
but this pgm is long
  • Reply
  •  1 Like
  •  1 Dislike
  • Report
  by deepak dahiya on 07-Oct-2013 10:15 am
thank you sooooooo much buddy
  • Reply
  •  3 Like
  •  2 Dislike
  • Report
  by yashwi on 18-Apr-2013 10:06 am
modify the program using file concept....write a program to input a string and output the reverse string ..i.e. if "usf" is input , the program has a output "fsu"use pointer notation ...
  • Reply
  •  2 Like
  •  2 Dislike
  • Report
  by satheesh kumar on 16-Jul-2012 05:30 pm
thankz a lot friend for giving the solution:)
  • Reply
  •  3 Like
  •  2 Dislike
  • Report
  by um... on 28-Nov-2010 03:24 am
Does it check the numbers are different (1 through 9)?
  • Reply
  •  3 Like
  •  3 Dislike
  • Report
  by Sachin Puri on 29-Nov-2010 10:37 am
No it don't check that
  • Reply
  •  5 Like
  •  5 Dislike
  • Report
  by Bhrigu kumar das. on 11-Jun-2011 05:28 am
thanx for the program.it is good to me.i like it.
  • Reply
  •  3 Like
  •  122 Dislike
  • Report