-
#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???..pls help
9 11673
Recursion means you have a program that calls itself. You show two functions. Do either of them call themselves?
@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
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??
#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???
@Tired
Did you try it?
kind regards,
Jos
@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.
yes Jos..i executed it..and its working properly..i just wanted to know if now this is proper recursion???
Thank you...
Amy..
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
@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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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)
|
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...
|
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...
|
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.
|
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...
|
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 ??
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |