In: Computer Science
1) Write a function equation(A, B) that returns a value C where A + B = C
a. Write the equation function:
b: Write the output of your function for a few values:
2) Write a non-recursive function called fact(n) that computes n! Try computing fact(n) for large values. Can you find the maximum value for n your program will compute correctly?
a) Write your fact function:
b) Write the output of your function for a few values:
3)
Write a function called printThem(n) which print the numbers from 1 to n with the following conditions:
In place of multiples of 3 print three.
In place of multiples of 5, print five.
If a number is divisible by both 3 and 5 print fifteen.
Do not print multiples of 7 (even if a multiple of 3)
Display 15 numbers per line. Include sample output for n=50. Note that 50 should be the parameter when you call your printThem function, not part of the function itself.
a) Print Them(n) function:
b) Write your output function
4)
Write a function makeChange(amount) that for a given amount of money makes change for that amount in nickels, dimes, and quarters. You can assume the amount will be 100 cents or less. For example:
Change for 100:
nickels: 4
dimes: 3
quarters: 2
When you are done, think about how you could calculate all possible ways to make change for that amount. That increases the complexity considerably, so it is not required for full credit.
a) Write the output of your makeChange function
b) Write your makeChange function:
*****************************************************************************************************************************************
Please answer these questions in C and write comments on why some actions are being done! Please give which function to write to each problem as well as the output function!! Thank you so much!
//C program
#include<stdio.h>
int equation(int a , int b){
return a+b;
}
int fact(int n){
if(n==0)return 1;
int f = 1;
while(n>0){
f*=n;
n--;
}
return f;
}
int printThem(int n){
int i;
int count=0;
for(i=1;i<=n;i++){
if(i%7==0)continue; //if number is
divisible by 7 then print nothing
else
if(i%3==0&&i%5==0)printf(" fifteen "); //if number is
divisible by 3 and 5 then print fifteen
else if(i%3==0)printf(" Three ");
//if number is divisible by 3 then print three
else if(i%5==0)printf(" five ");
//if number is divisible by 5 then print five
else printf("%d ",i); //else print
number
count++;
if(count==15)printf("\n");//in a
line only 15 numbers are possible
}
}
void makeChange(int amount){
int cents_count=0,nickels_count=0,
dimes_count=0,quarters_count=0;
printf("\nChange for : %d\n",amount);
while(amount>=25){
amount-=25;
quarters_count++;
}
while(amount>=10){
amount-=10;
dimes_count++;
}
while(amount>=5){
amount-=5;
nickels_count++;
}
while(amount>0){
amount--;
cents_count++;
}
printf("Cent : %d\n",cents_count);
printf("Nikel : %d\n",nickels_count);
printf("Dime : %d\n",dimes_count);
printf("Quarter : %d\n",quarters_count);
}
int main(){
printf("\n3+4 : %d\n",equation(3,4));
printf("\n5+5 : %d\n",equation(5,5));
printf("\n20+50 : %d\n",equation(20,50));
printf("\nFactorial of 5 : %d\n",fact(5));
printf("\n\n");
printThem(50);
printf("\n\n");
makeChange(98);
return 0;
}
//sample output