0% found this document useful (0 votes)
23 views9 pages

Document CS25

The document describes an algorithm to calculate the greatest common divisor (GCD) and least common multiple (LCM) of two numbers. It includes: 1. Reading two numbers from the user 2. Using the Euclidean algorithm to calculate the GCD by repeatedly dividing the larger number by the smaller number 3. Calculating the LCM using the formula LCM = (num1 * num2) / GCD 4. Displaying the results to the user

Uploaded by

mv7602456
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)
23 views9 pages

Document CS25

The document describes an algorithm to calculate the greatest common divisor (GCD) and least common multiple (LCM) of two numbers. It includes: 1. Reading two numbers from the user 2. Using the Euclidean algorithm to calculate the GCD by repeatedly dividing the larger number by the smaller number 3. Calculating the LCM using the formula LCM = (num1 * num2) / GCD 4. Displaying the results to the user

Uploaded by

mv7602456
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/ 9

Solution-Q01

Algorithm:

1. Start the program.


2. Read the first number (let's call it `num1`) and the second number
(let's call it `num2`) from the user.
3. Initialize a variable `gcd` to 0 to store the GCD (HCF) of the two
numbers.
4. Use the Euclidean algorithm to calculate the GCD (HCF) of `num1` and
`num2` as follows:
- While `num2` is not equal to 0, do the following:
- Calculate the remainder of `num1` divided by `num2`, and store it in
a temporary variable `temp`.
- Assign the value of `num2` to `num1`.
- Assign the value of `temp` to `num2`.
- When the loop ends, `num1` will hold the GCD (HCF) of the original
two numbers, which should be stored in the `gcd` variable.
5. Calculate the LCM of `num1` and `num2` using the formula: LCM =
(num1 * num2) / GCD.
6. Display the calculated GCD and LCM to the user.
7. End the program.

Program:

import java.util.Scanner;

public class GCDAndLCM {

// Method to calculate GCD of two numbers using Euclidean algorithm


public static int calculateGCD(int num1, int num2) {
while (num2 != 0) {
int temp = num2;
num2 = num1 % num2;
num1 = temp;
}
return num1;
}

// Method to calculate LCM of two numbers using GCD


public static int calculateLCM(int num1, int num2) {
int gcd = calculateGCD(num1, num2);
return (num1 * num2) / gcd;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: ");


int num1 = scanner.nextInt();

System.out.print("Enter the second number: ");


int num2 = scanner.nextInt();

// Calculate and display GCD and LCM


int gcd = calculateGCD(num1, num2);
int lcm = calculateLCM(num1, num2);

System.out.println("GCD (HCF) of " + num1 + " and " + num2 + " is: "
+ gcd);
System.out.println("LCM of " + num1 + " and " + num2 + " is: " +
lcm);

scanner.close();
}
}

Output:

Enter the first number: 22


Enter the second number: 44
GCD (HCF) of 22 and 44 is: 22
LCM of 22 and 44 is: 44

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;

public class SimpleArmstrongNumbersInRange {

// Method to check if a number is an Armstrong number


public static boolean isArmstrongNumber(int number) {
int originalNumber = number;
int sum = 0;
int numDigits = String.valueOf(number).length();

while (number > 0) {


int digit = number % 10;
sum += Math.pow(digit, numDigits);
number /= 10;
}

return sum == originalNumber;


}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the lower bound of the range: ");


int lowerBound = scanner.nextInt();

System.out.print("Enter the upper bound of the range: ");


int upperBound = scanner.nextInt();

System.out.println("Armstrong numbers between " + lowerBound +


" and " + upperBound + ":");
for (int i = lowerBound; i <= upperBound; i++) {
if (isArmstrongNumber(i)) {
System.out.print(i + " ");
}
}

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:

1. Start the program.


2. Read the number from the user and store it in a variable (let's call it
`number`).
3. Define a method `isPalindrome` 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 `reversedNumber` to 0 to store the reversed
number.
- Use a loop to extract each digit from `number`:
- Calculate the last digit of `number` (digit) using the remainder
operation (`number % 10`).
- Append the digit to the `reversedNumber` (multiply
`reversedNumber` by 10 and add `digit`).
- Remove the last digit from `number` by dividing it by 10
(`number /= 10`).
- After the loop, check if `originalNumber` is equal to
`reversedNumber`.
- If they are equal, return `true`; otherwise, return `false`.
4. Call the `isPalindrome` method with the entered `number` as an
argument.
5. Display the result to the user: If the number is a palindrome, print that
it's a palindrome; otherwise, print that it's not a palindrome.
6. End the program.

Program:

import java.util.Scanner;

public class SimplePalindromeNumber {


// Method to check if a number is a palindrome
public static boolean isPalindrome(int number) {
int reversedNumber = 0;
int originalNumber = number;

while (number > 0) {


int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;
}
return originalNumber == reversedNumber;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();

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 {

// Method to calculate and print Pascal's Triangle


public static void printPascalsTriangle(int numRows) {
for (int i = 0; i < numRows; i++) {
int number = 1;
for (int j = 0; j <= i; j++) {
System.out.print(number + " ");
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}

public static void main(String[] args) {


int numRows = 5; // Change this value to set the number of rows

System.out.println("Pascal's Triangle with " + numRows + " rows:");


printPascalsTriangle(numRows);
}
}

Output:

Pascal's Triangle with 5 rows:


1
11
121
1331
14641

Solution-Q05:
Algorithm:

1. Start the program.


2. Read the number from the user and store it in a variable (let's call it
`number`).
3. Define a method `reverseNumber` that takes an integer `number` as
input and returns an integer (the reversed number):
- Inside the method:
- Initialize a variable `reversedNumber` to 0 to store the reversed
number.
- Use a loop to extract each digit from `number`:
- Calculate the last digit of `number` (digit) using the remainder
operation (`number % 10`).
- Append the digit to the `reversedNumber` (multiply
`reversedNumber` by 10 and add `digit`).
- Remove the last digit from `number` by dividing it by 10
(`number /= 10`).
- After the loop, the `reversedNumber` will hold the reverse of the
original number.
- Return `reversedNumber`.
4. Call the `reverseNumber` method with the entered `number` as an
argument and store the result in a variable (let's call it
`reversedNumber`).
5. Display the `reversedNumber` to the user.
6. End the program.
Program:

import java.util.Scanner;

public class ReverseNumber {

// Method to find the reverse of a number


public static int reverseNumber(int number) {
int reversedNumber = 0;

while (number != 0) {
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;
}

return reversedNumber;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");


int number = scanner.nextInt();

int reversedNumber = reverseNumber(number);


System.out.println("Reverse of " + number + " is: " +
reversedNumber);

scanner.close();
}
}

Output:
Enter a number: 8990
Reverse of 8990 is: 998
….……………………………………………………………End of Assignment………………………………………………………………

You might also like