Repeat property checking with menu-driven logic (do-while) Dry Run in C

Repeat property checking with menu-driven logic (do-while) is an interactive C dry run visualizer from the Nested programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Repeat property checking with menu-driven logic (do-while) Program Code

#include <stdio.h>
int sumOfProperDivisors(int n) {
    int sum = 0, i;
    for(i = 1; i <= n/2; i++)
        if(n % i == 0) sum += i;
    return sum;
}

int isArmstrong(int n) {
    int original = n, sum = 0, digits = 0, temp = n, i;
    while(temp != 0) { digits++; temp /= 10; }
    temp = n;
    while(temp != 0) {
        int rem = temp % 10, power = 1;
        for(i = 1; i <= digits; i++) power *= rem;
        sum += power;
        temp /= 10;
    }
    return sum == original;
}

int main() {
    int num, choice, i;
    
    do {
        printf("\n========== NUMBER PROPERTY ANALYZER ==========\n");
        printf("1. Check Perfect Number\n");
        printf("2. Check Armstrong Number\n");
        printf("3. Check Strong Number\n");
        printf("4. Check Harshad Number\n");
        printf("5. Check Abundant/Deficient\n");
        printf("6. Show All Properties\n");
        printf("0. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        
        if(choice == 0) break;
        
        printf("Enter a number: ");
        scanf("%d", &num);
        
        switch(choice) {
            case 1: {
                int sum = sumOfProperDivisors(num);
                if(sum == num && num > 0)
                    printf("%d is a PERFECT number!\n", num);
                else
                    printf("%d is NOT a perfect number.\n", num);
                break;
            }
            case 2: {
                if(isArmstrong(num))
                    printf("%d is an ARMSTRONG number!\n", num);
                else
                    printf("%d is NOT an Armstrong number.\n", num);
                break;
            }
            case 3: {
                int temp = num, sum = 0;
                while(temp != 0) {
                    int digit = temp % 10, fact = 1;
                    for(i = 1; i <= digit; i++) fact *= i;
                    sum += fact;
                    temp /= 10;
                }
                if(sum == num)
                    printf("%d is a STRONG number!\n", num);
                else
                    printf("%d is NOT a strong number.\n", num);
                break;
            }
            case 4: {
                int temp = num, digitSum = 0;
                while(temp != 0) {
                    digitSum += temp % 10;
                    temp /= 10;
                }
                if(digitSum > 0 && num % digitSum == 0)
                    printf("%d is a HARSHAD number!\n", num);
                else
                    printf("%d is NOT a Harshad number.\n", num);
                break;
            }
            case 5: {
                int sum = sumOfProperDivisors(num);
                if(sum > num) printf("%d is ABUNDANT\n", num);
                else if(sum < num) printf("%d is DEFICIENT\n", num);
                else printf("%d is PERFECT\n", num);
                break;
            }
            case 6: {
                printf("\n--- Properties of %d ---\n", num);
                int sum = sumOfProperDivisors(num);
                printf("Perfect: %s\n", (sum == num) ? "YES" : "NO");
                printf("Armstrong: %s\n", isArmstrong(num) ? "YES" : "NO");
                int temp = num, strongSum = 0;
                while(temp != 0) {
                    int digit = temp % 10, fact = 1;
                    for(i = 1; i <= digit; i++) fact *= i;
                    strongSum += fact;
                    temp /= 10;
                }
                printf("Strong: %s\n", (strongSum == num) ? "YES" : "NO");
                temp = num; int digitSum = 0;
                while(temp != 0) { digitSum += temp % 10; temp /= 10; }
                printf("Harshad: %s\n", (digitSum > 0 && num % digitSum == 0) ? "YES" : "NO");
                if(sum > num) printf("Classification: ABUNDANT\n");
                else if(sum < num) printf("Classification: DEFICIENT\n");
                else printf("Classification: PERFECT\n");
                break;
            }
            default: printf("Invalid choice!\n");
        }
        
    } while(choice != 0);
    
    printf("Thank you for using Number Property Analyzer!\n");
    return 0;
}

View the complete Nested programs page.

Program Console Repeat property checking with menu-driven logic (do-while) Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.