Home > Software engineering >  The input is a three-digit number. Print the arithmetic mean of its digits
The input is a three-digit number. Print the arithmetic mean of its digits

Time:09-27

I have a homework assignment. The input is a three-digit number. Print the arithmetic mean of its digits. I am new to C and cannot write the code so that it takes 1 number as input to a string. I succeed, only in a column.

#include <iostream>
int main()
{
    int a,b,c;
    std::cin >> a >> b >> c;
    std::cout << (a b c)/3. << std::endl;
    return 0;
}

If you write it in Python it looks like this. But I don't know how to write the same thing in C :(

number = int(input())
digital3 = number % 10
digital2 = (number//10)
digital1 = number//100
summ = (digital1 digital2 digital3)/3
print(summ)

CodePudding user response:

The most direct translation from Python differs mostly in punctuation and the addition of types:

#include <iostream>
int main()
{
    int number;
    std::cin >> number;
    int digital3 = number % 10;
    int digital2 = (number/10)%10;
    int digital1 = number/100;
    int summ = (digital1 digital2 digital3)/3;
    std::cout << summ << std::endl;
}

CodePudding user response:

In your code, you use three different numbers and take the mean of their sum (not the sum of three-digits number). The right way is:

#include <iostream>
int main()
{
    int a;
    std::cin >> a;
    std::cout << ((a/100)   ((a/10)%10)   (a%10))/3.<< std::endl;
    return 0;
}

CodePudding user response:

EDIT: This answer is incorrect. I thought the goal was to average three numbers. Not three DIGITS. Bad reading on my part

*Old answer *

I'm not sure I'm interpreting the question correctly. I ran your code and confirmed it does what I expected it to...

Are you receiving three digit chars (0-9) and finding the average of them? If so, I'd trying using a

  • for loop using getChar()

Here is a range of functions that may be of use to you.

  • Regex strip
  • Convert string to int: int myInt = stoi(myStr.c_str())
  • Convert int to string: std::string myStr = myInt.to_string()

If you need to improve your printing format

  • Using printf
  • If using cout, you can kindve hack your way through it!

CodePudding user response:

The input is a three-digit number.

If it means, you'll be given a number that will always have 3 digits, then you can try the following approach.

  • Separate each digit
  • Find all digits sum
  • Divide the sum by 3

If you're given the number as a string, all you've to do is convert that string into int. Rest of the approach is the same as abve.

Sample code:

int main()
{
    int a;
    std::cin >> a;
    int sum = (a % 10);  // adding 3rd digit
    a /= 10;
    sum  = (a % 10);  // adding 2nd digit
    a /= 10;
    sum  = (a % 10);  // adding 1st digit
    
    std::cout << (double)sum / 3.0 << std::endl;
    return 0;
}

CodePudding user response:

Here's a possible solution using std::string:

EDIT added digits check

#include <iostream>
#include <string>
#include <cctype>    
int main()
{
    std::string s;
    std::cin >> s;
    if(s.length() == 3 && isdigit(s[0]) && isdigit(s[1]) && isdigit(s[2]))
    {
        std::cout<<double(s[0]   s[1]   s[2])/3 - '0'<<std::endl;
    }
    else
    {
       std::cout<<"Wrong input"<<std::endl;
    }
    return 0;
}
  •  Tags:  
  • c
  • Related