0% found this document useful (0 votes)
85 views24 pages

Algorithm Steps for Various Mathematical Problems

The algorithm takes a number as input from the user. It then reverses the digits of the number and checks if the reversed number is equal to the original number. If they are equal, it is a palindrome, otherwise it is not. The algorithm uses a temporary variable to store the number, extracts the last digit using modulo 10, stores it in the reversed number and removes the last digit from the temporary variable by integer division by 10. This process is repeated in a loop until the temporary variable becomes 0.

Uploaded by

fghf
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)
85 views24 pages

Algorithm Steps for Various Mathematical Problems

The algorithm takes a number as input from the user. It then reverses the digits of the number and checks if the reversed number is equal to the original number. If they are equal, it is a palindrome, otherwise it is not. The algorithm uses a temporary variable to store the number, extracts the last digit using modulo 10, stores it in the reversed number and removes the last digit from the temporary variable by integer division by 10. This process is repeated in a loop until the temporary variable becomes 0.

Uploaded by

fghf
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

Algorithm-

Step 1: Start

Step 2: Read the input number from the user N

Step 3: If N greater than 1000 then print "Invalid Input" and Exit

Step 4: Store input number into the another variable Num

Step 5: Store N/48 into another variable C

Step 6: If C greater than 0 than print C*48

Step 7: Find reminder from number, N equal to N MOD 48

Step 8: Now find new value of C from C equal to N divide by 24

Step 9: If C greater than 0 than print C*24

Step 10: Find reminder from number, N equal to N MOD 24

Step 11: Now find new value of C from C equal to N divide by 12

Step 12: If C greater than 0 than print C*12

Step 13: Find reminder from number, N equal to N MOD 12

Step 14: Now find new value of C from C equal to N divide by 6

Step 15: If C greater than 0 than print C*6

Step 16: Find reminder from number, N equal to N MOD 6

Step 17: If N equal to 0 than print "Remaining boxes =0" Else print

"Remaining boxes=" value of N

Step 18: Print "Total number of boxes=" value of Num

Step 19: Now find the Total of value of cartoonBox =48, cartoonBox =24,

cartoonBox =12, cartoonBox =6

Step 20: If N greater than Total equal to Total + 1


Stop 21: Print "Total number of cartons=" Value of Total

Step 22: Stop


Algorithm-

Step 1: Start

Step 2: Read or initialize a number N.

Step 3: Store it in another variable.

Step 4: Check if the given number N is even or not.

Step 5: If the number is odd, print "Invalid input".

Step 6: If the number is even, than

Step 7: Define two arrays one for storing the prime numbers and the other for

Calculating the prime number.

Step 8: Find all prime numbers till the entered number using a for loop and store
it in

an array.

Step 9: In the second array, store only odd prime numbers using if statement.

Step 10: Display the odd prime pairs.

Step 11: By using the for loop, store the sum of two odd prime numbers in a
variable (sum).

Step 12: Using the if statement, compare the sum with the original number N.

If the sum is equal to the original number, the given number N

is Goldbach Number, else not.

Step 13: Stop


Algorithm-
Step 1: Start
Step 2:Define an Array containing days number leaving blank(0th position)
Step 4: Define another Array containing Month names leaving blank(0th
position)
Step 5: Now Input Day Number, N number of days, Year from the user
Step 6: In a variable store the sum of Day number and N number (for
calculating date after n days)
Step 7: Now check if the year entered is leap year or not
Step 8: If it is leap year than increment the days number in the array from 2nd
position by 1
Step 9: Now check if the days entered are in the range or not
Step 10: If the days are in the range than calculate the date (date can be
calculated by subtracting the entered days number from the original
days number)
Step 11: If the days are not in the range than change the value of k to 1 and
display days are out of range and Program will not execute further.
Step 12: Now check if the 'N' number of days are in the range or not.
Step 13: If it is in range than calculate date after 'N' days
Step 14:If it is not in the range than change the value of k to 1 and display 'N
days are out of range' and Program will not execute further.
Step 15: If days and N days both are in the range than Display Date and Date
after N days.
Step 16: Another condition is If the year entered is not a Leap year
Step 17: Than do not increment the value of days number in the array.
Step 18: Stop
Algorithm-

Step 1: Start

Step 2: Read a number as input from the user.

Step 3: Find all the possible combinations of given number.

Step 4: Calculate the total digits of the number

Step 5: Use a loop to find all the cyclic shift combinations. The loop will run

for the total digits number of times. On each iteration, pick the right

most digit and add it to the left of the number.

Step 6: Add all numbers to a array.

Step 7: Use a loop to check for all combinations of the number. Check it all

combinations are prime or not. If it fails for any combination, return

