Ad

I Cant Understand Logic By Print All 3-digit Numbers Having Unique Digits In The Specified Range

- 1 answer

These are my past C language labwork mission and example outputs enter image description here text version : In this lab work, you are required to print all 3-digit numbers having unique digits in the specified range.

Users must enter 2 integers from 1 to 8 inclusive.

Those integers will represent the lower and upper limit of the number’s digits.

You must implement main function and use 3 for loops to traverse all numbers for the specified range. The main function:

• Ask 2 values from the user within a range of numbers from 1 to 8 inclusive: lower (int) and upper (int): (10 pts)

• Implement 3 for loops (15 pts each)

• Print the numbers (25 pts)

• Print count of the numbers (20 pts)

enter image description here MY FALSE OUTPUT

I try to make solve again but I cant. Please describe my faults or tell the logic

#include <stdio.h>
int main()
{
int low,high;
printf("Enter lowest digit\n");
scanf("%d",&low);
printf("Enter highest digit:\n");
scanf("%d",&high);
int a=low;
int b=low;
int c=low;
int i=high;
int j=high;
int k=high;
int counter =0;
for(a;a<i+1;a++)
{   printf("%d",a);
    for(b;b<j+1;b++)
    {
        printf("%d",b);
        for(c;c<k+1;c++)
            printf("%d",c);
        counter++;
        printf("\n");
    }
}
printf("\n counter is %d",counter);
return 0;}

and this is my false output

enter image description here

Ad

Answer

#define MIN 1
#define MAX 8


int print(int low, int high)
{
    int count = 0;
    for(int first = low; first <= high; first++)
    {
        for(int second = low; second <= high; second++)
        {
            if(second == first) continue;
            for(int third = low; third <= high; third++)
            {
                if(third == first || third == second) continue;
                count++;
                printf("%d%d%d%c", first, second, third, (count % 8) ? ' ' : '\n');
            }
        }
    }
    return count;
}

int main(void)
{
    int low, high, result;
    result = scanf("%d %d", &low, &high);
    if(result != 2 || low < MIN || low > MAX || high > MAX || high < MIN || low >= high)
    {
        printf("Wrong input\n");
    }
    else
    {
        printf("\n Count: %d\n", print(low, high));
    }
}

https://godbolt.org/z/eqfef6

Ad
source: stackoverflow.com
Ad