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

Algorithms

Uploaded by

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

Algorithms

Uploaded by

attarimohsin56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

HarshadNumber

Step 1. Import the java class Scanner.


Step 2. Class name declared "HarshadNumber".
Step 3. The main method started as "public static void main(String args[])".
Step 4. Create a Scanner class object "sc".
Step 5. Prompt the user to enter a number.
Step 6. Accept an integer input from the user and store it in variable "n".
Step 7. Initialize variable "c" to the value of "n" to retain the original number.
Step 8. Initialize variables "d" for digit extraction and "sum" to 0 to hold the sum of digits.
Step 9. Use a while loop to calculate the sum of the digits of "n" until "c" becomes 0.
Step 10. Inside the loop, extract the last digit by "d = c % 10".
Step 11. Add the extracted digit "d" to "sum".
Step 12. Remove the last digit from "c" using "c = c / 10".
Step 13. After the loop, check if "n" is divisible by "sum" using "if(n % sum == 0)".
Step 14. If true, print "{n} is a Harshad Number."
Step 15. Else, print "{n} is not a Harshad Number."
Step 16. End of the method.
Step 17. End of the class.
HCF
Step 1. Import the java class Scanner.
Step 2. Class name declared "HCF".
Step 3. The main method started as "public static void main(String args[])".
Step 4. Create a Scanner class object "sc".
Step 5. Prompt the user to enter the first number.
Step 6. Accept the first integer input from the user and store it in variable "n1".
Step 7. Prompt the user to enter the second number.
Step 8. Accept the second integer input from the user and store it in variable "n2".
Step 9. Initialize variable "r" to hold the remainder.
Step 10. Use a while loop to iterate until "n2" becomes 0.
Step 11. Inside the loop, calculate the remainder of "n1" divided by "n2" and store it in "r".
Step 12. Assign the value of "n2" to "n1".
Step 13. Assign the value of "r" to "n2".
Step 14. After the loop, print "HCF = {n1}".
Step 15. End of the method.
Step 16. End of the class
AutomorphicNumber
Step 1. Import the java class Scanner.
Step 2. Class name declared "AutomorphicNumber".
Step 3. The main method started as "public static void main(String args[])".
Step 4. Create a Scanner class object "sc".
Step 5. Prompt the user to enter a number.
Step 6. Accept an integer input from the user and store it in variable "num".
Step 7. Initialize variable "c" to 0 to count the digits of the number.
Step 8. Calculate the square of "num" and store it in variable "sqr".
Step 9. Create a temporary variable "temp" and copy the value of "num" to it.
Step 10. Use a while loop to count the digits of "num".
Step 11. Inside the loop, increment "c" by 1 for each digit and update "temp" by dividing it by 10
until it becomes 0.
Step 12. Calculate the last "c" digits of "sqr" using "sqr % (Math.pow(10, c))" and store it in
"lastSquareDigits".
Step 13. Check if "num" is equal to "lastSquareDigits" using "if(num == lastSquareDigits)".
Step 14. If true, print "Automorphic number".
Step 15. Else, print "Not an Automorphic number".
Step 16. End of the method.
Step 17. End of the class.
Short_Long_Word
Step 1. Import the java class Scanner.
Step 2. Class name declared "Short_long_word".
Step 3. The main method started as "public static void main(String args[])".
Step 4. Create a Scanner class object "sc".
Step 5. Prompt the user to enter a sentence.
Step 6. Accept the input sentence from the user and store it in the string variable "s".
Step 7. Append a space to the end of the string "s" to handle the last word.
Step 8. Calculate the length of the string "s" and store it in variable "len".
Step 9. Initialize variables "x", "maxw", and "minw" to hold the current word, longest word, and
shortest word, respectively.
Step 10. Initialize variables "ch", "p", "maxl" to 0 for maximum length, and "minl" to "len" for
minimum length.
Step 11. Use a for loop to iterate through each character in the string "s".
Step 12. Inside the loop, check if the character "ch" is not a space.
Step 13. If true, append "ch" to the variable "x".
Step 14. If "ch" is a space, get the length of the current word "x" and store it in "p".
Step 15. Check if "p" is less than "minl". If true, update "minl" with "p" and "minw" with "x".
Step 16. Check if "p" is greater than "maxl". If true, update "maxl" with "p" and "maxw" with "x".
Step 17. Reset "x" to an empty string.
Step 18. After the loop, print the shortest word and its length: "Shortest word = {minw}, Length =
{minl}".
Step 19. Print the longest word and its length: "Longest word = {maxw}, Length = {maxl}".
Step 20. End of the method.
Step 21. End of the class.
Special2DigitNumber
Step 1. Import the java class Scanner.
Step 2. Class name declared "Special2digitNumber".
Step 3. The main method started as "public static void main(String args[])".
Step 4. Initialize integer variables "product" to 1, "sum" to 0, and "t" to 0.
Step 5. Create a Scanner class object "sc".
Step 6. Prompt the user to enter a two-digit number.
Step 7. Accept the integer input from the user and store it in variable "n".
Step 8. Calculate the product of the digits using "(n % 10) * (n / 10)" and assign it to "product".
Step 9. Calculate the sum of the digits using "(n % 10) + (n / 10)" and assign it to "sum".
Step 10. Calculate "t" as the sum of "product" and "sum" using "t = product + sum".
Step 11. Check if "t" is equal to "n" using "if(t == n)".
Step 12. If true, print "SPECIAL TWO DIGIT NUMBER".
Step 13. Else, print "NOT A SPECIAL TWO DIGIT NUMBER".
Step 14. End of the method.
Step 15. End of the class.
FrequencyOfEachDigit
Step 1. Import the java class Scanner.
Step 2. Class name declared "FrequencyOfEachDigit".
Step 3. The main method started as "public static void main(String args[])".
Step 4. Create a Scanner class object "sc".
Step 5. Prompt the user to enter a number.
Step 6. Accept the integer input from the user and store it in variable "n".
Step 7. Declare an array "f" of size 10 to hold the frequency of each digit.
Step 8. Use a for loop to initialize each element of the array "f" to 0.
Step 9. Print the header for the output table: "DIGIT" and "FREQUENCY".
Step 10. Declare an integer variable "d" to hold the current digit.
Step 11. Use a while loop to extract each digit from "n" until "n" becomes 0.
Step 12. Inside the loop, get the last digit using "d = n % 10".
Step 13. Increment the frequency of the digit by 1 using "f[d]++".
Step 14. Remove the last digit from "n" using "n = n / 10".
Step 15. After the loop, use a for loop to iterate through each digit from 0 to 9.
Step 16. Inside the loop, check if the frequency of the digit "f[i]" is not equal to 0.
Step 17. If true, print the digit "i" and its frequency "f[i]".
Step 18. End of the method.
Step 19. End of the class.
Frequency_ALPHABET
Step 1. Import the java class Scanner.
Step 2. Class name declared "Frequency_ALPHABET".
Step 3. The main method started as "public static void main(String args[])".
Step 4. Create a Scanner class object "sc".
Step 5. Prompt the user to enter a string.
Step 6. Accept the input string from the user and store it in variable "s".
Step 7. Convert the string "s" to lowercase to ensure uniformity in counting.
Step 8. Calculate the length of the string "s" and store it in variable "l".
Step 9. Declare a character variable "ch" to hold individual characters.
Step 10. Print the header for the output table: "Alphabet" and "Frequency".
Step 11. Initialize an integer variable "count" to 0 for counting the frequency of each alphabet.
Step 12. Use a for loop to iterate through each alphabet from 'a' to 'z'.
Step 13. Inside the loop, reset "count" to 0 for each alphabet.
Step 14. Use a nested for loop to iterate through each character in the string "s".
Step 15. Inside the nested loop, assign the character at index "j" of "s" to "ch".
Step 16. Check if the character "ch" is equal to the current alphabet "i".
Step 17. If true, increment the count for that alphabet.
Step 18. After the nested loop, check if "count" is not equal to 0.
Step 19. If true, print the alphabet "i" and its frequency "count".
Step 20. End of the method.
Step 21. End of the class.
MERGE_2_ARRAY
Step 1. Import the java utility package.
Step 2. Class name declared "MERGE_2_ARRAY".
Step 3. The main method started as "public static void main(String args[])".
Step 4. Declare integer variables "m", "n", and "i".
Step 5. Create a Scanner class object "in".
Step 6. Prompt the user to enter the sizes of the two arrays.
Step 7. Accept the sizes of the arrays and store them in "m" and "n".
Step 8. Declare an integer array "A" of size "m".
Step 9. Declare an integer array "B" of size "n".
Step 10. Prompt the user to enter the elements of the first array.
Step 11. Use a for loop to iterate from 0 to "m" to accept elements for array "A".
Step 12. Inside the loop, store each element in the array "A".
Step 13. Prompt the user to enter the elements of the second array.
Step 14. Use a for loop to iterate from 0 to "n" to accept elements for array "B".
Step 15. Inside the loop, store each element in the array "B".
Step 16. Print a message indicating that the third array will be displayed after merging.
Step 17. Declare an integer array "C" of size "m + n" to hold the merged array.
Step 18. Use a for loop to copy elements from array "A" to array "C".
Step 19. Use another for loop to copy elements from array "B" to array "C", starting from index
"m".
Step 20. Use a final for loop to iterate through array "C" and print each element.
Step 21. End of the method.
Step 22. End of the class.
Binary_SEARCH
Step 1. Import the java class Scanner.
Step 2. Class name declared "Binary_SEARCH".
Step 3. The main method started as "public static void main(String args[])".
Step 4. Declare integer variables: "counter", "num", "item", "array[]", "first", "last", and "middle".
Step 5. Create a Scanner class object "input".
Step 6. Prompt the user to enter the number of elements.
Step 7. Accept the number of elements and store it in "num".
Step 8. Initialize the integer array "array" with size "num".
Step 9. Prompt the user to enter "num" integers.
Step 10. Use a for loop to iterate from 0 to "num" to accept elements for the array.
Step 11. Store each entered integer in the array "array[counter]".
Step 12. Prompt the user to enter the search value.
Step 13. Accept the integer value to search for and store it in "item".
Step 14. Initialize "first" to 0 and "last" to "num - 1".
Step 15. Calculate the initial "middle" index using "middle = (first + last) / 2".
Step 16. Use a while loop to continue searching while "first" is less than or equal to "last".
Step 17. Inside the loop, check if the value at "array[middle]" is less than "item".
Step 18. If true, update "first" to "middle + 1".
Step 19. Else if "array[middle]" is equal to "item", print that "item" is found at location "(middle +
1)" and break the loop.
Step 20. Else, update "last" to "middle - 1".
Step 21. Recalculate the "middle" index after updating "first" or "last".
Step 22. After the loop, check if "first" is greater than "last".
Step 23. If true, print that "item" is not found.
Step 24. End of the method.
Step 25. End of the class.
reverse
Step 1. Import the java class Scanner.
Step 2. Class name declared "reverse".
Step 3. The main method started as "public static void main(String args[])".
Step 4. Declare string variables "s", "w", and "r", and initialize "w" and "r" as empty strings.
Step 5. Declare a character variable "x".
Step 6. Declare integer variables "l" and "i".
Step 7. Create a Scanner class object "sc".
Step 8. Prompt the user to enter a sentence.
Step 9. Accept the input sentence and store it in the variable "s".
Step 10. Append a space to the end of the string "s" to handle the last word.
Step 11. Calculate the length of the string "s" and store it in "l".
Step 12. Use a for loop to iterate from 0 to "l - 1".
Step 13. Inside the loop, assign the character at index "i" of "s" to "x".
Step 14. Check if the character "x" is not a space.
Step 15. If true, concatenate "x" to "w".
Step 16. If "x" is a space, concatenate "w" to "r" and then concatenate a space to "r".
Step 17. After concatenating, reset "w" to an empty string to start collecting the next word.
Step 18. Continue the loop until all characters are processed.
Step 19. After the loop, print the reversed sentence stored in "r".
Step 20. End of the method.
Step 21. End of the class.
EvilNumber
Step 1. Import the java.util package.
Step 2. Class name declared "EvilNumber".
Step 3. Define a method "toBinary(int n)" that converts a decimal number to its binary
representation.
Step 4. Inside "toBinary", declare variables: integer "r" and string "s", and initialize "s" as an
empty string.
Step 5. Create a character array "dig" with elements '0' and '1'.
Step 6. Use a while loop to convert the decimal number "n" to binary:

