Answers for "Finding palindrome numbers"

1

palindrome between two numbers

#include <stdio.h>
#include <math.h>

int is_Palindrome(int num1, int num2)
{
    if (num1 > num2)
    {
        //printf("base condition\n");
        return 0;
    }

    else
    {
        
        if (num1 == reverse(num1))
            printf("%d ", num1);
        num1 = num1 + 1;
    }
    is_Palindrome(num1, num2);
}

int reverse(int num1)
{
    int dig = (int)log10(num1);
    if (num1 == 0)
    {
        // base condition
        return 0;
    }
    else
        return ((num1 % 10 * pow(10, dig)) + reverse(num1 / 10));
}

int main(int argc, char const *argv[])
{
    int num1, num2;
    printf("enter two numbers:\t");
    scanf("%d%d", &num1, &num2);
    is_Palindrome(num1, num2);

    return 0;
}
Posted by: Guest on July-25-2021
1

palindrome number

while(n>0){    
   r=n%10;  //getting remainder  
   sum=(sum*10)+r;    
   n=n/10;    
  }
Posted by: Guest on June-08-2021
0

Finding palindrome numbers

//Finding palindrome numbers

// first convert to string (slow)
fn is_palindrome1(num: i32) -> bool {
    return num.to_string().chars().rev().collect::<String>() == num.to_string();
}

// variation using zip
fn is_palindrome2(num: i32) -> bool {
    let s = num.to_string();
    for (i1, i2) in s.chars().zip(s.chars().rev()) {
        if i1 != i2 {
            return false;
        }
    }
    return true;
}

// divide by 10 (fast)
fn is_palindrome3(num: i32) -> bool {
    let mut reversed = 0;
    let mut number = num;
    while number > 0 {
        reversed = reversed * 10 + number % 10;
        number = number / 10;
    }
    return reversed == num || num == number / 10;
}

fn main() {
    for i in 700..750 {
        if is_palindrome1(i) {
            println!("Pal1: {}", i);
        }
        if is_palindrome2(i) {
            println!("Pal2: {}", i);
        }        
        if is_palindrome3(i) {
            println!("Pal3: {}", i);
        }
    }
}
Posted by: Guest on September-06-2021

Code answers related to "Finding palindrome numbers"

Browse Popular Code Answers by Language