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

Algo

Uploaded by

e9yliuuq8
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Algo

Uploaded by

e9yliuuq8
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

NUMBER SYSTEM CONVERSION

Binary To Decimal
Start
Input the binary number in input.
Initialize an integer total to 0 to store the decimal number.
Initialize an integer n to 1 (base value for the conversion).
Repeat the following steps until input is not equal to 0:
 while (input != 0) {
o remainder = input % 10;

o power = remainder * n;

o total = total + power;

o n = n * 2;

o input = input / 10; }

Output the decimal number (total).


End

Binary to hexadecimal
1. **Start**
2. Initialize a string `str` to "" to store the hexadecimal number.
3. Initialize a long integer `num` to 0 to store the decimal number.
4. Accept the binary number from the user and convert it to decimal:
- Read the binary number as a string from the user.
- Convert the binary string to a decimal number and store it in `num`.
5. Initialize a long integer `dnum` to `num`.
6. If `dnum` is 0, set `str` to "0".
7. Repeat the following steps until `dnum` is not equal to 0:
- while (`dnum` > 0) {
- `r = dnum % 16`;
- if (`r >= 10` && `r <= 15`) {
- `str = (char)('A' + (r - 10)) + str`;
- } else {
- `str = r + str`;
-}
- `dnum /= 16`;
-}
8. Output the hexadecimal number (`str`).
9. **End**

Decimal TO binary

1. **Start**
2. Input the decimal number in `input`.
3. Initialize an integer `intPart` to 0 to store the integer part of the decimal
number.
4. Initialize a string `converted` to "" to store the binary representation.
5. Accept the decimal number from the user and convert it to an integer:
- Read the decimal number as a string from the user and store it in `input`.
- If `input` contains a ".", extract the substring before the "." and store it in
`str1`.
- Otherwise, store `input` in `str1`.
- Convert `str1` to an integer and store it in `intPart`.
6. Initialize a `StringBuilder` `integerPart` to build the binary representation.
7. Repeat the following steps until `intPart` is not equal to 0:
- while (`intPart` != 0) {
- `remainder = intPart % 2`;
- Insert `remainder` at the beginning of `integerPart`.
- `intPart /= 2`;
-}
8. Set `converted` to the string representation of `integerPart`.
9. Output the binary representation (`converted`).
10. **End**

Decimal TO octal
1. **Start**
2. Input the decimal number in `input`.
3. Initialize an integer `intPart` to 0 to store the integer part of the decimal
number.
4. Initialize a string `converted` to "" to store the octal representation.
5. Accept the decimal number from the user and convert it to an integer:
- Read the decimal number as a string from the user and store it in `input`.
- If `input` contains a ".", extract the substring before the "." and store it in
`str1`.
- Otherwise, store `input` in `str1`.
- Convert `str1` to an integer and store it in `intPart`.
6. Initialize a string `integerPart` to build the octal representation.
7. Repeat the following steps until `intPart` is not equal to 0:
- while (`intPart` != 0) {
- `remainder = intPart % 8`;
- `integerPart = remainder + integerPart`;
- `intPart /= 8`;
-}
8. Set `converted` to the string representation of `integerPart`.
9. Output the octal representation (`converted`).
10. **End**

String Manipulations
Blank replace
 Start
 Input the sentence in input.
 Initialize a string result to "".
 Accept the sentence from the user and store it in input.
 Initialize a boolean spaceEncountered to false.
 Repeat the following steps for each character in input:
 for (each character c in input) {
o if (c is ' ') {

 if (spaceEncountered is false) {
 Append c to result.
 Set spaceEncountered to true.
 }
o } else {

 Append c to result.
 Set spaceEncountered to false.
o }

 }
 Output the modified sentence (result).
 End

Capital
1. Start
2. Initialize a string sent to store the sentence.
3. Initialize an integer freq to 0 to store the frequency of words beginning wit
h a capital letter.
input():
1. Start
2. Accept the sentence from the user and store it in sent:
isCap(String w):
5. If the length of the word w is greater than 0 and the first character of w is
an uppercase letter, return true.
6. Otherwise, return false.
display():
7. Split the sentence sent into words using whitespace as the delimiter and st
ore the words in an array words.
8. Initialize freq to 0.
9. Repeat the following steps for each word in words:
 for (String word : words) {
o if (isCap(word)) {

o freq++;

o }

 }
10.Output the sentence sent.
11.Output the frequency of words beginning with a capital letter (freq).
12.display() method.
13.End
Palindrome Word
1. Start
2. Initialize a string input to store the input word.
3. Initialize a string CapitalInput to store the capitalized input word.
4. Initialize a string reversed to store the reversed word.
input():
5. Accept the word from the user and store it in input:
Uppercase():
6. Convert input to uppercase and store it in CapitalInput.
reverse():
7. Initialize an integer n to the length of CapitalInput.
8. Initialize a character ch.
9. Repeat the following steps to reverse CapitalInput:
 for(int i=n-1; i>=0; i--){
o ch=CapitalInput.charAt(i);

o reversed=ch+reversed;

 }
check():
10.Check if CapitalInput is equal to reversed:
 If they are equal, print "It is a palindrome word".
 Otherwise, print "not a palindrome".

Recursion
Recursion Digit extraction
1. Start
2. Initialize the required variables and objects.
Extract(int a):
3. Check if a is equal to 0:
 If it is, return 0.
 Else, calculate the remainder rem as a % 10.
 Return rem plus the result of Extract(a / 10).
main():
4. Create a Scanner object to read user input.
5. Prompt the user to enter a number and store it in input.
6. Create an instance of RecursionDigitExtraction.
7. Call the Extract(input) method and print the result.
8. End

Binary Search Recursion


1. Start
2. Initialize an array array of size 10.
3. Initialize an integer search to store the number to be searched.
input():
4. Accept 10 integers from the user and store them in array:
 for(int i=0;i<array.length;i++){
o array[i]=sc.nextInt();

 }
5. Accept the number to be searched and store it in search.
6. Sort the array array using Arrays.sort(array).
recursionbinarysearching(int l, int h):
7. Calculate the middle index m as (l+h)/2.
8. Check if l is greater than h:
 If it is, return 0.
9. Check if array[m] is equal to search:
 If it is, print "number found at " + (m+1).
 Exit the program using System.exit(0).
 Return m+1.
10.Check if array[m] is less than search:
 If it is, return recursionbinarysearching(m+1,h).
11.Otherwise, return recursionbinarysearching(m-1,h).
main():
12.Create an instance of BinarySearchRecursion.
13.Call the input() method.
14.Call the recursionbinarysearching(0,9) method.
15.End

Linear Search Recursion


1. Start
2. Initialize an array array of size 10.
3. Initialize an integer search to store the number to be searched.
input():
4. Accept 10 integers from the user and store them in array:
 for (int i = 0; i < array.length; i++) {
o array[i] = sc.nextInt();

 }
5. Accept the number to be searched and store it in search.
6. Sort the array array using Arrays.sort(array).
linearsearch(int a):
7. Check if a is greater than or equal to the length of the array:
 If it is, print "number not found" and return -1.
8. Check if the element at index a in the array is equal to search:
 If it is, print "number found at " + (a + 1) and return a + 1.
9. Otherwise, call linearsearch(a + 1) recursively.
main():
10.Create an instance of LinearSearchRecursion.
11.Call the input() method.
12.Call the linearsearch(0) method.
13.End

Bubble sort recursion


1. Start
2. Initialize an array array of size 10.
input():
3. Accept 10 integers from the user and store them in array:
 for (int i = 0; i < array.length; i++) {
o array[i] = sc.nextInt();

 }
bubbleSortRecursive(int n, int i):
4. Check if n is equal to 1:
 If it is, return the array.
5. Check if i is less than n - 1:
 If it is, compare array[i] with array[i + 1]:
o If array[i] is greater than array[i + 1], swap them.

 Call bubbleSortRecursive(n, i + 1).


6. Otherwise, call bubbleSortRecursive(n - 1, 0).
bubbleSort(int n):
7. Call bubbleSortRecursive(n, 0).
display():
8. Return the sorted array array.
main():
9. Create an instance of BubblesortRecursion.
10.Call the input() method.
11.Call the bubbleSort(ob.array.length) method.
12.Print the sorted array using the display() method.

HCF
Algorithm for the HCF Method:

The different steps are:

Step 1: Start

Step 2: If i > a or i > b, return the current value of hcf

Step 3: Check if a % i == 0 and b % i == 0


a. If true, update hcf = i

Step 4: Call the HCF method recursively with arguments a, b, i + 1, and hcf

Step 5: Return the result from the recursive call

Step 6: Stop

Algorithm for the main Method:

The different steps are:

Step 1: Start

Step 2: Create a Scanner object to accept user input

Step 3: Display "Enter two numbers:"

Step 4: Accept two integers a and b from the user

