program to find the magic square using array



program to find the magic square using array..

Answer / sneh nagaonkar

/*Program to takes the the data of a 3x3 matrix and check if the given matrix is magic

square or not*/
#include <stdio.h>
#include <conio.h>
int num[3][3];
int sum[8];
int r,c,i;

/*Function to take the value in matrix from user*/
void read_matrix()
{
for(r=0;r<3; r++)
{
for(c=0;c<3; c++)
{
printf("Enter value for %d %d::",r,c);
scanf("%d",&num[r][c]);
}
}
}

/*Function to print matrix and get the sum of rows,cols and diaglons*/
void print_calc_rows_cols()
{
int sum_row,sum_col,ctr=0;

/*Print the Matrix and get the sum of rows*/
for(r=0;r<3; r++)
{
sum_row=0;
for(c=0;c<3; c++)
{
printf("\t%d",num[r][c]);
sum_row=sum_row+num[r][c];
}
sum[ctr]=sum_row;
ctr++;
printf(":: %d",sum_row);
printf("\n");
}
printf("\t::\t::\t::\n");

/*Get the sum of columns*/
for(c=0;c<3;c++)
{
sum_col=0;
for(r=0;r<3;r++)
sum_col=sum_col+num[r][c];

sum[ctr]=sum_col;
ctr++;
printf("\t%d",sum_col);
}
printf("\n");

/*Get the sum of diagnols*/
sum[6]=num[0][0]+num[1][1]+num[2][2];
sum[7]=num[0][2]+num[1][1]+num[2][0];

}

/*Function to Check the sum of rows,cols and diagnols*/
char check_matrix()
{
char c;
for(i=0;i<3; i++)
{
if(sum[i]==sum[i+1])
c='y';
else
{
c='n';
break;
}
}
return c;
}

void main()
{
char c;
clrscr();
read_matrix();
print_calc_rows_cols();
c=check_matrix();


/*Print the result*/
if(c=='y')
printf("\nThis Matrix is a Magic Square");
else
printf("\nThis Matrix is Not a Magic Square");

getch();
}

Is This Answer Correct ?    10 Yes 4 No

Post New Answer

More C++ Code Interview Questions

Write a simple encryption program using string function which apply the substitution method.

0 Answers  


Write a program to enter 10 number of integer entries into an array n and then odds up all the odd entries. the program then displays the result. plssss answer assss fast asss u can...

1 Answers  


i don't know about working of nested for loop can any one help me

0 Answers  


swap prog

3 Answers   TCS,


main(){int a=5,b 10,c=2, d;a=b c;d=++a=(--c)*2; printf("%d%d%d%d,a,b,c,d; return o;}

1 Answers  






Wrie a function which returns the most frequent number in a list of integers. Handle the case of more than one number which meets this criterion. public static int[] GetFrequency(int[] list)

3 Answers   Nagarro,


What is the time complexity T(n) of the following program? a) int n, d, i, j; cin >> n; for (d=1; d<=n; d++) for (i=1; i<=d; i++) for (j=1; j<=n; j += n/10) cout << d << " " << i << " " << j << endl; b) void main() { int n, s, t; cin >> n; for (s = 1; s <= n/4; s++) {t = s; while (t >= 1) { cout << s << " " << t << endl; t--; } } } c) void main() { int n, r, s, t; cin >> n; for (r = 2; r <= n; r = r * 2) for (s = 1; s <= n/4; s++) { t = s; while (t >= 1) { cout << s << " " << t << endl; t--; } } }

3 Answers   CTS, IBM, Infosys, Qatar University,


write a function – oriented program that calculates the sum of the squares from 1 to n. thus, if the input is 3, the output is 14

3 Answers  


#include<stdio.h> #include<conio.h> void main() { char str[10]; int,a,x,sw=0; clrscr(); printf("Enter a string:"); gets(str); for(x=0;x<=a;a++); for(x=0;x<=a;x++) { if(str[x]==str[a-1-x]) { sw=1; } else sw=0; } if(sw==10 printf("The entered string is palindrome:"); else printf("The entered string is not a palindrome:); } getch(); } wht would be the explanation with this written code???

2 Answers  


how to write a program that opens a file and display in reverse order?

0 Answers   SMC,


readers and writers problem

1 Answers   Cognizant,


Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the total number of disk writes by MySQL.

0 Answers  


Categories