Home > Back-end >  Why i got 11 10 10 from given range 10 to 1?
Why i got 11 10 10 from given range 10 to 1?

Time:09-22

I want to print numbers from left to right. If the left number is larger than the right, it will print numbers from smallest to largest. If the right number is larger than the left, it will print number from largest to smallest. And if the given numbers are equal, it will print just that number.

Expected behavior:

enter 2 integers : 4 10
4 5 6 7 8 9 10

enter 2 integers: 10 4
10 9 8 7 6 5 4

enter 2 integers: 4 4 
4

enter 2 integers: 4 3
4 3

This is my code:

int main() {
    int i, n1, n2;
    printf("Enter range (n1, n2 ) : ");
    scanf("%d %d", &n1, &n2);
    i = n1;
    while (i <= n2) {
        printf("%d ", i);
        i  ;
    }
    while (i >= n2) {
        printf("%d ", i);
        i--;
        if (i == n2) {
            printf("%d", i);
        }
    }
}

When I type input like 1 and 10, it prints output like this: 1 2 3 4 5 6 7 8 9 10 11 10 10.

But if I type input like 10 and 1, it prints output like this: 10 9 8 7 6 5 4 3 2 11.

If I type equal numbers, it prints so many numbers.

How can I fix this? I've been trying many ways, thanks for help.

CodePudding user response:

You unconditionally execute both loops and get output from both, the output you want and an edge case from the other loop.

Use an if to decide between the two loops and only exectute one of them.

That way you avoid that after the first loop the condition of the second one is true once. Note that after while (i <= n2) i ; it is sure that i>n2, e.g. i==n2 1. And in the second loop, after the first i--;, it is true that i==n2.

Note that the "11" in the second shown output is not an eleven, it is two "1"s, missing a blank. Coming each from one printf() inside the second loop.

Here is code to illustrate the problem (not to fix it):

#include <stdio.h>

int main() {
    int i, n1, n2;
    printf("Enter range (n1, n2 ) : ");
    scanf("%d %d", &n1, &n2);
    i = n1;
    while (i <= n2) {
        printf("1st:%d\n", i);
        i  ;
    }
    printf("\n1 done\n");
    while (i >= n2) {
        printf("2nd:%d\n", i);
        i--;
        if (i == n2) {
            printf("if:%d\n", i);
        }
    }
}

output for "1 10":

Enter range (n1, n2 ) : 1st:1
1st:2
1st:3
1st:4
1st:5
1st:6
1st:7
1st:8
1st:9
1st:10

1 done
2nd:11
if:10
2nd:10

output for "10 1":

Enter range (n1, n2 ) : 
1 done
2nd:10
2nd:9
2nd:8
2nd:7
2nd:6
2nd:5
2nd:4
2nd:3
2nd:2
if:1
2nd:1

Code to fix, adding an if and deleting a different if,
still with some visualisation:

#include <stdio.h>

int main() {
    int i, n1, n2;
    printf("Enter range (n1, n2 ) : ");
    scanf("%d %d", &n1, &n2);
    i = n1;
    if(n1 < n2)
    {
        while (i <= n2) {
            printf("1st:%d\n", i);
            i  ;
        }
    } else
    {
        while (i >= n2) {
            printf("2nd:%d\n", i);
            i--;
        }
    }
}

CodePudding user response:

You can print numbers in a given range using a single while loop and flag.

flag variable: flag to determine you have to increment or decrement (true means decrement)

 #include<stdio.h>
  int main() {
            int n1, n2;
            bool flag=false; 
            printf("Enter range (n1, n2 ) : ");
            scanf("%d %d", &n1, &n2);
           if(n1>n2)
             flag=true;
            else
             flag=false; 
            while(n1!=n2)
            {
            printf("%d ", n1);
            if(flag)
             n--;
            else
             n  ;
            }
           printf("%d", n1);
        }
  •  Tags:  
  • c
  • Related