+ 2
Battle Of The Cakes
I can't solve this practice please help, it goes like: Pastry chefs are competing to win the battle of the cakes. For each additional cake made, the number of eggs required increases by 1 (1 egg for the first cake, 2 eggs for the second, etc.). Take the number of cakes that must be baked as the input, calculate (recursively) how many eggs were used to bake them by the end of the battle and output the result. Sample Input 4 Sample Output 10 Explanation Since 4 cakes must be cooked, starting from the first 1 + 2 + 3 + 4 =10 eggs will be used.
5 Answers
+ 2
Nice! Here ya go, just needs couts and cins
https://code.sololearn.com/cMzm9JGK04TL/?ref=app
+ 3
Slick I have made my code work, anyways here it is:
#include <iostream>
using namespace std;
int sum = 0;
int recSum(int n) {
//complete the function
if (n <= 1) {
sum += 1;
} else {
for (int x = 1; x <= n;) {
sum = sum + n;
n--;
}
}
}
int main() {
//getting input
int n;
cin >> n;
//call the recursive function and print returned value
recSum(n);
cout << sum;
return 0;
}
+ 1
#include <iostream>
using namespace std;
int recSum(int n) {
//complete the function
int oneor0 = 0;
if (n <= 1) {
oneor0 += n;
return oneor0;
} else {
return n + recSum(n-1);
}
}
int main() {
//getting input
int n;
cin >> n;
//call the recursive function and print returned value
cout << recSum(n);
return 0;
}
0
I have code for you in C. Please show us your attempt
- 1
#include <iostream>
using namespace std;
int factorial(int n) {
if (n==1) {
return 1;
}else if (n==0){
return 0;
}else{
return n + factorial(n-1);
}
}
int main() {
int n;
cin >> n;
cout << factorial(n);
cout << endl;
return 0;
}