Coding Practice

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 program to find Hamming Distance

Hamming Distance

Hamming distance is a metric for comparing two binary data strings. While comparing two binary strings of equal length, Hamming distance is the number of bit positions in which the two bits are different.

Example: Find the distance between the vectors 01101010 and 11011011.
(01101010
11011011)
They differ in four places, So the Hamming distance d(01101010, 11011011) = 4.

Sample Output
Enter Comp1 data: 111101010
Enter Comp2 data: 010101010

Hamming Distance: 2
C-Source Code
#include<stdio.h>

void main()
{
    int i, comp1Len, comp2Len, hammingDistance = 0;
    char comp1[500], comp2[500];
input:
    printf("Enter Comp1 data: ");
    scanf("%s", comp1);
    printf("Enter Comp2 data: ");
    scanf("%s", comp2);

    for(i = 0; comp1[i] != '\0'; i++);
    comp1Len = i;

    for(i = 0; comp2[i] != '\0'; i++);
    comp2Len = i;

    if(comp1Len != comp2Len)
    {
        printf("\nComp1 and Comp2 length are not same!\n\n");
        goto input;
    }

    for(i = 0; i < comp1Len; i++)
    {
        if(comp1[i] != comp2[i])
        {
            hammingDistance++;
        }
    }
    printf("\nHamming Distance: %d\n\n", hammingDistance);
}
Sample Output
Enter Comp1 data: 01101010
Enter Comp2 data: 11011011

Hamming Distance: 4

Write a program to calculate the MINIMUM HAMMING DISTANCE for a given set of binary data taken as input

Minimum Hamming Distance
Sample Input:
    Number of inputs: 3
    10110
    01110
    11001
    Sample Output:
    Minimum hamming distance, (C1, C2) = 2
Sample Output
Number of inputs: 3
10110
01110
11001

Minimum hamming distance, (10110, 01110) = 2
C-Source Code
/**
I will share the code after 15 April 2021,
If you need urgent, please contact with me.
*/
Sample Output
Number of inputs: 4
1110
0011
0000
10

Data must be same length!
Enter 4 data, whose length 4 bits...
1110
0011
0000
1011

Minimum hamming distance, (0011, 1011) = 1

Write a program that choose Even parity / Odd parity according to user choose. And accordingly performs Even parity / Odd parity process

Parity Bit Checker

Example 1:
Enter 1 for Even parity and 2 for Odd parity.
Input: 1

Even parity:
Input:
Data: 110110
Output:
modified data: 1101100
Parity bit: 0

Example 2:
Enter 1 for Even parity and 2 for Odd parity.
Input: 2

Odd parity:
Input:
Data: 1101101
Output:
Modified data: 11011010
Parity bit: 0

Sample Output
Enter 1 for Even parity and 2 for Odd parity.

Enter your choice: 1

Even parity:
Data: 110110

Modified Data: 1101100
Parity Bit: 0
C-Source Code
#include<stdio.h>
#include<string.h>

void main()
{
    int i, choice, even_parity_len, count = 0, p_bit;
    char parity_bit, even_parity[100];
main:
    printf("Enter 1 for Even parity and 2 for Odd parity.\n\nEnter your choice: ");
    scanf("%d", &choice);

    switch(choice)
    {
    case 1:
        printf("\nEven parity:\nData: ");
        scanf("%s", even_parity);

        for(i = 0; even_parity[i] != '\0'; i++);
        even_parity_len = i;

        for(i = 0; i < even_parity[i]; i++)
        {
            if(even_parity[i] == '1')
            {
                count++;
            }
        }

        if(count%2 == 0)
        {
            even_parity[even_parity_len] = '0';
            p_bit = 0;
        }
        else
        {
            even_parity[even_parity_len] = '1';
            p_bit = 1;
        }
        break;

    case 2:
        printf("\nOdd parity:\nData: ");
        scanf("%s", even_parity);

        for(i = 0; even_parity[i] != '\0'; i++);
        even_parity_len = i;

        for(i = 0; i < even_parity[i]; i++)
        {
            if(even_parity[i] == '1')
            {
                count++;
            }
        }

        if(count%2 == 0)
        {
            even_parity[even_parity_len] = '1';
            p_bit = 1;
        }
        else
        {
            even_parity[even_parity_len] = '0';
            p_bit = 0;
        }
        break;

    default:
        printf("\nInvalid Input! Please enter 1 or 2.\n");
        goto main;
        break;
    }
    even_parity[even_parity_len+1] = '\0';

    printf("\nModified Data: %s\nParity Bit: %d\n\n", even_parity, p_bit);
}
Sample Output
Enter 1 for Even parity and 2 for Odd parity.

