0% found this document useful (0 votes)
12 views4 pages

Calculate GCD in Java

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)
12 views4 pages

Calculate GCD in Java

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

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)
[Link](num + " is a Harshad Number");
else
[Link](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 = [Link](num);
String squareStr = [Link](square);
if ([Link](numStr))
[Link](num + " is an Automorphic Number");
else
[Link](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;
}
[Link]("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++;
}
[Link]("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;
}
[Link]("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)
[Link]("Magic Number");
else
[Link]("Not a Magic Number");
}
}

17. Print factors of a number

public class Factors {


public static void main(String[] args) {
int num = 28;
[Link]("Factors of " + num + ": ");
for (int i = 1; i <= num; i++) {
if (num % i == 0)
[Link](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;
}
[Link]("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 = [Link](a, b);
while (true) {
if (max % a == 0 && max % b == 0) {
lcm = max;
break;
}
max++;
}
[Link]("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;
[Link]("After swap: a = " + a + ", b = " + b);
}
}

Common questions

Powered by AI

The program determines if a number is a Harshad number by calculating the sum of its digits and then checking if the original number is divisible by this sum without a remainder. This uses the mathematical concept of divisibility: if a number is divisible by the sum of its digits, it is a Harshad number .

The program terminates its loop when it finds a multiple of both input numbers by incrementing the larger of the two numbers until it is divisible by both. This condition ensures it finds the least common multiple (LCM) as it continuously checks using the modulus operator .

The program identifies a 'Magic Number' by iteratively summing its digits until a single digit remains. If this final digit is 1, the number is a Magic Number, otherwise it is not. This checks if repeated applications of summing the digits reduce the number to 1 .

The program reverses a number by repeatedly extracting the last digit of the number using the modulus operator, accumulating these digits into a new variable as its least significant digits, and then dividing the original number by 10 to remove the processed digit. This approach is effective because it directly transforms the number digit by digit into its reverse .

The program relies on the Euclidean algorithm to compute the greatest common divisor (GCD) of two numbers. It iteratively sets one number to the remainder of the division of the two numbers until the remainder becomes zero. The other number at this point is the GCD .

The program uses string conversion to directly compare the ending digits of the square and the original number. This method leverages string operations, which handle leading zeros and varying digit lengths, effectively addressing potential numeric precision issues and manual manipulation errors .

The program verifies if a number is Automorphic by first calculating the square of the number and converting both the number and its square to strings. It checks if the square string ends with the number string, thus confirming the automorphism property .

The program swaps two numbers without using a third variable by employing arithmetic operations. It adds both numbers, stores the result in one, then reconstructs the original values by subtracting from the sum. Specifically, it sets a = a + b, assigns b = a - b, thus b becomes the original a, and then assigns a = a - b, making a the original b .

Identifying the factors of a number is significant for various computational problems, including optimization, cryptography, and algorithm performance evaluation. The program precisely finds all divisors up to the number, establishing a foundation for more complex operations such as prime factorization, which is crucial in encryption algorithms .

The program calculates the sum of digits by using a loop to repeatedly extract the last digit of the number using the modulus operator and adding it to an accumulator. It then truncates the last digit by integer division, effectively iterating over each digit. Its complexity is O(n), where n is the number of digits, since it processes each digit exactly once .

You might also like