forked from randerson112358/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiplication_recursion_integers.c
62 lines (48 loc) · 1.37 KB
/
multiplication_recursion_integers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
This is a C program that multiplies 2 integers recursively both ( negative and positive )
Note: You can use either recursive case
*/
#include <stdio.h>
#include <stdlib.h>// abs()
int mult(int a, int b);
int main(){
int a = 2;
int b = -7;
int answer = mult(a, b);
printf("%d x %d = %d \n", a, b, answer);
return 0;
}
int mult(int a, int b){
//Base Cases
if(a == 0 || b ==0)
return 0;
//Handles Positive values
if(a == 1)
return b;
if(b == 1)
return a;
//Handles Negative Values
if(a == -1)
return -b;
if(b == -1)
return -a;
/*
//Recursive Case
if(a >0 && b > 0)//Checks if both values are positive
return mult(a, b-1) + a; // or we could've wrote mult(a-1, b) + b;
else if(a > 0 && b < 0) // Checks if a is positive and b is negative
return mult(a-1, b) + b;
else if(a < 0 && b > 0)// Checks if a is negative and b is positive
return mult(a, b-1) + a;
else if(a < 0 && b < 0)// Checks if both a and b are negative
return mult(-a, -b -1) + -a; // or we could've wrote mult(-a-1, -b) + -b;
else
return 0;*/
//Recuresive Case
if((a >0 && b > 0) || a < 0 && b < 0) // if (a and b) are either positive or negative
return mult(abs(a), abs(b)-1) + abs(a);
else if(a < 0 && b > 0)// if a is negative and b is positive
return mult(a, b-1) + a;
else//else b is negative and a is positive
return mult(a-1, b) + b;
}