0% found this document useful (0 votes)
0 views19 pages

Java Programs - Algorithms in Pseudocode

The document outlines various algorithms implemented in pseudocode for different programming tasks, including conversions, checks, and matrix operations. Each program is structured with input handling, processing logic, and output statements. Key algorithms include decimal to hexadecimal conversion, magic word checking, twin prime checking, and matrix sorting operations.

Uploaded by

kashyaproy1117
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)
0 views19 pages

Java Programs - Algorithms in Pseudocode

The document outlines various algorithms implemented in pseudocode for different programming tasks, including conversions, checks, and matrix operations. Each program is structured with input handling, processing logic, and output statements. Key algorithms include decimal to hexadecimal conversion, magic word checking, twin prime checking, and matrix sorting operations.

Uploaded by

kashyaproy1117
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/ 19

Java Programs - Algorithms in Pseudocode

Program 1: Decimal to Hexadecimal Conversion

ALGORITHM DecimalToHexadecimal
BEGIN
INPUT decimal_number

IF decimal_number = 0 THEN
hex_string = "0"
ELSE
hex_string = ""
temp = decimal_number

WHILE temp > 0 DO


remainder = temp MOD 16

IF remainder < 10 THEN


hex_string = remainder + hex_string
ELSE
hex_char = ASCII_CHARACTER('A' + remainder - 10)
hex_string = hex_char + hex_string
END IF

temp = temp ÷ 16
END WHILE
END IF

OUTPUT "Hexadecimal equivalent: " + hex_string


END

Program 2: Magic Word Checker


ALGORITHM MagicWordChecker
BEGIN
INPUT word
word = CONVERT_TO_UPPERCASE(word)

is_valid = TRUE
is_magic = TRUE

// Check if word contains only alphabetic characters


FOR i = 0 TO LENGTH(word) - 1 DO
char = word[i]
IF char < 'A' OR char > 'Z' THEN
is_valid = FALSE
BREAK
END IF
END FOR

// Check for repeated letters


IF is_valid THEN
FOR i = 0 TO LENGTH(word) - 1 DO
FOR j = i + 1 TO LENGTH(word) - 1 DO
IF word[i] = word[j] THEN
is_magic = FALSE
BREAK
END IF
END FOR
IF NOT is_magic THEN BREAK
END FOR
END IF

IF is_valid AND is_magic THEN


OUTPUT word + " is a Magic Word (no repeated letters)"
ELSE IF NOT is_valid THEN
OUTPUT "Invalid input! Word must contain only alphabetic characters"
ELSE
OUTPUT word + " is not a Magic Word (contains repeated letters)"
END IF
END

Program 3: Twin Prime Checker


ALGORITHM TwinPrimeChecker
BEGIN
FUNCTION isPrime(n)
IF n ≤ 1 THEN RETURN FALSE
IF n = 2 THEN RETURN TRUE
IF n MOD 2 = 0 THEN RETURN FALSE

FOR i = 3 TO √n STEP 2 DO
IF n MOD i = 0 THEN RETURN FALSE
END FOR

RETURN TRUE
END FUNCTION

INPUT number

IF isPrime(number) AND (isPrime(number + 2) OR isPrime(number - 2)) THEN


OUTPUT number + " is a Twin Prime number"

IF isPrime(number + 2) THEN
OUTPUT "Twin pair: (" + number + ", " + (number + 2) + ")"
END IF

IF isPrime(number - 2) THEN
OUTPUT "Twin pair: (" + (number - 2) + ", " + number + ")"
END IF
ELSE
OUTPUT number + " is not a Twin Prime number"
END IF
END

Program 4: Matrix Operations (Mirror Image and Border Sum)


ALGORITHM MatrixOperations
BEGIN
INPUT rows (M), columns (N)

IF M ≤ 2 OR M ≥ 10 OR N ≤ 2 OR N ≥ 10 THEN
OUTPUT "Invalid input! M and N must be between 3 and 9."
RETURN
END IF

DECLARE matrix[M][N], mirror[M][N]

// Input matrix elements


FOR i = 0 TO M - 1 DO
FOR j = 0 TO N - 1 DO
INPUT matrix[i][j]
END FOR
END FOR

// Display original matrix


OUTPUT "Original Matrix:"
FOR i = 0 TO M - 1 DO
FOR j = 0 TO N - 1 DO
OUTPUT matrix[i][j]
END FOR
OUTPUT newline
END FOR

// Create mirror image


FOR i = 0 TO M - 1 DO
FOR j = 0 TO N - 1 DO
mirror[i][j] = matrix[i][N - 1 - j]
END FOR
END FOR

// Display mirror matrix


OUTPUT "Mirror Image Matrix:"
FOR i = 0 TO M - 1 DO
FOR j = 0 TO N - 1 DO
OUTPUT mirror[i][j]
END FOR
OUTPUT newline
END FOR

// Calculate border sum


border_sum = 0
// Top and bottom rows
FOR j = 0 TO N - 1 DO
border_sum = border_sum + matrix[0][j]
IF M > 1 THEN
border_sum = border_sum + matrix[M-1][j]
END IF
END FOR

// Left and right columns (excluding corners)


FOR i = 1 TO M - 2 DO
border_sum = border_sum + matrix[i][0]
IF N > 1 THEN
border_sum = border_sum + matrix[i][N-1]
END IF
END FOR

OUTPUT "Sum of border elements: " + border_sum


END

Program 5: String Masking

ALGORITHM StringMasking
BEGIN
INPUT original_string, mask_string

result = ""

FOR i = 0 TO LENGTH(original_string) - 1 DO
char = original_string[i]
found = FALSE

FOR j = 0 TO LENGTH(mask_string) - 1 DO
IF char = mask_string[j] THEN
found = TRUE
BREAK
END IF
END FOR

IF NOT found THEN


result = result + char
END IF
END FOR

OUTPUT "Result: " + result


END
Program 6: Mix Array

ALGORITHM MixArray
BEGIN
INPUT size1, size2

IF size1 < 3 OR size2 < 3 THEN


OUTPUT "Array sizes must be >= 3"
RETURN
END IF

DECLARE array1[size1], array2[size2], result[6]

// Input first array


FOR i = 0 TO size1 - 1 DO
INPUT array1[i]
END FOR

// Input second array


FOR i = 0 TO size2 - 1 DO
INPUT array2[i]
END FOR

// Copy first 3 elements from both arrays


FOR i = 0 TO 2 DO
result[i] = array1[i]
result[i + 3] = array2[i]
END FOR

OUTPUT "Resultant Array:"


FOR i = 0 TO 5 DO
OUTPUT result[i]
END FOR
END

Program 7: Doubly Markov Matrix Checker


ALGORITHM DoublyMarkovMatrixChecker
BEGIN
INPUT matrix_order (N)

IF N < 3 OR N > 9 THEN


OUTPUT "Invalid input! N must be between 3 and 9."
RETURN
END IF

DECLARE matrix[N][N]

// Input matrix elements


FOR i = 0 TO N - 1 DO
FOR j = 0 TO N - 1 DO
INPUT matrix[i][j]
IF matrix[i][j] < 0 THEN
OUTPUT "Invalid input! All elements must be >= 0."
RETURN
END IF
END FOR
END FOR

is_doubly_markov = TRUE

// Check row sums


FOR i = 0 TO N - 1 DO
row_sum = 0
FOR j = 0 TO N - 1 DO
row_sum = row_sum + matrix[i][j]
END FOR
IF ABSOLUTE(row_sum - 1.0) > 0.001 THEN
is_doubly_markov = FALSE
BREAK
END IF
END FOR

// Check column sums


IF is_doubly_markov THEN
FOR j = 0 TO N - 1 DO
col_sum = 0
FOR i = 0 TO N - 1 DO
col_sum = col_sum + matrix[i][j]
END FOR
IF ABSOLUTE(col_sum - 1.0) > 0.001 THEN
is_doubly_markov = FALSE
BREAK
END IF
END FOR
END IF

IF is_doubly_markov THEN
OUTPUT "The matrix is a Doubly Markov Matrix"
ELSE
OUTPUT "The matrix is not a Doubly Markov Matrix"
END IF
END

Program 8: Snowball String Checker


ALGORITHM SnowballStringChecker
BEGIN
INPUT sentence

last_char = sentence[LENGTH(sentence) - 1]

IF last_char ≠ '.' AND last_char ≠ '?' THEN


OUTPUT "Error: Sentence must end with '.' or '?'"
RETURN
END IF

// Remove terminating character


sentence = SUBSTRING(sentence, 0, LENGTH(sentence) - 1)

// Split into words


words = SPLIT(sentence, " ")

is_snowball = TRUE

FOR i = 0 TO LENGTH(words) - 1 DO
IF LENGTH(words[i]) ≠ (i + 1) THEN
is_snowball = FALSE
BREAK
END IF
END FOR

IF is_snowball THEN
OUTPUT "The sentence is a Snowball String"
ELSE
OUTPUT "The sentence is not a Snowball String"
END IF

OUTPUT "Word lengths:"


FOR i = 0 TO LENGTH(words) - 1 DO
OUTPUT "'" + words[i] + "' = " + LENGTH(words[i])
END FOR
END

