0% found this document useful (0 votes)
6 views2 pages

Programming Practice Questions Guide

The document contains a list of programming practice questions focused on basic algorithms and number manipulation. Tasks include calculating averages of odd and even numbers, checking for prime numbers, displaying multiplication tables, identifying Armstrong numbers, reversing integers, generating a series based on a given algorithm, and finding the next perfect square. Each question is designed to enhance programming skills and understanding of mathematical concepts.

Uploaded by

raneerizal
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)
6 views2 pages

Programming Practice Questions Guide

The document contains a list of programming practice questions focused on basic algorithms and number manipulation. Tasks include calculating averages of odd and even numbers, checking for prime numbers, displaying multiplication tables, identifying Armstrong numbers, reversing integers, generating a series based on a given algorithm, and finding the next perfect square. Each question is designed to enhance programming skills and understanding of mathematical concepts.

Uploaded by

raneerizal
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

Practice Questions

1. WAP to enter 10 numbers and find and display average


of odds and evens separately.
2.

2. WAP to enter a number and check if it is prime or not.


2.1 WAP to display all primes less than 100
2.2 WAP to display first 50 primes.

3. WAP to enter as many positive numbers. End the loop if 0


or negative is entered. Finally display the average of all
entered positive numbers.

4. WAP to display a multiplication table upto 10 for a user


input number.

5. Display all Armstrong numbers from 1 to 1000. An


Armstrong number is one whose sum of cubes of its digits is
equal to the number. For eg. 371 = 33+73+13
6. Write a program to reverse an integer and print it.
Eg. Input Output
51 15
849 948
-512 -215
7. Consider the following algorithm:
 Start with a positive number n
 if n is even then divide by 2
 if n is odd then multiply by 3 and add 1
 continue this until n becomes 1
Input a positive number and generate the series given above.
8. Enter a number and find the next perfect square number.
Eg. Next perfect square of 5 is 9, 20 is 25, 36 is 49, 0 is 1, -9 is 0.

Common questions

Powered by AI

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 .

You might also like