C Programming • C Basics, Operators, Loops
C Programming / while — Exercises

while — Exercises

Practical 2 C Basics, Operators, Loops

Write a C program to reverse the digits of a given number using a while loop.

Practical / Solution

while Loop — Exercise 1

Problem Statement

Write a C program to reverse the digits of a given number using a while loop.

Learning Outcomes

  • Understand the working of a while loop.
  • Extract the last digit using the modulus operator.
  • Build a reversed number step by step.

Hint

Use % 10 to extract the last digit and /= 10 to remove the last digit.

Theory

A while loop repeatedly executes a block of statements while its condition remains true. Here, the loop continues until all digits of the number have been processed.

Program

#include <stdio.h> int main() { int num, reverse = 0, digit; // Read the number printf("Enter a number to reverse: "); scanf("%d", &num); // Extract digits and build the reversed number while (num != 0) { digit = num % 10; // get last digit reverse = reverse * 10 + digit; // add digit to reverse num /= 10; // remove last digit } printf("Reversed number = %d\n", reverse); return 0; }

Expected Output

Enter a number to reverse: 12345

Reversed number = 54321

Note

The condition is checked before every iteration, so the loop stops when the number becomes zero.

while Loop — Exercise 2

Problem Statement

Write a C program to count the number of digits in a given number using a while loop.

Learning Outcomes

  • Use a while loop for repeated processing.
  • Understand how integer division removes the last digit.
  • Count the digits of a number.

Hint

Repeatedly divide the number by 10 and increase the counter by 1 until the number becomes zero.

Theory

Each integer division by 10 removes the last digit of a positive integer. The number of such divisions required to reach zero gives the total number of digits.

Program

#include <stdio.h> int main() { int num, count = 0; // Read the number printf("Enter a number: "); scanf("%d", &num); // Count digits while (num != 0) { num /= 10; // remove the last digit count++; } printf("Number of digits = %d\n", count); return 0; }

Expected Output

Enter a number: 12345

Number of digits = 5

Note

This approach directly demonstrates how a loop can repeatedly process each digit of an integer.

while Loop — Exercise 3

Problem Statement

Write a C program to calculate the sum of the digits of a given number using a while loop.

Learning Outcomes

  • Extract individual digits from a number.
  • Perform repeated addition using a loop.
  • Understand digit-processing logic.

Hint

Extract the last digit using % 10, add it to sum, and remove the last digit using /= 10.

Theory

The modulus operator returns the last digit of an integer when it is divided by 10. By repeatedly extracting and adding these digits, the sum of all digits can be calculated.

Program

#include <stdio.h> int main() { int num, digit, sum = 0; // Read the number printf("Enter a number: "); scanf("%d", &num); // Add all digits while (num != 0) { digit = num % 10; // extract last digit sum += digit; // add digit to sum num /= 10; // remove last digit } printf("Sum of digits = %d\n", sum); return 0; }

Expected Output

Enter a number: 12345

Sum of digits = 15

Note

The same digit-extraction pattern is useful in many number-based programming problems.

while Loop — Exercise 4

Problem Statement

Write a C program to check whether a given number is a palindrome using a while loop.

Learning Outcomes

  • Reverse a number using a loop.
  • Compare the original number with its reverse.
  • Apply multiple operations inside a loop.

Hint

Store the original number, create its reverse using % 10 and /= 10, then compare both values.

Theory

A palindrome number reads the same from left to right and right to left. The program checks this by creating the reverse of the number.

Program

#include <stdio.h> int main() { int num, original, reverse = 0, digit; printf("Enter a number: "); scanf("%d", &num); original = num; // Create the reverse while (num != 0) { digit = num % 10; reverse = reverse * 10 + digit; num /= 10; } // Compare original and reverse if (original == reverse) printf("%d is a Palindrome.\n", original); else printf("%d is not a Palindrome.\n", original); return 0; }

Expected Output

Enter a number: 121

121 is a Palindrome.

Note

A copy of the original number is required because the working loop changes the value of the input number.

while Loop — Exercise 5

Problem Statement

Write a C program to check whether a given number is an Armstrong number using a while loop.

Learning Outcomes

  • Process digits repeatedly using a loop.
  • Calculate the cube of each digit.
  • Compare the calculated sum with the original number.

Hint

Extract each digit using % 10, add its cube to sum, and remove the digit using /= 10.

Theory

A three-digit Armstrong number is a number whose value is equal to the sum of the cubes of its digits. For example, 153 = 1³ + 5³ + 3³.

Program

#include <stdio.h> int main() { int num, original, digit, sum = 0; printf("Enter a number: "); scanf("%d", &num); original = num; // Calculate sum of cubes of digits while (num != 0) { digit = num % 10; sum += digit * digit * digit; num /= 10; } // Compare result with original number if (sum == original) printf("%d is an Armstrong number.\n", original); else printf("%d is not an Armstrong number.\n", original); return 0; }

