Coding Practice
Showing posts with label Java Programming Practice. Show all posts
Showing posts with label Java Programming Practice. Show all posts

Write a Java program to generate unique random numbers

Generate Unique Random Numbers
Sample Output
Enter a limit of random number: 10
1
5
2
4
9
3
10
6
8
7
Source Code
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Main {

    public static boolean isDuplicate(ArrayList<Integer> numbers, int randomNumber){
        
        for (int i = 0; i < numbers.size(); i++) {
            
            if (numbers.get(i) == randomNumber) {
                
                return true;
            }
        }
        
        return false;
    }
    
    public static void main(String args[]) {

        Random random = new Random();

        int uniqueNumberCount = 0, randomNumber, limit;
        
        ArrayList<Integer> pastNumber = new ArrayList<>();
        
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter a limit of random number: ");
        limit = in.nextInt();
        
        for (int i = 0; uniqueNumberCount < limit; i++) {
            
            randomNumber = random.nextInt(limit) + 1;
            
            if (!isDuplicate(pastNumber, randomNumber)) {
                
                System.out.println(randomNumber);
                
                pastNumber.add(uniqueNumberCount++, randomNumber);
            }
        }

    }
}
Sample Output
Enter a limit of random number: 5
5
4
2
1
3

Java Datatypes | HackerRank

Java Datatypes | HackerRank

Java has 8 primitive data types; char, boolean, byte, short, int, long, float, and double. For this exercise, we'll work with the primitives used to hold integer values (byte, short, int, and long):

  • A byte is an 8-bit signed integer.
  • A short is a 16-bit signed integer.
  • An int is a 32-bit signed integer.
  • A long is a 64-bit signed integer.

Given an input integer, you must determine which primitive data types are capable of properly storing that input.

To get you started, a portion of the solution is provided for you in the editor.

Reference: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Input Format

The first line contains an integer, T, denoting the number of test cases.

Each test case, T, is comprised of a single line with an integer, n, which can be arbitrarily large or small.

Output Format

For each input variable n and appropriate primitive dataType, you must determine if the given primitives are capable of storing it. If yes, then print:

n can be fitted in:
* dataType

If there is more than one appropriate data type, print each one on its own line and order them by size (i.e.: byte < short < int < long).

If the number cannot be stored in one of the four aforementioned primitives, print the line:

n can't be fitted anywhere.

Sample Output
5
-150
150000
1500000000
213333333333333333333333333333333333
-100000000000000
-150 can be fitted in:
* short
* int
* long
150000 can be fitted in:
* int
* long
1500000000 can be fitted in:
* int
* long
213333333333333333333333333333333333 can't be fitted anywhere.
-100000000000000 can be fitted in:
* long
Source Code
import java.util.*;
import java.io.*;

class Solution{
    public static void main(String []argh)
    {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        for(int i = 0; i < t; i++)
        {
            try
            {
                long x = sc.nextLong();

                System.out.println(x + " can be fitted in:");
                if(x >= -128 && x <= 127){
                    System.out.println("* byte");
                }
                if(x >= -32768 && x <= 32767){
                    System.out.println("* short");
                }
                if(x >= -2147483648 && x <= 2147483647){
                    System.out.println("* int");
                }
                if(x >= -9.22337204e18 && x <= 9.22337204e18){
                    System.out.println("* long");
                }
            }
            catch(Exception e)
            {
                System.out.println(sc.next() + " can't be fitted anywhere.");
            }

        }
    }
}
Sample Output
5
-150
150000
1500000000
213333333333333333333333333333333333
-100000000000000
-150 can be fitted in:
* short
* int
* long
150000 can be fitted in:
* int
* long
1500000000 can be fitted in:
* int
* long
213333333333333333333333333333333333 can't be fitted anywhere.
-100000000000000 can be fitted in:
* long

Write a Java program to check whether a number is Armstrong number or not

Check whether a number is Armstrong number or not
Sample Output
Check whether a number is Armstrong number or not.

Enter a number: 371
371 is an Armstrong number!

Java-Source Code
//package loopinjava;

import java.util.Scanner;
import static java.lang.Math.pow;

public class Main {