Enter your choice: 2

Odd parity:
Data: 1101101

Modified Data: 11011010
Parity Bit: 0

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

Write a Java program to calculate product of digits of a number

Calculate product of digits of a number
Sample Output
Calculate product of digits of a number.

Enter a number: 125
Product: 10
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int number, digit, product = 1;

        Scanner input = new Scanner(System.in);

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

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

        while (number != 0) {
            digit = (number % 10);
            product = product * digit;
            number = (number / 10);
        }
        System.out.println("Product: " + product);

    }
}
Sample Output
Calculate product of digits of a number.

Enter a number: 192
Product: 18

Write a Java program to calculate sum of digits of a number

Calculate sum of digits of a number
Sample Output
Calculate sum of digits of a number.

Enter a number: 192
Sum: 12
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int n, digit, number, sum = 0;

        Scanner input = new Scanner(System.in);

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

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

        while (number != 0) {
            
            digit = (number % 10);
            sum = sum + digit;
            number = (number / 10);
        }
        System.out.println("Sum: " + sum);
    }
}
Sample Output
Calculate sum of digits of a number.

Enter a number: 2021
Sum: 5

Write a Java program to swap first and last digits of a number

Swap first and last digits of a number
Sample Output
Swap first and last digits of a number.

Enter number: 122334
Swapped Number: 422331
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int digit, number, revNumber = 0;
        int swappedNumber, temp, firstDigit, lastDigit;

        Scanner input = new Scanner(System.in);

        System.out.println("Swap first and last digits of a number.\n");

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

        /** Reverse Number */
        lastDigit = number % 10;
        while (number >= 10) {
            digit = (number % 10);
            number = (number / 10);
            revNumber = (10 * revNumber) + digit;
        }
        firstDigit = number;
        digit = (number % 10);
        revNumber = ((10 * revNumber) + digit);
        number = revNumber; // Reverse number end

        /**
         * Again reverse the number but ignore first and last digits
         */
        swappedNumber = lastDigit; /// Ignore first digits, it swapped!

        number = number / 10;
        while (number >= 10) {
            
            digit = (number % 10);
            number = number / 10;
            swappedNumber = ((10 * swappedNumber) + digit);
        }
        swappedNumber = ((10 * swappedNumber) + firstDigit); /// Ignore last digits, it swapped!.
        System.out.println("Swapped Number: " + swappedNumber);
    }
}
Sample Output
Swap first and last digits of a number.

Enter number: 1920024
Swapped Number: 4920021

Write a Java program to Find sum of first and last digit of a number

Find sum of first and last digit of a number
Sample Output
Find sum of first and last digit of a number.

Enter a number: 13334
First Digit: 1, Last Digit: 4
Sum: 5
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int n, number, firstDigit, lastDigit, sum;

        Scanner input = new Scanner(System.in);

        System.out.println("Find sum of first and last digit of a number.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();
        
        lastDigit = number % 10;
        firstDigit = number;
        
        while(firstDigit >= 10) {
            
            firstDigit = firstDigit / 10;
        }
        sum = firstDigit + lastDigit;
        
        System.out.println("First Digit: " + firstDigit + ", Last Digit: " + lastDigit);
        System.out.println("Sum: " + sum);
    }
}
Sample Output
Find sum of first and last digit of a number.

Enter a number: 9876
First Digit: 9, Last Digit: 6
Sum: 15

Write a Java program to find first and last digit of a number

Find first and last digit of a number
Sample Output
Find first and last digit of a number.

Enter a number: 1445
First Digit: 1
Last Digit: 5
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int n, number, firstDigit, lastDigit;

        Scanner input = new Scanner(System.in);

        System.out.println("Find first and last digit of a number.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();
        
        lastDigit = number % 10;
        firstDigit = number;
        
        while(firstDigit >= 10) {
            
            firstDigit = firstDigit / 10;
        }
        System.out.println("First Digit: " + firstDigit + "\nLast Digit: " + lastDigit);
    }
}
Sample Output
Find first and last digit of a number.

Enter a number: 2021
First Digit: 2
Last Digit: 1

