Looping Statements Assignments in C — 30 Programs with Solutions
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

Looping Statements Assignments in C Language with Explanation and Solutions

📑 Jump to a category:
  • GCD, LCM & Divisibility (1–5)
  • Digit Statistics & Operations (6–13)
  • Number-System Conversion & Bits (14–18)
  • Advanced Patterns & Nested Loops (19–24)
  • Series Variants & Algorithms (25–30)
  • Previous / Next Topic
📚 What you'll practice across these 30 assignments:
  • Writing while, do-while, and for loops for real problems
  • Designing correct loop termination conditions and avoiding infinite loops
  • Implementing the Euclidean algorithm (GCD) with a while loop and repeated subtraction
  • Computing LCM, coprime checks, and common divisors with loops
  • Counting, extracting, and analyzing the digits of an integer
  • Converting between binary, octal, decimal, and hexadecimal using loops
  • Counting set bits (popcount) with a while loop
  • Building nested loops for Pascal's triangle, Floyd's triangle, X, plus, and number pyramids
  • Computing series such as ∑1/n², ∑n!, and Tribonacci numbers
  • Finding the largest prime factor of an integer with a while loop
  • Implementing iterative binary search using a while loop
  • Checking whether two numbers are amicable using a for loop

Loops are the engine of every non-trivial C program. This page collects 30 looping / iterative statement assignments that go beyond the usual "print 1 to N" warm-ups. You'll implement the Euclidean algorithm, dissect integers digit by digit, convert between number systems, draw advanced star and number patterns with nested loops, and finish with classic interview algorithms like iterative binary search, largest prime factor, and amicable numbers. Every assignment links to a full tutorial with the program code, sample output, and a line-by-line explanation.

Looping statements do while, while and for loop assignments in C Programming along with practical solutions.

🔹 GCD, LCM & Divisibility

Classic number-theory loops — Euclid's algorithm, multiples, common divisors, and coprime checks.

  • 1. Write a program in C to find the GCD (Greatest Common Divisor) of two numbers using a while loop (Euclidean algorithm).
  • 2. Write a program in C to find the LCM (Least Common Multiple) of two numbers using a for loop.
  • 3. Write a program in C to check whether two numbers are coprime using a while loop.
  • 4. Write a program in C to find all common divisors of two numbers using a for loop.
  • 5. Write a program in C to calculate the GCD of three numbers using nested loops.

🔹 Digit Statistics & Operations

Extract and analyze the individual digits of an integer with while, do-while, and for loops.

  • 6. Write a program in C to count the number of digits in an integer using a while loop.
  • 7. Write a program in C to find the largest digit in an integer using a do-while loop.
  • 8. Write a program in C to find the smallest digit in an integer using a while loop.
  • 9. Write a program in C to count the occurrences of a specific digit (e.g., 5) in an integer using a for loop.
  • 10. Write a program in C to calculate the product of all digits of an integer using a while loop.
  • 11. Write a program in C to check whether an integer is a single-digit number using a do-while loop.
  • 12. Write a program in C to output the digits of an integer in reverse order using a while loop (digit by digit, without reconstructing the number).
  • 13. Write a program in C to determine whether all digits of an integer are even using a for loop.

🔹 Number-System Conversion & Bits

Convert between bases and inspect the bit-level representation of integers with loops.

  • 14. Write a program in C to convert a binary number to decimal using a while loop.
  • 15. Write a program in C to convert an octal number to decimal using a for loop.
  • 16. Write a program in C to convert a decimal number to octal using a do-while loop.
  • 17. Write a program in C to count the number of 1s in the binary representation of an integer using a while loop.
  • 18. Write a program in C to convert a hexadecimal string to decimal using a for loop.

🔹 Advanced Patterns & Nested Loops

Interview-favourite star and number patterns built with nested for and while loops.

  • 19. Write a program in C to print Pascal's triangle using nested for loops.
  • 20. Write a program in C to print Floyd's triangle using nested for loops.
  • 21. Write a program in C to print an X-shaped star pattern using nested for loops.
  • 22. Write a program in C to print a plus-sign star pattern using nested for loops.
  • 23. Write a program in C to print a number palindrome pyramid (e.g., 1, 121, 12321) using nested loops.
  • 24. Write a program in C to print a full symmetric number pyramid using nested while loops.

🔹 Series Variants & Algorithms

Series computations and classic loop-driven algorithms frequently asked in interviews.

  • 25. Write a program in C to calculate the sum of the series 1 + 1/2² + 1/3² + … + 1/N² using a for loop.
  • 26. Write a program in C to calculate the sum of the series 1! + 2! + 3! + … + N! using a for loop.
  • 27. Write a program in C to generate the first N terms of the Tribonacci sequence using a while loop.
  • 28. Write a program in C to find the largest prime factor of an integer using a while loop.
  • 29. Write a program in C to implement iterative binary search using a while loop (non-recursive).
  • 30. Write a program in C to check whether two integers are amicable numbers using a for loop.

How to Get the Most Out of These Assignments

Reading a solution is not the same as writing one. For each assignment, follow this three-step workflow:

  1. Attempt first. Open a blank editor and try to solve the problem using only the assignment description. Even if you get stuck, the act of trying cements the concept.
  2. Compare carefully. Open the solution page and compare your approach to the reference. Look for differences in loop choice (while vs do-while vs for), termination conditions, and edge cases like 0 and negative inputs.
  3. Re-implement from scratch. Close the solution page and write the program again without looking. This is the step most learners skip — and it's the one that actually builds fluency.

💡 Tip: Trace every loop by hand on paper for a small input (like N = 5) before running it. Watch the loop variable and any accumulators at each iteration — most loop bugs are off-by-one errors or a missing update inside the loop body. Compile with gcc -Wall -Wextra -std=c11 -g so the compiler warns you about uninitialized variables and unreachable code.

❓ Loop Viva Questions & Quick Answers

Q1. When should I use a while loop instead of a for loop?

Use for when the number of iterations is known in advance (e.g., print the first N terms). Use while when the loop should run until a condition changes and the iteration count is unknown (e.g., Euclidean GCD, digit extraction).

Q2. What is the difference between while and do-while?

while tests the condition before the first iteration — the body may run zero times. do-while tests after the body, so it always runs at least once. This matters for input validation loops.

Q3. How do I avoid an infinite loop when extracting digits?

Make sure the update step (n /= 10) is inside the loop and that the condition (n != 0) eventually becomes false. For negative numbers, take the absolute value first or loop on n <= -1 || n >= 1.

Q4. How does the Euclidean algorithm compute GCD with a loop?

Repeatedly replace (a, b) with (b, a % b) until b becomes 0. The remaining value of a is the GCD. The repeated-subtraction variant uses a = a - b (or b = b - a) instead of the modulo.

Q5. What is the time complexity of iterative binary search?

O(log n). Each iteration halves the search range, so a sorted array of one million elements needs at most about 20 iterations.

Q6. Why do nested loops have O(n²) time complexity?

If the outer loop runs N times and, for each outer iteration, the inner loop also runs N times, the body executes N × N times. Patterns like Pascal's triangle and Floyd's triangle use nested loops but only execute the body about N²/2 times — still O(n²).

📖 Related Tutorials

  • Introduction to Loops
  • While Loop
  • Do-while Loop
  • C FOR Loop
  • Nested for Loop
  • Nested While Loop
  • Difference Between while, do-while & for
  • Continue in Loops
  • break in Loops
  • Loops FAQ
  • Loops Multiple-Choice Questions

Previous Topic: -->> Conditional Statements Assignments in C   ||   Next topic: -->> Arrays Assignments in C


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.