Program 9: Matrix Max/Min Sort (Descending)


ALGORITHM MatrixMaxMinSortDescending
BEGIN
FUNCTION bubbleSortDescending(arr)
n = LENGTH(arr)
FOR i = 0 TO n - 2 DO
FOR j = 0 TO n - i - 2 DO
IF arr[j] < arr[j + 1] THEN
SWAP arr[j] AND arr[j + 1]
END IF
END FOR
END FOR
END FUNCTION

INPUT rows (M), columns (N)

IF M ≤ 2 OR M ≥ 20 OR N ≤ 2 OR N ≥ 20 THEN
OUTPUT "Invalid input! M and N must be between 3 and 19."
RETURN
END IF

DECLARE matrix[M][N]

// Input matrix elements


FOR i = 0 TO M - 1 DO
FOR j = 0 TO N - 1 DO
INPUT matrix[i][j]
END FOR
END FOR

// Display original matrix


OUTPUT "Original Matrix:"
FOR i = 0 TO M - 1 DO
FOR j = 0 TO N - 1 DO
OUTPUT matrix[i][j]
END FOR
OUTPUT newline
END FOR

// Find max and min


max = matrix[0][0]
min = matrix[0][0]
max_row = 0, max_col = 0
min_row = 0, min_col = 0

FOR i = 0 TO M - 1 DO
FOR j = 0 TO N - 1 DO
IF matrix[i][j] > max THEN
max = matrix[i][j]
max_row = i
max_col = j
END IF
IF matrix[i][j] < min THEN
min = matrix[i][j]
min_row = i
min_col = j
END IF
END FOR
END FOR

OUTPUT "Maximum value: " + max + " at position (" + max_row + ", " + max_col + ")"
OUTPUT "Minimum value: " + min + " at position (" + min_row + ", " + min_col + ")"

// Convert to 1D array
DECLARE arr[M * N]
k=0
FOR i = 0 TO M - 1 DO
FOR j = 0 TO N - 1 DO
arr[k] = matrix[i][j]
k=k+1
END FOR
END FOR

// Sort in descending order


CALL bubbleSortDescending(arr)

// Convert back to 2D array


k=0
FOR i = 0 TO M - 1 DO
FOR j = 0 TO N - 1 DO
matrix[i][j] = arr[k]
k=k+1
END FOR
END FOR

// Display sorted matrix


OUTPUT "Matrix after sorting in descending order:"
FOR i = 0 TO M - 1 DO
FOR j = 0 TO N - 1 DO
OUTPUT matrix[i][j]
END FOR
OUTPUT newline
END FOR
END

Program 10: Kaprekar Numbers


ALGORITHM KaprekarNumbers
BEGIN
FUNCTION isKaprekar(n)
square = n * n
square_str = CONVERT_TO_STRING(square)
digits = LENGTH(CONVERT_TO_STRING(n))

IF LENGTH(square_str) < digits THEN


RETURN FALSE
END IF

right_part = SUBSTRING(square_str, LENGTH(square_str) - digits, digits)


left_part = SUBSTRING(square_str, 0, LENGTH(square_str) - digits)

IF left_part = "" THEN


left_part = "0"
END IF

right = CONVERT_TO_INTEGER(right_part)
left = CONVERT_TO_INTEGER(left_part)

RETURN (left + right = n)


END FUNCTION

INPUT lower_limit (p), upper_limit (q)

IF p ≥ q THEN
OUTPUT "Invalid range! p must be less than q."
RETURN
END IF

OUTPUT "Kaprekar numbers in range [" + p + ", " + q + "]:"


count = 0

FOR i = p TO q DO
IF isKaprekar(i) THEN
OUTPUT i
count = count + 1
END IF
END FOR

OUTPUT "Total Kaprekar numbers: " + count


END

Program 11: Sentence Processing (Alphabetical Sort)


ALGORITHM SentenceProcessingAlphabetical
BEGIN
FUNCTION bubbleSortAlphabetical(arr)
n = LENGTH(arr)
FOR i = 0 TO n - 2 DO
FOR j = 0 TO n - i - 2 DO
IF arr[j] > arr[j + 1] THEN
SWAP arr[j] AND arr[j + 1]
END IF
END FOR
END FOR
END FUNCTION

INPUT sentence

last_char = sentence[LENGTH(sentence) - 1]

IF last_char ≠ '.' AND last_char ≠ '?' AND last_char ≠ '!' THEN


OUTPUT "Error: Sentence must end with '.', '?', or '!'"
RETURN
END IF

// Remove terminating character


sentence = SUBSTRING(sentence, 0, LENGTH(sentence) - 1)

// Split into words


words = SPLIT(sentence, " ")