    public static void main(String[] args) {

        int i, j, digit, number, numberClone, count = 0, newNumber = 0, temp;

        Scanner input = new Scanner(System.in);

        System.out.println("Check whether a number is Armstrong number or not.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();

        numberClone = number;
        
        while (number != 0) {
            digit = (number % 10);
            number /= 10; /// Or, number = number / 10;
            count++;
        }

        number = numberClone;
        while (number != 0) {
            digit = (number % 10);
            temp = (int) pow(digit, count);
            newNumber += temp;
            number /= 10; /// Or, number = number / 10;
        }

        number = numberClone;
        
        if (newNumber == number) {
            
            System.out.println(number + " is an Armstrong number!\n\n");
            
        } else {
            
            System.out.println(number + " is not an Armstrong number.\n\n");
        }
    }
}
Sample Output
Check whether a number is Armstrong number or not.

Enter a number: 372
372 is not an Armstrong number.

Write a Java program to print all Perfect numbers between 1 to n

Print all Perfect numbers between 1 to n
Sample Output
Print all Perfect numbers between 1 to n.

Enter a number: 10000
6
28
496
8128
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main33 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int i, j, number, newNumber = 0;
        System.out.println("Print all Perfect numbers between 1 to n.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();

        for (i = 1; i < number; i++) {
            newNumber = 0;

            for (j = 1; j < i; j++) {
                
                if (i % j == 0) {
                    newNumber = newNumber + j; //Or, newNumber += j;
                }
            }
            if (i == newNumber) {
                System.out.println(i);
            }
        }
    }
}
Sample Output
Print all Perfect numbers between 1 to n.

Enter a number: 100
6
28

Write a Java program to check whether a number is Perfect number or not

Check whether a number is Perfect number or not
Sample Output
Check whether a number is Perfect number or not.

Enter a number: 6
6 is a perfect number.

Java-Source Code
//package loopinjava;

import java.util.Scanner;
import static java.lang.Math.pow;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int n, number, newNumber = 0;
        System.out.println("Check whether a number is Perfect number or not.\n");
        
        System.out.print("Enter a number: ");
        number = input.nextInt();

        for (n = 1; n < number; n++) {
            if (number % n == 0) {
                newNumber = newNumber + n; //Or, newNumber += n;
            }
        }
        if (number == newNumber) {
            System.out.println(number + " is a perfect number.\n");
        } else {
            System.out.println(number + " is not a perfect number.\n");
        }
    }
}
Sample Output
Check whether a number is Perfect number or not.

Enter a number: 5
5 is not a perfect number.

Write a Java program to find all prime factors of a number

Find all prime factors of a number
Sample Output
Find all prime factors of a number.

Enter a number: 15
Prime factors of 15: 3 5 
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int i, j, number, isPrime;

        Scanner input = new Scanner(System.in);

        System.out.println("Find all prime factors of a number.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();

        System.out.print("Prime factors of " + number + ": ");
        for (i = 1; i <= number; i++) {

            if (number % i == 0) {
                isPrime = 1;
                for (j = 2; j < i; j++) {
                    if (i % j == 0) {
                        isPrime = 0;
                    }
                }
                if (isPrime == 1) {
                    if (i == 1) {
                        /**
                         * 1 is not a prime number
                         */
                    } else {
                        System.out.print(i + " ");
                    }
                }
            }
        }
        System.out.println();
    }
}
Sample Output
Find all prime factors of a number.

Enter a number: 10
Prime factors of 10: 2 5

Write a Java program to print all Prime numbers between 1 to n

Print all Prime numbers between 1 to n
Sample Output
Print all Prime numbers between 1 to n.

Enter a number: 11
Prime numbers are:
2
3
5
7
11
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int i, j, number;

        Scanner input = new Scanner(System.in);

        System.out.println("Print all Prime numbers between 1 to n.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();

        System.out.println("Prime numbers are:");
        for (i = 1; i <= number; i++) {
            
            int isPrime = 1;
            for (j = 2; j < i; j++) {
                if (i % j == 0) {
                    isPrime = 0;
                }
            }
            if (isPrime == 1) {
                if (i == 1) {
                    /**
                     * 1 is not a prime number
                     */
                } else {
                    System.out.println(i);
                }
            }
        }
    }
}
Sample Output
Print all Prime numbers between 1 to n.

Enter a number: 21
Prime numbers are:
2
3
5
7
11
13
17
19

Write a Java program to find LCM of two numbers

Find LCM of two numbers
Sample Output
Find LCM of two numbers.

Enter first number: 120
Enter second number: 72
GCD: 24
LCM: 360
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int dividend, divisor, remainder = 1, lcm, dividendClone, divisorClone;

        Scanner input = new Scanner(System.in);

        System.out.println("Find LCM of two numbers.\n");

        System.out.print("Enter first number: ");
        dividend = input.nextInt();

        System.out.print("Enter second number: ");
        divisor = input.nextInt();
        
        dividendClone = dividend;
        divisorClone = divisor;
        
        while (remainder != 0) {
            remainder = dividend % divisor;
            dividend = divisor;
            divisor = remainder;
        }
        System.out.print("GCD: " + dividend + "\n");
        
        lcm = (dividendClone * divisorClone) / dividend;
 
        System.out.println("LCM: " + lcm + "\n");
    }
}
Sample Output
Find LCM of two numbers.

Enter first number: 12
Enter second number: 30
GCD: 6
LCM: 60

