0% found this document useful (0 votes)
7 views1 page

C Programming While Loop Exercises

Uploaded by

ashishbalija80
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

C Programming While Loop Exercises

Uploaded by

ashishbalija80
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

WHILE LOOP EXERCISES – C PROGRAMMING

Print numbers from 1 to 10 using a while loop.

Print even numbers from 2 to 20 using a while loop.

Find the sum of first N natural numbers using a while loop.


Example: N = 5 → 1 + 2 + 3 + 4 + 5 = 15

Reverse the digits of a number using a while loop.


Example: 1234 → 4321

Count the digits of a number using a while loop.


Example: 10509 → 5 digits

Find the factorial of a number using a while loop.


Example: 5 → 120

Check if a number is a palindrome using a while loop.


Example: 121 → palindrome, 123 → not palindrome

Find the sum of digits of a number using a while loop.


Example: 987 → 9 + 8 + 7 = 24

Check if a number is an Armstrong number using a while loop.


Example: 153 → 1³ + 5³ + 3³ = 153

Print the multiplication table of a number using a while loop.


Example: Input = 5 → Output: 5 × 1 = 5 … 5 × 10 = 50

Find the smallest digit in a number using a while loop.


Example: 472 → 2

Find the largest digit in a number using a while loop.


Example: 472 → 7

Check if a number is prime using a while loop (do not use for loop).

Print the Fibonacci series up to N terms using a while loop.


Example: 0, 1, 1, 2, 3, 5, 8 …

Find the sum of all even and odd digits separately in a number using a while loop.

Convert a binary number to decimal manually using a while loop.


Example: 1011 → 11

Calculate power (x^y) using a while loop (do not use pow() function).

Find LCM and GCD of two numbers using a while loop.

Print all digits of a number in words using a while loop.


Example: 123 → One Two Three

Check if a number is a strong number using a while loop.


(Sum of factorials of digits equals the number)
Example: 145 → 1! + 4! + 5! = 145

Common questions

Powered by AI

To count the digits in a number using a while loop, initialize a counter to 0. Divide the number by 10 repeatedly until it becomes zero, incrementing the counter on each iteration. The counter's final value will be the number of digits in the original number .

To manually convert a binary number to decimal using a while loop, start from the least significant digit. Initialize a result variable at 0. For each binary digit, if it's 1, add the current power of 2 to the result. Calculate the power of 2 based on the position of the digit, updating the position on each iteration of the loop. The process continues until all binary digits are processed .

To print the Fibonacci series up to N terms using a while loop, start with the first two terms, 0 and 1. Use a loop that updates the next term as the sum of the last two terms, then shift the last two terms forward. Continue this process for N iterations. Keep track of the terms generated to print them .

To reverse the digits of a number using a while loop, initialize a variable to hold the reversed number. Extract the last digit using modulo 10 and add it to the reversed number after multiplying the existing reversed number by 10. Then eliminate the last digit of the original number by dividing by 10. Continue this process until the original number becomes zero. Key consideration includes handling of numbers ending with zero as the reversed number would not preserve them .

To check if a number is an Armstrong number using a while loop, you need to find the sum of its digits each raised to the power of the number of digits. Start by determining the number of digits, then use a loop to isolate each digit, raise it to the power of the total digits, and accumulate this sum. Finally, compare this sum to the original number to verify it is an Armstrong number .

To identify the largest digit in a number using a while loop, initialize a variable to keep track of the maximum digit found. Extract each digit using modulo 10, and compare it with the current maximum, updating if the current digit is larger. Remove the last digit using integer division by 10 and repeat until the number becomes zero .

To calculate the factorial of a number using a while loop, initialize a variable to 1 which will hold the result. Use another variable to iterate from 1 up to the given number. In each iteration, multiply the result by the loop variable and update the result. Continue until the iteration reaches the given number .

To check if a number is prime using only while loops, first handle edge cases for numbers less than 2. Start a loop beginning from 2, continuing while the loop counter is less than or equal to the square root of the number, checking divisibility by iterating factors. If divisibility is found, the number is not prime. Otherwise, if no divisors are found, it is prime .

To calculate the LCM and GCD using while loops, implement the Euclidean algorithm for the GCD, where you repeatedly subtract the smaller number from the larger one until one of them is zero. The non-zero value at this point is the GCD. For LCM, use the relation LCM(a, b) = (a*b)/GCD(a, b) after obtaining the GCD .

To find the sum of the digits of a number using a while loop, initialize a sum variable to 0. Use the loop to separate each digit of the number by performing modulo 10, adding it to the sum, and then removing the last digit by dividing the number by 10. Repeat until the number is reduced to zero .

You might also like