41 lines
1.4 KiB
C++
41 lines
1.4 KiB
C++
#include <iostream>
|
|
using namespace std;
|
|
|
|
int main() {
|
|
int number;
|
|
|
|
cout << "Enter a 6-digit number: ";
|
|
cin >> number;
|
|
|
|
// Check if the number is 6-digit (from 100000 to 999999)
|
|
if (number < 100000 || number > 999999) {
|
|
cout << "Error! Please enter exactly a 6-digit number (from 100000 to 999999)" << endl;
|
|
return 1;
|
|
}
|
|
|
|
// Extract digits separately
|
|
int digit1 = number / 100000; // first digit
|
|
int digit2 = (number / 10000) % 10; // second digit
|
|
int digit3 = (number / 1000) % 10; // third digit
|
|
int digit4 = (number / 100) % 10; // fourth digit
|
|
int digit5 = (number / 10) % 10; // fifth digit
|
|
int digit6 = number % 10; // sixth digit
|
|
|
|
// Calculate sums
|
|
int sum_first_three = digit1 + digit2 + digit3;
|
|
int sum_last_three = digit4 + digit5 + digit6;
|
|
|
|
// Check if lucky
|
|
if (sum_first_three == sum_last_three) {
|
|
cout << "Congratulations! Number " << number << " is lucky!" << endl;
|
|
cout << "Sum of first three digits: " << sum_first_three << endl;
|
|
cout << "Sum of last three digits: " << sum_last_three << endl;
|
|
} else {
|
|
cout << "Number " << number << " is not lucky." << endl;
|
|
cout << "Sum of first three digits: " << sum_first_three << endl;
|
|
cout << "Sum of last three digits: " << sum_last_three << endl;
|
|
}
|
|
|
|
return 0;
|
|
}
|