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.
#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;
}
Comments
Leave a comment