Check a number is palindrome or not

PALINDROME:

A palindrome is a word, phrase, or sequence that reads the same backward as forward. A number palindrome is a numerical palindrome, where the digits read the same when reversed.

For example, 12321 is a palindrome whereas 12345 is not a palindrome.


PROGRAM:

#include <stdio.h>

int main()

{

    int n,m,rem,rev=0;

    printf("Enter any number: ");

    scanf("%d",&n);

    m=n;

    while(n!=0)

    {

        rem=n%10;

        rev=rev*10+rem;

        n=n/10;

    }

    if(rev == m)

    printf("%d is a palindrome", m);

    else

    printf("%d is not a palindrome",m);

    return 0;

}


TIME COMPLEXITY:

The time complexity of the program is indeed O(log n) because the number of iterations in the while loop is proportional to the number of digits in the input number 'n'. As the number of digits increases, the number of iterations required to reverse the number also increases logarithmically.


SPACE COMPLEXITY:

The space complexity is indeed O(1) because the amount of extra space used remains constant regardless of the input size. Only a few integer variables are used to store the input number, reversed number, remainder, and loop control variables, which doesn't depend on the size of the input.


Positive test case



Negative test case





Comments

Popular posts from this blog

Swap two numbers without using third variable

Program to check leap year