Practice Exercises to Build Your Logic

Once you get the base program working, try these three variations. Each one builds on what you just learned.

  • Range check: Change the code to report whether a number is positive AND between 1 and 100. You will need the logical AND operator, &&.
  • Even or odd: If the number is positive, go a step further and print whether it is even or odd. Use the modulo operator, %.
  • Two-number comparison: Ask the user for two numbers, then print whether the second is higher, lower, or equal to the first. Same if-else pattern, different question.

Frequently Asked Questions

Q: How do I write a C program to check whether a number is positive, negative, or zero?

Use if-else if-else. Read the number with scanf, then use if (n > 0) for positive, else if (n < 0) for negative, and else for zero. Only one block runs.

Q: What happens if the user enters 0?

Both if (n > 0) and else if (n < 0) fail, so the final else runs and prints "The number is zero." That is the correct behavior.

Q: What is the difference between = and == in this program?

A single equals sign (=) assigns. A double equals sign (==) compares. Writing if (n = 0) assigns zero and evaluates as false; writing if (n == 0) checks whether n is zero. Always use == inside conditions.

Q: Can I use a switch statement instead of if-else?

You can, but it is a poor fit. Switch works on fixed values like 1, 2, 3, not on ranges like n > 0. If-else is the correct tool for range checks.

Q: What if the user enters a decimal number like 4.5?

The program uses %d, which reads only whole numbers. If you type 4.5, scanf will read 4 and leave .5 in the input buffer. If you need decimals, use float or double with %f.


C While & Do-While Loop Programs (Practice Set 1)

Practice the most important C while loop and do-while loop programs asked in exams and interviews. These beginner-to-advanced problems will help you master loop concepts quickly.

  1. C Program to Print Odd Numbers from 1 to N (While & Do-While)

    Learn how to print odd numbers using loops in C. A basic problem to understand loop iteration.

  2. C Program to Print Even Numbers from 1 to N

    Understand how to display even numbers using while and do-while loops in C.

  3. C Program to Print Uppercase Alphabets (A to Z)

    Print all uppercase letters using loops. Helps in understanding ASCII values and iteration.

  4. C Program to Print Lowercase Alphabets (a to z)

    Simple loop program to print lowercase alphabets in C.

  5. C Program to Print Numbers from 1 to 10

    Basic beginner example to understand number printing using loops.

  6. C Program to Print Multiplication Table using While Loop

    Take user input and generate a multiplication table. Common interview question.

  7. C Program to Find Factorial using While Loop

    Calculate factorial using loops. Important for coding interviews.

  8. C Program to Find Sum of First N Natural Numbers

    Compute sum from 1 to N using loop logic. Core beginner problem.

  9. C Program to Print Prime Numbers from 1 to N

    Learn prime number logic using nested loops in C programming.

  10. C Program to Print Armstrong Numbers from 1 to N

    Understand Armstrong number logic using loops and mathematical operations.

  11. C Program to Print Leap Years using While Loop

    Find leap years using conditions and loops. Useful for real-world logic building.

  12. C Program to Reverse a Number using While Loop

    Reverse digits of a number using loops. Frequently asked interview question.


Practice/Assignment Set 2: C While & Do-While Loop Series Programs

1. C Program to Find Sum of Squares from 1 to N using While Loop
Learn how to calculate the sum of squares (1² + 2² + 3² + ... + N²) using while and do-while loops in C. This is a commonly asked logic-building problem in exams and coding interviews.

2. C Program to Find Sum of First N Natural Numbers using While Loop
Write a C program to calculate the sum of natural numbers (1 + 2 + 3 + ... + N) using loops. A basic and important program for beginners learning loop concepts.

3. C Program to Find Sum of Series (1/1! + 2/2! + ... + N/N!)
Practice a slightly advanced loop problem by computing the sum of factorial-based series using while or do-while loops. Helps strengthen logic and mathematical programming skills.

4. C Program to Find Sum of Harmonic Series (1 + 1/2 + 1/3 + ... + 1/N)
Learn how to calculate the harmonic series using loops in C. This program improves understanding of floating-point calculations and loop control.

5. C Program to Find Sum of Series (1 + 3²/3³ + 5²/5³ + ... up to N Terms)
Solve an advanced series-based problem using while loop logic. This type of question is frequently asked in competitive programming and technical interviews.

Other Tutorials
SQL FAQ Java FAQ  Python FAQ


Previous Topic: -->> Difference Between while and do while loop in C  ||  Next topic: -->> Write C program to show EVEN numbers from 1 to N using while loop and do while loop.