Recursion program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nethra
    New Member
    • Apr 2007
    • 10

    #1

    Recursion program

    Hi , I need the program of reversing a number using recursion.
    Ex: if input is 123435 , output to be 534321.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

    Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.

    Then when you are ready post a new question in this thread.

    MODERATOR

    Comment

    • faceboy
      New Member
      • Apr 2007
      • 4

      #3
      yow thry this function

      Comment

      • palani12kumar
        New Member
        • Oct 2006
        • 60

        #4
        Originally posted by Nethra
        Hi , I need the program of reversing a number using recursion.
        Ex: if input is 123435 , output to be 534321.
        try this:

        i think it will give what you want....
        all the best......

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          faceboy & palani12kumar-

          This is a site where we attempt to help people, however posting code answers is not permitted. The OP will not learn anything by merely using your code, it is much more beneficial to have them attempt it, and then help them fix that code they attempted. I have removed the code you have posted so others cannot copy it, and please do not directly post code unless it is required, or a modification of code that has been posted.

          Comment

          • Nethra
            New Member
            • Apr 2007
            • 10

            #6
            Hi,
            I have tried with this code
            Code:
            {
                                if (n == 0)
                                      return;
                                else
                                {
                                     digit = num % 10;
                                     rev = rev * 0 + digit;
                                     num = numrv(num / 10);
                                 }
                               }
            here numrv is the recurssive function.but for this i am getting only the last digit as output.

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              What is rev supposed to be doing? In your operation, you set

              Code:
              rev = rev * 0 + digit;
              which is evaluated as

              rev = (rev * 0) + digit --> rev = 0 + digit --> rev = digit;

              Comment

              • Motoma
                Recognized Expert Specialist
                • Jan 2007
                • 3236

                #8
                Pseudo code
                Code:
                function reverse(string)
                {
                    if(length(first(string)) == 1) return string;
                    return reverse(butfirst(string)) + first(string);
                
                /*
                The first function returns the first character of the string
                The length function returns the length of a string
                The butfirst function returns the string without the first character.
                */
                }

                Comment

                • Nethra
                  New Member
                  • Apr 2007
                  • 10

                  #9
                  Code:
                   if (n == 0)
                                            return;
                                      else
                                      {
                                           digit = num % 10;
                                           rev = rev * 10 + digit;
                                           num = numrv(num / 10);
                                           return rev;
                                       }
                                     }
                  actually that was 10 and not 0.I also missed the return statement.now i have mentioned the correct code.the logic here is ,
                  the digit variable get that last digit with mod operator which is mutiplied with 10 to push that digit to left side of the num.then the actual num is divided by 10 to remove the last digit.this continues until num becomes zero.

                  Comment

                  • Nethra
                    New Member
                    • Apr 2007
                    • 10

                    #10
                    Hi,
                    Sorry I didnot get the pseudocode provided above. my doubt is that should the address of the string be passed to the string parameter,what will be the defnition for first and butfirst function.Can the logic be provided in simple, please.

                    Comment

                    • Savage
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1759

                      #11
                      Originally posted by Nethra
                      Hi,
                      Sorry I didnot get the pseudocode provided above. my doubt is that should the address of the string be passed to the string parameter,what will be the defnition for first and butfirst function.Can the logic be provided in simple, please.
                      Are u entering number as int or as array of chars(string)?

                      If you are entering as a int you need to convert it to array of chars.After that use
                      a recursion so that fisrt element becomes last.To do this you need to find the lenght using strlen and then something like this in function:


                      Code:
                      if(k<(strlen(number)-1)/2)
                      {
                            temp=number[k],number[k]=number[strlen(number)-k],number[strlen(number)-k]=temp; // k will start as 0 and increase
                            //call the function with k+1 argument.
                      }
                      return number;
                      Now,let us do a test:

                      1.

                      INPUT: number=12345;

                      strlen=5;

                      k=0,k<2,temp=nu mber[0],number[0]=number[4],number[4]=temp; (52341)
                      k=1,k<2,temp=nu mber[1],number[1]=number[3],number[3]=temp; (54321)

                      2.

                      INPUT: number=345678

                      strlen=6;

                      k=0;k<3,temp=nu mber[0],number[0]=number[5],number[5]=temp; (845673)
                      k=1,k<3,temp=nu mber[1],number[1]=number[4],number[4]=temp; (875643)
                      k=2,k<3,temp=nu mber[2],number[2]=number[3],number[3]=temp; (876543)

                      As we can see it works now is up to you to create such a function.

                      Savage

                      Comment

                      • metalgod4eva
                        New Member
                        • Mar 2007
                        • 4

                        #12
                        Here is a great example of recursion without using any input and still have 12344321 as an output. This doesn't give it away nor will it work on your input variable but its a great way to break down recusion programs and theorize how to acomplish your goal.
                        Code:
                        #include <stdio.h>
                        int factorial (int x)
                        {
                                printf("%d",x);
                               if(x < 5){
                               factorial(x+1)
                               }
                              printf("%d",x);
                        }
                        
                        int main()
                        {
                        int x =1;
                        factorial(x);
                        getchar();
                        }

                        Comment

                        • Nethra
                          New Member
                          • Apr 2007
                          • 10

                          #13
                          HI,
                          Thanks,I got program with logic.

                          Comment

                          • Nethra
                            New Member
                            • Apr 2007
                            • 10

                            #14
                            Can any one say be how to store the information in bits.

                            For ex,the program- store the VIBGYOR color info in bits.When the given input is a number the output should give which bits indicate the color.

                            Thanks in Advance.

                            Comment

                            • Nethra
                              New Member
                              • Apr 2007
                              • 10

                              #15
                              Can any one say how to store the information in bits.

                              For ex,the program- store the VIBGYOR color info in bits.When the given input is a number the output should give which bits indicate the color.

                              Thanks in Advance

                              Comment

                              Working...