Write a Java program to check whether a number is Prime number or not

Check whether a number is Prime number or not
Sample Output
Check whether a number is Prime number or not.

Enter a number: 7
7 is prime a number.
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main26 {

    public static void main(String[] args) {

        int n, number, isPrime = 1;

        Scanner input = new Scanner(System.in);

        System.out.println("Check whether a number is Prime number or not.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();

        for (n = 2; n < number; n++) {
            
            if (number % n == 0) {
                isPrime = 0;
            }
        }
        if (isPrime == 1) {
            
            System.out.println(number + " is prime a number.\n");
            
        } else {
            
            System.out.println(number + " is prime not a number.\n");
        }
    }
}
Sample Output
Check whether a number is Prime number or not.

Enter a number: 21
21 is prime not a number.

Write a Java program to find sum of all prime numbers between 1 to n

find sum of all prime numbers between 1 to n
Sample Output
Find sum of all prime numbers between 1 to n.

Enter a number: 19
2
3
5
7
11
13
17
19
Sum of all prime numbers between 1 to 19 = 77
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int i, j, number, sum = 0, isPrime;

        Scanner input = new Scanner(System.in);

        System.out.println("Find sum of all prime numbers between 1 to n.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();

        for (i = 1; i <= number; i++) {
            
            isPrime = 1;
            for (j = 2; j < i; j++) {
                if (i % j == 0) {
                    isPrime = 0;
                }
            }
            if (isPrime == 1) {
                if (i == 1) {
                    /**
                     * 1 is not a prime number
                     */
                } else {
                    System.out.println(i);
                    sum += i; // sum = sum + i;
                }
            }
        }
        System.out.println("Sum of all prime numbers between 1 to " + number + " = " + sum);
    }
}
Sample Output
Find sum of all prime numbers between 1 to n.

Enter a number: 7
2
3
5
7
Sum of all prime numbers between 1 to 7 = 17

Write a Java program to find HCF (GCD) of two numbers

Find HCF (GCD) of two numbers
Sample Output
Find HCF (GCD) of two numbers.

Enter first number: 30
Enter second number: 12
GCD: 6
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int dividend, divisor, remainder = 1;

        Scanner input = new Scanner(System.in);

        System.out.println("Find HCF (GCD) of two numbers.\n");

        System.out.print("Enter first number: ");
        dividend = input.nextInt();

        System.out.print("Enter second number: ");
        divisor = input.nextInt();

        while (remainder != 0) {
            remainder = dividend % divisor;
            dividend = divisor;
            divisor = remainder;
        }
        System.out.print("GCD: " + dividend + "\n");
    }
}
Sample Output
Find HCF (GCD) of two numbers.

Enter first number: 153
Enter second number: 81
GCD: 9

Write a Java program to calculate factorial of a number

Calculate factorial of a number
Sample Output
Calculate factorial of a number.

Enter a number: 5

Factorial of 5 = 120
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int n, number, fact = 1;

        Scanner input = new Scanner(System.in);

        System.out.println("Calculate factorial of a number.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();

        for (n = 1; n <= number; n++) {
            
            fact = fact * n;
        }
        System.out.print("\nFactorial of " + number + " = " + fact + "\n");
    }
}
Sample Output
Calculate factorial of a number.

Enter a number: 4

Factorial of 4 = 24

Write a Java program to find all factors of a number

Find all factors of a number
Sample Output
Find all factors of a number.

Enter a number: 12
Factors are: 1 2 3 4 6 12
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int i, number = 100;

        Scanner input = new Scanner(System.in);

        System.out.println("Find all factors of a number.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();

        System.out.print("Factors are: ");
        for (i = 1; i <= number; i++) {
            
            if ((number % i) == 0) {
                
                System.out.print(i + " ");
            }
        }
    }
}
Sample Output
Find all factors of a number.

Enter a number: 20
Factors are: 1 2 4 5 10 20

Write a Java program to enter a number and print it in words

Enter a number and print it in words
Variable declaration
  • 'number' is input number.
  • 'digit' is every single digit. i.e number = 123, digit = 1, 2 and 3.
  • 'revNumber' is numbers reverse form.
  • 'lastZero' means if the numbers last digit is 0 (ZERO).
Sample Output
Enter a number and print it in words.