Step 5: Create an object ob of the HCFrecursion class

Step 6: Call the HCF method on ob with arguments a, b, 1, and 1, and store the result in result

Step 7: Display the value of result

Step 8: Stop

Arrays
Transpose
Algorithm for the fillarray Method:
The different steps are:
Step 1: Start
Step 2: Display "Enter the elements"
Step 3: For i = 0 to m - 1
a. For j = 0 to m - 1
i. Accept an integer from the user and store it in arr[i][j]
b. End inner loop
End outer loop
Step 4: Stop

Algorithm for the transpose Method:


The different steps are:
Step 1: Start
Step 2: Display "The transpose array in matrix form:"
Step 3: For i = 0 to m - 1
a. For j = 0 to m - 1
i. Display arr[j][i] with a space
b. End inner loop
c. Move to the next line
End outer loop
Step 4: Stop

Algorithm for the display Method:


The different steps are:
Step 1: Start
Step 2: Display "The original array in matrix form:"
Step 3: For i = 0 to m - 1
a. For j = 0 to 2
i. Display arr[i][j] with a space
b. End inner loop
c. Move to the next line
End outer loop
Step 4: Call the transpose method
Step 5: Stop

Algorithm for the main Method:


The different steps are:
Step 1: Start
Step 2: Create a Scanner object sc
Step 3: Display "Enter the size of the array"
Step 4: Accept an integer input and store it in size
Step 5: Create an object ob of the Transpose class with size as the argument
Step 6: Call the fillarray method on ob to accept the elements of the array
Step 7: Call the display method on ob to display the array and its transpose
Step 8: Stop

Binary Search
Algorithm for the input Method
Step 1: Start
Step 2: Display "Enter the input"
Step 3: For i = 0 to array.length - 1
a. Accept an integer from the user and store it in array[i]
EndFor
Step 4: Display "Enter the number you want to search"
Step 5: Accept an integer from the user and store it in search
Step 6: Sort the array using the Arrays.sort method
Step 7: Stop
Algorithm for the BinarySearch Method
Step 1: Start
Step 2: Initialize l to 0 (the lowest index of the array)
Step 3: Initialize h to array.length - 1 (the highest index of the array)
Step 4: While l is less than or equal to h, repeat the following:
a. Calculate the middle index m as (l + h) / 2
b. If array[m] equals search
i. Display "Number found at (m + 1)"
ii. Return m + 1
c. Else if array[m] is less than search
i. Update l to m + 1
d. Else
i. Update h to m - 1
EndWhile
Step 5: If the loop completes without finding the search value:
a. Display "Number not found"
b. Return 0
Step 6: Stop
Algorithm for the main Method
Step 1: Start
Step 2: Create an object ob of class BinarySearch
Step 3: Call the input method on ob to accept the array and the search value
Step 4: Call the BinarySearch method on ob to search for the value and display
the result
Step 5: Stop

Linear Search
Algorithm for the input Method
Step 1: Start
Step 2: Display "Enter the input"
Step 3: For i = 0 to array.length - 1
a. Accept an integer from the user and store it in array[i]
EndFor
Step 4: Display "Enter the number you want to search"
Step 5: Accept an integer from the user and store it in search
Step 6: Sort the array using the Arrays.sort method
Step 7: Stop
Algorithm for the LinearSearch Method
Step 1: Start
Step 2: For i = 0 to array.length - 1
a. If array[i] equals search
i. Display "Number found at (i + 1)"
ii. Return i + 1
EndIf
EndFor
Step 3: If the loop completes without finding the search value
a. Display "Number not found"
b. Return -1
Step 4: Stop
Algorithm for the main Method
Step 1: Start
Step 2: Create an object ob of class LinearSearch
Step 3: Call the input method on ob to accept the array and the search value
Step 4: Call the LinearSearch method on ob to search for the value and display
the result
Step 5: Stop

