Computer Science, asked by jaydeepjd7646, 4 days ago

Given an integer N, subtract the first digit of the number N from it i. e if n is 245 then subtract 2 from it making it 245-2 = 243. Keep on doing this operation until the number becomes 0.
Your task is to calculate the number of operation required to make this number N zero

Answers

Answered by ArchBTW
0

Answer:

In this case I am using C++ language here. Not sure if I am right or not... So here is my answer.

Explanation:

#include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter number: ";

cin >> n;

cout << "The number of turns it took is " << n / 2 << endl;

return 0;

}

Answered by anvimalik867
0

Concept:-

An expression set up of a subject, action word, item, and modifiers. A gathering of modifiers, a subject, an action word, and an item. A sentence that is developed with a subject, action word, item, and modifiers. a blend of subjects, action words, items, and modifiers.

Given:-

Given that an integer N, subtract the first digit of the number N from it i. e if n is 245 then subtract 2 from it making it 245-2 = 243. Keep on doing this operation until the number becomes 0.

Find:-

We need to find the number of operation required to make this number N zero.

Solution:-

// C++ program to find the count of Steps to

// reduce N to zero by subtracting its most

// significant digit at every step

#include &lt; bits/stdc++.h &gt;

using namespace std;

// Function to count the number

// of digits in a number m

int countdig(int m)

\{

if (m == 0)

return 0;

else

return 1 + countdig(m / 10);

\}

// Function to count the number of

// steps to reach 0

int countSteps(int x)

\{

// count the total number of steps

int  c= 0;

int last = x;

// iterate till we reach 0

while (last) \{

// count the digits in last

int digits = countdig(last);

// decrease it by 1

digits -= 1;

// find the number on whose division,

// we get the first digit

int divisor = pow(10, digits);

// first digit in last

int first = last/divisor;

// find the first number less than

// last where the first digit changes

int lastnumber = first \times divisor;

// find the number of numbers

// with same first digit that are jumped

int skipped = (last - lastnumber) / first;

skipped+= 1;

// count the steps

c += skipped;

// the next number with a different

// first digit

last = last - (first \times skipped);

\}

return c;

\}

// Driver code

int main()

{

int n = 245;

cout &lt;  &lt; countSteps(n);

return 0;

\}

Hence, the operation can be done with N to be zero.

#SPJ2

Similar questions