Please answer in C++! Please write comments in order to help me understand your
ID: 672630 • Letter: P
Question
Please answer in C++!
Please write comments in order to help me understand your steps!
Here is the link that describes Fizz Buzz : https://en.wikipedia.org/wiki/Fizz_buzz#Programming_interviews
You guys don't need to create any classes or objects for this assignment, you can put everything in main if you'd like. Below, I list the directions and functions I want you to create. Write a factorial function using iteration. int Factorial_iteration(int n); //thisfunction should return n! ^^ same thing as above but use recursion. int Factorial_recursion(int n); // this function should return n! Write a function that returns the sum of all the odd numbers up to n, using iteration, int OddSum_iteration(int n); //should return 1+3+5+7... + n ^^ same as above but use recursion. int OddSum_recursion(int n); //should return 1+3+5+7... + n FizzBuzz For every number that is divisible by 3, print "Fizz" for every number divisible by 5, print "Buzz." If they are divisible by both 3 and 5, print "Fizz Buzz." For every other number, just print the number. Do this using iteration up to the number n. (you guys can just print to console instead of a file) void FizzBuzz_iteration(int n); ^^ Same thing but use recursion, void FizzBuzz_recursion(int n);Explanation / Answer
1
include <stdio.h>
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate it's factorial ");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %d ", n, fact);
return 0;
}
2
#include<stdio.h>
long factorial(int);
int main()
{
int n;
long f;
printf("Enter an integer to find factorial ");
scanf("%d", &n);
if (n < 0)
printf("Negative integers are not allowed. ");
else
{
f = factorial(n);
printf("%d! = %ld ", n, f);
}
return 0;
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
3
# include <stdio.h>
# include <conio.h>
void main()
{
int n, i, osum = 0, esum = 0 ;
clrscr() ;
printf("Enter the limit : ") ;
scanf("%d", &n) ;
for(i = 1 ; i <= n ; i = i + 2)
osum = osum + i ;
printf(" The odd numbers sum is : %d", osum) ;
getch() ;
}
4.
private static int CalcOdd(int n)
{
if (n <= 1)
return 1;
else
if (n % 2 == 0)
n--;
return n + CalcOdd(n - 2);
}
5
Let's start by printing out all of the numbers from 1 and 20,
// and let's do it the quick and easy way.
for( var i=1; i<21; i++ ) {
if( (i%3) === 0 && (i%5) === 0 ) {
console.log( "FizzBuzz" );
}else if( (i%3) === 0 ) {
console.log( "Fizz" );
}else if( (i%5) === 0 ) {
console.log( "Buzz" );
}else{
console.log( i );
}
}