Diagonal Sum
Algorithm for the input Method
Step 1: Start
Step 2: Display "Enter the elements"
Step 3: For i = 0 to array.length - 1
a. For j = 0 to array.length - 1
i. Accept an integer from the user and store it in array[i][j]
EndFor
EndFor
Step 4: Stop
Algorithm for the print Method
Step 1: Start
Step 2: Display "The array is:"
Step 3: For i = 0 to array.length - 1
a. For j = 0 to array.length - 1
i. Display array[i][j] with a space
EndFor
b. Move to the next line
EndFor
Step 4: Stop
Algorithm for the diagonalsum Method
Step 1: Start
Step 2: Initialize sum1 = 0 and sum2 = 0
Step 3: Calculate the primary diagonal sum:
a. For i = 0 to array.length - 1
i. Add array[i][i] to sum1
EndFor
Step 4: Calculate the secondary diagonal sum:
a. For i = 0 to array.length - 1
i. For j = 0 to array.length - 1
- If i + j == 2, add array[i][j] to sum2
EndFor
EndFor
Step 5: Calculate the total diagonal sum as (sum1 + sum2 - array[1][1])
Step 6: Display the result
Step 7: Stop
Algorithm for the main Method
Step 1: Start
Step 2: Create an object ob of class DiagonalSum
Step 3: Call the input method on ob to accept the 2D array
Step 4: Call the print method on ob to display the array
Step 5: Call the diagonalsum method on ob to calculate and display the diagonal
sum
Step 6: Stop

Bubble sort
Algorithm for the input Method

Step 1: Start.
Step 2: Display "Enter the input:".
Step 3: Accept 10 integers from the user and store them in the array.

for (int i = 0; i < array.length; i++) {

array[i] = sc.nextInt();

Step 4: Stop.

Algorithm for the bubbleSort Method

Step 1: Start.
Step 2: Get the length of the array in variable n.

int n = array.length;
Step 3:

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - 1 - i; j++) {

if (array[j] > array[j + 1]) {

int temp = array[j];

array[j] = array[j + 1];

array[j + 1] = temp;

Step 4: Stop.

Algorithm for the display Method

Step 1: Start.
Step 2: Iterate through the array and display each element separated by a space.

for (int i = 0; i < array.length; i++) {

System.out.print(array[i] + " ");

Step 3: Move to the next line after displaying the array.


Step 4: Stop.

Algorithm for the main Method

Step 1: Start.
Step 2: Create an object ob of the BubbleSort class.
Step 3: Call the input method to accept the array elements from the user.
Step 4: Call the bubbleSort method to sort the array.
Step 5: Call the display method to display the sorted array.
Step 6: Stop.

Data Handling
DataInputStreamTXT
Algorithm for main Method

Step 1: Start
Step 2: Initialize FileInputStream object fin to null.
Step 3: Declare an integer variable a.
Step 4: Display the message "The input from the text file".
Step 5: Open the file "ABC.txt" for reading using FileInputStream.
Step 6: Read characters from the file using a while loop:

while (((a = fin.read()) != -1)) {

System.out.print((char)a);

a. Read the next character from the file into variable a.


b. Check if the end of the file (a != -1) is reached.
c. If not, cast the integer value of a to a character and display it.
Step 7: Close the file using fin.close().
Step 8: Stop.

DataOutputStreamTXT
Algorithm for the main Method

Step 1: Start.
Step 2: Declare and initialize a byte array with the elements {'A', 'G', 'A', 'S', 'T', 'H', 'Y', 'A'}.
Step 3: Declare a FileOutputStream object fout and initialize it to null.
Step 4: Open the file "DataOutputStreamTXT.txt" for writing using FileOutputStream.

fout = new FileOutputStream("DataOutputStreamTXT.txt");

Step 5: Write the byte array to the file.

fout.write(array);

Step 6: Close the file using fout.close().


Step 7: Stop.

DataInputStreamDAT
Algorithm for the DataOutputStreamDAT Program

Step 1: Start

Step 2: Initialize a byte array array with the characters {'A', 'g', 'a', 's', 't', 'h', 'y', 'a'}.

Step 3: Declare a FileOutputStream object fout and initialize it to null.

Step 4: Open a file named DataOutputStreamDAT.DAT using the FileOutputStream class and assign it
to fout:

fout = new FileOutputStream("DataOutputStreamDAT.DAT");

Step 5: Write the contents of the array to the file using the write method:

fout.write(array);

Step 6: Close the file stream using the close method:

fout.close();

Step 7: Stop
DataOutputStreamDAT
Algorithm for the main Method

Step 1: Start.
Step 2: Declare and initialize a byte array with the elements {'A', 'G', 'A', 'S', 'T', 'H', 'Y', 'A'}.
Step 3: Declare a FileOutputStream object fout and initialize it to null.
Step 4: Open the file "DataOutputStreamTXT.txt" for writing using FileOutputStream.

fout = new FileOutputStream("DataOutputStreamDAT.dat");

Step 5: Write the byte array to the file.

fout.write(array);

Step 6: Close the file using fout.close().


Step 7: Stop.

You might also like