Home > Software engineering >  I am new to programming and I've been stuck in this problem (C Language)
I am new to programming and I've been stuck in this problem (C Language)

Time:11-04

I need to use do-while loop for this. My problem is that when I enter a positive number "5" the output should be 5 4 3 2 1 0 but instead, I get an output of 56543210 And if I enter a negative number "-5" the output should be -5 -4 3- -2 -1 0 but instead, I get an out put of -5-4-3-2-1010 Lastly, when I enter a number "0" the output should just be 0 ,but I get an output of 010

here is my code:

#include <stdio.h>
int main() {
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);

    do {
        printf("%d", n);
        n  ;
    } while(n<=0);
    
    do {
        printf("%d", n);
        n--;
    } while(n>=0);

    return 0;
}

I tried several ways but it didn't work. I also tried to incorporate switch case and if statement with do-while loop, but I keep on having errors.

Any advice will help. thanks!! Sorry for grammatical errors and if my explanation is not clear.

CodePudding user response:

the output should be -5 -4 -3 -2 -1 0

In that case the second do-while loop doesn't make the slightest sense and should be removed completely.

You might want to add some manner of output formatting like printf("%d ", n); to insert a space between numbers.

With those two minor fixes, the correct output is displayed.

CodePudding user response:

You have a number of issues here

  • A do ... while will be executed at least once hence both loops will be executed.
  • Both loops count the value of n being 0 as valid.

A simple readjustement to your code using while loops would give you the desired output.

while(n<0)
{
    printf(" %d", n);
    n  ;
} 

while(n>0)
{
    printf(" %d", n);
    n--;
}
printf(" %d", n); //at this point n should be 0 and always printed.

If you must use a do ... while loop, then you must prevent both loops running - say by testing to see if the value of n is negative and only running the first loop.

CodePudding user response:

When you use a do{}while(); the code from the loop executes at least one before verifying the condition. In your case if takes the number, print it, increase n and then enters the loop needed. In this type of cases you don't want to use a do..while, but, if you have to use it there is a solution. If you use an if(){} statement you can use a do..while. The answer in this case would be:

#include <stdio.h>
int main() {
int n;

printf("Enter a number: ");
scanf("%d", &n);

if(n < 0){
    do {
    printf("%d", n);
    n  ;
    }while(n<=0);
}
else if(n > 0){
    do {
    printf("%d", n);
    n--;
    }while(n>=0);
}
else{
    printf("%d", n);
}
return 0;
}

CodePudding user response:

You must read more about C first. The n increment is similar to n=n 1 (left to right operation), and the same goes for the n-- decrement. Also, do-while execute at least one time.

Check my comment added to your code for more understanding.

#include <stdio.h>
int main() 
{
    int n;

    printf("Enter a number: ");
    scanf("%d", &n); //consider you have entered number 5
    do {
        printf("%d", n); //prints number 5
        n  ;             //post increment to 6
    } while(n<=0);       //6 <= 0 --> Flase
                         //Now at this point the number is still 6   
    do {
        printf("%d", n); //prints number 5     
        n--;             //post decrement to 5
    } while(n>=0);       //5 >= 0 --> True
    return 0;
}

Try this, maybe this is what you are trying to achieve

#include <stdio.h>
int main() 
{
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);
    
    if(n<0)
    {
        while(n<=0){   printf("%d", n);  n  ;    }
    }
    else if(n>0)
    {
        while(n>=0){   printf("%d", n);  n--;    }
    }
    else printf("%d", n);

return 0;
}

CodePudding user response:

The solution comes from analysing the problem.

Given a starting integer, the objective is to print successive values that approach zero. From a positive starting value, each step is -1; from a negative value each step is 1. Then, the solution is trivial:

#include <stdio.h>

int main( void ) {
    int n;

    for( ;; ) {
        printf("Enter a number: ");
        scanf("%d", &n);
        /* Omitting test for success */

        int incr = n < 0 ? 1 : -1;

        do printf(" %d", n); while((n  = incr) != incr);
        putchar( '\n' );
    }

    return 0;
}
Enter a number: -5
 -5 -4 -3 -2 -1 0
Enter a number: 5
 5 4 3 2 1 0
Enter a number:
  •  Tags:  
  • c
  • Related