loop and switch.. help:)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • houserocks13
    New Member
    • Nov 2006
    • 5

    loop and switch.. help:)

    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
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    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>

    Comment

    • houserocks13
      New Member
      • Nov 2006
      • 5

      #3
      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...

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by houserocks13
        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

        Comment

        • houserocks13
          New Member
          • Nov 2006
          • 5

          #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=(

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            you had a few errors
            Code:
            #include <iostream.h>
            
            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: "<<evensum<< endl;
            cout<< "odd is "<< oddsum<<endl;
            }
            you should now be able to extend this to solve your other problems

            Comment

            • koder
              New Member
              • Sep 2006
              • 23

              #7
              Originally posted by horace1
              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?

              Comment

              • horace1
                Recognized Expert Top Contributor
                • Nov 2006
                • 1510

                #8
                Originally posted by koder
                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
                Code:
                #include <iostream>
                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: "<<evensum<< endl;
                cout<< "odd is "<< oddsum<<endl;
                }
                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.

                Comment

                Working...