Answer to Question #285811 in C for santosh reddy

Question #285811


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.




1
Expert's answer
2022-01-09T10:43:57-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS