0% found this document useful (0 votes)
10 views17 pages

Binary and Octal Conversion Tool

Computer programming project

Uploaded by

Farman Alam
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)
10 views17 pages

Binary and Octal Conversion Tool

Computer programming project

Uploaded by

Farman Alam
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

public static void main() {

Scanner sc = new Scanner([Link]);

[Link]("1. Press 1 to convert binary to integer when fractional part = 0.");

[Link]("2. Press 2 to convert binary to integer when fractional part is not 0.");

[Link]("3. Press 3 to convert octal to integer when fractional part = 0.");

[Link]("4. Press 4 to convert octal to integer when fractional part is not 0.");

[Link]("We will only consider the digits corresponding to the base.");

int x = [Link]();

switch (x) {

case 1:

[Link]("Enter the number:");

int x1 = [Link]();

x1 = [Link](x1);

[Link]("(" + x1 + ")base 2 = (" + btd(x1) + ")base 10");

break;

case 2:

[Link]("Enter the number:");

double x2 = [Link]();

[Link]("Enter the number of digits after point in case of recurring:");

int k = [Link]();

x2 = [Link](x2);

k = [Link](k);

[Link]("(" + x2 + ")base 2 = (" + btdf(x2, k) + ")base 10");

break;

case 3:

[Link]("Enter the number:");

int x3 = [Link]();

x3 = [Link](x3);

[Link]("(" + x3 + ")base 8 = (" + otd(x3) + ")base 10");

break;
case 4:

[Link]("Enter the number:");

double x4 = [Link]();

[Link]("Enter the number of digits after point in case of recurring:");

int k2 = [Link]();

x4 = [Link](x4);

k2 = [Link](k2);

[Link]("(" + x4 + ")base 8 = (" + otdf(x4, k2) + ")base 10");

break;

default:

[Link]("Invalid input.");

}
 Start

 Input Initialization

1. Prompt the user to enter the number of sentences.

2. Read the integer n, representing the number of sentences.

 Read Sentences

1. Initialize an array a of size n.

2. For each index i from 0 to n-1:

1. Prompt the user to enter a sentence.

2. Read the sentence and store it in a[i].

 Process Each Sentence

1. For each index i from 0 to n-1:

1. Print "Sentence i+1 : Palindrome words -".

2. Initialize an empty string x.

3. Surround a[i] with spaces at the beginning and end.

4. Initialize a counter f to 0.

 Extract and Check Words

1. For each character j in the sentence a[i]:

1. If the character is alphabetic:

1. Initialize an empty string x.

2. For each character k from j to the next space:

1. If the character is alphabetic:

1. Append the character to x.

3. If x is a palindrome:

1. Print the word x.

2. Increment the counter f by 1.

4. Move j to the next space character or the end of the sentence.

 Handle No Palindromes

1. If the counter f is 0:

1. Print "No palindrome words in the sentence".

2. Print "Frequency of palindrome words = 0".

 Print Frequency
1. If the counter f is greater than 0:

1. Print the frequency of palindrome words in the sentence.

 End

Vdt for 1st prog

Variable number Name Data type Scope Description


1 x String NoOfV(String x) Parameter
2 a[] String array main() Strores
sentences
3 I int NoOfV(String x) LCV
4 I int main() LCV
5 x int main() Stores words
6 f Int main() Flag
7 j int main() LCV

Second program

1. Start

2. Input Initialization

1. Prompt the user to enter the number of elements in the array.

2. Read the integer n, representing the number of elements.

3. Read Array Elements

1. Initialize an array a of size n.

2. Prompt the user to enter the elements of the array.

3. For each index i from 0 to n-1:

1. Read the element and store it in a[i].

4. Check if Array is Sorted

1. Initialize a boolean variable isSorteda to true (to check if the array is sorted in ascending
order).

2. Initialize a boolean variable isSortedd to true (to check if the array is sorted in descending
order).

3. For each index i from 0 to n-2:

1. If a[i] > a[i+1], set isSorteda to false and break the loop.

4. For each index i from 0 to n-2:

1. If a[i] < a[i+1], set isSortedd to false and break the loop.

5. Output Result
1. If isSorteda or isSortedd is true:

1. Print "Array is sorted".

2. Else:

1. Sort the array in ascending order using a nested loop:

1. For each index i from 0 to n-1:

1. For each index j from 0 to n-2-i:

1. If a[j+1] < a[j], swap the elements a[j+1] and a[j] without
using a temporary variable.

2. Print "Array sorted in ascending order: ".

3. Print the sorted array.

6. End

3 Algorithm for 3rd prog

1. Start

2. Input Initialization

1. Prompt the user to enter the size of the array.

2. Read the integer n, representing the size of the array.

3. Read Array Elements

1. Initialize an array a of size n.

2. Initialize an array b of size n.

3. Initialize an integer s to the maximum possible value (2^31 - 1).

4. For each index i from 0 to n-1:

1. Prompt the user to enter the elements of the array.

2. Read the element and store it in a[i].

3. If a[i] is less than s:

1. Set s to a[i].

2. Set b[0] to a[i].

4. Sort the Array

1. For each index i from 1 to n-1:

1. Set s to the maximum possible value (2^31 - 1).

2. For each index j from 0 to n-1:


1. If a[j] is less than s and greater than b[i-1]:

1. Set s to a[j].

3. Set b[i] to s.

5. Copy Sorted Elements

1. For each index i from 0 to n-1:

1. Copy b[i] to a[i].

2. Print the element a[i].

[Link]

Variable number Variable Name Datatype Scope Description


1 n int main() Stores size
2 a[] integer Array main() Store elements
3 b[] integer array main() Stores sorted
elements
4 s int main() Stores min value
5 I int main() LCV
6 J int main() LCV

Algo for 4th prog

Algorithm for dtb(int x)


1. Start
2. Initialize dtb as an empty string and dtb1 as an empty string.
3. While Loop: While x is greater than 0:
1. If x % 2 is 0, set ch to '0'.
2. Else, set ch to '1'.
3. Append ch to dtb.
4. Divide x by 2 (integer division).
4. Reverse String: Reverse dtb and assign it to dtb1.
5. Return: Return dtb1.
Algorithm for dto(int x)
1. Start
2. Initialize dto as an empty string and dto1 as an empty string.
3. While Loop: While x is greater than 0:
1. Determine ch based on x % 8:
 If x % 8 is 0, set ch to '0'.
 If x % 8 is 1, set ch to '1'.
 If x % 8 is 2, set ch to '2'.
 If x % 8 is 3, set ch to '3'.
 If x % 8 is 4, set ch to '4'.
 If x % 8 is 5, set ch to '5'.
 If x % 8 is 6, set ch to '6'.
 If x % 8 is 7, set ch to '7'.
2. Append ch to dto.
3. Divide x by 8 (integer division).
4. Reverse String: Reverse dto and assign it to dto1.
5. Return: Return dto1.
Algorithm for dtbf(double x, int k)
1. Start
2. Extract the integer part of x as intPart.
3. Extract the fractional part of x as fracPart.
4. Initialize dtb1 with the result of dtb(intPart) and append a period ('.').
5. While Loop: While fracPart is greater than 0 and k is greater than 0:
1. Multiply fracPart by 2.
2. If the integer part of fracPart is 0, set ch to '0'.
3. If the integer part of fracPart is 1, set ch to '1'.
4. Append ch to dtb1.
5. Subtract the integer part of fracPart from fracPart.
6. Decrease k by 1.
7. If fracPart is 0, break the loop.
6. Return: Return dtb1.
Algorithm for dtof(double x, int k)
1. Start
2. Extract the integer part of x as intPart.
3. Extract the fractional part of x as fracPart.
4. Initialize dto1 with the result of dto(intPart) and append a period ('.').
5. While Loop: While fracPart is greater than 0 and k is greater than 0:
1. Multiply fracPart by 8.
2. Convert the integer part of fracPart to a character ch.
3. Append ch to dto1.
4. Subtract the integer part of fracPart from fracPart.
5. Decrease k by 1.
6. If fracPart is 0, break the loop.
6. Return: Return dto1.
Algorithm for main()
1. Start
2. Prompt the user to select an option:
o Press 1 to convert integer to binary when fractional part = 0.
o Press 2 to convert integer to binary when fractional part is not 0.
o Press 3 to convert integer to octal when fractional part = 0.
o Press 4 to convert integer to octal when fractional part is not 0.
3. Read the integer x representing the selected option.
4. Switch Case:
1. Case 1:
1. Prompt the user to enter an integer.
2. Read the integer x1.
3. Call dtb(x1) and print the result.
2. Case 2:
1. Prompt the user to enter a decimal number.
2. Read the double x2.
3. Prompt the user to enter the number of fractional digits (greater than 5).
4. Read the integer k.
5. If k is less than 5, print "Didn't follow instruction" and exit.
6. Call dtbf(x2, k) and print the result.
3. Case 3:
1. Prompt the user to enter an integer.
2. Read the integer x3.
3. Call dto(x3) and print the result.
4. Case 4:
1. Prompt the user to enter a decimal number.
2. Read the double x4.
3. Prompt the user to enter the number of fractional digits (greater than 5).
4. Read the integer k2.
5. If k2 is less than 5, print "Didn't follow instruction" and exit.
6. Call dtof(x4, k2) and print the result.
5. Default:
1. Print "Invalid input".
5. End

Algorithm for 5th program


Function dtb(int x)
1. Start
2. Initialize dtb as an empty string.
3. Initialize dtb1 as an empty string.
4. While Loop: Repeat while x > 0:
1. Determine Bit:
 If x % 2 == 0, set ch to '0'.
 Otherwise, set ch to '1'.
2. Append ch to dtb.
3. Update x to x / 2 (integer division).
5. Reverse dtb: Reverse dtb and assign it to dtb1.
6. Return: Return dtb1.
Function dto(int x)
1. Start
2. Initialize dto as an empty string.
3. Initialize dto1 as an empty string.
4. While Loop: Repeat while x > 0:
1. Determine Octal Digit:
 Set ch based on x % 8:
 If x % 8 == 0, set ch to '0'.
 If x % 8 == 1, set ch to '1'.
 If x % 8 == 2, set ch to '2'.
 If x % 8 == 3, set ch to '3'.
 If x % 8 == 4, set ch to '4'.
 If x % 8 == 5, set ch to '5'.
 If x % 8 == 6, set ch to '6'.
 If x % 8 == 7, set ch to '7'.
2. Append ch to dto.
3. Update x to x / 8 (integer division).
5. Reverse dto: Reverse dto and assign it to dto1.
6. Return: Return dto1.
Function dtbf(double x, int k)
1. Start
2. Extract the integer part of x as intPart.
3. Extract the fractional part of x as fracPart.
4. Initialize dtb1 with the result of dtb(intPart) and append a period ('.').
5. While Loop: Repeat while fracPart > 0 and k > 0:
1. Multiply fracPart by 2.
2. Determine Bit:
 If the integer part of fracPart is 0, set ch to '0'.
 Otherwise, set ch to '1'.
3. Append ch to dtb1.
4. Update fracPart to fracPart - (int) fracPart.
5. Decrease k by 1.
6. If fracPart is 0, break the loop.
6. Return: Return dtb1.
Function dtof(double x, int k)
1. Start
2. Extract the integer part of x as intPart.
3. Extract the fractional part of x as fracPart.
4. Initialize dto1 with the result of dto(intPart) and append a period ('.').
5. While Loop: Repeat while fracPart > 0 and k > 0:
1. Multiply fracPart by 8.
2. Convert the integer part of fracPart to a character ch.
3. Append ch to dto1.
4. Update fracPart to fracPart - (int) fracPart.
5. Decrease k by 1.
6. If fracPart is 0, break the loop.
6. Return: Return dto1.
Main Method
1. Start
2. Print options for conversion:
o Press 1 to convert integer to binary when fractional part = 0.
o Press 2 to convert integer to binary when fractional part is not 0.
o Press 3 to convert integer to octal when fractional part = 0.
o Press 4 to convert integer to octal when fractional part is not 0.
3. Read the integer x representing the user's choice.
4. Switch Case:
1. Case 1:
1. Prompt the user to enter an integer.
2. Read the integer x1.
3. Print x1 in binary format by calling dtb(x1).
2. Case 2:
1. Prompt the user to enter a decimal number.
2. Read the double x2.
3. Prompt the user to enter the number of fractional digits (greater than 5).
4. Read the integer k.
5. If k is less than 5, print "Didn't follow instruction" and exit.
6. Print x2 in binary format by calling dtbf(x2, k).
3. Case 3:
1. Prompt the user to enter an integer.
2. Read the integer x3.
3. Print x3 in octal format by calling dto(x3).
4. Case 4:
1. Prompt the user to enter a decimal number.
2. Read the double x4.
3. Prompt the user to enter the number of fractional digits (greater than 5).
4. Read the integer k2.
5. If k2 is less than 5, print "Didn't follow instruction" and exit.
6. Print x4 in octal format by calling dtof(x4, k2).
5. Default:
1. Print "Invalid input".
5. End

Algo for 6th prog

1 Start
2 Input: Read the number n from the user.
3 Loop: Iterate over possible values of i from 1 to n/2:
 Loop: Iterate over possible values of j from 1 to n/2:
o Check: If i*i*i + j*j*j == n
 Output: Print n + " = " + i + "^3 +" + j + "^3"
 Output: Print "Hence a Ramanujan number"
 Terminate the program
4 Output: Print "Not a Ramanujan number"

5 End
Algo for 7th prog

1. DayNo Function

Purpose: To determine the number of days in a given month.

Algorithm:

1. Start

2. Input: Month number x

3. If-Else Conditions:

o If x is 9, 4, 6, or 11:

 Output: 30 (These months have 30 days)

o Else If x is 2:

 Output: 28 (February has 28 days assuming a non-leap year)

o Else (For all other months):

 Output: 31 (These months have 31 days)

4. End

2. main Function

Purpose: To calculate and print the age and remaining time between two dates.

Algorithm:

1. Start

2. Input:

o Year of birth y

o Month of birth m

o Day of birth d

o Current year y2

o Current month m2

o Current day d2

3. Validation:

o If y2 < y (current year is before birth year) or y2 < 1903 (invalid year) or m < 1 or m
> 12 (invalid birth month) or m2 < 1 or m2 > 12 (invalid current month) or d2 >
DayNo(m2) (invalid current day) or d > DayNo(m) (invalid birth day):

 Print: "Invalid .."

 Exit the program

4. Calculate Age:
o If m2 >= m (current month is the same or after birth month):

 Age in years = y2 - y

o Else (current month is before birth month):

 Age in years = y2 - y - 1

5. Calculate Remaining Time:

o If m2 == m (current month is the same as birth month):

 If d2 > d (current day is after birth day):

 Remaining months = 0

 Remaining days = d2 - d

 Else (current day is before birth day):

 Remaining months = 11

 Remaining days = DayNo(m2) - d + d2

o Else (current month is after birth month):

 If d2 > d (current day is after birth day):

 Remaining months = m2 - m

 Remaining days = d2 - d

 Else (current day is before birth day):

 Remaining months = m2 - m - 1

 Remaining days = DayNo(m2) - d + d2

6. Print: Age in years, remaining months, and remaining days.

7. End
Algo for 8th prog

 Start

 Input: Read a sentence s from the user.

 Preprocessing:

 Remove leading and trailing spaces from s.

 Initialize variable n to count the number of words.

 Count Words:

 Initialize n to 0.

 Loop through each character of s:

o Increment n for each character.

o Move to the next word position by updating i to the index of the next space or the
end of the string.

 Initialize:

 Create an array wo of size n to store words.

 Initialize index k to 0.

 Initialize an empty string w.

 Append a space to the end of s to facilitate word extraction.

 Extract Words:

 Loop through each character of s:

o If the character is not a space, append it to w.

o If the character is a space:

 Assign w to wo[k].

 Increment k.

 Reset w to an empty string.

 Sort Words:

 Loop through each word index i from 0 to n-1:

o Assume max is i.

o Loop through each word index j from i+1 to n-1:

 If the length of wo[max] is less than the length of wo[j], update max to j.
o If max is not i:

 Swap wo[i] with wo[max].

 Output:

 Print "Words in descending length: ".

 Loop through each word index i from 0 to n-1:

o Print each word wo[i] followed by its length in parentheses.

 End
Algo for 9th prog

 Start

 Input: Read a sentence s from the user.

 Preprocessing:

 Remove leading and trailing spaces from s.

 Initialize n to count the number of words.

 Count Words:

 Initialize n to 0.

 For each character in s:

o Increment n.

o Update i to the index of the next space or the end of the string.

 Initialize Arrays:

 Create an array wo of size n to store words.

 Initialize variable k to 0.

 Initialize an empty string w.

 Append a space to the end of s.

 Extract Words:

 For each character in s:

o If the character is not a space, append it to w.

o If the character is a space:

 Assign w to wo[k].

 Increment k.

 Reset w to an empty string.

 Sort Words by Length:

 For each word index i from 0 to n-1:

o Set max to i.

o For each word index j from i+1 to n-1:

 If the length of wo[max] is less than the length of wo[j], update max to j.

o If max is not equal to i:

 Swap wo[i] with wo[max].

 Output:
 Print "Words in descending length: ".

 For each word index i from 0 to n-1:

o Print each word wo[i] followed by its length in parentheses.

 End
1. Start

2. Input:

o Read elements into a 3x3 matrix a.

3. Initialize Matrix:

o Create a 3x3 integer matrix a.

o For each row index i from 0 to 2:

 For each column index j from 0 to 2:

 Read an integer from the user and assign it to a[i][j].

4. Display Original Matrix:

o Print "Elements of array in matrix form:".

o For each row index i from 0 to 2:

 For each column index j from 0 to 2:

 Print a[i][j] followed by a tab character.

 Print a newline character.

5. Mirror Matrix:

o For each row index i from 0 to 2:

 Swap the elements a[i][0] and a[i][2].

6. Display Mirrored Matrix:

o Print "Mirror matrix:".

o For each row index i from 0 to 2:

 For each column index j from 0 to 2:

 Print a[i][j] followed by a tab character.

 Print a newline character.

7. End

Common questions

Powered by AI

Reversing the order of collected digits in conversion processes is crucial because these algorithms collect digits from least significant to most, as the division reduces the number towards zero. Reversing aligns the final output with conventional number systems, presenting from most to least significant digit .

Date validation includes checking temporal logic such as year and month boundaries and compatibility with a defined DayNo function, ensuring days align with current month standards. Age calculation adjusts based on differences in current and birth months/days, correctly managing edge cases like pre-birth month with precision .

The algorithm tests if any pair of integers (i, j), up to n/2, satisfies the condition i^3 + j^3 = n. It leverages nested loops to systematically explore possible combinations, printing a confirmation upon successful identification. The process effectively identifies Ramanujan numbers by exhaustive search within a defined range .

This limitation ensures accuracy and precision in conversions, particularly in algorithms dealing with fractional parts in binary or octal systems. Outputs are constrained by this parameter to prevent infinite loops in cases where fractional values would not stabilize quickly. The enforcement of this rule is to maintain output fidelity and usability .

The algorithm involves separating the integer and fractional parts of a decimal number. The integer part is converted using division by 8. For the fractional part, multiply it by 8, extract the integer portion, and append to 'dto1'. This process continues while the fractional part isn't zero and until a specified number of digits 'k' is reached, appending results after a period .

The algorithm swaps elements in each row between the first and third columns in a 3x3 matrix, executing this row-wise across the entire structure. This columnar mirroring transformation involves constant time operations for each swap, resulting in an O(n) complexity, where n is the number of rows .

The algorithm uses a character-level iteration that continually builds words by appending non-space characters to a temporary string. Space characters signal word boundaries, prompting an assignment of the accumulated word to an array before resetting for the next word. This approach effectively parses continuous sequences segmented by spaces .

The algorithm effectively removes leading/trailing spaces and tokenizes the sentence into words. It then employs a selection sort approach, identifying the longest unsorted word and swapping it into the next position of the sorted portion. This method prioritizes simplicity over computational efficiency, suitable for relatively small datasets as evidenced by its O(n^2) complexity .

The algorithm separates the decimal number into integer and fractional parts. It converts the integer part using standard binary conversion and handles the fractional part by multiplying it by 2, capturing the integer portion, and appending it to the result string 'dtb1'. This fractional conversion process repeats while decrementing a count 'k' to limit iterations. The results for fractions are appended after the binary integer representation, separated by a period .

The algorithm initializes an empty string 'dtb' to store binary digits. It repeatedly divides the input integer by 2 and appends '0' or '1' to 'dtb' based on whether the remainder is 0 or 1. This process continues until the integer is reduced to 0. The string 'dtb' is then reversed to get the correct binary representation, which is stored in 'dtb1' and returned .

You might also like