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 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
Change Theme
X