Answer to Question #327051 in C++ for Pragnan

Question #327051

Use basic to class type conversion to covert a long integer value to class datatype



Enter a 4 digit number and display the value at thousands position,hundreds position, tens position and ones position.

1
Expert's answer
2022-04-11T07:45:50-0400
#include <iostream>
using namespace std;


class Integer {
    long value;
public:
    Integer(long v=0) : value(v) {}
    void print() const {
        cout << "The value is " << value;
    }
};


int main() {
    Integer I;
    long x = 1234567890;


    I = x;
    I.print();


    return 0;
}


#include <iostream>
using namespace std;


int main() {
    int n, d;


    cout << "Enter a 4 digits number: ";
    cin >> n;


    d = n / 1000;
    cout << "Thousands: " << d << endl;


    d = (n % 1000) / 100;
    cout << "Hundreds: " << d << endl;


    d = (n % 100) / 10;
    cout << "Tens: " << d << endl;


    d = n % 10;
    cout << "Ones: " << d << endl;


    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!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS