Repeat prime checking until user exits (do-while) Dry Run in C

Repeat prime checking until user exits (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 prime checking until user exits (do-while) Program Code

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

int main() {
    int num, choice;
    
    do {
        printf("\n=== Prime Number Checker ===\n");
        printf("Enter a number: ");
        scanf("%d", &num);
        
        if(isPrime(num))
            printf("%d is a PRIME number\n", num);
        else
            printf("%d is NOT a prime number\n", num);
        
        // Find next prime
        int next = num + 1;
        while(!isPrime(next)) next++;
        printf("Next prime after %d is %d\n", num, next);
        
        // Find previous prime
        int prev = num - 1;
        while(prev >= 2 && !isPrime(prev)) prev--;
        if(prev >= 2)
            printf("Previous prime before %d is %d\n", num, prev);
        
        printf("\nPress 1 to check another number, 0 to exit: ");
        scanf("%d", &choice);
        
    } while(choice == 1);
    
    printf("Thank you for using Prime Checker!\n");
    return 0;
}

View the complete Nested programs page.

Program Console Repeat prime checking until user exits (do-while) Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.