Thursday, 6 June 2013

Program for check the number is palindroe or not in c#


The palindrome number is the number which has the reverse number of it self is the same as the main number is called the palindrome number.
For example 151,232,454 numbers are the palindrome number.

Here in palindrome number's logic first we take the while loop for the number grater then the zero.
The palindrome number is always grater then the zero if not then the while loop is stopped.

In the while loop the main number(which is we check it is palindrome or not) is put into the num integer variable and it is initially declare by the user using the console.Readline(); this command is read the number and put as the string. for convert the number into the string to the integer hear we used the another command convert.Toint32(); this command convert the String value into the integer and the integer value is stored into the num variable.

now in the while loop one new intiger variable temp is stored the last part of the number like if our number is 151 then in first loop 1 is stored into the temp after it in second command the onother rev intiger variable stored temp + rev*10 value.and after it num is divided by the 10 for the make number shorter this while loop is execuit when the number is lessthen the zero.

If the Rev variable's value is same as the number so it is palindrome number and if not then it is not palindrome number. which is execute by the if - else statement.

at last we used Console.Read(); for the read the output  screen.

code for the palindrome number in C#:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HHH
{
    class palindrome   

 {
         static void Main(string[] args)
        {
        restart:
       
            int num, temp, rev = 0,temp2;
            Console.WriteLine("Enter the value");
            num = Convert.ToInt32(Console.ReadLine());
            temp2 = num;
            while (num >0)
            {
                temp = num % 10;
                rev=rev * 10+ temp;
                num = num / 10;               

            }
            if (temp2 == rev)
            {
                Console.WriteLine("Enter number is pelindrome");
                goto restart;
            }

            else
            {
                Console.WriteLine("Enter number is not pelindrome");
                goto restart;
            }

            Console.Read();
        }

    }
}
   
If you copy and pest the code into visual stdio 2010 so the output willbe see as below:
Palindrome or not in C#
  
 here we check different different vales if entered value is palindrome so it called the palindrome and not then its calls not palindrome.

Program for the check number is palindrome or not in JAVA:


same as the c# logic we used hear but the difference is  only syntax of the java or c#.

Code for the check number is palindrome or not in JAVA:

package test;

import java.util.Scanner;

public class palindrome {
   
    public static void main(String[] args) {
             
          int num, temp, rev = 0,temp2; 
          System.out.println("Enter the any number");
          Scanner in = new Scanner(System.in);
          num = in.nextInt();
          temp2 = num;
        
           while (num >0)
            {
                temp = num % 10;
                rev=rev * 10+ temp;
                num = num / 10;               

            }
            if (temp2 == rev)
            {
                System.out.println("Enter number is palindrome");
            }

            else
            {
                System.out.println("Enter number is not palindrome");
            }
         
    }
}

Output:

For palindrome:
Palindrome or not in JAVA




for not palindrome:
Palindrome or not in JAVA


Program for check the number is palindrome or not in C Language:


The same logic is used as used above in c# and the java language we used just syntax is different.

code for to check number is palindrome or not in C language :


 #include<stdio.h>
 #include<conio.h>

 void main()
 {

       int  num, temp, rev = 0,temp2;
        clrscr();

        printf("Enter the value");
        scanf ("%d",&num);

        temp2 = num;
        while (num >0)
        {
        temp = num % 10;
        rev=rev * 10+ temp;
        num = num / 10;

        }
        if (temp2 == rev)
        {
          printf("Enter number is pelindrome");

        }

        else
        {
        printf("Enter number is not pelindrome");

         }

         getch();
  }
 

  Output:


For palindrome:
 
Palindrome or not in C

    For not Palindrome:


1 comments:

Post a Comment