Write a Java program to print multiplication table of any number

Print multiplication table of any number
Sample Output
Print multiplication table of any number.

Enter a number: 5
Enter limit: 10
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Java-Source Code
//package loopinjava;
import java.util.Scanner;

public class Main9 {

    public static void main(String[] args) {
        
        int n, number, limit;
        
        Scanner input = new Scanner(System.in);
        
        System.out.println("Print multiplication table of any number.\n");
        
        System.out.print("Enter a number: ");
        number = input.nextInt();

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

        n = number;
        for(n = 1; n <= limit; n++){
            
            System.out.println(number + " * " + n + " = " + (number * n));
        }
    }
}
Sample Output
Print multiplication table of any number.

Enter a number: 10
Enter limit: 5
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50

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

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

Enter a number: 4
Natural numbers are...
1
2
3
4
Sum: 10
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int n, number, sum = 0;

        Scanner input = new Scanner(System.in);

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

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

        System.out.println("Natural numbers are...");

        for (n = 1; n <= number; n++) {
            sum += n; // Or, sum = sum + n;
            System.out.println(n);
            
        }
        System.out.println("Sum: " + sum);
    }
}
Sample Output
Find sum of all natural numbers between 1 to n.

Enter a number: 6
Natural numbers are...
1
2
3
4
5
6
Sum: 21

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

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

Enter a number: 10
2
4
6
8
10
Sum: 30
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int n, number, sum = 0;

        Scanner input = new Scanner(System.in);

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

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

        for (n = 1; n <= number; n++) {
            
            if (n % 2 == 0) {
                
                sum += n; // Or, sum = sum + n;
                System.out.println(n);
            }

        }
        System.out.println("Sum: " + sum);
    }
}
Sample Output
Find sum of all even numbers between 1 to n.

Enter a number: 12
2
4
6
8
10
12
Sum: 42

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

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

Enter a number: 10
1
3
5
7
9
Sum: 25
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int n, number, sum = 0;

        Scanner input = new Scanner(System.in);

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

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

        for (n = 1; n <= number; n++) {
            
            if (n % 2 != 0) {
                
                sum += n; // Or, sum = sum + n;
                System.out.println(n);
            }

        }
        System.out.println("Sum: " + sum);
    }
}
Sample Output
Find sum of all odd numbers between 1 to n.

Enter a number: 12
1
3
5
7
9
11
Sum: 36

Write a Java program to count number of digits in a number

Count number of digits in a number
Sample Output
Count number of digits in a number.

Enter a number: 192
Total 3 digit(s).
Java-Source Code
//package loopinjava;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        
        int number, n, count = 0;
        
        Scanner input = new Scanner(System.in);
        
        System.out.println("Count number of digits in a number.\n");
        
        System.out.print("Enter a number: ");
        number = input.nextInt();
        
        while (number != 0) {
            
            number /= 10; //Or, number = number / 10;
            count++;
        }
        System.out.println("Total " + count +" digit(s).");
    }
}
Sample Output
Count number of digits in a number.

Enter a number: 2021
Total 4 digit(s).

Write a Java program to print all even numbers from 1 to 100. - Using while loop

Print all even numbers from 1 to 100. - Using while loop
Sample Output
Print all even numbers from 1 to 100. - Using while loop.
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
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 even numbers from 1 to 100. - Using while loop.");
        
        n = 1;
        while (n <= 100) {
            if(n % 2 == 0){
                System.out.println(n);
            }
            n++;
        }
    }
}
Sample Output
Print all even numbers from 1 to 100. - Using while loop.
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98

Write a Java program to print all odd number from 1 to 100

Print all odd numbers from 1 to 100. - Using while loop
Sample Output
Print all odd numbers from 1 to 100. - Using while loop.
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
55
57
59
61
63
65
67
69
71
73
75
77
79
81
83
85
87
89
91
93
95
97
99
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 odd numbers from 1 to 100. - Using while loop.");

        n = 1;
        while (n <= 100) {
            if(n % 2 != 0){
                System.out.println(n);
            }
            n++;
        }
    }
}
Sample Output
Print all odd numbers from 1 to 100. - Using while loop.
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
55
57
59
61
63
65
67
69
71
73
75
77
79
81
83
85
87
89
91
93
95
97
99

Write a Java program to print all-natural numbers in reverse (from n to 1). - Using while loop

