Cordas
Cordas

Reputation: 3

Multiply numbers among themselves

I want to build the following program:

The user has to insert a number between 100 and 999 (like 100 < i < 999) and the numbers have to be multiplied among themselves. Example:

I tried to achieve the first part i.e. checking for the number given as input to be within 100 and 999 in the two ways below, but my approach isn't said to be elegant:

#include <stdio.h>
int main()
{
    char n[4];    
    scanf("%s", n);
    printf("%d\n", (n[0]-'0')*(n[1]-'0')*(n[2]-'0'));
    return 0;
}

or

#include<stdio.h>
int main()
{
    int array[3];
    scanf("%1d%1d%1d", &array[0],&array[1],&array[2]);
    for ( int i=0; i<3; i++) {
        printf("%d\n", array[i]);
    }        
    return 0;
}

Are they any better ways to achieve the same?

I'm looking for a C++ solution.

Upvotes: 0

Views: 333

Answers (3)

Dluzak
Dluzak

Reputation: 313

More elegant solution will be to obtain the number as an integer and then decompose it to the single digits with division and modulo operations.

This greatly increases the ability to validate the input number and knowing how to decompose the integer may be useful if the number is not given as a string, but from other parts of the program as a number.

Sample code:

#include <stdio.h>

int main() {
    int number;
    scanf("%d", &number);

    if(number > 999 || number < 100) {
        printf("Number not in range\n");
        return -1;
    }

    printf("%d\n",
            (number / 100) *        // hundreds digit
            (number / 10 % 10) *    // tens digit
            (number % 10)           // units digit
    );

    return 0;
}

Upvotes: 0

user11422223
user11422223

Reputation:

You can impose std::cin as a conditional in a while:

int x;
while (std::cin >> x && x>=100 && x <=999)
\\ Do what you want

For multiplying the digits, simply extract each digit by getting its remainder when divided by 10, multiply that with the current product (set a variable, with inital value 1) then divide by 10 subsequently in a loop till you get the product of all digits. For example, create a function which returns the product of digits of a number:

int digitproduct(int x) 
{  int product = 1;
   while (x != 0)   
   {
        product *= (n % 10);
        x /= 10; 
    } 
    return product; 
} 

Call that inside the while:

int x;
while (std::cin >> x && x>=100 && x <=999)
{   cout<< digitproduct(x);
    break;
}

Upvotes: 1

WolverinDEV
WolverinDEV

Reputation: 1524

People may say, your solution isn't elegant, because it does not easily scales if you may want to add more digest or remove some.
The first one will also have a buffer overflow as soon the user enters more than 4 characters!
But out of my option I like the second one quite much, because it shows a much better understanding of C than just reading the number via int n; std::cin >> n;, validating it, and then calculating the result.

But there is a small flaw as well, you need to check the return value of scanf in order to detect if a number has been successfully parsed or not.

int res = scanf("%1d%1d%1d", &array[0],&array[1],&array[2]);
if(res != 3) {
   printf("Invalid number format. Expected a three digit number, but got %d", res);
   return 0;
}

Upvotes: 0

Related Questions