OUTPUT "Length of sentence (in words): " + LENGTH(words)

// Sort alphabetically
CALL bubbleSortAlphabetical(words)

OUTPUT "Sentence in alphabetical order:"


FOR each word in words DO
OUTPUT word + " "
END FOR
OUTPUT newline
END

Program 12: Matrix Ascending Sort


ALGORITHM MatrixAscendingSort
BEGIN
FUNCTION bubbleSortAscending(arr)
n = LENGTH(arr)
FOR i = 0 TO n - 2 DO
FOR j = 0 TO n - i - 2 DO
IF arr[j] > arr[j + 1] THEN
SWAP arr[j] AND arr[j + 1]
END IF
END FOR
END FOR
END FUNCTION

INPUT rows (M), columns (N)

IF M ≤ 2 OR M ≥ 20 OR N ≤ 2 OR N ≥ 20 THEN
OUTPUT "Invalid input! M and N must be between 3 and 19."
RETURN
END IF

DECLARE matrix[M][N]

// Input matrix elements


FOR i = 0 TO M - 1 DO
FOR j = 0 TO N - 1 DO
INPUT matrix[i][j]
END FOR
END FOR

// Display original matrix


OUTPUT "Original Matrix:"
FOR i = 0 TO M - 1 DO
FOR j = 0 TO N - 1 DO
OUTPUT matrix[i][j]
END FOR
OUTPUT newline
END FOR

// Find max and min


max = matrix[0][0]
min = matrix[0][0]
max_row = 0, max_col = 0
min_row = 0, min_col = 0

FOR i = 0 TO M - 1 DO
FOR j = 0 TO N - 1 DO
IF matrix[i][j] > max THEN
max = matrix[i][j]
max_row = i
max_col = j
END IF
IF matrix[i][j] < min THEN
min = matrix[i][j]
min_row = i
min_col = j
END IF
END FOR
END FOR

OUTPUT "Maximum value: " + max + " at position (" + max_row + ", " + max_col + ")"
OUTPUT "Minimum value: " + min + " at position (" + min_row + ", " + min_col + ")"

// Convert to 1D array
DECLARE arr[M * N]
k=0
FOR i = 0 TO M - 1 DO
FOR j = 0 TO N - 1 DO
arr[k] = matrix[i][j]
k=k+1
END FOR
END FOR

// Sort in ascending order


CALL bubbleSortAscending(arr)

// Convert back to 2D array


k=0
FOR i = 0 TO M - 1 DO
FOR j = 0 TO N - 1 DO
matrix[i][j] = arr[k]
k=k+1
END FOR
END FOR

// Display sorted matrix


OUTPUT "Matrix after sorting in ascending order:"
FOR i = 0 TO M - 1 DO
FOR j = 0 TO N - 1 DO
OUTPUT matrix[i][j]
END FOR
OUTPUT newline
END FOR
END

Program 13: Vowel Words Processing


ALGORITHM VowelWordsProcessing
BEGIN
FUNCTION isVowel(ch)
RETURN ch = 'A' OR ch = 'E' OR ch = 'I' OR ch = 'O' OR ch = 'U'
END FUNCTION

FUNCTION beginsAndEndsWithVowel(word)
RETURN isVowel(word[0]) AND isVowel(word[LENGTH(word) - 1])
END FUNCTION

INPUT sentence

last_char = sentence[LENGTH(sentence) - 1]

IF last_char ≠ '.' AND last_char ≠ '?' AND last_char ≠ '!' THEN


OUTPUT "Error: Sentence must end with '.', '?', or '!'"
RETURN
END IF

// Remove terminating character


sentence = SUBSTRING(sentence, 0, LENGTH(sentence) - 1)

// Split into words


words = SPLIT(sentence, " ")

// Count vowel words


vowel_word_count = 0
FOR each word in words DO
IF beginsAndEndsWithVowel(word) THEN
vowel_word_count = vowel_word_count + 1
END IF
END FOR

OUTPUT "Number of words beginning and ending with vowel: " + vowel_word_count

// Separate vowel words and other words


DECLARE vowel_words[vowel_word_count]
DECLARE other_words[LENGTH(words) - vowel_word_count]

vowel_index = 0
other_index = 0

FOR each word in words DO


IF beginsAndEndsWithVowel(word) THEN
vowel_words[vowel_index] = word
vowel_index = vowel_index + 1
ELSE
other_words[other_index] = word
other_index = other_index + 1
END IF
END FOR

OUTPUT "Rearranged sentence:"


FOR each word in vowel_words DO
OUTPUT word + " "
END FOR
FOR each word in other_words DO
OUTPUT word + " "
END FOR
OUTPUT newline
END

You might also like