Print all natural numbers in reverse (from n to 1). - using while loop
Sample Output
Print all natural numbers in reverse (from n to 1). - using while loop.

Enter a number: 7
7
6
5
4
3
2
1
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 natural numbers in reverse (from n to 1). - using while loop.\n");
        
        System.out.print("Enter a number: ");
        number = input.nextInt();

        n = number;
        while (n >= 1) {
            System.out.println(n);
            n--;
        }
    }
}
Sample Output
Print all natural numbers in reverse (from n to 1). - using while loop.

Enter a number: 7
7
6
5
4
3
2
1

Write a Java program to print all alphabets from a to z. - using while loop

Print all alphabets from a to z. - using while loop
Sample Output
Print all alphabets from a to z. - using while loop.
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Java-Source Code
//package loopinjava;

public class Main {

    public static void main(String[] args) {
        
        char ch;
        
        System.out.println("Print all alphabets from a to z. - using while loop.");

        ch = 'a';
        while (ch <= 'z') {
            System.out.println(ch);
            ch++;
        }
    }
}
Sample Output
Print all alphabets from a to z. - using while loop.
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z

Write a Java program to print all-natural numbers from 1 to n. - using while loop

Print all-natural numbers from 1 to n. - using while loop
Sample Output
Print all natural numbers from 1 to n. - using while loop.

Enter a number: 7
1
2
3
4
5
6
7
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 natural numbers in reverse (from n to 1). - using while loop.\n");
        
        System.out.print("Enter a number: ");
        number = input.nextInt();

        n = number;
        while (n >= 1) {
            System.out.println(n);
            n--;
        }
    }
}
Sample Output
Print all natural numbers from 1 to n. - using while loop.

Enter a number: 7
1
2
3
4
5
6
7

Write a Java program to calculate profit or loss

Calculate profit or loss
Sample Output
Calculate profit or loss.

Purchase Price: 7
Selling Price: 9
Profit!
Java-Source Code
//package ifelsejava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int buy, sell;

        Scanner input = new Scanner(System.in);

        System.out.println("Calculate profit or loss.\n");

        System.out.print("Purchase Price: ");
        buy = input.nextInt();

        System.out.print("Selling Price: ");
        sell = input.nextInt();

        if (buy < sell) {
            System.out.println("Profit!");
            
        } else if (sell == buy) {
            System.out.println("No Loss No Profit.");
        
        } else {
            System.out.println("Loss!");
        
        }
    }
}
Sample Output
Calculate profit or loss.

Purchase Price: 100
Selling Price: 90
Loss!

Write a Java program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer

Input marks of five subjects and calculate the grade

Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following:

  • Percentage >= 80% : Grade A+
  • Percentage >= 75% : Grade A
  • Percentage >= 70% : Grade A-
  • Percentage >= 65% : Grade B+
  • Percentage >= 60% : Grade B
  • Percentage >= 55% : Grade B-
  • Percentage >= 50% : Grade C+
  • Percentage >= 45% : Grade C
  • Percentage >= 40% : Grade D
  • Percentage < 40% : Grade F

Sample Output
Input marks of five subjects and calculate the grade.

Physics: 87
Chemistry: 98
Biology: 71
Mathematics: 80
Computer: 85
Grade A+
Java-Source Code
//package ifelsejava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        
        int phy, che, bio, math, com, avg_mark;

        System.out.println("Input marks of five subjects and calculate the grade.\n");

        System.out.print("Physics: ");
        phy = input.nextInt();
        
        System.out.print("Chemistry: ");
        che = input.nextInt();
        
        System.out.print("Biology: ");
        bio = input.nextInt();
        
        System.out.print("Mathematics: ");
        math = input.nextInt();
        
        System.out.print("Computer: ");
        com = input.nextInt();

        avg_mark = (phy + che + bio + math + com) / 5;

        if (avg_mark >= 80 && avg_mark <= 100) {
            System.out.println("Grade A+");
            
        } else if (avg_mark >= 75 && avg_mark <= 79) {
            System.out.println("Grade A");
            
        } else if (avg_mark >= 70 && avg_mark <= 74) {
            System.out.println("Grade A-");
            
        } else if (avg_mark >= 65 && avg_mark <= 69) {
            System.out.println("Grade B+");
            
        } else if (avg_mark >= 60 && avg_mark <= 64) {
            System.out.println("Grade B");
            
        } else if (avg_mark >= 55 && avg_mark <= 59) {
            System.out.println("Grade B-");
            
        } else if (avg_mark >= 50 && avg_mark <= 54) {
            System.out.println("Grade C+");
            
        } else if (avg_mark >= 45 && avg_mark <= 49) {
            System.out.println("Grade C");
            
        } else if (avg_mark >= 40 && avg_mark <= 44) {
            System.out.println("Grade D");
            
        } else if (avg_mark < 40) {
            System.out.println("Fail");
            
        }
    }
}
Sample Output
Input marks of five subjects and calculate the grade.

