### How Can I Know The No. Of Digits in C++ ???

Write a program to do the followings:
• Read a number from the user ( N1 ), and check if it exactly has 3 digits.
• Read another number from the user ( N2 ), and check if it exactly has 3 digits.
• In (N1) check that the multiplication of the most left 2 digits is equal to the most right digit.
• In (N21) check that the multiplication of the most right left 2 digits is equal to the most left digit.
• In case the previous checks were true, then compose a new variable that carries the most right digit of (N1), & the most left digit of (N2).

e.g.
N1 : 236  (2 x 3 = 6)
N2 : 824.  (8 = 2 x 4)

Then 
New variable  (68)

How Can I Know The No. Of Digits in C++ ???
There are a few ways.

You can either do it by dividing an integer by 10 n times until it is 0. n will be the number of digits.

eg:

80 : n = 0
8 : n = 1
0 : n = 2
2 digits in 80

Or, you can convert it to a string and count the number of characters.

Considering the mathematical nature of the assignment I'd guess you're expected to use the first method.

EDIT: Typos
Last edited on
thank you for help :)

how i can solve the problem ?
The '%' (mod) operator, which returns the remainder after dividing, will be helpful here.

To get a certain digit of a number, you mod it by its place + 1, and then divide it by its place.

To get 3 (it's in the 10^3 or 1000ths place):
823467 % 10^4 = 3467
3467 / 10^3 = 3
3

(Note that the caret symbol '^' DOES NOT MEAN "to the power of" in C++. You can use pow() from <cmath> to do this instead)

Using mod on a number (by a multiple of 10) essentially gets rid of unwanted leftmost digits, while dividing gets rid of rightmost digits. Use this concept to find out how to get the leftmost 2 digits of a number or the rightmost 2.
Last edited on
ooh really thank you that's mean a lot <3
I have an exam Tomorrow tarrarraaa!!
Topic archived. No new replies allowed.