Enter a number: 2580
Words: Two Five Eight Zero
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int number, digit, revNumber = 0, lastZero = 0;

        Scanner input = new Scanner(System.in);

        /**
         * Variable declaration
         * -----------------------
         * 'number' is input number.
         * 'digit' is every single digit. i.e number = 123, digit = 1, 2 and 3.
         * 'revNumber' is numbers reverse form.
         * 'lastZero' means if the numbers last digit is 0 (ZERO).
         */
        System.out.println("Enter a number and print it in words.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();

        if (number % 10 == 0) {
            lastZero = 1;
        }
        while (number >= 10) {
            
            digit = number % 10;
            revNumber = (revNumber * 10) + digit;
            number = (number / 10);
        }
        revNumber = (revNumber * 10) + number;
        number = revNumber;

        System.out.print("Words: ");
        while (number != 0) {
            
            digit = number % 10;
            number = number / 10;
            switch (digit) {
                case 0:
                    System.out.print("Zero ");
                    break;
                case 1:
                    System.out.print("One ");
                    break;
                case 2:
                    System.out.print("Two ");
                    break;
                case 3:
                    System.out.print("Three ");
                    break;
                case 4:
                    System.out.print("Four ");
                    break;
                case 5:
                    System.out.print("Five ");
                    break;
                case 6:
                    System.out.print("Six ");
                    break;
                case 7:
                    System.out.print("Seven ");
                    break;
                case 8:
                    System.out.print("Eight ");
                    break;
                case 9:
                    System.out.print("Nine ");
                    break;
            }
        }
        if(lastZero == 1)
        {
            System.out.print("Zero");
        }
        System.out.println();
    }
}
Sample Output
Enter a number and print it in words.

Enter a number: 19224
Words: One Nine Two Two Four

Write a Java program to find power of a number using for loop

Find power of a number using for loop
Sample Output
Find power of a number using for loop.

Enter a number: 2
Enter a number: 5
2 Power 5 = 32
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int i, number, power, result = 1;

        Scanner input = new Scanner(System.in);

        System.out.println("Find power of a number using for loop.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();

        System.out.print("Enter a number: ");
        power = input.nextInt();

        for (i = 0; i < power; i++) {
            result = result * number; // result *= number;
        }
        System.out.println(number + " Power " + i + " = " + result);
    }
}
Sample Output
Find power of a number using for loop.

Enter a number: 4
Enter a number: 4
4 Power 4 = 256

Write a Java program to print all ASCII character with their values

Print all ASCII character with their values
Sample Output
Print all ASCII character with their values.


ASCII value of character   =  0

ASCII value of character ☺ =  1

ASCII value of character ☻ =  2

ASCII value of character ♥ =  3

ASCII value of character ♦ =  4

ASCII value of character ♣ =  5

ASCII value of character ♠ =  6

ASCII value of character  =  7

ASCII value of character =  8

ASCII value of character         =  9

ASCII value of character
 =  10

ASCII value of character ♂ =  11

ASCII value of character ♀ =  12

 =  13value of character

ASCII value of character ♫ =  14

ASCII value of character ☼ =  15

ASCII value of character ► =  16

ASCII value of character ◄ =  17

ASCII value of character ↕ =  18

ASCII value of character ‼ =  19

ASCII value of character ¶ =  20

ASCII value of character § =  21

ASCII value of character ▬ =  22

ASCII value of character ↨ =  23

ASCII value of character ↑ =  24

ASCII value of character ↓ =  25

ASCII value of character → =  26

ASCII value of character ← =  27

ASCII value of character ∟ =  28

ASCII value of character ↔ =  29

ASCII value of character ▲ =  30

ASCII value of character ▼ =  31

ASCII value of character   =  32

ASCII value of character ! =  33

ASCII value of character " =  34

ASCII value of character # =  35

ASCII value of character $ =  36

ASCII value of character % =  37

ASCII value of character & =  38

ASCII value of character ' =  39

ASCII value of character ( =  40

ASCII value of character ) =  41

ASCII value of character * =  42

ASCII value of character + =  43

ASCII value of character , =  44

ASCII value of character - =  45

ASCII value of character . =  46

ASCII value of character / =  47

ASCII value of character 0 =  48

ASCII value of character 1 =  49

ASCII value of character 2 =  50

ASCII value of character 3 =  51

ASCII value of character 4 =  52

ASCII value of character 5 =  53

ASCII value of character 6 =  54

ASCII value of character 7 =  55

ASCII value of character 8 =  56

ASCII value of character 9 =  57

ASCII value of character : =  58

ASCII value of character ; =  59

ASCII value of character < =  60

ASCII value of character = =  61

ASCII value of character > =  62

ASCII value of character ? =  63

ASCII value of character @ =  64

ASCII value of character A =  65

ASCII value of character B =  66

ASCII value of character C =  67

ASCII value of character D =  68

ASCII value of character E =  69

ASCII value of character F =  70

ASCII value of character G =  71

ASCII value of character H =  72

ASCII value of character I =  73

ASCII value of character J =  74

ASCII value of character K =  75

ASCII value of character L =  76

ASCII value of character M =  77

ASCII value of character N =  78

ASCII value of character O =  79

ASCII value of character P =  80

ASCII value of character Q =  81

ASCII value of character R =  82

ASCII value of character S =  83

ASCII value of character T =  84

