C Program to Print Counting from M to N Using for, while and do-while Loops for, while और do-while Loop की सहायता से M से N तक Counting प्रिंट करने का C प्रोग्राम

Counting from M to N means printing all integer numbers starting from the lower limit (M) up to the upper limit (N). In C programming, we can perform this task using three different loops: for, while, and do-while.

M से N तक Counting प्रिंट करने का अर्थ है Lower Limit (M) से शुरू करके Upper Limit (N) तक सभी पूर्णांक संख्याओं को प्रिंट करना। C Programming में यह कार्य तीन अलग-अलग Loops for, while और do-while की सहायता से किया जा सकता है।

1. Counting Using for Loop

The for loop is generally used when the number of iterations is known or when a counting variable is required.

#include <stdio.h>

int main(void) {
    int i, m, n;

    printf("Enter range of counting m and n:\n");

    if (scanf("%d%d", &m, &n) != 2) {
        printf("Invalid input! Please enter two integers.\n");
        return 1;
    }

    if (m > n) {
        printf("Invalid range! M must be less than or equal to N.\n");
        return 1;
    }

    printf("Counting from %d to %d:\n", m, n);

    for (i = m; i <= n; i++) {
        printf("%d ", i);

        if (i % 10 == 0)
            printf("\n");
    }

    printf("\n");
    return 0;
}

2. Counting Using while Loop

The while loop checks the condition before executing the loop body. The counting variable is initialized before the loop and incremented inside it.

#include <stdio.h>

int main(void) {
    int i, m, n;

    printf("Enter range of counting m and n:\n");

    if (scanf("%d%d", &m, &n) != 2) {
        printf("Invalid input! Please enter two integers.\n");
        return 1;
    }

    if (m > n) {
        printf("Invalid range! M must be less than or equal to N.\n");
        return 1;
    }

    printf("Counting from %d to %d:\n", m, n);

    i = m;

    while (i <= n) {
        printf("%d ", i);

        if (i % 10 == 0)
            printf("\n");

        i++;
    }

    printf("\n");
    return 0;
}

3. Counting Using do-while Loop

The do-while loop executes its body before checking the condition. Therefore, it is important to validate the range before entering this loop.

#include <stdio.h>

int main(void) {
    int i, m, n;

    printf("Enter range of counting m and n:\n");

    if (scanf("%d%d", &m, &n) != 2) {
        printf("Invalid input! Please enter two integers.\n");
        return 1;
    }

    if (m > n) {
        printf("Invalid range! M must be less than or equal to N.\n");
        return 1;
    }

    printf("Counting from %d to %d:\n", m, n);

    i = m;

    do {
        printf("%d ", i);

        if (i % 10 == 0)
            printf("\n");

        i++;
    } while (i <= n);

    printf("\n");
    return 0;
}

Sample Output

The output is the same for all three programs.

Enter range of counting m and n:
1 25
Counting from 1 to 25:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25

Explanation

All three programs print numbers from m to n. The variable i is used as the counting variable. After printing each number, i is incremented by 1.

The condition i % 10 == 0 prints a new line after every multiple of 10, making the output easier to read.

तीनों प्रोग्राम m से n तक संख्याएँ प्रिंट करते हैं। यहाँ i वेरिएबल Counting के लिए उपयोग किया जाता है। प्रत्येक संख्या प्रिंट करने के बाद i का मान 1 बढ़ा दिया जाता है।

Condition i % 10 == 0 प्रत्येक 10 के गुणज के बाद नई लाइन प्रिंट करती है, जिससे Output पढ़ने में आसान होता है।

Difference Between for, while and do-while Loops

for Loopwhile Loopdo-while Loop
Condition is checked before execution.Condition is checked before execution.Condition is checked after execution.
Initialization, condition and update are written together.Initialization and update are written separately.Initialization and update are written separately.
May execute zero times.May execute zero times.Executes at least once.

Modern C Compatibility Note

These programs use standard C and work with modern compilers such as GCC, Clang, and Visual Studio C. They do not require conio.h, clrscr(), or getch(). 

These three programs demonstrate how the same counting task can be performed using for, while, and do-while loops in C programming.

ये तीनों प्रोग्राम C Programming में for, while और do-while Loops की सहायता से एक ही Counting Task को करने का तरीका समझाते हैं।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025