Pages

Thursday, January 2, 2025

Palindromic Number checker with C

 Mistakes:- 

  1. 1. i forgot to put } before "else" statement.
  2. Logic: Store original, then compare original == reversed.
  1. Key Operations: Use % to extract digits, / to remove digits, and *10 to build reversed.
  2. Important: Preserve original for comparison.


Tip:-  Always close if block with } before starting an else block.

correct:- if (condition) { /* Code */ } else { /* Code */ }


#include <stdio.h>

int main() {
    int n, original, reversed = 0, remainder;

    printf("Enter the integer: ");  // Input the integer value
    scanf("%d", &n);

    // Store the original value of n
    original = n;

    // Get the reversed value of the number
    while (n != 0) {
        remainder = n % 10;          // Get the last digit
        reversed = reversed * 10 + remainder;  // Build the reversed number
        n /= 10;                     // Remove the last digit
    }

    // Check if the original number is equal to the reversed number
    if (original == reversed) {
        printf("The number is a Palindrome.\n");
    } else {
        printf("The number is not a Palindrome.\n");
    }

    return 0;
}






No comments:

Post a Comment

3917. Count Indices With Opposite Parity (Brute Force) O(n2) + Optimized Solution O(n) + tips LEETCODE WEEKLY 500

  class Solution {     public int [] countOppositeParity ( int [] nums ) {          // approach 1 - checks every pair         int n = n...