ASCII value of character U =  85

ASCII value of character V =  86

ASCII value of character W =  87

ASCII value of character X =  88

ASCII value of character Y =  89

ASCII value of character Z =  90

ASCII value of character [ =  91

ASCII value of character \ =  92

ASCII value of character ] =  93

ASCII value of character ^ =  94

ASCII value of character _ =  95

ASCII value of character ` =  96

ASCII value of character a =  97

ASCII value of character b =  98

ASCII value of character c =  99

ASCII value of character d =  100

ASCII value of character e =  101

ASCII value of character f =  102

ASCII value of character g =  103

ASCII value of character h =  104

ASCII value of character i =  105

ASCII value of character j =  106

ASCII value of character k =  107

ASCII value of character l =  108

ASCII value of character m =  109

ASCII value of character n =  110

ASCII value of character o =  111

ASCII value of character p =  112

ASCII value of character q =  113

ASCII value of character r =  114

ASCII value of character s =  115

ASCII value of character t =  116

ASCII value of character u =  117

ASCII value of character v =  118

ASCII value of character w =  119

ASCII value of character x =  120

ASCII value of character y =  121

ASCII value of character z =  122

ASCII value of character { =  123

ASCII value of character | =  124

ASCII value of character } =  125

ASCII value of character ~ =  126

ASCII value of character ⌂ =  127

ASCII value of character Ç =  128

ASCII value of character ü =  129

ASCII value of character é =  130

ASCII value of character â =  131

ASCII value of character ä =  132

ASCII value of character à =  133

ASCII value of character å =  134

ASCII value of character ç =  135

ASCII value of character ê =  136

ASCII value of character ë =  137

ASCII value of character è =  138

ASCII value of character ï =  139

ASCII value of character î =  140

ASCII value of character ì =  141

ASCII value of character Ä =  142

ASCII value of character Å =  143

ASCII value of character É =  144

ASCII value of character æ =  145

ASCII value of character Æ =  146

ASCII value of character ô =  147

ASCII value of character ö =  148

ASCII value of character ò =  149

ASCII value of character û =  150

ASCII value of character ù =  151

ASCII value of character ÿ =  152

ASCII value of character Ö =  153

ASCII value of character Ü =  154

ASCII value of character ¢ =  155

ASCII value of character £ =  156

ASCII value of character ¥ =  157

ASCII value of character ₧ =  158

ASCII value of character ƒ =  159

ASCII value of character á =  160

ASCII value of character í =  161

ASCII value of character ó =  162

ASCII value of character ú =  163

ASCII value of character ñ =  164

ASCII value of character Ñ =  165

ASCII value of character ª =  166

ASCII value of character º =  167

ASCII value of character ¿ =  168

ASCII value of character ⌐ =  169

ASCII value of character ¬ =  170

ASCII value of character ½ =  171

ASCII value of character ¼ =  172

ASCII value of character ¡ =  173

ASCII value of character « =  174

ASCII value of character » =  175

ASCII value of character ░ =  176

ASCII value of character ▒ =  177

ASCII value of character ▓ =  178

ASCII value of character │ =  179

ASCII value of character ┤ =  180

ASCII value of character ╡ =  181

ASCII value of character ╢ =  182

ASCII value of character ╖ =  183

ASCII value of character ╕ =  184

ASCII value of character ╣ =  185

ASCII value of character ║ =  186

ASCII value of character ╗ =  187

ASCII value of character ╝ =  188

ASCII value of character ╜ =  189

ASCII value of character ╛ =  190

ASCII value of character ┐ =  191

ASCII value of character └ =  192

ASCII value of character ┴ =  193

ASCII value of character ┬ =  194

ASCII value of character ├ =  195

ASCII value of character ─ =  196

ASCII value of character ┼ =  197

ASCII value of character ╞ =  198

ASCII value of character ╟ =  199

ASCII value of character ╚ =  200

ASCII value of character ╔ =  201

ASCII value of character ╩ =  202

ASCII value of character ╦ =  203

ASCII value of character ╠ =  204

ASCII value of character ═ =  205

ASCII value of character ╬ =  206

ASCII value of character ╧ =  207

ASCII value of character ╨ =  208

ASCII value of character ╤ =  209

ASCII value of character ╥ =  210

ASCII value of character ╙ =  211

ASCII value of character ╘ =  212

ASCII value of character ╒ =  213

ASCII value of character ╓ =  214

ASCII value of character ╫ =  215

ASCII value of character ╪ =  216

ASCII value of character ┘ =  217

ASCII value of character ┌ =  218

ASCII value of character █ =  219

ASCII value of character ▄ =  220

ASCII value of character ▌ =  221

ASCII value of character ▐ =  222

ASCII value of character ▀ =  223

ASCII value of character α =  224

ASCII value of character ß =  225

ASCII value of character Γ =  226

ASCII value of character π =  227

ASCII value of character Σ =  228

ASCII value of character σ =  229

ASCII value of character µ =  230

ASCII value of character τ =  231

ASCII value of character Φ =  232

ASCII value of character Θ =  233

ASCII value of character Ω =  234

ASCII value of character δ =  235

ASCII value of character ∞ =  236

ASCII value of character φ =  237

ASCII value of character ε =  238

ASCII value of character ∩ =  239

ASCII value of character ≡ =  240

ASCII value of character ± =  241

ASCII value of character ≥ =  242

ASCII value of character ≤ =  243

ASCII value of character ⌠ =  244

ASCII value of character ⌡ =  245

ASCII value of character ÷ =  246

ASCII value of character ≈ =  247

ASCII value of character ° =  248

ASCII value of character ∙ =  249

ASCII value of character · =  250

ASCII value of character √ =  251

ASCII value of character ⁿ =  252

ASCII value of character ² =  253

ASCII value of character ■ =  254

ASCII value of character   =  255
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int number, n;

        Scanner input = new Scanner(System.in);

        System.out.println("Print all ASCII character with their values.\n");

        for (n = 0; n <= 255; n++) {
            
            System.out.println("ASCII value of character " + (char)n + " = " + n + "\n");
        }
    }
}
Sample Output
Print all ASCII character with their values.


ASCII value of character   =  0

ASCII value of character ☺ =  1

ASCII value of character ☻ =  2

ASCII value of character ♥ =  3

ASCII value of character ♦ =  4

ASCII value of character ♣ =  5

ASCII value of character ♠ =  6

ASCII value of character  =  7

ASCII value of character =  8

ASCII value of character         =  9

ASCII value of character
 =  10

ASCII value of character ♂ =  11

ASCII value of character ♀ =  12

 =  13value of character

ASCII value of character ♫ =  14

ASCII value of character ☼ =  15

ASCII value of character ► =  16

ASCII value of character ◄ =  17

ASCII value of character ↕ =  18

ASCII value of character ‼ =  19

ASCII value of character ¶ =  20

ASCII value of character § =  21

ASCII value of character ▬ =  22

ASCII value of character ↨ =  23

ASCII value of character ↑ =  24

ASCII value of character ↓ =  25

ASCII value of character → =  26

ASCII value of character ← =  27

ASCII value of character ∟ =  28

ASCII value of character ↔ =  29

ASCII value of character ▲ =  30

ASCII value of character ▼ =  31

ASCII value of character   =  32

ASCII value of character ! =  33

ASCII value of character " =  34

ASCII value of character # =  35

ASCII value of character $ =  36

ASCII value of character % =  37

ASCII value of character & =  38

ASCII value of character ' =  39

ASCII value of character ( =  40

ASCII value of character ) =  41

ASCII value of character * =  42

ASCII value of character + =  43

ASCII value of character , =  44

ASCII value of character - =  45

ASCII value of character . =  46

ASCII value of character / =  47

ASCII value of character 0 =  48

ASCII value of character 1 =  49

ASCII value of character 2 =  50

ASCII value of character 3 =  51

ASCII value of character 4 =  52

ASCII value of character 5 =  53

ASCII value of character 6 =  54

ASCII value of character 7 =  55

ASCII value of character 8 =  56

ASCII value of character 9 =  57

ASCII value of character : =  58

ASCII value of character ; =  59

ASCII value of character < =  60

ASCII value of character = =  61

ASCII value of character > =  62

ASCII value of character ? =  63

ASCII value of character @ =  64

ASCII value of character A =  65

ASCII value of character B =  66

ASCII value of character C =  67

ASCII value of character D =  68

ASCII value of character E =  69

ASCII value of character F =  70

ASCII value of character G =  71

ASCII value of character H =  72

ASCII value of character I =  73

ASCII value of character J =  74

ASCII value of character K =  75

ASCII value of character L =  76

ASCII value of character M =  77

ASCII value of character N =  78

ASCII value of character O =  79

ASCII value of character P =  80

ASCII value of character Q =  81

ASCII value of character R =  82

ASCII value of character S =  83

ASCII value of character T =  84

ASCII value of character U =  85

ASCII value of character V =  86

ASCII value of character W =  87

ASCII value of character X =  88

ASCII value of character Y =  89

ASCII value of character Z =  90

ASCII value of character [ =  91

ASCII value of character \ =  92

ASCII value of character ] =  93

ASCII value of character ^ =  94

ASCII value of character _ =  95

ASCII value of character ` =  96

