Multiplication using recursion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tired
    New Member
    • Feb 2009
    • 13

    Multiplication using recursion

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    	int mul(int,int);
    	int n1,n2,f=1,ans;
    	clrscr();
    	while(f==1)
    	{
    		printf("\n***MULTIPLICATION OF TWO NATURAL NUMBERS***");
    		printf("\nEnter Two Numbers: ");
    		scanf("%d %d",&n1,&n2);
    		if(n1<=0||n2<=0)
    		{
    			printf("\nInvalid Choice...");
    			printf("\nDo You Want To Try Again...\n1.Yes\n2.No");
    			scanf("%d",&f);
    		}
    		else
    		{
    			printf("\nAns = %d",mul(n1,n2),"\n");
    			printf("\nDo You Want To Continue:\n1.Yes\n2.No\n");
    			scanf("%d",&f);
    		}
    	}
    }
    int mul(int a,int b)
    {
    	return((b-1)*a+a);
    }
    //can this be called a proper recursion program???i mean lyk hw do u explain its recursion???..p ls help
    Last edited by JosAH; Feb 20 '09, 06:45 PM. Reason: fixed the [code] ... [/code] tags
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Recursion means you have a program that calls itself. You show two functions. Do either of them call themselves?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Tired
      Code:
      int mul(int a,int b)
      {
      	return((b-1)*a+a);
      }
      //can this be called a proper recursion program???i mean lyk hw do u explain its recursion???..p ls help
      I don't see any recursion at all; where does the mul() function call itself? What is the sentinel condition that stops the recursion?

      Note that mul(a, b) for a > 0 and b > 0 is b iff a == 1, else it is b+mul(a-1, b)

      kind regards,

      Jos

      Comment

      • Tired
        New Member
        • Feb 2009
        • 13

        #4
        so then how do i mk it recursion???i dnt understnd wat u r tryin to explain...m confused..can u explain it by puttin it in d prog??

        Comment

        • Tired
          New Member
          • Feb 2009
          • 13

          #5
          #include<stdio. h>
          #include<conio. h>
          void main()
          {
          int mul(int,int);
          int n1,n2,f=1,ans;
          clrscr();
          while(f==1)
          {
          printf("\n***MU LTIPLICATION OF TWO NATURAL NUMBERS***");
          printf("\nEnter Two Numbers: ");
          scanf("%d %d",&n1,&n2);
          if(n1<=0||n2<=0 )
          {
          printf("\nInval id Choice...");
          printf("\nDo You Want To Try Again...\n1.Yes \n2.No");
          scanf("%d",&f);
          }
          else
          {
          printf("\nAns = %d",mul(n1,n2), "\n");
          printf("\nDo You Want To Continue:\n1.Ye s\n2.No\n");
          scanf("%d",&f);
          }
          }
          }
          int mul(int a,int b)
          {
          if(b==1)
          return a;
          else
          return(a+mul(a, b-1));
          }
          //now i hope this is proper???

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Tired
            Code:
            int mul(int a,int b)
            {
            	if(b==1)
            		return a;
            	else
            		return(a+mul(a,b-1));
            }
            //now i hope this is proper???
            Did you try it?

            kind regards,

            Jos

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Originally posted by Tired
              Code:
              int mul(int a,int b)
              {
                  if (b==1)
                      return a;
                  else
                      return(a+mul(a,b-1));
              }
              Your main program filters out inputs values that are zero or negative; but multiplication is a well-defined operation for that domain. Do the instructions for this project permit you to exclude those values? Even if you're allowed to exclude that domain, might you get more credit if you handled them?

              Your mult function keys off the value of 'b' to decide what to do. Consider the cases where a is positive, but b is zero or negative. First question: does your function accidentally happen to handle those cases? If not, what would have to change to make it work?

              Once you have the function working for all permissible values of b then consider what happens if a is zero or negative. Ask the same two questions.

              Professional programmers also worry about performance. Consider these two function calls: mult(1000,1) vs mult(1,1000). They both will yield the same answer, but the execution time and stack usage is substantially different. Can you find an easy way to improve the performance of the mult function?

              Professional programmers also worry about exception conditions. The product of two int's will not always fit in an int. When it doesn't you have "arithmetic overflow". You need to decide what you want your program to do if overflow occurs. Then you need to consider what it takes to make the program do that.

              Comment

              • Tired
                New Member
                • Feb 2009
                • 13

                #8
                yes Jos..i executed it..and its working properly..i just wanted to know if now this is proper recursion???

                Thank you...
                Amy..

                Comment

                • Tired
                  New Member
                  • Feb 2009
                  • 13

                  #9
                  hey donboc...
                  I am using this function for multiplying 2 natural numbers and if either of the numbers is zero its being handled by the main block...and about (1000*1) and (1000*1)..i have added one else condition for- if a==1 return b.And thanks for mentioning the overflow concept i was having problems with multiplying larger numbers.The solution i see is making the mul function from int to long.
                  And my actual question was is this a proper recursion program?I am trying to make school project on recursion.

                  Thank you for replying...
                  Regards,
                  Amy

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by Tired
                    yes Jos..i executed it..and its working properly..i just wanted to know if now this is proper recursion???

                    Thank you...
                    Amy..
                    You have a function that calls itself if a sentinel condition fails so you have a recursive function there. It isn't very efficient though, i.e. for a*b it adds 'a' to itself 'b-1' times. There are better ways, e.g. 10*6 == (10+10)*(6>>1) == ((10+10)+(10+10 ))*(6>>2)+(10+1 0)

                    kind regards,

                    Jos

                    Comment

                    Working...