Programming Practice Questions Guide
Programming Practice Questions Guide
The document illustrates that basic arithmetic operations underpin a wide range of numerical problems, from calculating averages and generating series to discerning number properties like primality and formulating algorithms for special numbers such as Armstrong and perfect squares. This reflects arithmetic's foundational role in constructing and solving algorithmic challenges .
The algorithm described is known as The Collatz Conjecture or the "3n + 1 problem." It begins with a positive integer, and the process is as follows: if the integer n is even, it is divided by 2. If n is odd, it is multiplied by 3 and then 1 is added. This process is repeated until the number becomes 1 .
To find the next perfect square of a given number, calculate the square root of the input number, round it up to the nearest whole number, and square the result to get the next perfect square. This works because perfect squares advance by integer increments of their square roots .
Ending a loop upon receiving a zero or negative number serves as a sentinel control structure to signal completion of input, which is helpful in user-driven environments where the input length is undetermined. This control ensures that only valid positive inputs are processed and computations like averages are only based on meaningful data .
To identify all Armstrong numbers in the range of 1 to 1000, you would iterate over each number, split each into its individual digits, and compute the sum of the cubes of these digits. If this sum equals the original number, the number is an Armstrong number. You would repeat this check for each number in the range to identify every Armstrong number .
To calculate the average of entered positive numbers, you would start by initializing a sum and a count variable. As the numbers are entered, add each positive number to the sum and increment the count. The loop continues until a zero or negative number is entered. The average is then calculated by dividing the sum by the count of numbers entered. This approach effectively ignores non-positive inputs while accumulating the desired values .
Reversing an integer can be achieved by repeatedly extracting the last digit of a number using modulo 10, appending this to a new number that is multiplied by 10 each iteration to shift digits left, and then removing the last digit from the original number using integer division by 10. Special consideration for negative numbers involves preserving the negative sign separately and attaching it back to the reversed value .
A program determines if a number is prime by checking divisibility from 2 to the square root of the number; a prime number only divides by 1 and itself without leaving a remainder. Additionally, the document extends this goal by having separate programs that display all prime numbers under 100 and the first 50 prime numbers .
To generate a multiplication table for a number up to ten, iterate from 1 to 10. For each iteration, multiply the input number by the current iteration number to produce the corresponding product. Display each multiplication expression and its result sequentially .
To separately calculate the averages of odd and even numbers among ten inputs, maintain two sums and two counts—one for even numbers and one for odd numbers. For each number, determine its parity. Add to the corresponding sum and increment the respective count. Calculate each average by dividing the sum by its respective count .