C While Loop Essentials: Logic, Flowcharts and Practice Problems

In the previous section we discussed the basics of C variables. Now let us take a look at some real-life application Problems.

 

1. Write a C Program to reverse a user-entered number

				
					#include <stdio.h>
int main() {
    int num, reverse_number = 0;

    printf("Enter number: ");
    scanf("%d", &num);

    while (num > 0) {
        reverse_number = reverse_number* 10 +num % 10;
        num /= 10;
    }

    printf("Reversed number: %d\n", reverse_number);
    return 0;
}

// Enter number: 1234
// Reversed number: 4321

				
			
Iterationnum (Before)num % 10 (Last Digit)reverse_number Calculationnum /= 10 (Remaining)
112344(0 * 10) + 4 = 4123
21233(4 * 10) + 3 = 4312
3122(43 * 10) + 2 = 4321
411(432 * 10) + 1 = 43210
End0N/ALoop terminates0

2. Write a C program to count the number of digits in a number

				
					#include <stdio.h>

int main() {
    int num, num_count = 0;

    printf("Enter number: ");
    scanf("%d", &num);

    while (num != 0) {
        num_count++;
        num /= 10;
    }

    printf("Number of digits: %d\n", num_count);
    return 0;
}

// Enter number: 25355
// Number of digits: 5

				
			
Iterationnum (Before)Action (num_count++)num /= 10 (Remaining)num_count
125355Increment25355 / 10 = 25351
22535Increment2535 / 10 = 2532
3253Increment253 / 10 = 253
425Increment25 / 10 = 24
52Increment2 / 10 = 05
End0N/ALoop terminates5

3. Write a C Program to check if the number is an Armstrong number or not

				
					#include <stdio.h>
#include <math.h>
int main() {
    int num, temp, digit, count = 0;
    int sum = 0;

    printf("Enter a number: ");
    scanf("%d", &num);

    temp = num;

    while (temp != 0) {
        count++;
        temp /= 10;
    }

    temp = num;
    while (temp != 0) {
        digit = temp % 10;
        sum += pow(digit, count);
        temp /= 10;
    }
    if (sum == num)
        printf("%d is an Armstrong number\n", num);
    else
        printf("%d is not an Armstrong number\n", num);

    return 0;
}

// Enter a number: 156
// 156 is not an Armstrong number

// Enter a number: 153
// 153 is an Armstrong number


				
			
Iterationdigit (temp % 10)Calculation (pow(digit, 3))sum += resulttemp /= 10
133^3 = 270 + 27 = 2715
255^3 = 12527 + 125 = 1521
311^3 = 1152 + 1 = 1530

4. Write a C Program to print the below Pattern by using while Loop

* * * *

* * * *

* * * *

* * * *

				
					#include <stdio.h>

int main() {
    int i = 1, j, n = 4;

    while (i <= n) {
        j = 1;
        while (j <= n) {
            printf("* ");
            j++;
        }
        printf("\n");
        i++;
    }
    return 0;
}

// * * * * 
// * * * * 
// * * * * 
// * * * * 

				
			
Outer Iteration (i)Inner Iteration (j)ActionOutput in Row
i = 1j = 1, 2, 3, 4Print * four times* * * *
j = 5j <= 4 is FalseMove to next line (\n)
i = 2j = 1, 2, 3, 4Print * four times* * * *
j = 5j <= 4 is FalseMove to next line (\n)
i = 3j = 1, 2, 3, 4Print * four times* * * *
j = 5j <= 4 is FalseMove to next line (\n)
i = 4j = 1, 2, 3, 4Print * four times* * * *
j = 5j <= 4 is FalseMove to next line (\n)
i <= 4 is FalseLoop terminates

5. Write a C Program to print the below pattern

1

1 2

1 2 3

1 2 3 4

				
					#include <stdio.h>
int main() {
    int i = 1, j;

    while (i <= 4) {
        j = 1;
        while (j <= i) {
            printf("%d ", j);
            j++;
        }
        printf("\n");
        i++;
    }
    return 0;
}

// 1 
// 1 2 
// 1 2 3 
// 1 2 3 4 

				
			
Row (i)Condition (i <= 4)Inner (j)Condition ( j <= i)ActionRow Output
1True11 <= 1 (True)Print 11
22 <= 1 (False)Next Line
2True1, 21 <= 2(True),
2 <= 2 (True)
Print 1 , 21 2
33 <= 2(False)Next Line
3True1, 2, 31 <= 3(True),
2 <= 3(True),
3 <= 3(True)
Print 1, 2, 31 2 3
44 <= 3(False)Next Line
4True1, 2, 3, 41 <= 4, .......... 4 <= 4 (True)Print 1, 2, 3, 41 2 3 4
55 <= 4(False)Next Line
5False--Terminate

6. Write a C Program to print the pyramid pattern

   *

  * *

 * * *

* * * *

				
					#include <stdio.h>

int main() {
    int i = 1, j, space;

    while (i <= 4) {
        space = 4 - i;
        while (space-- > 0)
            printf(" ");

        j = 1;
        while (j <= i) {
            printf("* ");
            j++;
        }
        printf("\n");
        i++;
    }
    return 0;
}

//    * 
//   * * 
//  * * * 
// * * * *