By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,332 Members | 1,863 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,332 IT Pros & Developers. It's quick & easy.

loop and switch.. help:)

houserocks13
P: 5
im quite confused with loops and switch.. will you please help me:)
i have 3 problems..:)

1.Write a program that will ask for a long number and count how many digits are there in the number.
ex. Enter num: 10854
Output: 5

2.Write a program that will ask for a long number and count how many digits in the number are even and how many are odd.
ex. Enter num: 80572
Output: 3 digits are even
2 digits are odd

3.Write a program that asks for an input integer and displays the digits in reverse.
ex. Enter number: 3562 ex. Enter number: 10240
Output: 2653 Output: 04201
Nov 13 '06 #1
Share this Question
Share on Google+
7 Replies


Expert 100+
P: 1,510
implement and test part 1 first then deal with the others when that is working, i.e.

1.Write a program that will ask for a long number and count how many digits are there in the number.
ex. Enter num: 10854
Output: 5

probably the simplest way is to read the number into a char array and use the string processing functions in <string.h>
Nov 13 '06 #2

houserocks13
P: 5
hehehe.. we havent tackeld arrays yet.. only loops.. so we are supposed to loop for this program.. my syntaz doesnt work..=( it doesnt display the 0 wahhh...
Nov 14 '06 #3

10K+
P: 13,264
hehehe.. we havent tackeld arrays yet.. only loops.. so we are supposed to loop for this program.. my syntaz doesnt work..=( it doesnt display the 0 wahhh...
Well have you done string manipulation then? You'd need to read in the number as a string and access each character of the string for manipulation using string methods
Nov 14 '06 #4

houserocks13
P: 5
not yet=(

for this number
2.Write a program that will ask for a long number and sum the even and odd digits in the number.

my prof changed it

i have this code:
#include <iostream.h>


void main()
{
int num, r, evensum=0, oddsum=0;

cout<<"Enter number: "<< endl;
cin>>num;

r=num/10;
while (num>0)
{
if (r%2==0)
evensum=evensum+r;
else
oddsum=oddsum+r;
num--;
}
cout<<"even is: "<<evensum<< endl;
cout<< "odd is "<< oddsum<<endl;
}


but it wont print the sum.. huhu=(
Nov 14 '06 #5

Expert 100+
P: 1,510
you had a few errors
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2.  
  3. int main()
  4. {
  5. int num, r, evensum=0, oddsum=0;
  6.  
  7. cout<<"Enter number: "<< endl;
  8. cin>>num;
  9.  
  10. while (num>0)           // loop thru all digits
  11. {
  12. if (num%2==0)           // check if even number
  13. evensum=evensum+1;
  14. else
  15. oddsum=oddsum+1;
  16. num=num/10;             // check next digit
  17. }
  18. cout<<"even is: "<<evensum<< endl;
  19. cout<< "odd is "<< oddsum<<endl;
  20. }
  21.  
  22.  
you should now be able to extend this to solve your other problems
Nov 15 '06 #6

P: 23
implement and test part 1 first then deal with the others when that is working, i.e.

1.Write a program that will ask for a long number and count how many digits are there in the number.
ex. Enter num: 10854
Output: 5

probably the simplest way is to read the number into a char array and use the string processing functions in <string.h>
better use '%' (Modulus operator) ok?
Nov 15 '06 #7

Expert 100+
P: 1,510
better use '%' (Modulus operator) ok?
Usually there are serveral ways of implementing an algorithm which vary in simplicity, efficency (run time or memeory required), etc.
For example, using a char array to count the number of even and odd digits entered at the keyboard
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int evensum=0, oddsum=0;
  7. char number[50];
  8. cout<<"Enter number: "<< endl;
  9. cin>>number;                           // read in as characters
  10.  
  11. for(int i=0; number[i] != '\0'; i++)   // loop thru all digits
  12.   if ((number[i] & 1) == 0)            // check if even number
  13.        evensum=evensum+1;
  14.    else
  15.        oddsum=oddsum+1;
  16. cout<<"even is: "<<evensum<< endl;
  17. cout<< "odd is "<< oddsum<<endl;
  18. }
  19.  
This uses the & operator to determine if a character is odd or even.
It can deal with a larger number of digits than reading an int and at the end of this code we still have the original data in number[].
It all depends what the specification is.
Nov 15 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.