help with a question

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>
using namespace 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";
}
#1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

bool isOdd(int i)
{
    return i%2;
}

int main()
{
    int number;
    cin >> number;
    
    int evenSum = 0;
    int oddSum = 0;
    
    while (number)
    {
        int onesDigit = number%10;
        
        if (isOdd(onesDigit)
            oddSum += onesDigit;
        else
            evenSum += onesDigit;

        number /= 10;
    }
    
    if (oddSum == evenSum)
        cout << "yes";
    else
        cout << "no";
}


#2 (minimal changes)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

bool isOdd(int i)
{
    return i%2;
}

int main()
{
    int number;
    cin >> number;
    
    int evenSum = 0;
    int oddSum = 0;
    
    for (int i = 0; number; ++i)
    {
        int onesDigit = number%10;
        
        if (isOdd(i))
            oddSum += onesDigit;
        else
            evenSum += onesDigit;

        number /= 10;
    }
    
    if (oddSum == evenSum)
        cout << "yes";
    else
        cout << "no";
}
Last edited on
But actually my teacher hasn't taught me what is bool
Last edited on
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.
i don't know this, but the programs run even without the bool function
I need a turbo c++ program though I have written it in code::blocks
i'm afraid you cannot use bool type in old turbo C++ and you have to change #include <iostream> to #include <iostream.h>
Dknight wrote:
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".

Hope all is well.
Topic archived. No new replies allowed.