Expected Output

Enter a number: 153

153 is an Armstrong number.

Note

This exercise is intended for the basic three-digit Armstrong number concept taught at this level.

while Loop — Exercise 6

Problem Statement

Write a C program to check whether a given number is prime using a while loop.

Learning Outcomes

  • Use a loop to test possible divisors.
  • Understand divisibility and remainder.
  • Use a counter to track successful divisions.

Hint

Check divisibility from 1 up to the given number using the modulus operator and count the number of divisors.

Theory

A prime number has exactly two positive divisors: 1 and itself. The program uses a while loop to test possible divisors.

Program

#include <stdio.h> int main() { int num, i = 1, count = 0; printf("Enter a number: "); scanf("%d", &num); // Count divisors while (i <= num) { if (num % i == 0) count++; i++; } if (num > 1 && count == 2) printf("%d is a Prime number.\n", num); else printf("%d is not a Prime number.\n", num); return 0; }

Expected Output

Enter a number: 17

17 is a Prime number.

Note

This version emphasizes the basic idea of checking divisors, making it suitable for first-year beginners.

while Loop — Exercise 7

Problem Statement

Write a C program to calculate the factorial of a number using a while loop.

Learning Outcomes

  • Use a loop for repeated multiplication.
  • Understand factorial calculation.
  • Update a result variable during each iteration.

Hint

Initialize fact = 1 and multiply it by each number from 1 to n.

Theory

The factorial of a non-negative integer N is the product of all positive integers from 1 to N. It is written as N!.

Program

#include <stdio.h> int main() { int n, i = 1; long long fact = 1; printf("Enter a number: "); scanf("%d", &n); // Calculate factorial while (i <= n) { fact *= i; i++; } printf("Factorial = %lld\n", fact); return 0; }

Expected Output

Enter a number: 5

Factorial = 120

Note

The factorial of 0 is also defined as 1. For large values, the factorial may exceed the range of the selected data type.

while Loop — Exercise 8

Problem Statement

Write a C program to generate the Fibonacci series up to N terms using a while loop.

Learning Outcomes

  • Understand repeated sequence generation.
  • Update multiple variables inside a loop.
  • Use a loop counter to control repetitions.

Hint

Start with 0 and 1. Each next term is obtained by adding the previous two terms.

Theory

In the Fibonacci series, each term is the sum of the two previous terms. The sequence starts with 0 and 1.

Program

#include <stdio.h> int main() { int n, count = 1; int first = 0, second = 1, next; printf("Enter number of terms: "); scanf("%d", &n); // Generate Fibonacci series while (count <= n) { printf("%d ", first); next = first + second; first = second; second = next; count++; } printf("\n"); return 0; }

Expected Output

Enter number of terms: 7

0 1 1 2 3 5 8

Note

The loop is controlled by the number of terms rather than by the value of a number being reduced.

while Loop — Exercise 9

Problem Statement

Write a C program to calculate the sum of the first N natural numbers using a while loop.

Learning Outcomes

  • Use a counter-controlled loop.
  • Perform repeated addition.
  • Understand how the loop variable changes after each iteration.

Hint

Start with sum = 0 and add each number from 1 to N.

Theory

Natural numbers begin with 1. Their sum can be calculated by repeatedly adding each number until N is reached.

Program

#include <stdio.h> int main() { int n, i = 1, sum = 0; printf("Enter N: "); scanf("%d", &n); // Add natural numbers from 1 to N while (i <= n) { sum += i; i++; } printf("Sum = %d\n", sum); return 0; }

Expected Output

Enter N: 10

Sum = 55

Note

This exercise shows a simple counter-controlled while loop and is useful for understanding initialization, condition, body, and update.

while Loop — Exercise 10

Problem Statement

Write a C program to print the multiplication table of a given number using a while loop.

Learning Outcomes

  • Use a counter variable with a while loop.
  • Perform repeated multiplication.
  • Generate structured output using a loop.

Hint

Start the counter from 1 and continue until it reaches 10.

Theory

A multiplication table can be generated by repeatedly multiplying a fixed number by consecutive integers. A while loop avoids writing the same statement ten times.

Program

#include <stdio.h> int main() { int num, i = 1; printf("Enter a number: "); scanf("%d", &num); // Print table from 1 to 10 while (i <= 10) { printf("%d x %d = %d\n", num, i, num * i); i++; } return 0; }

Expected Output

Enter a number: 5

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Note

The counter is initialized before the loop and updated at the end of each iteration. This is a basic example of a counter-controlled while loop.