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:(A) funSum1(20) = 57(B) funSum2(20) = 57(C) funSum3(20) = 57(D) funSum4(20) = 57

Respuesta :

Answer:

public class DivisionBy6or7 {

   

   public static int funSum1(int n){

       int sum = 0;

       for (int i = 6; i <= n; i++){

           if(i % 6 == 0 || i % 7 == 0){

               sum = sum + i;

           }

       }

       

       return sum;

   }

   

   public static int funSum2(int n){

       int sum = 0;

       int i = 6;

       while (i <= n){

           if(i % 6 == 0 || i % 7 == 0){

               sum = sum + i;

           }

           i++;

       }

       

       return sum;

   }

   

   public static int funSum3(int n){

       int sum = 0;

       int i = 6;

       do{

           if(i % 6 == 0 || i % 7 == 0){

               sum = sum + i;

           }

           i++;

       }while(i <= n);

       

       return sum;

   }

   

   public static int funSum4(int n){

       if (n < 6){

           return 0;

       }

       else{

           if(n % 6 == 0 || n % 7 == 0){

               return n + funSum4(n-1);

           }

           else{

               return funSum4(n-1);

           }

       }

   }

   public static void main(String[] args) {

       System.out.println("funSum1(20) = " + funSum1(20));

       System.out.println("funSum2(20) = " + funSum2(20));

       System.out.println("funSum3(20) = " + funSum3(20));

       System.out.println("funSum4(20) = " + funSum4(20));

   }  

}

Explanation:

=> function funSum1()

   public static int funSum1(int n){

      // initialize an int variable sum to zero to cumulatively

      // find and store the sum of the integers

       int sum = 0;

     

       // write a for loop that goes from i = 6 to i = n

       // At every cycle of the loop, check if i is divisible by 6 or 7

       // If it is, add it to the value of sum

       for (int i = 6; i <= n; i++){

           if(i % 6 == 0 || i % 7 == 0){

               sum = sum + i;

           }

       }                

       return sum;      // Return the value of sum

   }                             // End of method funSum1

=> function funSum2()

   public static int funSum2(int n){

      // initialize an int variable sum to zero to cumulatively

      // find and store the sum of the integers

       int sum = 0;

     

       // write a while loop that goes from i = 6 to i = n

       // At every cycle of the loop, check if i is divisible by 6 or 7

       // If it is, add it to the value of sum

       int i = 6;

       while (i <= n){

           if(i % 6 == 0 || i % 7 == 0){

               sum = sum + i;

           }

           i++;

       }

       return sum;      

   }                            

=> function funSum3()

   public static int funSum3(int n){

      // initialize an int variable sum to zero to cumulatively

      // find and store the sum of the integers

       int sum = 0;

     

       // write a do..while loop that goes from i = 6 to i = n

       // At every cycle of the loop, check if i is divisible by 6 or 7

       // If it is, add it to the value of sum

       int i = 6;

       do{

           if(i % 6 == 0 || i % 7 == 0){

               sum = sum + i;

           }

           i++;

         }while(i <= n);

       return sum;      

   }                            

=> function funSum4()

  // The method recursively calls itself

   public static int funSum4(int n){        

       // check if the value of n is less than 6.

      // If it is, return 0

       if (n < 6){

           return 0;

       }

       

      // If the value of n is not less than 6,

      // do the following

       else{

           // Check if the value of n is divisible by 6 or 7

           // If it is, return n, reduce n by 1 and recall funSum4 using the

           // reduced value of n

           if(n % 6 == 0 || n % 7 == 0){

               return n + funSum4(n-1);

           }

           // If the values of n is not divisible by 6 or 7,

           // reduce n by 1 and recall funSum4 using the reduced value of n

           else{

               return funSum4(n-1);

           }

       }

   }    

=> function main()

  // This is the main method of the program

  // This is where java begins its execution

  // The four functions are being called and tested here.

  public static void main(String[] args) {

       System.out.println("funSum1(20) = " + funSum1(20));

       System.out.println("funSum2(20) = " + funSum2(20));

       System.out.println("funSum3(20) = " + funSum3(20));

       System.out.println("funSum4(20) = " + funSum4(20));

   }  

Output:

> funSum1(20) = 57

> funSum2(20) = 57

> funSum320) = 57

> funSum4(20) = 57

Notes:

The source code of this code has been attached to this response. Please download it for better formatting and readability.

Ver imagen stigawithfun

Following are the solution to the given question:

Program Explanation:

  • Declaring header file.
  • Defining four methods "funSum1, funSum2, funSum3, and funSum4" that takes an integer "n" in its parameter.
  • Inside the method, a loop has defined that use if block that checks "n%6" and "n%7" value equal to 0 with using OR logical gate, and use sum variable that adds value in "i".
  • return sum value.
  • Outside the method calling the above method and prints its value.

Program:

#include //header file

int funsum1(int n)//defining a method funsum1 that takes one integer variable inside the parameter  

{

int sum=0,i=0;//defining integr variable sum and i that initializes with 0

for(i=0;i<=n;i++)//defining for loop that check value

{

if(i%6==0 || i%7==0)//defining if block that check n % 6 and n % 7 value equal to 0 with using OR logical gate

sum+=i;//defining sum variable that adds value in i  

}return sum;//using return keyword that prints sum variable value

}

int funsum2(int n)//defining a method funsum2 that takes one integer variable inside the parameter

{

int sum=0,i=0;//defining integr variable sum and i that initializes with 0

while(i<=n)//defining loop that check i value less than equal to n

{

if(i%6==0 || i%7==0)//defining if block that check n % 6 and n % 7 value equal to 0 with using OR logical gate

sum+=i;//defining sum variable that adds value in i  

i++;//incrementing the value of i

}return sum;//using return keyword that prints sum variable value

}

int funsum3(int n)//defining a method funsum3 that takes one integer variable inside the parameter

{

int sum=0,i=0;//defining integr variable sum and i that initializes with 0

do//defining do while loop

{

if(i%6==0 || i%7==0)//defining if block that check n % 6 and n % 7 value equal to 0 with using OR logical gate

sum+=i;//defining sum variable that adds value in i  

i++;//incrementing i value

}

while(i<=n);//defining loop that check i value less than equal to n

return sum; //using return keyword that prints sum variable value  

}

int funsum4(int n)//defining a method funsum4 that takes one integer variable inside the parameter

{

if(n==0)//defining if block that check  n value equal to 0

return 0;//return value 0

if(n%6==0 || n%7==0)//defining if block that check n % 6 and n % 7 value equal to 0 with using OR logical gate  

return n+funsum4(n-1);//using return keyword that calls funsum4 and return its value

return funsum4(n-1); //using return keyword that calls funsum4 and return its value  

}

int main()//defining main method

{

printf("funsum1(20)=%d\n",funsum1(20));//calling the method and print its return value

printf("funsum2(20)=%d\n",funsum2(20));//calling the method and print its return value

printf("funsum3(20)=%d\n",funsum3(20));//calling the method and print its return value

printf("funsum4(20)=%d\n",funsum4(20));//calling the method and print its return value

return 0;

}

Output:

Please find the attached file.

Learn more:

brainly.com/question/16027903

Ver imagen codiepienagoya
ACCESS MORE