- /*
- Reverse Number using Java
- This Java Reverse Number Example shows how to reverse a given number.
- */
- public class ReverseNumber {
- public static void main(String[] args) {
- //original number
- int number = 1234;
- int reversedNumber = 0;
- int temp = 0;
- while(number > 0){
- //use modulus operator to strip off the last digit
- temp = number%10;
- //create the reversed number
- reversedNumber = reversedNumber * 10 + temp;
- number = number/10;
- }
- //output the reversed number
- System.out.println("Reversed Number is: " + reversedNumber);
- }
- }
- /*
- Output of this Number Reverse program would be
- Reversed Number is: 4321
- */
Monday, 28 October 2013
Reverse Number program
Java Palindrome Number Example
- /*
- Java Palindrome Number Example
- This Java Palindrome Number Example shows how to find if the given
- number is palindrome number or not.
- */
- public class JavaPalindromeNumberExample {
- public static void main(String[] args) {
- //array of numbers to be checked
- int numbers[] = new int[]{121,13,34,11,22,54};
- //iterate through the numbers
- for(int i=0; i < numbers.length; i++){
- int number = numbers[i];
- int reversedNumber = 0;
- int temp=0;
- /*
- * If the number is equal to it's reversed number, then
- * the given number is a palindrome number.
- *
- * For example, 121 is a palindrome number while 12 is not.
- */
- //reverse the number
- while(number > 0){
- temp = number % 10;
- number = number / 10;
- reversedNumber = reversedNumber * 10 + temp;
- }
- if(numbers[i] == reversedNumber)
- System.out.println(numbers[i] + " is a palindrome number");
- else
- System.out.println(numbers[i] + " is not a palindrome number");
- }
- }
- }
- /*
- Output of Java Palindrome Number Example would be
- 121 is a palindrome number
- 13 is not a palindrome number
- 34 is not a palindrome number
- 11 is a palindrome number
- 22 is a palindrome number
- 54 is not a palindrome number
- */
Logic to checking Even Odd program in java
- /*
- Even Odd Number Example
- This Java Even Odd Number Example shows how to check if the given
- number is even or odd.
- */
- public class FindEvenOrOddNumber {
- public static void main(String[] args) {
- //create an array of 10 numbers
- int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10};
- for(int i=0; i < numbers.length; i++){
- /*
- * use modulus operator to check if the number is even or odd.
- * If we divide any number by 2 and reminder is 0 then the number is
- * even, otherwise it is odd.
- */
- if(numbers[i]%2 == 0)
- System.out.println(numbers[i] + " is even number.");
- else
- System.out.println(numbers[i] + " is odd number.");
- }
- }
- }
- /*
- Output of the program would be
- 1 is odd number.
- 2 is even number.
- 3 is odd number.
- 4 is even number.
- 5 is odd number.
- 6 is even number.
- 7 is odd number.
- 8 is even number.
- 9 is odd number.
- 10 is even number.
- */
Subscribe to:
Posts (Atom)