Hello, I have got a questions from my C++ teacher which I am unable to solve.
Please help me to solve them.
The question is:-
1) Write a program to enter any number and check if the sum of even digits is equal to the sum of odd digits or not. Ex. 122374, sum of even digits = 2+2+4 sum of odd digits = 1+3+7
2) Write a program to enter any number and check if the sum of odd placed digits is equal to the sum of even placed digits or not.
My code for 1st one is:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
int main()
{int n, so, se, r;
cin>>n;
do{
r=n%10;
if(r%2==0)
se+=r;
if(r%2!=0)
so+=r;
}while(r!=0);
if(se==so)
cout<<"Yes";
else
cout<<"No";
}
But actually my teacher hasn't taught me what is bool
The reply:
Josue Molina wrote:
If he hasn't taught you what bool is, then how can you use if, while, for, etc.? Their functionality is based on Boolean values.
That is not quite right, not trying to be pedantic - just pointing out the difference. Conditional tests work on boolean logic, whereas bool is a particular type in C++. ANSI C never had boolean types, but there is plenty of boolean logic happening, same with any other language that has if , while etc.
Dknight wrote:
i don't know this, but the programs run even without the bool function
The functions in Stewbond's code return a boolean type, they are not "bool functions".