ASCII value of character a =  97

ASCII value of character b =  98

ASCII value of character c =  99

ASCII value of character d =  100

ASCII value of character e =  101

ASCII value of character f =  102

ASCII value of character g =  103

ASCII value of character h =  104

ASCII value of character i =  105

ASCII value of character j =  106

ASCII value of character k =  107

ASCII value of character l =  108

ASCII value of character m =  109

ASCII value of character n =  110

ASCII value of character o =  111

ASCII value of character p =  112

ASCII value of character q =  113

ASCII value of character r =  114

ASCII value of character s =  115

ASCII value of character t =  116

ASCII value of character u =  117

ASCII value of character v =  118

ASCII value of character w =  119

ASCII value of character x =  120

ASCII value of character y =  121

ASCII value of character z =  122

ASCII value of character { =  123

ASCII value of character | =  124

ASCII value of character } =  125

ASCII value of character ~ =  126

ASCII value of character ⌂ =  127

ASCII value of character Ç =  128

ASCII value of character ü =  129

ASCII value of character é =  130

ASCII value of character â =  131

ASCII value of character ä =  132

ASCII value of character à =  133

ASCII value of character å =  134

ASCII value of character ç =  135

ASCII value of character ê =  136

ASCII value of character ë =  137

ASCII value of character è =  138