Physics: 79
Chemistry: 79
Biology: 79
Mathematics: 79
Computer: 79
Grade A

Write a Java program to input basic salary of an employee and calculate its Gross salary according

Input basic salary of an employee and calculate its Gross salary

Calculate salary according to following:

  • Basic Salary <= 10000 : HRA = 20%, DA = 80%
  • Basic Salary <= 20000 : HRA = 25%, DA = 90%
  • Basic Salary > 20000 : HRA = 30%, DA = 95%

Sample Output
Input basic salary of an employee and calculate its Gross salary.

Enter employee basic salary: 20000
Gross Salary: 43000.0
Java-Source Code
//package ifelsejava;

import java.util.Scanner;

public class Main {

    public static double basicSalary, grossSalary, hra, da;
    
    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);

        System.out.println("Input basic salary of an employee and calculate its Gross salary.\n");

        System.out.print("Enter employee basic salary: ");
        basicSalary = input.nextDouble();

        if (basicSalary <= 10000) {
            hra = basicSalary * 0.2;
            da = basicSalary * 0.8;
            grossSalary = basicSalary + hra + da;
            
        } else if (basicSalary <= 20000) {
            hra = basicSalary * 0.25;
            da = basicSalary * 0.9;
            grossSalary = basicSalary + hra + da;
            
        } else if (basicSalary > 20000) {
            hra = basicSalary * 0.3;
            da = basicSalary * 0.95;
            grossSalary = basicSalary + hra + da;
            
        }
        System.out.println("Gross Salary: " + grossSalary);
    }
}
Sample Output
Input basic salary of an employee and calculate its Gross salary.

Enter employee basic salary: 35000
Gross Salary: 78750.0

Write a Java program to input electricity unit charges and calculate total electricity bill

Input electricity unit charges and calculate total electricity bill

Calculate total electricity bill according to the given condition:

  • For first 50 units Tk. 1.50/unit
  • For next 100 units Tk. 2.25/unit
  • For next 100 units Tk. 3.00/unit
  • For unit above 250 Tk. 3.75/unit
An additional surcharge of 20% is added to the bill

Sample Output
Input electricity unit charges and calculate total electricity bill.

Enter unit: 50
Total cost: 90.0
Java-Source Code
//package ifelsejava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        
        double unit, surcharge, taka = 0;
        Scanner input = new Scanner(System.in);

        System.out.println("Input electricity unit charges and calculate total electricity bill.\n");

        System.out.print("Enter unit: ");
        unit = input.nextDouble();

        if (unit >= 1 && unit <= 50) {
            //For first 50 units Tk. 1.50/unit
            taka = unit * 1.5;
            surcharge = taka * 0.2; //Surcharge 20%
            taka = taka + surcharge;
            
        } else if (unit >= 51 && unit <= 100) {
            //For next 100 units Tk. 2.25/unit
            taka = unit * 2.25;
            surcharge = taka * 0.2; //Surcharge 20%
            taka = taka + surcharge;
            
        } else if (unit >= 101 && unit <= 250) {
            //For next 100 units Tk. 3.00/unit
            taka = unit * 3.0;
            surcharge = taka * 0.2; //Surcharge 20%
            taka = taka + surcharge;
            
        } else if (unit >= 251) {
            //For unit above 250 Tk. 3.75/unit
            taka = unit * 3.75;
            surcharge = taka * 0.2; //Surcharge 20%
            taka = taka + surcharge;
            
        }
        System.out.println("Total cost: " + taka);
    }
}
Sample Output
Input electricity unit charges and calculate total electricity bill.

Enter unit: 300
Total cost: 1350.0

Write a Java program to find all roots of a quadratic equation

Find all roots of a quadratic equation