false.Else, return true if all numbers are prime.

Step 8: A number is called a prime number if it has only 2 factors, 1 is the

number itself.

Step 9: To check if a number is prime or not, we can run one loop that will
run from 2 to number/2.

Step 10: On each iteration, it will check if that value can divide the number

or not.

Step 11: If value is divide, than it is not a prime number. Else, it is prime
number.

Step 12: Stop


Algorithm-

Step 1:Start

Step 2: Input a integer number amount and temp for storing converted notes,

and rem for storing rest of notes

Step 3:Then First we divide num by 1000 then we get 8, 1000 rs notes then go

to step 3

Step 4: After dividing 1000 we get a remainder, we know that reminder is not

divisible by 500 to go to the next step

Step 5: Now divide remainder by 100 then we get 1, 100 rs note now the

remainder is 8 go to the next step

Step 6: 8 is divisible by 50 and 20 nor 10 so we escape now go to the next step

Step 7: Now divide 8 by 5 we get 1, 5 rs notes and the remainder is 3 to go to

the next step

Step 8: Now divide 3 by 2 we get 1, 2 rs notes and the remainder is 1 so follow

the next step

Step 9: this is the Last step divide 1 by 1 we get zero remainder now print the

total no of denominations needed along with the total no of counts

require to fulfill a requirement

Step 10: Stop


Algorithm-

STEP 1: START

STEP 2: REPEAT STEP 3 UNTIL (i<10)

STEP 3: if(check(i)) then PRINT i

STEP 4: END

check(int n)

STEP 1: START

STEP 2: Read a integer number, sq_n for square of number, and count_digits

for counts the digits of number.

STEP 3: if(n==1) RETURN true

STEP 4: Find the square of number from sq_n = n*n

STEP 5: SET count_digits = 0

STEP 6: REPEAT STEP 6 and 7 UNTIL (sq_n!=0)

STEP 7: count_digits++

STEP 8: Find the next sq_n by sq_n/=10

STEP 9: And find again square of new number from sq_n = n*n

STEP 10: REPEAT STEP 10 to 13 UNTIL r_digits<count_digits

STEP 11: To find the new number to check the equality of prevoius number

with current number eq_parts = (10)r_digits

STEP 12: if both number are equal (eq_parts==n) then continue

STEP 13: And find sum of, sum = sq_n/eq_parts + sq_n%eq_parts

STEP 14: if sum equals to number (sum==n)then RETURN true

STEP 15: RETURN false.

STEP 16: END


Algorithm-

Step 1: Start

Step 2: Create an instance of the Scanner class.

Step 3: Declare a variable.

Step 4: Ask the user to initialize the number.

Step 5: Define 3 methods, one to check Armstrong, another to calculate the

order, and another to calculate the raised to the power value.

Step 6: Calculate the total number of digits in order function using a while

loop.

Step 7:Calculate the value of digit raised to the power order value.

Step 8: In the isArmstrong method check whether the entered number is

Armstrong or not.

Step 9: If found Armstrong, then return true else return false.

Step 10: If true, then print a message that the entered number is an Armstrong.

Step 11: If false, then print a message that the entered number is not an

Armstrong.

Step 12: Display the message.

Step 13: Stop.


Algorithm-

Step 1: Start

Step 2: Create an instance of the Scanner class.

Step 3: Declare a variable.

Step 4: Ask the user to initialize the variable.

Step 5: Call a method to check whether the number is perfect or not.

Step 6: Declare a variable to store the sum of divisors.

Step 7: Initialize the sum to 1.

Step 8: Use a for loop to find the divisors of the entered number.

Step 9: Update the sum each time a divisor of the entered number encounters.

Step 10: If the sum of all the divisors of the entered numbers is equal to the
entered

number, then print it as a perfect number.

Step 11: If the sum of all the divisors of the entered numbers is not equal to the

entered number, then print it as not a perfect number.

Step 12: Display the result.

Step 13: Stop


Algorithm-

Step 1: Start

Step 2 : Read a number inputNumber be the number obtained from the user.

Step 3 : Calculate noOfDigits in inputNumber.

Step 4 : Copy inputNumber into copyOfInputNumber.

Step 5 : Extract lastDigit from inputNumber from mod by 10.

Step 6 : Add lastDigit to sum after raising it to the power of its position.

Step 7 : Remove lastDigit from inputNumber and decrement noOfDigits.

Step 8 : Repeat step 4, 5 and 6 until inputNumber > 0.

Step 8 : Check whether sum is equal to copyOfInputNumber.

Step 9: If it is equal, given number is Disarium number.

Step 10: Display the number is Disarium number.

Step 11: Otherwise, display number is not Disarium number.

Step 12: Stop


Algorithm-

Step 1: Start

Step 2: Input a number

Step 3: Store it in another variable


Step 4: Now using while loop extract last digit of the number
Step 5: Than by using for loop store the factorial of the digit
Step 6: Than in another variable store the sum of factorial
Step 7: initialize sum variable =1 again
Step 8: update the value of number by storing it without its last digit
Step 9: After the condition satifies completely
Step 10: Compare the original number with the sum of factorial of digits
Step 11: If both are equal than display "It is a special number"
Step 12: Else display "It is not a special number
Step 13: Stop
Algorithm-

Step 1: Start

Step 2: Initialize n, temp, rev, digit, I, k with

Step 3: Print "Enter a number"

Step 4: Read n

Step 5: Initialize temp with n

Step 6: If temp is greater than 0, go to step 7 or else go to step 11

Step 7: Find the digit from temp mod by 10

Step 8: Store the digit in rev from rev=rev*10+digit

Step 9: Find the next number of temp from temp divide by 10

Step 10: Go to step 11

Step 11: If step 17==true && n!=rev, then go to step 26 or else go to step 27

Step 12: Initialize k with 0. Go to step 18

Step 13: Initialize I with 2. Go to step 19

Step 14: If I greater than n, go to step 20 or else go to step 23

Step 15: If rev mod I equals to 0, go to step 21 or else go to step 22

Step 16: k incremented by 1. Go to step 22

Step 17: I incremented by 1. Go to step 19

Step 18: If k initialize by 0, go to step 19 or else go to step 20

Step 19: true. Go to step 17

Step 20: false. Go to step 17

Step 21: Print "The given number is an Emirp Number". Go to step 28

Step 22: Print "The given number is not an Emirp Number".


Step 23: Stop
Algorithm-

Step 1: Start

Step 2: Declare string variable str, vstr, cstr , and integer variable len and
character variable ch.

Step 3: Initialize str with a string and nstr, and store length into len, ch with
default

value.

Step 4: Change the str into upper case .

Step 5: Calculate the length of str in len

Step 6: Start a loop from starting index of str up to last index number of str

Step 7: Store the character value in ch from index.

Step 8: Check the ch value with vowel.

Step 9: IF ch value equals to any vowel then add ch value in vstr

Step 10: Otherwise ch value store in cstr

Step 11: Go to Step 5 until loop condition is true

Step 12: After finishing the loop concate the vstr and cstr.

Step 13: Display the concatenation result.

Step 14: Stop


Algorithm-

Step 1: Start

Step 2: Declare string variable str, nstr, integer variable len, freq, and character

variable ch.

Step 3: Initialize str with a sentence, nstr with default value freq with default

value and ch with default value.

Step 4: Add a space in str and store length into len.

Step 5: Start a loop from starting index of str up to last index number of str

Step 6: Store the character value in ch from index.

Step 7: Check the ch with white space,

Step 8: IF ch is not equals to white space then

Step 9: Stores the ch into nstr

Step 10: And check the first index character of nstr with capital letter

Step 11: IF condition is true the freq incremented by 1.

Step 12: Display the nstr.

Step 12: nstr initialize with default value.

Step 13: IF condition is false then Go To Step 5 to Step 11 until loop is


continue.

Step 14: Display the freq.

Step 15: Sto


Algorithm-

Step 1: Start

Step 2: Declares string variable sen, rev, w and integer variable size, character

variable ch

Step 3: Read a sentence in sen, and rev, w, size, and ch with default value.

Step 4: Add a white space in sen.

Step 5: Store the length of sen in size.

Step 6: Start a loop from starting index of sen up to less than size.

Step 7: Store the character from index number by loop.

Step 8: Check the character is not equals to white space,

Step 9: IF it is true, than stores the value of ch in w

Step 10: IF it is false, than find the length of the w.

Step 11: Store the 0 index character in a variable ch1, and store the last index
character in ch2

Step 12: If loop condition is not false, than Go to step 7

Step 13: If loop condition is false than, add ch1, ch2, and extracted word from

index 1 to size-1 through substring method in rev.

Step 14: Display the rev.

Step 15: Stop


Algorithm-

Step 1: Start

Step 2: Declare String variable word, shiftwrd, changewrd and integer variable

len

Step 3: Initialize the variables with default value

Step 4: Read a string in word and add a white space in the word

Step 5: Find the length of string into len

Step 6: Start a loop from starting index of string up to less than len.

Step 7: Check the every character with the space.

Step 8: If character is equal to white space than

Step 9: Start a loop from starting index of word up to less than length of word
before space.

Step 10: Checks the every character of index with consonant, if it is true than

character stores in the shiftwrd, otherwise store in another string.

