[Go to site: main page, start]

0% found this document useful (0 votes)
11 views32 pages

Java Programming Exercises List

The document is a practical file for Java programming containing a list of 40 programs, including basic operations like addition, subtraction, and multiplication, as well as more advanced concepts such as checking for prime numbers, palindromes, and calculating factorials. Each program includes a brief description and code implementation in Java, along with expected output. The file serves as a comprehensive guide for practicing Java programming skills.

Uploaded by

123ayushboss
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views32 pages

Java Programming Exercises List

The document is a practical file for Java programming containing a list of 40 programs, including basic operations like addition, subtraction, and multiplication, as well as more advanced concepts such as checking for prime numbers, palindromes, and calculating factorials. Each program includes a brief description and code implementation in Java, along with expected output. The file serves as a comprehensive guide for practicing Java programming skills.

Uploaded by

123ayushboss
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Programming Practical File.

 List of Programs.

1. Print “Hello, World!”

2. Add two numbers

3. Subtract two numbers

4. Multiply two numbers

5. Divide two numbers

6. Find the remainder of two numbers

7. Swap two numbers using a third variable

8. Swap two numbers without using a third variable

9. Find the largest of two numbers

10. Find the largest of three numbers

11. Check if a number is even or odd

12. Check if a number is positive, negative, or zero

13. Find the factorial of a number

14. Print the Fibonacci series

15. Check if a number is prime

16. Check if a number is palindrome

17. Check if a string is palindrome


1
18. Find the sum of digits of a number

19. Reverse a number

20. Calculate the power of a number (using loops)

21. Find the greatest common divisor (GCD) of two numbers

22. Find the least common multiple (LCM) of two numbers

23. Count the number of digits in a number

24. Print multiplication table of a given number

25. Check whether a year is a leap year

26. Check if a number is Armstrong

27. Check if a number is Strong (factorial sum equals number)

28. Check if a number is Perfect

29. Display all factors of a number

30. Find sum of first N natural numbers

31. Find sum of even and odd numbers separately

32. Print all prime numbers between 1 to 100

33. Print all Armstrong numbers between 1 to 1000

34. Find the reverse of a string

35. Count vowels and consonants in a string

36. Count words in a sentence

37. Check if two strings are anagrams

38. Convert string to uppercase and lowercase

39. Sort characters of a string in alphabetical order

40. Find the frequency of characters in a string.

2
1. Print "Hello, World!"

