Find co-prime pairs in a range (for loop) Dry Run in C

Find co-prime pairs in a range (for loop) 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.

Find co-prime pairs in a range (for loop) Program Code

#include <stdio.h>
int gcd(int a, int b) {
    while(b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int main() {
    int n, a, b;
    printf("Enter range limit n: ");
    scanf("%d", &n);
    
    printf("Co-prime pairs (a,b) with 1 ≤ a < b ≤ %d:\n", n);
    int count = 0;
    
    for(a = 1; a <= n; a++) {
        for(b = a + 1; b <= n; b++) {  // b > a to avoid duplicates
            if(gcd(a, b) == 1) {
                printf("(%d,%d) ", a, b);
                count++;
            }
        }
    }
    printf("\nTotal co-prime pairs: %d\n", count);
    return 0;
}

View the complete Nested programs page.

Program Console Find co-prime pairs in a range (for loop) Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.