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!

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.

This article has been dead for over six months. Start a new discussion instead.