class HelloWorld {

public static void main(String[] args) {

[Link]("Hello, World!");

Output:

Hello, World!

2. Add Two Numbers

class AddTwoNumbers {

public static void main(String[] args) {

int a = 10, b = 20;

int sum = a + b;

[Link]("Sum = " + sum);

Output:

Sum = 30

3
3. Subtract Two Numbers

class SubtractTwoNumbers {

public static void main(String[] args) {

int a = 50, b = 20;

int diff = a - b;

[Link]("Difference = " + diff);

Output:

Difference = 30

4. Multiply Two Numbers

class MultiplyTwoNumbers {

public static void main(String[] args) {

int a = 5, b = 4;

int product = a * b;

[Link]("Product = " + product);

4
Output:

Product = 20

5. Divide Two Numbers

class DivideTwoNumbers {

public static void main(String[] args) {

int a = 20, b = 5;

int quotient = a / b;

[Link]("Quotient = " + quotient);

Output:

Quotient = 4

6. Find the Remainder

class Remainder {

public static void main(String[] args) {

int a = 20, b = 3;

int remainder = a % b;

[Link]("Remainder = " + remainder);

5
}

Output:

Remainder = 2

7. Swap Two Numbers (using third variable)

class SwapWithThird {

public static void main(String[] args) {

int a = 5, b = 10, temp;

temp = a;

a = b;

b = temp;

[Link]("After swapping: a = " + a + ", b = " + b);

Output:

After swapping: a = 10, b = 5

8. Swap Without Third Variable

class SwapWithoutThird {

6
public static void main(String[] args) {

int a = 5, b = 10;

a = a + b;

b = a - b;

a = a - b;

[Link]("After swapping: a = " + a + ", b = " + b);

Output:

After swapping: a = 10, b = 5

9. Largest of Two Numbers

class LargestOfTwo {

public static void main(String[] args) {

int a = 15, b = 25;

if(a > b)

[Link](a + " is larger");

else

[Link](b + " is larger");

7
}

Output:

25 is larger

10. Largest of Three Numbers

class LargestOfThree {

public static void main(String[] args) {

int a = 10, b = 20, c = 15;

if(a >= b && a >= c)

[Link](a + " is largest");

else if(b >= a && b >= c)

[Link](b + " is largest");

else

[Link](c + " is largest");

Output:

20 is largest

11. Even or Odd

8
class EvenOdd {

public static void main(String[] args) {

int n = 7;

if(n % 2 == 0)

[Link](n + " is even");

else

[Link](n + " is odd");

Output:

7 is odd

12. Positive, Negative or Zero

class NumberCheck {

public static void main(String[] args) {

int n = -10;

if(n > 0)

[Link]("Positive");

else if(n < 0)

[Link]("Negative");

9
else

[Link]("Zero");

Output:

Negative

13. Factorial of a Number

class Factorial {

public static void main(String[] args) {

int n = 5;

int fact = 1;

for(int i = 1; i <= n; i++)

fact *= i;

[Link]("Factorial = " + fact);

Output:

Factorial = 120

10
14. Fibonacci Series

class Fibonacci {

public static void main(String[] args) {

int n = 10, a = 0, b = 1;

[Link]("Fibonacci Series: " + a + " " + b);

for(int i = 2; i < n; i++) {

int c = a + b;

[Link](" " + c);

a = b;

b = c;

Output:

Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

15. Prime Number Check

class PrimeCheck {

public static void main(String[] args) {

int n = 7;

11
boolean prime = true;

for(int i = 2; i <= n/2; i++) {

if(n % i == 0) {

prime = false;

break;

if(prime)

[Link](n + " is a prime number");

else

[Link](n + " is not a prime number");

Output:

7 is a prime number

16. Palindrome Number

class PalindromeNumber {

public static void main(String[] args) {

int n = 121, temp = n, rev = 0;

12
while(n > 0) {

rev = rev * 10 + n % 10;

n /= 10;

if(temp == rev)

[Link]("Palindrome number");

else

[Link]("Not a palindrome");

Output:

Palindrome number

17. Palindrome String

class PalindromeString {

public static void main(String[] args) {

String str = "madam", rev = "";

for(int i = [Link]() - 1; i >= 0; i--)

rev += [Link](i);

if([Link](rev))

13
[Link]("Palindrome string");

else

[Link]("Not a palindrome");

Output:

Palindrome string

18. Sum of Digits

class SumOfDigits {

public static void main(String[] args) {

int n = 1234, sum = 0;

while(n > 0) {

sum += n % 10;

n /= 10;

[Link]("Sum of digits = " + sum);

Output:

14
Sum of digits = 10

19. Reverse a Number

class ReverseNumber {

public static void main(String[] args) {

int n = 1234, rev = 0;

while(n > 0) {

rev = rev * 10 + n % 10;

n /= 10;

[Link]("Reversed number = " + rev);

Output:

Reversed number = 4321

20. Power of a Number

class PowerOfNumber {

public static void main(String[] args) {

int base = 2, exp = 5;

15
int result = 1;

for(int i = 1; i <= exp; i++)

result *= base;

[Link](base + "^" + exp + " = " + result);

Output:

2^5 = 32

21. GCD of Two Numbers

class GCD {

public static void main(String[] args) {

int a = 36, b = 60;

while(b != 0) {

int temp = b;

b = a % b;

a = temp;

[Link]("GCD = " + a);

16
}

Output:

GCD = 12

22. LCM of Two Numbers

class LCM {

public static void main(String[] args) {

int a = 12, b = 15;

int gcd = a, temp = b;

while(temp != 0) {

int t = temp;

temp = gcd % temp;

gcd = t;

int lcm = (a * b) / gcd;

[Link]("LCM = " + lcm);

Output:

LCM = 60

17
23. Count Digits in a Number

class CountDigits {

public static void main(String[] args) {

int n = 12345, count = 0;

while(n > 0) {

n /= 10;

count++;

[Link]("Number of digits = " + count);

Output:

Number of digits = 5

24. Multiplication Table

class MultiplicationTable {

public static void main(String[] args) {

int n = 5;

for(int i = 1; i <= 10; i++)

18
[Link](n + " x " + i + " = " + (n * i));

Output:

5x1=5

5 x 2 = 10

5 x 3 = 15

...

5 x 10 = 50

25. Leap Year Check

class LeapYear {

public static void main(String[] args) {

int year = 2024;

if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))


[Link](year + " is a leap year");
else
[Link](year + " is not a leap year");
}
}
Output:
2024 is a leap year.
26. Check Armstrong Number

19
(Armstrong number → sum of cubes of digits = number itself, e.g., 153)

class ArmstrongNumber {

public static void main(String[] args) {

int n = 153, temp = n, sum = 0;

while (n > 0) {

int digit = n % 10;

sum += digit * digit * digit;

n /= 10;

if (sum == temp)

[Link](temp + " is an Armstrong number");

else

[Link](temp + " is not an Armstrong number");

Output:

153 is an Armstrong number

27. Check Strong Number

(A number is strong if sum of factorial of its digits = number itself, e.g., 145)

20
class StrongNumber {

public static void main(String[] args) {

int n = 145, temp = n, sum = 0;

while (n > 0) {

int digit = n % 10, fact = 1;

for (int i = 1; i <= digit; i++)

fact *= i;

sum += fact;

n /= 10;

if (sum == temp)

[Link](temp + " is a Strong number");

else

[Link](temp + " is not a Strong number");

Output:

145 is a Strong number

28. Check Perfect Number

21
(Perfect number → sum of divisors = number, e.g., 6 → 1+2+3=6)

class PerfectNumber {

public static void main(String[] args) {

int n = 6, sum = 0;

for (int i = 1; i <= n / 2; i++) {

if (n % i == 0)

sum += i;

if (sum == n)

[Link](n + " is a Perfect number");

else

[Link](n + " is not a Perfect number");

Output:

6 is a Perfect number

29. Display All Factors of a Number

class Factors {

public static void main(String[] args) {

22
int n = 12;

[Link]("Factors of " + n + ": ");

for (int i = 1; i <= n; i++) {

if (n % i == 0)

[Link](i + " ");

Output:

Factors of 12: 1 2 3 4 6 12

30. Sum of First N Natural Numbers

class SumNatural {

public static void main(String[] args) {

int n = 10, sum = 0;

for (int i = 1; i <= n; i++)

sum += i;

[Link]("Sum = " + sum);

23
Output:

Sum = 55

31. Sum of Even and Odd Numbers Separately

class SumEvenOdd {

public static void main(String[] args) {

int n = 10, evenSum = 0, oddSum = 0;

for (int i = 1; i <= n; i++) {

if (i % 2 == 0)

evenSum += i;

else

oddSum += i;

[Link]("Even Sum = " + evenSum);

[Link]("Odd Sum = " + oddSum);

Output:

Even Sum = 30

Odd Sum = 25

24
32. Print Prime Numbers Between 1 to 100

class PrimeRange {

public static void main(String[] args) {

[Link]("Prime numbers between 1 and 100:");

for (int n = 2; n <= 100; n++) {

boolean prime = true;

for (int i = 2; i <= n / 2; i++) {

if (n % i == 0) {

prime = false;

break;

if (prime)

[Link](n + " ");

Output (Partial):

Prime numbers between 1 and 100:

25
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

33. Print Armstrong Numbers Between 1 to 1000

class ArmstrongRange {

public static void main(String[] args) {

[Link]("Armstrong numbers between 1 and 1000:");

for (int n = 1; n <= 1000; n++) {

int temp = n, sum = 0;

while (temp > 0) {

int d = temp % 10;

sum += d * d * d;

temp /= 10;

if (sum == n)

[Link](n + " ");

Output:

Armstrong numbers between 1 and 1000:

26
1 153 370 371 407

34. Reverse a String

class ReverseString {

public static void main(String[] args) {

String str = "Hello", rev = "";

for (int i = [Link]() - 1; i >= 0; i--)

rev += [Link](i);

[Link]("Reversed string: " + rev);

Output:

Reversed string: olleH

35. Count Vowels and Consonants in a String

class VowelConsonant {

public static void main(String[] args) {

String str = "Java Programming".toLowerCase();

int vowels = 0, consonants = 0;

for (int i = 0; i < [Link](); i++) {

27
char ch = [Link](i);

if (ch >= 'a' && ch <= 'z') {

if ("aeiou".indexOf(ch) != -1)

vowels++;

else

consonants++;

[Link]("Vowels: " + vowels);

[Link]("Consonants: " + consonants);

Output:

Vowels: 5

Consonants: 9

36. Count Words in a Sentence

class WordCount {

public static void main(String[] args) {

String str = "Java is fun to learn";

28
String[] words = [Link](" ");

[Link]("Number of words = " + [Link]);

Output:

Number of words = 5

37. Check if Two Strings Are Anagrams

import [Link];

class AnagramCheck {

public static void main(String[] args) {

String s1 = "listen", s2 = "silent";

char[] a1 = [Link]();

char[] a2 = [Link]();

[Link](a1);

[Link](a2);

if ([Link](a1, a2))

[Link]("Strings are Anagrams");

else

29
[Link]("Strings are not Anagrams");

Output:

Strings are Anagrams

38. Convert String to Uppercase and Lowercase

class StringCase {

public static void main(String[] args) {

String str = "Java Programming";

[Link]("Uppercase: " + [Link]());

[Link]("Lowercase: " + [Link]());

Output:

Uppercase: JAVA PROGRAMMING

Lowercase: java programming

39. Sort Characters of a String

import [Link];

30
class SortString {

public static void main(String[] args) {

String str = "program";

char[] arr = [Link]();

[Link](arr);

[Link]("Sorted string: " + new String(arr));

Output:

Sorted string: agmoprr

40. Frequency of Each Character

class CharFrequency {

public static void main(String[] args) {

String str = "banana";

int[] freq = new int[256];

for (int i = 0; i < [Link](); i++)

freq[[Link](i)]++;

[Link]("Character frequencies:");

31
for (int i = 0; i < 256; i++) {

if (freq[i] != 0)

[Link]((char)i + " → " + freq[i]);

Output:

Character frequencies:

a→3

b→1

n→2

----------------------------------------------------------------------------------------------------------------

32

You might also like