A quadratic equation is same as middle term, but quadratic equation can find exact value of x1 and x2.

  • => x^2 - 5x + 6 = 0
  • Here, a = 1, b = -5 and c = 6
  • => x^2 - 3x - 2x + 6 = 0
  • => x(x - 3) - 2(x - 3) = 0
  • => x = 3, 2

Sample Output
Find all roots of a quadratic equation.

Value of a: 1
Value of b: -5
Value of c: 6
x1: 3.0
x2: 2.0
Java-Source Code
//package ifelsejava;

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

public class Main {

    public static void main(String[] args) {

        int a, b, c;
        double x1, x2, root;

        Scanner input = new Scanner(System.in);

        System.out.println("Find all roots of a quadratic equation.\n");

        System.out.print("Value of a: ");
        a = input.nextInt();
        
        System.out.print("Value of b: ");
        b = input.nextInt();
        
        System.out.print("Value of c: ");
        c = input.nextInt();
        
        root = sqrt(pow(b, 2) - (4 * a * c));
        x1 = (- b + root) / (2 * a);
        x2 = (- b - root) / (2 * a);
        
        System.out.println("x1: " + x1 + "\nx2: " + x2);
    }
}
Sample Output
Find all roots of a quadratic equation.

Value of a: 1
Value of b: 4
Value of c: 2
x1: -0.5857864376269049
x2: -3.414213562373095

Write a Java program to check whether the triangle is equilateral, isosceles or scalene triangle

Check whether the triangle is equilateral, isosceles or scalene triangle
Sample Output
Input all sides of a triangle and check whether triangle is valid or not.

Enter three sides...
4
4
5
The triangle is isosceles.
Java-Source Code
//package ifelsejava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int side1, side2, side3;

        Scanner input = new Scanner(System.in);

        System.out.println("Check whether the triangle is equilateral, isosceles or scalene triangle.\n");

        System.out.println("Enter three sides...");
        side1 = input.nextInt();
        side2 = input.nextInt();
        side3 = input.nextInt();

        if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1) {
            
            if (side1 == side2 && side2 == side3) {
                System.out.println("The triangle is equilateral.");
                
            } else if (side1 == side2 || side2 == side3 || side1 == side3) {
                System.out.println("The triangle is isosceles.");
                
            } else {
                System.out.println("The triangle is scalene.");
                
            }
        } else {
            System.out.println("The triangle is not valid.");
            
        }
    }
}
Sample Output
Check whether the triangle is equilateral, isosceles or scalene triangle.

Enter three sides...
6
6
6
The triangle is equilateral.

Write a Java program to input angles of a triangle and check whether triangle is valid or not

Input angles of a triangle and check whether triangle is valid or not
Sample Output
Input angles of a triangle and check whether triangle is valid or not.

Enter three angles...
45
45
90
Triangle is valid.
Java-Source Code
//package ifelsejava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int angle1, angle2, angle3;

        Scanner input = new Scanner(System.in);

        System.out.println("Input angles of a triangle and check whether triangle is valid or not.\n");

        System.out.println("Enter three angles...");
        angle1 = input.nextInt();
        angle2 = input.nextInt();
        angle3 = input.nextInt();

        if (angle1 + angle2 + angle3 == 180) {
            System.out.println("Triangle is valid.");
        
        } else {
            System.out.println("Triangle is not valid.");
            
        }
    }
}
Sample Output
Input angles of a triangle and check whether triangle is valid or not.

Enter three angles...
65
65
65
Triangle is not valid.

Write a Java program to input all sides of a triangle and check whether triangle is valid or not

Input all sides of a triangle and check whether triangle is valid or not
Sample Output
Input all sides of a triangle and check whether triangle is valid or not.

Enter three sides...
5
7
6
Triangle is valid.
Java-Source Code
//package ifelsejava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int side1, side2, side3;

        Scanner input = new Scanner(System.in);

        System.out.println("Input all sides of a triangle and check whether triangle is valid or not.\n");

        System.out.println("Enter three sides...");
        side1 = input.nextInt();
        side2 = input.nextInt();
        side3 = input.nextInt();

        if ((side1 + side2 > side3) && (side1 + side3 > side2) && (side2 + side3 > side1)) {
            System.out.println("Triangle is valid.");
        
        } else {
            System.out.println("Triangle is not valid.");
            
        }
    }
}
Sample Output
Input all sides of a triangle and check whether triangle is valid or not.

Enter three sides...
2
3
5
Triangle is not valid.
Change Theme
X