0

Im a beginner in c++, and i was trying hard. I was so close in this attempt, but can somebody tell me why my console crash... Problem:: Little girl Tanya is learning how to decrease a number by one, but she does it wrong with a number consisting of two or more digits. Tanya subtracts one from a number by the following algorithm:

if the last digit of the number is non-zero, she decreases the number by one; if the last digit of the number is zero, she divides the number by 10 (i.e. removes the last digit). You are given an integer number n. Tanya will subtract one from it k times. Your task is to print the result after all k subtractions.

#include <bits/stdc++.h>
using namespace std;
int main(){
    
    int n, k,counter;
    cin>>n>>k;
    int last = n%10;
    for(int i=0;i<k;i++){
        counter++;
        while(counter!=k){
            if(last!=0){
                last-=1;
                n-=1;   
            }
            else{
                n/=10;
                n-=1;
            }   
        }
        
    }
cout<<n;
    
    return 0;
}
Yes_ _Br0
  • 23
  • 4
  • 1
    You never initialize `counter`, so its value is undefined. Also, once your program enters the `while(counter!=k)` loop, it will never leave the loop, since nothing inside that loop ever changes the value of either `counter` or `k`, so they can never become equal. – Jeremy Friesner Nov 04 '22 at 03:31
  • 1
    Since you're a beginner in C++, try to not shoot yourself in the foot as easily as you can with `#include ` ([why](https://stackoverflow.com/q/31816095/4581301)) and `using namespace std;` ([why](https://stackoverflow.com/q/1452721/4581301)) – user4581301 Nov 04 '22 at 03:56

1 Answers1

1

I've changed the code a bit and have given the explanation below it at least from what I've understood from the question.

Hope it helps!

#include<iostream>
using namespace std;
int main(){
    
    int n, k;
    cin>>n>>k;
    for(int i=0;i<k;i++){
        int last = n%10;
            if(n==0|| last!=0){
                n-=1;   
            }
            else{
                n/=10;
            }   
        }
        
cout<<n;
    
    return 0;
}

1- Try to use the <iostream> library and using namespace std instead of #include <bits/stdc++.h> as user4581301 also mentioned

2 - As Jeremy Friesner pointed out too, you've not initialised the value for the count variable. As a result it will lead to an infinity loop as the condition will never fail.

3- You have to declare the variable last within the for loop as every time you're subtracting, you want to compare the last digit of that value, since you're only comparing the last digit of the question's number.

4- I don't necessarily think you would need the while loop with the counter variable as the job is already being done by the for loop.

5 - The question mentions that if the number is non-zero , the number should be subtracted by one and if it ends with a zero to divide the number by 10, from your program, you're subtracting 1 to the last digit whereas you have to subtract 1 from the original number, n and for the condition where the last digit is zero, you're re-subtracting the number by 1 which is not required.

TheCSGuy
  • 61
  • 5
  • It is unclear, but it looks like that should be `if (n==0 || last!=0)` to ensure a number is only reduced by `10` if it is a 2-digit number and divisible by 10? e.g. *"she does it wrong with a number consisting of two or more digits"....* – David C. Rankin Nov 04 '22 at 04:04
  • @DavidC.Rankin, You're correct, thank you for pointing that out, I did not think about that case too. – TheCSGuy Nov 04 '22 at 04:38