What is the error in the code?

Please have a look at this program guys..its for displaying the number of odd digits and even digits in a number and the sum of the odd digits and even digits separately. I don't know why is the program not giving the correct output. Like when I input 34567, it should give
Even digits=2
Odd digits = 3
Sumodd=15
Sumeven=10

but instead it gives all of them as 0...needhelp!

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
#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a, even=0, odd=0, sumeven=0, sumodd=0, p;
 cout<<"This program displays the number of even & odd digits in a number you enter and displays their sum"<<endl;
 cout<<"Enter a number: ";
 cin>>a;
 while(a>0)
 {
  p=a%10;
  a=a/10;
  if(p%2==0)
   {
    even++;
    sumeven+=p;
   }
  else
   {
    odd++;
    sumodd+=p;
   }
 }
 cout<<"Number of even digits in the number= "<<even<<endl;
 cout<<"Sum of even digits in the number= "<<sumeven<<endl;
 cout<<"Number of odd digits in the number= "<<odd<<endl;
 cout<<"Sum of odd digits in the number= "<<sumodd<<endl;
 getch();
}
Last edited on
Need the reply fast! Its urgent..! :|
give a sample input and a sample output you are getting
Looks like it's time to upgrade that ancient compiler. Remember with this 16 bit fossil the maximum size of a signed int is 32767.
I got it correct jlb...thanks :)
Wow. Yeah if your compiler is using 16 bit ints, you really need to update.
Topic archived. No new replies allowed.