Posts

Showing posts with the label string

Java program to check whether a given number is palindrome or not

import java.util.*; class palindrome  {   public static void main(String s[] ){   try{   Scanner sc =new Scanner(System.in);   System.out.println("Enter number");   int num= sc.nextInt();   int n = num;   int rev=0;   System.out.println("Number: ");   System.out.println(" "+ num);   while(num>0){   int r=num%10;   num=num/10;   rev=rev*10+r;     }   System.out.println("After reversing the number: "+ " ");   System.out.println(" "+ rev);    if(n == rev){   System.out.print("Number is palindrome!");   }   else{   System.out.println("Number is not palindrome!");   }   }   catch(Exception e){   System.out.println("Out of range!");   }   } }

Read a string and check whether it is palindrome or not.

 //Read a string and print the string is palindrome or NOT palindrome. # include <stdio.h> void main() { char a[500],b[500]; clrscr(); printf(" \n Enter A String :-  "); scanf(" %s ",&a); strcpy(b,a); //copies string a to b strrev(b);  //reverses string b if(strcmp(a,b)==0)  //compares if the original and reverse strings are same printf(" \n %s is a palindrome ",a); else printf(" \n %s is not a palindrome ",a); getch(); } //Read a string and print the string is palindrome or NOT palindrome without using inbuilt functions. # include <stdio.h> void main() { char a[500]; int i,b; clrscr(); printf(" \n Enter A String :-  "); scanf(" %s ",&a); b = strlen(a); for (i=0;i<=((b-1)/2);i++) {     if(a[i] != a[b-1-i])     printf(" \n %s is not palindrome ",a); } printf(" \n %s is palindrome ",a); getch(); }