ASCII value of character ï =  139

ASCII value of character î =  140

ASCII value of character ì =  141

ASCII value of character Ä =  142

ASCII value of character Å =  143

ASCII value of character É =  144

ASCII value of character æ =  145

ASCII value of character Æ =  146

ASCII value of character ô =  147

ASCII value of character ö =  148

ASCII value of character ò =  149

ASCII value of character û =  150

ASCII value of character ù =  151

ASCII value of character ÿ =  152

ASCII value of character Ö =  153

ASCII value of character Ü =  154

ASCII value of character ¢ =  155

ASCII value of character £ =  156

ASCII value of character ¥ =  157

ASCII value of character ₧ =  158

ASCII value of character ƒ =  159

ASCII value of character á =  160

ASCII value of character í =  161

ASCII value of character ó =  162

ASCII value of character ú =  163

ASCII value of character ñ =  164

ASCII value of character Ñ =  165

ASCII value of character ª =  166

ASCII value of character º =  167

ASCII value of character ¿ =  168

ASCII value of character ⌐ =  169

ASCII value of character ¬ =  170

ASCII value of character ½ =  171

ASCII value of character ¼ =  172

ASCII value of character ¡ =  173

ASCII value of character « =  174

ASCII value of character » =  175

ASCII value of character ░ =  176

ASCII value of character ▒ =  177

ASCII value of character ▓ =  178

ASCII value of character │ =  179

ASCII value of character ┤ =  180

ASCII value of character ╡ =  181

ASCII value of character ╢ =  182

ASCII value of character ╖ =  183

ASCII value of character ╕ =  184

ASCII value of character ╣ =  185

ASCII value of character ║ =  186

ASCII value of character ╗ =  187

ASCII value of character ╝ =  188

ASCII value of character ╜ =  189

ASCII value of character ╛ =  190

ASCII value of character ┐ =  191

ASCII value of character └ =  192

ASCII value of character ┴ =  193

ASCII value of character ┬ =  194

ASCII value of character ├ =  195

ASCII value of character ─ =  196

ASCII value of character ┼ =  197

ASCII value of character ╞ =  198

ASCII value of character ╟ =  199

ASCII value of character ╚ =  200

ASCII value of character ╔ =  201

ASCII value of character ╩ =  202

ASCII value of character ╦ =  203

ASCII value of character ╠ =  204

ASCII value of character ═ =  205

ASCII value of character ╬ =  206

ASCII value of character ╧ =  207

ASCII value of character ╨ =  208

ASCII value of character ╤ =  209

ASCII value of character ╥ =  210

ASCII value of character ╙ =  211

ASCII value of character ╘ =  212

ASCII value of character ╒ =  213

ASCII value of character ╓ =  214

ASCII value of character ╫ =  215

ASCII value of character ╪ =  216

ASCII value of character ┘ =  217

ASCII value of character ┌ =  218

ASCII value of character █ =  219

ASCII value of character ▄ =  220

ASCII value of character ▌ =  221

ASCII value of character ▐ =  222

ASCII value of character ▀ =  223

ASCII value of character α =  224

ASCII value of character ß =  225

ASCII value of character Γ =  226

ASCII value of character π =  227

ASCII value of character Σ =  228

ASCII value of character σ =  229

ASCII value of character µ =  230

ASCII value of character τ =  231

ASCII value of character Φ =  232

ASCII value of character Θ =  233

ASCII value of character Ω =  234

ASCII value of character δ =  235

ASCII value of character ∞ =  236

ASCII value of character φ =  237

ASCII value of character ε =  238

ASCII value of character ∩ =  239

ASCII value of character ≡ =  240

ASCII value of character ± =  241

ASCII value of character ≥ =  242

ASCII value of character ≤ =  243

