Question #37214

Write C++ program that reads an integer number (positive) from user, your program should accepts only 4 digits number. If user enters number greater than 4 digits, your program should print “Not 4 digits” . Your program should print the average of digits in that number. For example if user enters 7326
Your output should 4.5 which is (7+3+2+6 divided by 4).
1

Expert's answer

2013-11-26T09:54:09-0500
#include <iostream>
using namespace std;
int main()
{
    int num, d[4];
    double av;
    cout<<"Enter 4-digit number: ";
    cin>>num;
    if (num <= 9999 && num >= 1000)
    {
        d[0] = num / 1000;
        d[1] = num / 100 - d[0] *10;
        d[2] = num /10 - d[0] *100 - d[1] * 10;
        d[3] = num - d[0] *1000 - d[1] * 100 - d[2] *10;
        av = (double)(d[0] + d[1] + d[2] + d[3]) / 4;
        cout<<"Average: "<<av<<endl;
    }
    cin.ignore(100, '\n');
    cin.get();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS