C89 Programming The program will use incrementors and decrementors in an add() f
ID: 669343 • Letter: C
Question
C89 Programming
The program will use incrementors and decrementors in an add() function to substitute for the addition fuction. It will also need to use recursion to add the two numbers. Can't use pointers, only recursion with incrementors and decrementors, and the program will need to work with both positive and negative values. Here's what I have so far:
#include <stdio.h> //standard io header
int add (int n, int m);
int main(void){
int n, m, sum;
printf("Enter the 1st number: ");
scanf("%d", &n);
printf("Enter the 2nd number: ");
scanf("%d", &m);
printf("Sum of two numbers : %d", sum);
return 0;
}
int add (int n, int m){
int sum;
while(m--){
sum++;
}
while(n--){
sum++;
}
return sum;
}
Explanation / Answer
#include <stdio.h> //standard io header
int add (int n, int m);
int main(void){
int n, m, sum;
printf("Enter the 1st number: ");
scanf("%d", &n);
printf("Enter the 2nd number: ");
scanf("%d", &m);
sum=add(n, m);
printf("Sum of two numbers : %d", sum);
return 0;
}
int add (int n, int m){
int sum=0;
if(n!=0){
return sum+add(n-1,m);
}
if(m!=0){
return sum+add(n,m-1);
}
return sum;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.