programming exercise 2 documentation
programming exercise 2 documentation
Title: Exercise 2
Due Date: 2024
TABLE OF CONTENT
INTRODUCTION...........................................................................................................................................2
Question 1: Create a program that identifies if a number is a palindrome.................................................3
Question 2: Write a program to identify if a number is an Armstrong number.........................................10
Question 3: Create a program that prints the Fibonacci series.................................................................15
Question 4: Write a program to convert a decimal number to a binary number......................................19
INTRODUCTION
Programming is essential for effectively resolving challenging real-world issues. Widely utilized
in many software development sectors, the C programming language is renowned for its
performance and command over system resources. Through real-world situations, this exercise
offers a chance to investigate basic C programming concepts. This exercise attempts to
strengthen fundamental programming skills by creating programs for typical computational tasks
like recognizing palindromes, Armstrong numbers, creating Fibonacci sequences, and converting
decimal numbers to binary.
This exercise's programs show how to use fundamental programming components like loops,
conditionals, arrays, and user input handling. Every program aims to teach effective problem-
solving strategies, fundamental algorithmic thinking, and numerical manipulation. In subsequent
assignments, more complex programming tasks will be built upon the knowledge acquired from
creating and evaluating these programs.
LABORATORY WORK
Task Description
Step 4: Extract digits from UserInput and store them in UserInputValues array:
PROGRAM CODE
CONCLUSION
This application effectively determines if a user-inputted integer is a palindrome. The software
extracts, stores, and reverses the number's digits, then compares the original and reversed
sequences to see if they match. If they do, the software verifies that the number is a palindrome;
if not, it makes a note to that effect. In the process, the program reinforces fundamental
programming concepts like loops, conditional statements, and receiving user input while
demonstrating a method of manipulating numbers and basic array operations. This method works
well for checking for palindromic integers and can be modified for various programming
comparisons or digit-based manipulations.
Question 2: Write a program to identify if a number is an Armstrong
number.
Aim:
This program's objective is to identify if an integer is an Armstrong number, sometimes referred
to as a narcissistic number. A three-digit number that equals the sum of the cubes of its digits is
known as an Armstrong number. After extracting the input number's component digits and
raising each one to the power of three, the software determines whether the sum of these cubes
equals the original number. The program recognizes the number as an Armstrong number if the
total equals the original number; if not, it does not.
Task Description
METHOD/ ALGORITHM
1. Start
2. Declare variables:
a. UserInput: To store the input number.
b. PlaceHolderCounter: To count the digits of the number.
c. DivisionResultInt: To store the integer part after division by 10.
d. loop: To control the extraction of digits.
e. CurrentPlaceHolderInt: To store each digit.
f. BaseCounter, Sum, Base, PowerFinder: For calculating the sum of powered digits.
g. Indice: Set to 3 (since we are dealing with a 3-digit number).
h. NewPlaceHolderCounter: To track the last index of digits.
3. Input the number:
a. Display the prompt: "Armstrong number: "
b. Read the UserInput number.
4. Extract digits:
a. Set DivisionResultDouble to UserInput.
b. Initialize PlaceHolderCounter = 0 and loop = 0.
c. While loop == 0:
i. Divide DivisionResultDouble by 10.
ii. Set DivisionResultInt to the integer part of DivisionResultDouble.
iii. Calculate CurrentPlaceHolderDouble as the fractional part of
DivisionResultDouble.
PROGRAM CODE
CONCLUSION
By following a methodical procedure, the computer successfully ascertains whether a given
number is an Armstrong number. The input number's digits are extracted, the total of the cubes
of those digits is calculated, and the sum is compared to the original number. It verifies if the
number is an Armstrong number if the sum equals the input number; if not, it does not.
The program can be made to handle numbers with any number of digits by dynamically varying
the power according to the number of digits, even though it is made for three-digit numbers
because the power of three is hardcoded. The program is an excellent illustration of number-
based reasoning in programming since it illustrates fundamental ideas of digit manipulation,
loops, and arithmetic operations.
Task Description
METHOD/ ALGORITHM
Start
Declare UserInput, FibonacciCounter
Declare number_1 as 0, number_2 as 1, number_3
Print "Input Required Length of Fibonacci series: "
Read UserInput
If UserInput is less than 1
o Print "Invalid Input. Please enter a positive integer."
o Exit
Print "Fibonacci series:"
If UserInput is equal to 1
o Print number_1
Else If UserInput is equal to 2
o Print number_1, number_2
Else
o Print number_1, number_2
Set FibonacciCounter to 2
While FibonacciCounter is less than UserInput
o Set number_3 to the sum of number_1 and number_2
o Print number_3
o Set number_1 to number_2
o Set number_2 to number_3
o Increment FibonacciCounter by 1
End While
Print a new line
End
PROGRAM CODE
CONCLUSION
Up to a user-specified number of words, this software can properly construct and show the
Fibonacci series. In order to ensure that the input is a valid positive integer, it first asks the user
to enter the desired length of the series. The application manages many situations, such as when
the user wants more than two terms, the first two terms, or just the first term. Until the desired
length is attained, it iteratively computes the Fibonacci sequence, updating and printing the
subsequent term in the series.
The application guarantees effective operation and user-friendly interaction by employing input
validation and generating the Fibonacci numbers using a straightforward loop. The program is a
reliable method for creating Fibonacci sequences since it can handle varying series lengths and
clearly indicate incorrect input.
Start.
Input: Ask the user to input a decimal number.
Extract Whole and Fractional Parts:
Convert the decimal number to an integer and store it in UserInputInt (this will give the
whole number part).
Subtract the whole number part from the original decimal number to get the fractional
part (UserInputDouble).
Initialize Arrays and Variables:
Create an array WholeNumberBinValue[] to store the binary digits of the whole number
part.
Create a variable WholeNumberBinValueCounter to count the number of binary digits.
Initialize NewUserInputInt to the whole number part UserInputInt.
Convert Whole Number to Binary:
While NewUserInputInt is not 0:
o Find the remainder when dividing NewUserInputInt by 2. This gives the binary
digit (either 0 or 1).
o Store the binary digit in
WholeNumberBinValue[WholeNumberBinValueCounter].
o Update NewUserInputInt by dividing it by 2 (integer division).
o Increment WholeNumberBinValueCounter.
Reverse the Binary Digits:
Create a new array ReverseWholeNumberBinValue[] of size
WholeNumberBinValueCounter.
Copy the binary digits from WholeNumberBinValue[] into
ReverseWholeNumberBinValue[] in reverse order.
Print the Binary Output:
Print the binary digits stored in ReverseWholeNumberBinValue[] from the first to the
last digit.
End.
METHOD/ ALGORITHM
START
Declare UserInput, UserInputDouble as double
Declare UserInputInt as integer
Declare WholeNumberBinValue as array of integers
Declare ReverseWholeNumberBinValue as array of integers
Declare BinNumber, NewUserInputInt, WholeNumberBinValueCounter, Reverse,
Forward as integer
PRINT "Input Decimal Number: "
READ UserInput
UserInputInt = Integer part of UserInput
UserInputDouble = Fractional part of UserInput
WholeNumberBinValueCounter = 0
NewUserInputInt = UserInputInt
WHILE NewUserInputInt != 0
BinNumber = NewUserInputInt % 2
WholeNumberBinValue[WholeNumberBinValueCounter] = BinNumber
NewUserInputInt = NewUserInputInt / 2
WholeNumberBinValueCounter = WholeNumberBinValueCounter + 1
END WHILE
// Reverse the binary digits
Reverse = WholeNumberBinValueCounter - 1
Forward = 0
WHILE Reverse >= 0
ReverseWholeNumberBinValue[Forward] = WholeNumberBinValue[Reverse]
Forward = Forward + 1
Reverse = Reverse - 1
END WHILE
// Display the binary equivalent
PRINT "Binary Output Number: "
FOR i = 0 TO WholeNumberBinValueCounter - 1
PRINT ReverseWholeNumberBinValue[i]
END FOR
END
PROGRAM CODE
CONCLUSION
To sum up, this software effectively translates a decimal input's whole number portion into its
binary equivalent. To show the correct binary representation, the program harvests binary digits
and stores them in reverse order using a division-by-2 approach. This program illustrates the
fundamentals of binary conversion, which can be expanded to incorporate the fractional portion
of the input, even if it only processes the integer portion. In computer science, the software
provides a strong basis for comprehending number systems and the conversion process.
Handling negative values, translating the fractional portion, and enhancing memory management
are possible additional improvements.