ASCII value of character ⌠ =  244

ASCII value of character ⌡ =  245

ASCII value of character ÷ =  246

ASCII value of character ≈ =  247

ASCII value of character ° =  248

ASCII value of character ∙ =  249

ASCII value of character · =  250

ASCII value of character √ =  251

ASCII value of character ⁿ =  252

ASCII value of character ² =  253

ASCII value of character ■ =  254

ASCII value of character   =  255

Write a Java program to find frequency of each digit in a given integer-2

Find frequency of each digit in a given integers
Sample Output
Find frequency of each digit in a given integer.

Enter a number: 20221
Frequency of 0 = 1
Frequency of 1 = 1
Frequency of 2 = 3
Frequency of 3 = 0
Frequency of 4 = 0
Frequency of 5 = 0
Frequency of 6 = 0
Frequency of 7 = 0
Frequency of 8 = 0
Frequency of 9 = 0
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main181 {

    public static void main(String[] args) {

        int i, count, digit, freqCount[] = new int[10], number;

        Scanner input = new Scanner(System.in);

        System.out.println("Find frequency of each digit in a given integer.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();

        while (number != 0) {
            
            digit = (number % 10);
            number = (number / 10);
            freqCount[digit]++;
        }
        for (i = 0; i <= 9; i++) {

            /// if(freqCount[i] > 0) /** This line will be print, if frequency is greater than 0.*/
            {
                System.out.println("Frequency of " + i + " = " + freqCount[i]);
            }
        }

    }
}
Sample Output
Find frequency of each digit in a given integer.

Enter a number: 192004599
Frequency of 0 = 2
Frequency of 1 = 1
Frequency of 2 = 1
Frequency of 3 = 0
Frequency of 4 = 1
Frequency of 5 = 1
Frequency of 6 = 0
Frequency of 7 = 0
Frequency of 8 = 0
Frequency of 9 = 3

Write a Java program to find frequency of each digit in a given integer

Find frequency of each digit in a given integers
Sample Output
Find frequency of each digit in a given integer.

Enter a number: 192002024
Frequency of 0 = 3
Frequency of 1 = 1
Frequency of 2 = 3
Frequency of 3 = 0
Frequency of 4 = 1
Frequency of 5 = 0
Frequency of 6 = 0
Frequency of 7 = 0
Frequency of 8 = 0
Frequency of 9 = 1
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int i, count, digit, number, numberClone;

        Scanner input = new Scanner(System.in);

        System.out.println("Find frequency of each digit in a given integer.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();

        numberClone = number;

        for (i = 0; i <= 9; i++) {
            count = 0;
            number = numberClone;

            while (number != 0) {
                
                digit = (number % 10);
                number = (number / 10);
                if (digit == i) {
                    count++;
                }
            }
            /// if(count > 0) /** This line will be print, if frequency is greater than 0.*/
            {
                System.out.println("Frequency of " + i + " = " + count);
            }
        }
    }
}
Sample Output
Find frequency of each digit in a given integer.

Enter a number: 2021
Frequency of 0 = 1
Frequency of 1 = 1
Frequency of 2 = 2
Frequency of 3 = 0
Frequency of 4 = 0
Frequency of 5 = 0
Frequency of 6 = 0
Frequency of 7 = 0
Frequency of 8 = 0
Frequency of 9 = 0

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

Check whether a number is palindrome or not
Sample Output
Check whether a number is palindrome or not.

Enter a number: 12321
12321 is palindrome!
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int digit, number, numberClone, revNumber = 0;

        Scanner input = new Scanner(System.in);

        System.out.println("Check whether a number is palindrome or not.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();

        numberClone = number;

        // Reverse Number
        while (number != 0) {
            
            digit = (number % 10);
            number = (number / 10);
            revNumber = (10 * revNumber) + digit;
        } // Reverse number end
        
        if (numberClone == revNumber) {
            
            System.out.println(numberClone + " is palindrome!");
        } else {
            
            System.out.println(numberClone + " is not palindrome.");
        }
    }
}
Sample Output
Check whether a number is palindrome or not.

Enter a number: 6789
6789 is not palindrome.

Write a Java program to enter a number and print its reverse

Enter a number and print its reverse
Sample Output
Enter a number and print its reverse.

Enter a number: 12345
Reverse Number: 54321
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int digit, number, revNumber = 0;

        Scanner input = new Scanner(System.in);

        System.out.println("Enter a number and print its reverse.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();

        // Reverse Number
        while (number != 0) {
        
            digit = (number % 10);
            number = (number / 10);
            revNumber = (10 * revNumber) + digit;
        }
        // Reverse number end
        
        System.out.println("Reverse Number: " + revNumber);
    }
}
Sample Output
Enter a number and print its reverse.

Enter a number: 2021
Reverse Number: 1202
Change Theme
X