Odd/Even Digits in a Number-Help!

This is a discussion on Odd/Even Digits in a Number-Help! within the C++ Programming forums, part of the General Programming Boards category; Hey everyone, I need some help doing this ..i got most of the source code, but there's a little problem. ...

  1. #1
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Odd/Even Digits in a Number-Help!

    Hey everyone, I need some help doing this ..i got most of the source code, but there's a little problem. When I input a three digit number, sometimes it'll tell me that the digits are odd/even/mixed when i know they're not.

    1 Example:
    input> 333
    "Digits are Mixed(odd and even)" <THIS is wrong

    2 Example:
    input>222
    "All Digits are even" <This is right

    3 Example:
    input>123
    "Digits are Mixed(odd and even)" <this is also right

    What could be the problem in my code...Take a Look Please
    #include<iomanip.h>
    main()
    {

    int Num, Dig1,Dig2,Dig3,Val1,Val2,Val3;

    cout << "Please enter a 3 digit number and Press
    Enter : ";
    cin >> Num;
    cout<<Num<<endl;

    Dig1=Num/100;
    Val1=Dig1%2;
    Dig2=Val1/10;
    Val2=Dig2%2;
    Dig3=Val2/1;
    Val3=Dig3%2;


    if((Val1==0) && (Val2==0) && (Val3==0))
    cout<<"All Digits are Even";
    else if((Val1>0) && (Val2>0) &&(Val3>0))
    cout<<"All Digits are Odd";
    else
    cout<<"The Digits are Mixed (Odd and Even)";


    return 0;
    }


    Thanks for taking a look

  2. #2
    paultwang
    Guest
    // ...
    Dig1=Num/100;
    Val1=Dig1%2;
    Dig2 = (Num-Dig1*100)/10; // here.
    Val2=Dig2%2;
    Dig3 = (Num-Dig1*100-Dig2*10); // here.
    Val3=Dig3%2;

    // ...

  3. #3
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Thanks Bro

    Thanks a lot I appreciate your help.. damn i love this site!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting number of digits in a variable
    By Saeid87 in forum C Programming
    Replies: 6
    Last Post: 06-05-2009, 01:13 PM
  2. Learning Memory, Ins and Outs?
    By Zoiked in forum C Programming
    Replies: 1
    Last Post: 08-27-2007, 04:43 PM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. combining digits into a number
    By waxydock in forum C Programming
    Replies: 5
    Last Post: 05-12-2007, 05:59 AM
  5. Count the number of vowels, consonants, digits etc.
    By kumar14878 in forum C Programming
    Replies: 3
    Last Post: 05-09-2005, 12:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21