number of digits + sum of even digits

what is the wronge?

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
34
35
36
37
38
39
40
41
42
43
// Q(3) - (25 marks)
// 1- number of digits of positive long integer number
// 2- sum of even digits entered by the user

// Sample Input/Output:
// Enter a positive long number: 372946
// Number of digits = 6
// Sum of even digits = 12

#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
    int x;
    int n=0;
    
    cout<<" Enter a positive long number:";
    cin>>x;
    
    while (x)
    {
          x=x/10;
          n++;
    }
    
    cout<<" Number of digits ="<<n<<endl;
    
    for (int j=5; j>0; --j)
    j=j+n;
    
    while (j)
    {
          --j;
          if (x/j%2==0)
             ++s;
    }
    cout<<"sum of even digits ="<<n;
    
    system("PAUSE");  
    return 0;
}
 
What is it you're trying to achieve?

lines 29-30, you're likely to loop indefinitely here, perhaps you meant to use 2 variables instead of 1?
line 32, j is now out of scope and undefined
line 36 s is undefined
line 38, since you've made no further changes to n since line 27, the veracity of this statement is dubious
What is it you're trying to achieve?

25 marks :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main()
{
    int x, evenSum=0;
    int n=0;
	x=123456;
    
    while (x)
    {
    	if((x%10)%2==0) evenSum+= (x%10);
          x=x/10;
          n++;
    }
    
    cout<<" Number of digits ="<<n<<endl;

    cout<<"sum of even digits ="<<evenSum;
 
    return 0;
}
 


only line 11 is new.
thank all of u guys 4 your interest
Topic archived. No new replies allowed.