● Step 6.1. Inside the loop, calculate "r" as "n % 2".


● Step 6.2. Prepend "dig[r]" to string "s".
● Step 6.3. Update "n" to "n / 2".
Step 7. Return the binary string "s".
Step 8. Define a method "countOne(String s)" that counts the number of '1's in the binary
string "s".
Step 9. Inside "countOne", declare variables: integer "c" initialized to 0 and integer "l" as
the length of "s".
Step 10. Use a for loop to iterate through the string "s":
● Step 10.1. If the character at index "i" is '1', increment "c".
Step 11. Return the count "c".
Step 12. In the main method, create an instance of the "EvilNumber" class.
Step 13. Create a Scanner object "sc".
Step 14. Prompt the user to enter a positive number.
Step 15. Read the integer input and store it in "n".
Step 16. Call the "toBinary" method and store the returned binary string in "bin".
Step 17. Print the binary equivalent.
Step 18. Call the "countOne" method with "bin" and store the result in "x".
Step 19. Print the number of '1's found.
Step 20. Check if "x" is even:
● Step 20.1. If true, print that "n" is an Evil Number.
● Step 20.2. If false, print that "n" is Not an Evil Number.
Step 21. End of the method.
Step 22. End of the class.
PigLatin
Step 1. Import the java.util package.
Step 2. Class name declared "PigLatin".
Step 3. In the main method:

