1
(a)Read a matrix of order M X N
(b)Display the matrix of order M X N.
(c)Write a function check Palindrome which takes as a parameter an integer and returns true if its palindrome and false if not.
(d)Use the above function to replace all palindrome numbers in the given
matrix by 1
’(e) Display the resultant in matrix form.
#include <stdio.h>
#include <stdbool.h>
#define M 3
#define N 3
// Write a function check Palindrome which takes as a parameter an integer and returns true if its palindrome and false if not.
bool IsPalindrome(int value) {
int copy = value;
int reversed = 0;
while (copy) {
int remainder = copy % 10;
reversed = reversed * 10 + remainder;
copy /= 10;
}
return reversed == value;
}
int main() {
int matrix[M][N];
// Read a matrix of order M X N
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Display the matrix of order M X N.
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
// Use the above function to replace all palindrome numbers in the given matrix by 1
if (IsPalindrome(matrix[i][j])) {
matrix[i][j] = 1;
}
}
}
// Display the matrix of order M X N.
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Comments
Leave a comment