454,332 Members | 1,863 Online
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:)

 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
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 Nov 13 '06 #2

 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

 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 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: "<

 Expert 100+ P: 1,510 you had a few errors Expand|Select|Wrap|Line Numbers #include    int main() { int num, r, evensum=0, oddsum=0;   cout<<"Enter number: "<< endl; cin>>num;   while (num>0)           // loop thru all digits { if (num%2==0)           // check if even number evensum=evensum+1; else oddsum=oddsum+1; num=num/10;             // check next digit } cout<<"even is: "<

 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 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 #include  using namespace std;   int main() { int evensum=0, oddsum=0; char number[50]; cout<<"Enter number: "<< endl; cin>>number;                           // read in as characters   for(int i=0; number[i] != '\0'; i++)   // loop thru all digits   if ((number[i] & 1) == 0)            // check if even number        evensum=evensum+1;    else        oddsum=oddsum+1; cout<<"even is: "<