Question

In: Computer Science

Using C: Implement four versions of a function funSum that takes a positive integer n as...

Using C:

Implement four versions of a function funSum that takes a positive integer n as input and returns the sum of all integers up to and including n that are divisible by 6 or 7: using a for loop in funSum1, using a while loop in funSum2, using a do-while loop in funSum3, and using recursion in funSum4. Your output for the included test code should be:

funSum1(20) = 57
funSum2(20) = 57
funSum3(20) = 57
funSum4(20) = 57

Solutions

Expert Solution

#include <stdio.h>

int funsum1(int n)
{
int sum=0,i=0;
for(i=0;i<=n;i++){
if(i%6==0 || i%7==0)
sum+=i;
}return sum;
}
int funsum2(int n)
{
int sum=0,i=0;
while(i<=n){
if(i%6==0 || i%7==0)
sum+=i;
i++;
}return sum;
}

int funsum3(int n)
{
int sum=0,i=0;
do{
if(i%6==0 || i%7==0)
sum+=i;
i++;
}while(i<=n);
return sum;
}

int funsum4(int n)
{
if(n==0)
return 0;
if(n%6==0 || n%7==0)
return n+funsum4(n-1);
return funsum4(n-1);   
}
int main()
{
printf("funsum1(20)=%d\n",funsum1(20));
printf("funsum2(20)=%d\n",funsum2(20));
printf("funsum3(20)=%d\n",funsum3(20));
printf("funsum4(20)=%d\n",funsum4(20));
return 0;
}


Related Solutions

C++ 1. Write a function decimalToBinary() that takes in a positive integer as a parameter and...
C++ 1. Write a function decimalToBinary() that takes in a positive integer as a parameter and use as stack to convert the integer to a its corresponding binary representation. Hint: divide the integer by 2. 2. A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward—assuming that you ignore spaces, punctuation, and case. For example, Race car is a palindrome. So is A man, a...
Write a Scheme function that takes a positive integer n and returns the list of all...
Write a Scheme function that takes a positive integer n and returns the list of all first n positive integers in decreasing order: (decreasing-numbers 10) (10 9 8 7 6 5 4 3 2 1) Please explain every step
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer value n and returns an approximation of e as (1 + 1/n)^n I am not even sure what the question is asking me to do but I think it is asking me to code that function. Does anyone know how to do this?
Write a C function boolean isPrime (int n), that would take a positive integer n as...
Write a C function boolean isPrime (int n), that would take a positive integer n as a parameter and return true or false whether the number is a prime number. You should check the range of n and print proper message (For example, if n is a genitive number, you should print out an error message).
In C++, type a function function(int n, int base) that converts a positive integer x to...
In C++, type a function function(int n, int base) that converts a positive integer x to any base between 2 and 9. The function HAS to do this using a stack, and using methods from below: +isEmpty(): boolean +push(newEntry: ItemType): boolean +pop(): boolean +peek(): ItemType (Also, type a program to test the function). Hint: could use simple iteration continually divides the decimal number by base and keeps track of the remainder by using a stack.
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as a parameter, put all prime numbers less than n into an array, and print out the array and the sum of the numbers in that array. You can use the isPrime function above to save time.
Read about the Sieve of Sundaram. Implement the algorithm using function composition. Given an integer n,...
Read about the Sieve of Sundaram. Implement the algorithm using function composition. Given an integer n, your function should generate all the odd prime numbers up to 2n+2. sieveSundaram :: Integer -> [Integer] sieveSundaram = ... To give you some help, below is a function to compute the Cartesian product of two lists. This is similar to zip, but it produces all possible pairs instead of matching up the list elements. For example, cartProd [1,2] ['a','b'] == [(1,'a'), (1,'b'), (2,'a'),...
In R studio Write a function that takes as an input a positive integer and uses...
In R studio Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Use the lapply function, do not use any of the loop commands in your code.
using the fact that : A positive integer n ≥ 3 is constructive if it is...
using the fact that : A positive integer n ≥ 3 is constructive if it is possible to construct a regular n-gon by straightedge and compass, it is possible to construct the angle 2π/n. And that if both angles α and β can be constructed by straightedge and compass then so are their sums and differences.The outside angle of a regular n-gon is 2π/n. 1. Suppose that n = p^(α1) ··· p^(αk) where p ,··· , pk are distinct odd...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT