# Program to print the minimum steps to reach 0 in C++

In this tutorial, we will learn how to print the **minimum steps to reach 0 or zero in C++**.

The program accepts the number ‘N’ and any integer ‘K’.

The program prints the **minimum number of steps required to reach 0** from N based on certain steps

If N is divisible by K then divide N by K

If N is not divisible by K then decrement N by 1.

## How to find the minimum steps to reach zero?

If the number N is 29 and K is 3, the program checks for the divisibility of N by K if so, then the N is divided by K.

If the divisibility is not satisfied then decrement N by 1.

For N=29 the steps are: `29 -> 28 -> 27 -> 9 -> 3 -> 1 -> 0`

Therefore the **minimum steps to reach zero** is 6 hence it is printed as output.

**INPUT:**

#include<iostream.h> #include<bits/stdc++.h> unsigned long long int n,k; cout<<"Enter the N value :"<<"\t"; scanf("%llu\n",&n); cout<<"Enter the K value :"<<"\t"; scanf("%llu",&k);

In the above code,

The values are obtained using scanf() .The scanf() statement can be used in C++ only when the header file #include<bits/stdc++.h>

For large integer values, N and K values <= 10^18 we can use unsigned long long int data type.

**INPUT 1:**

Enter the N value : 29 Enter the K value : 3

### C++ code to print the minimum steps to reach 0 :

#include <iostream> #include<bits/stdc++.h> using namespace std; int main() { unsigned long long int n,k,cnt=1,flag; cout<<"Enter the N value:"<<"\t"; scanf("%llu",&n); flag=n; cout<<"\nEnter the K value:"<<"\t"; scanf("%llu",&k); while(n!=0){ unsigned long long int quo,rem; quo=n/k; rem=n%k; if(n>=2){ if(rem!=0){ n=n-rem; if(n!=0){ cnt+=rem; } else{ cnt+=(rem-1); } } else{ n=n/k; cnt++; } } else if(n==0){ break; } else{ n/=k; if(n!=0){ cnt+=rem; } } } cout<<"\nThe minimum steps from "<<flag<<" to reach zero : "<<cnt; }

In the above code,

The number N and X are obtained as input. The quotient and remainder are stored in the separate variable.

The count variable is initialized with 0, to count the number of steps to reach 0.

If n is greater than or equal to 2 and remainder not equal to 0 then the remainder is subtracted from the integer n.

The n is also checked whether it is not equal to 0 if so the count value is incremented with respect to rem. Else adds rem -1 to the count.

If the rem is equal to zero then the integer is divided by k to move towards 0 and the count is incremented by 1.

The else if statement checks whether n is equal to 0, if so the program prints the count.

If n is less than 0 then the program divides the integer by k and the count is incremented accordingly with respect to rem and the count is printed as output.

**OUTPUT 1:**

The minimum steps from 29 to reach zero : 6

**INPUT 2:**

Enter the N value : 98765432346789 Enter the K value : 9876543467898

**OUTPUT 2:**

The minimum steps from 98765432346789 to reach zero : 9876541135717

Thus the program prints the **minimum steps to reach 0**.

We hope this tutorial helped you to understand how to print the count of steps to reach zero in C++.

Also learn:

## Leave a Reply