Codeforces 977A Wrong Subtraction solution in CPP

Question link:

Codeforces 977A Wrong Subtraction solution in CPP

Wrong Subtraction

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k,r=0;
    cin>>n>>k;
    for(int i=1;i<=k;i++) {

    r=n%10;
    if(r==0)
    {
        n/=10;
    }

       else
       {
           n=n-1;

    }
    }
    cout<<n;
}

Code Explanation

This is a C++ code that takes two integers n and k as input from the user, and then performs a loop that iterates k times. In each iteration, it does the following:

Calculate the remainder of n divided by 10 and store it in the variable r.
If r is equal to 0, divide n by 10.
Otherwise, subtract 1 from n.
After the loop finishes, the code prints the final value of n.    [Free Code Center]

The purpose of this code is to simulate a process where you can either divide a number by 10 (if it is divisible by 10), or subtract 1 from it, and repeat this process k times. The final value of n is the result of performing this process k times.Codeforces 977A Wrong Subtraction solution in CPP.codeforces problem solution .

For example, if n is 432 and k is 3, the first iteration of the loop will subtract 1 from n, since the remainder of 432 divided by 10 is not equal to 0. This will make n equal to 431. The second iteration will divide n by 10, making it equal to 43. The third iteration will also divide n by 10, making it equal to 4. Therefore, the final value of n will be 4.

Note that the bits/stdc++.h header file is not a standard C++ header file and is not guaranteed to be available on all compilers. It includes all standard library header files and some non-standard ones, and is commonly used in competitive programming. It is recommended to include only the necessary header files instead of relying on this non-standard header.
Stay with us!

Codeforces 110A Nearly Lucky Number Solution in CPP

1 thought on “Codeforces 977A Wrong Subtraction solution in CPP”

Leave a Comment