Here it is...

Design, write, and compile a program to determine if the digits in a three-digit number are all odd, even, or mixed odd and even. Your program should prompt the user to input a three-digit number and echo-print the number. If the digits in the number are all odd, write "This number contains all odd digits." If the digits are all even, write "This number contains all even digits." If the number contains both odd and even digits, write "This number contains both odd and even digits." Use integer division and modulus to access the digits in the number.

If anyone could help and design such a program for me, I'de be a billion times thankful!

Thank you!

Dani AI

Generated

Nice progress, . is right that you should accept one three-digit number and then examine its digits. You can keep it short and clean by looping over the digits with modulus and integer division, instead of writing three separate tests. That also lines up with ’s flag approach.

#include <iostream>

int main() {
    int n;
    std::cout << "Enter a three-digit integer: ";
    if (!(std::cin >> n) || n < 100 || n > 999) {
        std::cout << "Please enter a value from 100 to 999.\n";
        return 0;
    }

    bool seenOdd = false, seenEven = false;
    for (int x = n, i = 0; i < 3; ++i, x /= 10) {
        int d = x % 10;
        if (d % 2) seenOdd = true; else seenEven = true;
    }

    std::cout << n << '\n';
    if (seenOdd && !seenEven)      std::cout << "All digits are odd.\n";
    else if (!seenOdd && seenEven) std::cout << "All digits are even.\n";
    else                           std::cout << "Digits include both odd and even.\n";
}

Tips:

  • If you want to allow numbers like 007, read a string, ensure it has exactly 3 digits, then convert and run the same loop. Otherwise, the 100..999 check enforces the usual definition of three-digit numbers.
  • To scale beyond three digits, replace the fixed for with while (x > 0) and count odds/evens; the reporting logic stays the same.
  • If an assignment requires exact output wording, just swap the three std::cout messages to match the spec.

Recommended Answers

All 7 Replies

There is no practical purpose for this.

Therefore, this is a homework problem.

Therefore, show what you've tried, and if you demonstrate thought, you might get help.

Its not a homework problem, nor is it a school assignment...

I'm self learning C++
I've managed to program a program to determine single digit nubmers where its odd or even... but as you see... the tast is much hardet than that...

here is what I've got so far...

#include <stdio.h>

int main()
{
   int number1, number2, number3 ;
   printf("Enter the first single digit number\n");
   scanf("%d",&number1);
   if ( number1 % 2 == 0 )
      printf("Your first number is even.\n");
   else
      printf("Your first number is odd.\n");


   printf("Enter the second single digit number\n");
   scanf("%d",&number2);
   if ( number2 % 2 == 0 )
      printf("Your second number is even.\n");
   else
      printf("Your second number is odd.\n");


   printf("Enter the third single digit  number\n");
   scanf("%d",&number3);
   if ( number3 % 2 == 0 )
      printf("Your third number is even.\n");
   else
      printf("Your third number is odd.\n");
   return 0;
}

<< moderator edit: added indentation and code tags: [code][/code] >>

Your program is not what the question asked. It wants you to enter a 3-digit number all at one time, not one digit at a time. So your program should get the number as a string, then test each character of the string. Use fgets() to get they keyboard input.

char number[5];
fgets(number,sizeof(number),stdin);

Sure, that will let you enter anything, but at this stage of your programming you don't need to worry too much about that, just make sure you enter three digits. Once you get that working then you can "enhance" your program to include error checking (valid digits).

Convert a digit to its numeric equlivant by subtracting it from '0'

int n = number[i] - '0';

now in the above you can test n for odd or even.

Thanks alot, I hope i can put this stuff together in my head to get this thing done.

1) get input as a string from user.
2) validate str is 3 chars + null terminator.
3) convert your str to an int. hint stringstreams or atoi
4) have a variable thats a 4 way flag. odd,even,both,indeterminate. Set to indeterminate.
LOOP
5) split digit from your int using % and integer division.
6) set flag accordingly.
7) any digits left? no-break loop
END LOOP
8) report findings

convert that pseudocode int c++ and you will be pretty close.

Hmm that's all great and all.. but I've got a better idea..

how about this...

#include <iostream>
using namespace std;

int main()
{
   int iNumber;

   cout << "Enter a three digit number: ";
   cin >> iNumber;
   cout << "The number you chose is: " << iNumber << endl;

   if (iNumber % 2 == 0 && iNumber/10 % 2 == 0 && (iNumber/10)/10 % 2 == 0)
   {
       cout << "All digits are even" << endl;
   }
   else if (iNumber % 2 == 1 && iNumber/10 % 2 == 1 && (iNumber/10)/10 % 2 == 1)
   {
       cout << "All digits are odd" << endl;
   }
   else
   {
       cout << "Digits are odd and even" << endl;
   }

   return 0;
}

<< moderator edit: added [code][/code] tags >>


Pretty simple ehh?

Hey Everyone,
Just a quick question,

Is there anything i can do to make this command shorter?

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.