मान लीजिए हमारे पास तीन संख्याएं a, b, c हैं, हमें जांचना है कि संख्याओं से सभी 0 हटाने के बाद a + b = c है या नहीं। मान लीजिए संख्याएं a = 102, b = 130, c = 2005 हैं, तो 0 हटाने के बाद, संख्याएं होंगी a + b = c : (12 + 13 = 25) यह सत्य है।
हम एक संख्या से सभी 0 हटाएंगे, फिर जांचेंगे कि 0 हटाने के बाद, a + b = c है या नहीं।
उदाहरण
#include <iostream> #include <algorithm> using namespace std; int deleteZeros(int n) { int res = 0; int place = 1; while (n > 0) { if (n % 10 != 0) { //if the last digit is not 0 res += (n % 10) * place; place *= 10; } n /= 10; } return res; } bool isSame(int a, int b, int c){ if(deleteZeros(a) + deleteZeros(b) == deleteZeros(c)) return true; return false; } int main() { int a = 102, b = 130, c = 2005; if(isSame(a, b, c)) cout << "a + b = c is maintained"; else cout << "a + b = c is not maintained"; }आउटपुट
a + b = c is maintained