Professional Documents
Culture Documents
Algorithm:
Program:
import java.util.Scanner;
System.out.println("GCD (HCF) of " + num1 + " and " + num2 + " is: "
+ gcd);
System.out.println("LCM of " + num1 + " and " + num2 + " is: " +
lcm);
scanner.close();
}
}
Output:
Solution-Q02:
Algorithm:
1. Start the program.
2. Read the lower bound of the range (let's call it `lowerBound`) and the
upper bound of the range (let's call it `upperBound`) from the user.
3. Define a method `isArmstrongNumber` that takes an integer `number`
as input and returns a boolean value:
- Inside the method:
- Initialize a variable `originalNumber` to store the original value of
`number`.
- Initialize a variable `sum` to store the sum of the digits raised to the
power of the number of digits in the number (`numDigits`).
- Calculate the number of digits in `number` and store it in
`numDigits` by converting the number to a string and getting its length.
- Use a loop to extract each digit from `number` and add the digit
raised to the power of `numDigits` to the `sum`.
- Reduce the value of `number` by removing the last digit in each
iteration of the loop.
- Check if the `sum` is equal to the `originalNumber`.
- Return `true` if the number is an Armstrong number; otherwise,
return `false`.
4. Print the message asking the user to enter the range and read the
values of `lowerBound` and `upperBound` from the user.
5. Print the message indicating the range of numbers to be checked for
Armstrong numbers.
6. Use a loop to iterate from `lowerBound` to `upperBound` (inclusive)
and do the following in each iteration:
- Check if the current number is an Armstrong number using the
`isArmstrongNumber` method.
- If it is an Armstrong number, print it.
7. End the program.
Program:
import java.util.Scanner;
scanner.close();
}
}
Output:
Enter the lower bound of the range: 99
Enter the upper bound of the range: 199
Armstrong numbers between 99 and 199:
153
Solution-Q03:
Algorithm:
Program:
import java.util.Scanner;
if (isPalindrome(number)) {
System.out.println(number + " is a palindrome number.");
} else {
System.out.println(number + " is not a palindrome number.");
}
scanner.close();
}
}
Output:
Enter a number: 88
88 is a palindrome number.
Solution-Q04:
Algorithm:
1. Start the program.
2. Define a method `printPascalsTriangle` that takes the number of rows
to be printed as input (let's call it `numRows`):
- Inside the method:
- Use a loop to iterate through each row from 0 to `numRows - 1`:
- Initialize a variable `number` to 1 for each row, as the first and last
element of each row in Pascal's Triangle is 1.
- Use another loop to print the elements of each row:
- Print the value of `number`.
- Calculate the next value of `number` for the next element in the
row:
- `number = number * (i - j) / (j + 1)` where `i` is the row index and
`j` is the column index.
- Separate each element with a space.
- Move to the next line after printing all the elements of the current
row.
3. In the `main` method:
- Hardcode the number of rows you want for Pascal's Triangle (let's say
`numRows = 5`, for example).
- Print a message indicating the number of rows you are going to print.
- Call the `printPascalsTriangle` method with the specified number of
rows as an argument.
4. End the program.
Program:
public class PascalTriangle {
Output:
Solution-Q05:
Algorithm:
import java.util.Scanner;
while (number != 0) {
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;
}
return reversedNumber;
}
scanner.close();
}
}
Output:
Enter a number: 8990
Reverse of 8990 is: 998
….……………………………………………………………End of Assignment………………………………………………………………