Step 11: Go To step 5 to Step 10 till loop condition is false.

Step 12: Now start a loop from starting index of shiftwrd up to last index of
shiftwrd

Step 13: Checks every consonant character with occurring consonant.

Step 14: Convert the occurring consonant in upper case and store in
changewrd.

Step 15: Display the word, shiftwrd and changewrd.

Step 16: Stop


Algorithm-

Step 1: Start

Step 2: Declare a string variable str and initialize with default value.

Step 3: Read a string into str.

Step 4: Start a loop from starting index or str up to end index of str.

Step 5: Compare the indexed character with next indexed character.

Step 6: If first index character greater than next index character than

Step 7: Replace the first character with second character and store each
character in a array.

Step 8: Go to step 4 to step 7.

Step 9: Display Array values.

Step 10: Stop.


Algorithm-

Step 1: Start

Step 2: Declare integers variables x, y, z, n.

Step 3: Initialize x by a, y by b and z by ba.

Step 4: Read a integer value in n.

Step 5: Start a loop from 1 to n and incremented by 1

Step 6: Concate first and second string and store in third string and print this

Step 7: Now store second string in first string and third into second

Step 8: Concate again first and second and Store in third string and then print.

Step 9: Go to step 5 to step 8 until loop is continue.

Step 10: Stop.


Algorithm-

Step 1: Start

Step 2: Declare a string wrd, swapwrd and sortwrd integer variable len

Step 3: Read a string into wrd

Step 4: Find the length of wrd

Step 5: Check if the length of the word is less than or equal to 1. If so, return
the word as it is.

Step 6: Otherwise, store the first character of the word and the last character of
the word in separate variables.

Step 7: Create a substring of the word, excluding the first and last characters.

Step 8: Concatenate the last character, the substring, and finally the first
character to form the interchanged word.

Step 9: Return the interchanged word.

Step 10: Display the sort, and swapwrd

Step 11: Stop


Algorithm-

Step 1: Start

Step 2: Start by defining a class called `Matrix` that represents a matrix.

Step 3: Within the `Matrix` class, define instance variables to store the number

of rows (`rows`) and number of columns (`columns`) of the matrix.

Step 4: Create a constructor for the `Matrix` class that takes the number of

rows and number of columns as parameters and initializes the instance

variables accordingly.

Step 5: Define a two-dimensional array called `data` in the `Matrix` class to

store the elements of the matrix.

Step 6 : Create a method `subtract` that takes another `Matrix` object as a

parameter and returns the result of subtracting the calling matrix from

the parameter matrix.

Step 7: Within the `subtract` method, first check if the dimensions of the two

matrices are compatible for subtraction.

Step 8: If the dimensions are incompatible (the number of rows and columns

do not match), return an error message or throw an exception.

Step 9: If the dimensions are compatible, initialize a new `Matrix` object

called `result` with the same dimensions as the calling matrix and the

parameter matrix .

Step 10: Iterate over each element of the calling matrix and subtract the

corresponding element from the parameter matrix, storing the result

in the ` result` matrix.

Step 11: Once all elements have been subtracted, return the `result` matrix.
Step 12: Display the resultant array.

Step 13: Stop.


Algorithm-

Step 1: Start

Step 2: defining two sorted arrays, let's call them `array1` and `array2`, which

have already been sorted in ascending order.

Step 3: Create a new array with a length equal to the sum of the lengths of

array1 and array2.

Step 4: Initialize three variables:

i` to keep track of the current index of `array1` (starting from 0)

j` to keep track of the current index of `array2` (starting from 0)

`k` to keep track of the current index of `mergedArray` (starting from 0)

Step 5: Create a loop that iterates until either `i` reaches the end of `array1` or

`j` reaches the end of `array2`.

Step 6: Within the loop, compare the elements at indexes `i` and `j` from

`array1` and `array2` respectively.

Step 7: If `array1[i]` is smaller than or equal to `array2[j]`, store `array1[i]` in


`mergedArray[k]` and increment `i` and `k` by 1.

Step 8: If `array2[j]` is smaller than `array1[i]`, store `array2[j]` in

`mergedArray[k]` and increment `j` and `k` by 1.

Step 9: Repeat steps 6-7 until either `i` reaches the end of `array1` or `j`

reaches the end of `array2`.

Step 10: After the loop, check if there are any remaining elements in `array1`

or `array2`.

Step 11: If there are remaining elements in `array1`, copy them to

`mergedArray` starting from index `k`.

Step 12: If there are remaining elements in `array2`, copy them to


`mergedArray”starting from index `k`.

Step 13: Display the elements of `mergedArray` to show the merged and

sorted array.

Step 14: Stop

You might also like