473,322 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Multiplication using recursion

13
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5.     int mul(int,int);
  6.     int n1,n2,f=1,ans;
  7.     clrscr();
  8.     while(f==1)
  9.     {
  10.         printf("\n***MULTIPLICATION OF TWO NATURAL NUMBERS***");
  11.         printf("\nEnter Two Numbers: ");
  12.         scanf("%d %d",&n1,&n2);
  13.         if(n1<=0||n2<=0)
  14.         {
  15.             printf("\nInvalid Choice...");
  16.             printf("\nDo You Want To Try Again...\n1.Yes\n2.No");
  17.             scanf("%d",&f);
  18.         }
  19.         else
  20.         {
  21.             printf("\nAns = %d",mul(n1,n2),"\n");
  22.             printf("\nDo You Want To Continue:\n1.Yes\n2.No\n");
  23.             scanf("%d",&f);
  24.         }
  25.     }
  26. }
  27. int mul(int a,int b)
  28. {
  29.     return((b-1)*a+a);
  30. }
  31.  
//can this be called a proper recursion program???i mean lyk hw do u explain its recursion???..pls help
Feb 20 '09 #1
9 11673
donbock
2,426 Expert 2GB
Recursion means you have a program that calls itself. You show two functions. Do either of them call themselves?
Feb 20 '09 #2
JosAH
11,448 Expert 8TB
@Tired
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
Feb 20 '09 #3
Tired
13
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??
Feb 21 '09 #4
Tired
13
#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)
{
if(b==1)
return a;
else
return(a+mul(a,b-1));
}
//now i hope this is proper???
Feb 21 '09 #5
JosAH
11,448 Expert 8TB
@Tired
Did you try it?

kind regards,

Jos
Feb 21 '09 #6
donbock
2,426 Expert 2GB
@Tired
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.
Feb 21 '09 #7
Tired
13
yes Jos..i executed it..and its working properly..i just wanted to know if now this is proper recursion???

Thank you...
Amy..
Feb 22 '09 #8
Tired
13
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
Feb 22 '09 #9
JosAH
11,448 Expert 8TB
@Tired
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+10)

kind regards,

Jos
Feb 22 '09 #10

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

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
54
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This...
9
by: Ralf Hildebrandt | last post by:
Hi all! First of all: I am a C-newbie. I have noticed a "strange" behavior with the standart integer multiplication. The code is: void main(void)
87
by: Vijay Kumar R Zanvar | last post by:
Hi, Why multiplication of pointers is not allowed? Till now I only know this, but not the reason why! PS: As a rule, I searched the FAQ, but could not find an answer. -- Vijay Kumar R...
17
by: Christopher Dyken | last post by:
Hi group, I'm trying to implement two routines to handle 32x32-bits and 64x64-bits signed integer multiplication on a 32 bits machine in C. It easy to find descriptions of non-signed...
11
by: mjdeesh_hi | last post by:
How can we perfom multiplication programatically without using + or * operator. Can any one help out in this one. Jagadeesh.
1
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.