● Step 3.1. Create a Scanner object "in".


● Step 3.2. Prompt the user to enter a word.
● Step 3.3. Read the input word as a string "s".
● Step 3.4. Convert the string "s" to lowercase.
Step 4. Initialize an integer variable "i" to 0.
Step 5. Use a for loop to iterate through each character of the string "s":
● Step 5.1. Inside the loop, get the character "ch" at index "i".
● Step 5.2. If "ch" is a vowel (i.e., 'a', 'e', 'i', 'o', 'u'), break the loop.
Step 6. Create the Pig Latin word "f" by concatenating the substring of "s" from index "i"
to the end, the substring from the start to index "i", and then append "ay".
Step 7. Print the Pig Latin word "f".
Step 8. End of the method.
Step 9. End of the class.
BubbleSort
Step 1. Import the java.util.Scanner package.
Step 2. Class name declared "BubbleSort".
Step 3. In the main method:

● Step 3.1. Declare integer variables "num", "i", "j", and "temp".
● Step 3.2. Create a Scanner object "input".
● Step 3.3. Prompt the user to enter the number of integers to sort and read it into "num".
● Step 3.4. Declare an integer array "array" of size "num".
● Step 3.5. Prompt the user to enter "num" integers and read them into the array.
Step 4. Use a for loop to iterate from 0 to (num - 1) (let's call this loop index "i"):
● Step 4.1. Inside this loop, use another for loop to iterate from 0 to (num - i - 1) (let's call
this loop index "j"):
○ Step 4.1.1. If "array[j]" is greater than "array[j + 1]", swap the two elements:
■ Step 4.1.1.1. Set "temp" to "array[j]".
■ Step 4.1.1.2. Assign "array[j + 1]" to "array[j]".
■ Step 4.1.1.3. Assign "temp" to "array[j + 1]".
Step 5. After sorting, print the sorted list of integers by iterating through
the "array".
Step 6. End of the method.
Step 7. End of the class.
EmployeeTax
Step 1. Import the java.util.Scanner package.
Step 2. Declare a public class named "EmployeeTax".
Step 3. Define a method "tax" that takes a String parameter "name" and an integer parameter
"income":

● Step 3.1. Declare a double variable "tax".


● Step 3.2. Check if "income" is less than or equal to 250,000:
○ Step 3.2.1. If true, set "tax" to 0.
● Step 3.3. Else, check if "income" is less than or equal to 500,000:
○ Step 3.3.1. If true, calculate tax as (income - 250,000) * 0.1.
● Step 3.4. Else, check if "income" is less than or equal to 1,000,000:
○ Step 3.4.1. If true, calculate tax as 30,000 + ((income - 500,000) * 0.2).
● Step 3.5. Else, calculate tax as 50,000 + ((income - 1,000,000) * 0.3).
● Step 3.6. Print the employee's name and the calculated income tax.
Step 4. In the main method:
● Step 4.1. Create a Scanner object "in".
● Step 4.2. Prompt the user to enter their name and read it into the String variable "n".
● Step 4.3. Prompt the user to enter their annual income and read it into the integer
variable "i".
● Step 4.4. Create an instance of EmployeeTax named "obj".
● Step 4.5. Call the "tax" method on "obj" with "n" and "i" as arguments.
Step 5. End of the method.
Step 6. End of the class.
Area
Step 1. Import the java.util.Scanner package.
Step 2. Declare a public class named "Area".
Step 3. Define three overloaded methods named "area":

● Step 3.1. Method 1:


○ Parameters: double base, double height.
○ Return type: double.
○ Calculate the area of a parallelogram using the formula: area = base *
height.
● Step 3.2. Method 2:
○ Parameters: double c (for the constant 0.5), double d1 (first diagonal), double d2
(second diagonal).
○ Return type: double.
○ Calculate the area of a rhombus using the formula: area = 0.5 * d1 * d2.
● Step 3.3. Method 3:
○ Parameters: double c (for the constant 0.5), double a (first parallel side), double b
(second parallel side), double h (height).
○ Return type: double.
○ Calculate the area of a trapezium using the formula: area = 0.5 * (a + b)
* h.
Step 4. In the main method:
● Step 4.1. Create a Scanner object "in".
● Step 4.2. Create an instance of Area named "obj".
● Step 4.3. Prompt the user to enter the base and height of the parallelogram and
calculate the area using the first "area" method.
● Step 4.4. Prompt the user to enter the diagonals of the rhombus and calculate the area
using the second "area" method.
● Step 4.5. Prompt the user to enter the parallel sides and height of the trapezium and
calculate the area using the third "area" method.
Step 5. Print the calculated areas.
Step 6. End of the method.
Step 7. End of the class.
OVERLOAD
Step 1. Import the java.util.Scanner package.
Step 2. Declare a public class named "OVERLOAD".
Step 3. Define three overloaded methods named "display":

● Step 3.1. Method 1:


○ Parameters: String str, char ch.
○ Check if the first and last characters of the string str are equal to the character
ch.
○ If they are equal, print "SPECIAL WORD"; otherwise, print "NOT A SPECIAL
WORD".
● Step 3.2. Method 2:
○ Parameters: String str1, String str2.
○ Check if str1 is equal to str2.
○ If they are equal, print "STRINGS ARE EQUAL"; otherwise, print "STRINGS ARE
NOT EQUAL".
● Step 3.3. Method 3:
○ Parameters: String str, int n.
○ Extract the character at position n-1 from the string str.
○ Print the extracted character along with its position.
Step 4. In the main method:
● Step 4.1. Create a Scanner object "sc".
● Step 4.2. Prompt the user to enter a word and a character to check for a special word,
then call the first "display" method.
● Step 4.3. Prompt the user to enter two strings to compare for equality and call the
second "display" method.
● Step 4.4. Prompt the user to enter a word and a position, then call the third "display"
method to extract the character.
Step 5. End of the method.
Step 6. End of the class.
TwinPrime
Step 1. Import the java.util.Scanner package.
Step 2. Declare a public class named "TwinPrime".
Step 3. In the main method:

● Step 3.1. Create a Scanner object named in.


● Step 3.2. Prompt the user to enter the first number and read it into variable a.
● Step 3.3. Prompt the user to enter the second number and read it into variable b.
● Step 3.4. Initialize a boolean variable isAPrime to true to check if a is prime.
● Step 3.5. Use a loop from 2 to a / 2 to check if a is divisible by any number in that
range:
○ If a is divisible by any i, set isAPrime to false and break the loop.
● Step 3.6. Check if isAPrime is true and the absolute difference between a and b is 2.
○ If both conditions are met, initialize a boolean variable isBPrime to true to
check if b is prime.
○ Step 3.7. Use a loop from 2 to b / 2 to check if b is divisible by any number in
that range:
■ If b is divisible by any i, set isBPrime to false and break the loop.
○ Step 3.8. If isBPrime is true, print that a and b are twin primes.
○ Otherwise, print that a and b are not twin primes.
● Step 3.9. If either isAPrime is false or the absolute difference is not 2, print that a
and b are not twin primes.
Step 4. End of the method.
Step 5. End of the class.
Pattern
Step 1. Import the java.util.Scanner package.
Step 2. Declare a public class named "Pattern".
Step 3. In the main method:

● Step 3.1. Create a Scanner object named in.


● Step 3.2. Print options for the user to choose from:
○ "Type 1 for Floyd's triangle"
○ "Type 2 for an ICSE pattern"
● Step 3.3. Prompt the user to enter their choice and read it into variable ch.
● Step 3.4. Use a switch statement to handle the user’s choice:
○ Case 1 (Floyd's triangle):
■ Step 3.4.1. Initialize a variable a to 1.
■ Step 3.4.2. Use a loop i from 1 to 5 (inclusive) to control the number of
rows.
■ Step 3.4.2.1. Inside this loop, use another loop j from 1 to i to
print the numbers in each row.
■ Step 3.4.2.1.1. Print the value of a, then increment a.
■ Step 3.4.2.2. After printing all numbers in a row, print a new line.
○ Case 2 (ICSE pattern):
■ Step 3.4.3. Initialize a string variable s to "ICSE".
■ Step 3.4.4. Use a loop i from 0 to the length of s (exclusive).
■ Step 3.4.4.1. Inside this loop, use another loop j from 0 to i
(inclusive) to print characters of the string.
■ Step 3.4.4.1.1. Print the character at index j of the string
followed by a space.
■ Step 3.4.4.2. After printing the characters in a row, print a new
line.
○ Default case:
■ Step 3.4.5. Print "Incorrect Choice" if the user input does not match 1 or
2.
Step 4. End of the method.
Step 5. End of the class.
BusFare
Step 1. Import the java.util.Scanner package.
Step 2. Declare a public class named "BusFare".
Step 3. In the main method:

● Step 3.1. Create a Scanner object named in to read user input.


● Step 3.2. Prompt the user to enter the distance traveled and read it into the integer
variable dist.
● Step 3.3. Initialize an integer variable fare to 0.
● Step 3.4. Use an if-else statement to determine the fare based on the value of dist:
○ If dist is less than or equal to 0:
■ Step 3.4.1. Set fare to 0.
○ Else if dist is less than or equal to 10:
■ Step 3.4.2. Set fare to 80.
○ Else if dist is less than or equal to 20:
■ Step 3.4.3. Calculate the fare as 80 + (dist - 10) * 6.
○ Else if dist is less than or equal to 30:
■ Step 3.4.4. Calculate the fare as 80 + 60 + (dist - 20) * 5.
○ Else (if dist is greater than 30):
■ Step 3.4.5. Calculate the fare as 80 + 60 + 50 + (dist - 30) *
4.
● Step 3.5. Print the calculated fare.
Step 4. End of the method.
Step 5. End of the class.
SERIES
Step 1. Initialize a variable sum to 0.0 to store the total sum.

Step 2. Prompt the user to enter the values of xxx and nnn.

Step 3. Read the integer value for xxx from user input.

Step 4. Read the integer value for nnn from user input.

Step 5. Loop from i=0i = 0i=0 to i=ni = ni=n:

Step 5.1. Initialize a variable f to 1 to compute the factorial of iii.

Step 5.2. If iii is greater than 0, loop from j=1j = 1j=1 to j=ij = ij=i:

Step 5.2.1. Update f by multiplying it with jjj (i.e., f = f * j).

Step 5.3. Calculate the term xii!\frac{x^i}{i!}i!xi​using Math.pow(x, i) and divide by f.

Step 5.4. Add the calculated term to sum (i.e., sum = sum + (Math.pow(x, i)) / f).

Step 6. After the loop ends, print the value of sum as the result.

Step 7. End.

You might also like