void main()
{
int n, col, row, i, j = 0, k, r, c, diag = 0, sum, found, search_value, choice, nn, summ, R, h;
int magic[mag][mag];
printf("*****************************\n");
printf("Welcome to the world of magic\n");
printf("Enter an odd number < 500:\n");
printf("*****************************\n");
scanf_s("%d", &n);
sum = (n*n*n + n) / 2;
printf("%d has a sum of %d in each row,column and diagonal!\n", n, sum);
printf("Here is the %d x %d magic sqaure:\n", n, n);
getchar();
if (n % 2 == 0)
{
printf("Please enter odd number!!");
getchar();
getchar();
return;
}
if (n<3)
{
printf("Please enter an odd number more than 3!!");
getchar();
return;
}
for (j = 0; j< n + 1;j++)
for (k = 0; k < n + 1; k++)
magic[j][k] = 0;
row = 0; col = n / 2;
magic[row][col] = 1;
for (i = 2; i <= n*n; i++)
{
if (--row < 0)
row = (n - 1);
if (++col>(n - 1))
col = 0;
if (magic[row][col] != 0)
{
if (++row > (n - 1))
row = 0;
if (--col < 0)
col = n - 1;
while (magic[row][col]!=0)
if (++row>(n - 1))
row = 0;
}
magic[row][col] = i;
}
for (r = 0; r < n; r++)//sum of row
{
for (c = 0; c < n; c++)
{
magic[r][n] += magic[r][c];
}
}
for (c = 0; c < n; c++)//sum of col
{
for (r = 0; r < n; r++)
{
magic[n][c] += magic[r][c];
}
}
for (r = 0; r < n; r++)//sum of diag
{
magic[n][n] += magic[r][r];
}
for (r = 0; r < n; r++)
{
diag += magic[r][c];
}
printf("\n");
for (r = 0; r < n + 1; r++)
{
printf("\n");
for (c = 0; c < n + 1; c++)
printf("%5d", magic[r][c]);
}
printf("\n\nEnter the value to be searched:");//search inter
scanf_s("%d",&search_value);
found = 0;
for (j = 0; j < mag; j++)
{
for (k = 0; k < mag; k++)
{
if (magic[j][k] == search_value)
{
found = 1;
break;
}
}
}
if (found == 1)
printf("This value is found in %d x %d magic sqaure\n ",n,n);// print out
else
printf("This value is not found\n");
printf("Do you want to continue(1:yes 2:no)?\n");// ask user to cont
scanf_s("%d", &choice);
if (choice == 1)
{
printf("*********************************\n");
printf("Welcome to the world of magic again!\n");
printf("Enter an odd number < 500:\n");
printf("*********************************\n");
scanf_s("%d", &nn);
summ = (nn*nn*nn + nn) / 2;
printf("%d has a sum of %d in each row,column and diagonal!\n", nn, summ);
printf("Here is the %d x %d magic sqaure:\n", nn, nn);
getchar();
if (nn % 2 == 0)
{
printf("Please enter odd number!!");
getchar();
getchar();
return;
}
if (nn<3)
{
printf("Please enter an odd number more than 3!!");
getchar();
return;
}
for (j = 0; j < nn + 1; j++)
for (k = 0; k < nn + 1; k++)
magic[j][k] = 0;
row = 0; col = nn / 2;
magic[row][col] = 1;
for (i = 2; i <= nn*nn; i++)
{
if (--row < 0)
row = (nn - 1);
if (++col>(nn - 1))
col = 0;
if (magic[row][col] != 0)
{
if (++row > (nn - 1))
row = 0;
if (--col < 0)
col = nn - 1;
while (magic[row][col] != 0)
if (++row>(nn - 1))
row = 0;
}
magic[row][col] = i;
}
for (r = 0; r < nn; r++)
{
for (c = 0; c < nn; c++)
{
magic[r][nn] += magic[r][c];
}
}
for (c = 0; c < nn; c++)
{
for (r = 0; r < nn; r++)
{
magic[nn][c] += magic[r][c];
}
}
for (r = 0; r < nn; r++)
{
magic[nn][nn] += magic[r][r];
}
for (r = 1; r < (nn - 1); r++)
{
c = nn - r + 1;
diag += magic[r][c];
}
printf("\n");
for (r = 0; r < nn + 1; r++)
{
printf("\n");
for (c = 0; c < nn + 1; c++)
printf("%5d", magic[r][c]);
}
printf("\n\nEnter the value to be searched:");
scanf_s("%d", &search_value);
found = 0;
for (row = 0; row < mag; row++)
{
for (col = 0; col < mag; col++)
{
if (magic[row][col] == search_value)
{
found = 1;
break;
}
}
}
if (found == 1)
printf("This value is found in %d x %d magic sqaure\n ", nn, nn);
else
printf("This value is not found\n");
printf("You learn fast!\n");
printf("Let's call it a day!");
getchar();
getchar();
return;
}
if (choice == 2)
{
printf("BYEEEEE!!!", choice);
getchar();
getchar();
return;
}
else
{
if (choice >= 3)
{
printf("ERROR!!", choice);
getchar();
getchar();
return;
}
else
{
printf("ERROR 2!!", choice);
getchar();
getchar();
return;
}