0% found this document useful (0 votes)
7 views

Java Programs 11 to 20

The document contains Java programs numbered 11 to 20, each demonstrating a specific algorithm or function. These include checking for Harshad and Automorphic numbers, reversing a number, counting digits, summing digits, checking for magic numbers, printing factors, calculating GCD and LCM, and swapping two numbers without a temporary variable. Each program is presented with its code and a brief description of its functionality.

Uploaded by

luciferajay15
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)
7 views

Java Programs 11 to 20

The document contains Java programs numbered 11 to 20, each demonstrating a specific algorithm or function. These include checking for Harshad and Automorphic numbers, reversing a number, counting digits, summing digits, checking for magic numbers, printing factors, calculating GCD and LCM, and swapping two numbers without a temporary variable. Each program is presented with its code and a brief description of its functionality.

Uploaded by

luciferajay15
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
You are on page 1/ 4

Java Programs 11 to 20

11. Check if a number is Harshad

public class HarshadNumber {


public static void main(String[] args) {
int num = 18, sum = 0, temp = num;
while (temp > 0) {
sum += temp % 10;
temp /= 10;
}
if (num % sum == 0)
System.out.println(num + " is a Harshad Number");
else
System.out.println(num + " is Not a Harshad Number");
}
}

12. Check if a number is Automorphic

public class AutomorphicNumber {


public static void main(String[] args) {
int num = 76;
int square = num * num;
String numStr = String.valueOf(num);
String squareStr = String.valueOf(square);
if (squareStr.endsWith(numStr))
System.out.println(num + " is an Automorphic Number");
else
System.out.println(num + " is Not an Automorphic Number");
}
}

13. Reverse a number

public class ReverseNumber {


public static void main(String[] args) {
int num = 1234, rev = 0;
while (num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
System.out.println("Reversed number is " + rev);
}
}

14. Count number of digits in a number

public class CountDigits {


public static void main(String[] args) {
int num = 12345, count = 0;
while (num != 0) {
num /= 10;
count++;
}
System.out.println("Number of digits: " + count);
}
}

15. Sum of digits in a number

public class SumOfDigits {


public static void main(String[] args) {
int num = 1234, sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum of digits: " + sum);
}
}

16. Check if a number is magic

public class MagicNumber {


public static void main(String[] args) {
int num = 1729;
while (num > 9) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
if (num == 1)
System.out.println("Magic Number");
else
System.out.println("Not a Magic Number");
}
}

17. Print factors of a number

public class Factors {


public static void main(String[] args) {
int num = 28;
System.out.print("Factors of " + num + ": ");
for (int i = 1; i <= num; i++) {
if (num % i == 0)
System.out.print(i + " ");
}
}
}

18. Calculate GCD of two numbers

public class GCD {


public static void main(String[] args) {
int a = 54, b = 24;
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
System.out.println("GCD is " + a);
}
}

19. Calculate LCM of two numbers

public class LCM {


public static void main(String[] args) {
int a = 12, b = 18, lcm;
int max = Math.max(a, b);
while (true) {
if (max % a == 0 && max % b == 0) {
lcm = max;
break;
}
max++;
}
System.out.println("LCM is " + lcm);
}
}

20. Swap two numbers without using third variable

public class SwapWithoutTemp {


public static void main(String[] args) {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
System.out.println("After swap: a = " + a + ", b = " + b);
}
}

You might also like