Create all 6 functions and use switch case function in driver to run program giv
ID: 3664799 • Letter: C
Question
Create all 6 functions and use switch case function in driver to run program given the case sensitive option AaBbCcDc AaBbCcD TNormall1 No Spac.. | Heading 1 Heading 2 Sub Title x,x'la-.a-m--=15-12-. Font Paragraph Styles D. Exponential (base ) E. Factorial (N!) Csc 210-In-class 04-Besursion Part 2 (Chapter 6) F. Sum power -some of all integers from 1 to N using power K (1 You are to create a recursive programs (LastnameFI Recusive2.cpp), which allow the user to select the calculation based on the provided information. 3+N) G. Exit You will create 6 recursive functions (or methods), one for each of the menu. Name your function using the following convention (Where XOXX is your first initials and abcd are last 4 digit of your student ID) If option A, B, C, or E is selected, then you will ask user to enter value of N If option D is selected, then you will ask user to enter value of bessand power if option F is selected, then you will ask user to enter value of M.aud power K Due Date end of the class muamtsa. function name A. Sum of all integers from1toN (1+2+3+ N) B. Sum square of all integers from 1 to N (1 2+3+4+N') C. Sum square-root of all integers from 1 to N (1V+2V3+31+4+ XXexpBasstidl function name D. Exponential (base xatea anata function name E. Factorial (N) function name F. Sum power- some of all integers from 1 to N using power K(+ in the driver, a menu will be presented (you can use switch/case) as follow and this will run repeatedly until option or or is chosen. A.Sum of all integers from 1 to N (1 + 2 + 3+..+N) B.Sum square of all integers from 1 to N (1) + 2.3.41 +-+Nr) C. Sum square-root of all integers from 1 to N (1n+2+3V+4raExplanation / Answer
Note:
Programming language not specified so Java is used.
// import required packages
import java.io.*;
import java.util.*;
import java.util.Scanner;
// class to perform the recursive calculations
public class recfun
{
// main method
public static void main(String[] args)
{
// declare the required variables
char opt;
int n,base,k;
Scanner dt = new Scanner(System.in);
// loop for itertive call
do
{
System.out.println("A. Sum of all integers from 1 to N ");
System.out.println("B. Sum square of all integers from 1 to N");
System.out.println("C. Sum square-root of all integers from 1 to N ");
System.out.println("D. Exponential for (Base^N)");
System.out.println("E. Factorial (N!) ");
System.out.println("F. Sum power of all integers from 1 to N ");
System.out.println("G. Exit");
opt=dt.next().charAt(0);
// switch case proccess
switch(opt)
{
case 'A':
case 'a':
System.out.println("Enter value for N ");
n=dt.nextInt();
System.out.println("Sum of N "+ kar_sumNa(n));
break;
case 'B':
case 'b':
System.out.println("Enter value for N ");
n=dt.nextInt();
System.out.println("Sum square of N "+ kar_sumSquareNb(n));
break;
case 'C':
case 'c':
System.out.println("Enter value for N ");
n=dt.nextInt();
System.out.println("Sum square root of N "+ kar_sumSquareRootNc(n));
break;
case 'D':
case 'd':
System.out.println("Enter value for Base ");
base=dt.nextInt();
System.out.println("Enter value for power ");
n=dt.nextInt();
System.out.println("Exponent of base N "+ kar_expoBaseNd(base,n));
break;
case 'E':
case 'e':
System.out.println("Enter value for N ");
n=dt.nextInt();
System.out.println("Factorial of N "+ kar_factorialNa(n));
break;
case 'F':
case 'f':
System.out.println("Enter value for N ");
n=dt.nextInt();
System.out.println("Enter value for power ");
k=dt.nextInt();
System.out.println("SUM power of N "+ kar_sumPowerNb(n,k));
break;
}
}while(Character.toLowerCase(opt)!=Character.toLowerCase('G'));
}
// Recursive method to calculate sum of numbers from 1 to N
public static int kar_sumNa(int n)
{
if(n==1)
return 1;
else
return n+ kar_sumNa(n-1) ;
}
// Recursive method to calculate sum square of numbers from 1 to N
public static int kar_sumSquareNb(int n)
{
if (n == 1)
{
return 1;
}
else
return (int)Math.pow(n, 2)+kar_sumSquareNb(n-1);
}
// Recursive method to calculate sum of square root of numbers from 1 to N
public static int kar_sumSquareRootNc(int n)
{
int result=0;
if (n == 1)
{
return 1;
}
else
return (int) Math.sqrt(n)+kar_sumSquareRootNc(n-1) ;
}
// Recursive method to calculate exponent of a number
public static int kar_expoBaseNd(int base, int n)
{
if (n ==0)
return 1;
else
return base * kar_expoBaseNd(base, n-1) ;
}
// Recursive method to find factorial value
public static int kar_factorialNa(int n)
{
if (n < 0)
{
throw new IllegalArgumentException("Illegal Power Argument");
}
if (n == 0)
{
return 1;
} else
{
return n * kar_factorialNa(n - 1);
}
}
// Recursive method to find sum of power of values from 1 to N
public static int kar_sumPowerNb(int n, int k)
{
if (n < 0)
{
throw new IllegalArgumentException("Illegal Power Argument");
}
if (n ==1)
return 1;
else
return (int)Math.pow(n, k)+kar_sumPowerNb(n-1, k) ;
}
}
Result:
A. Sum of all integers from 1 to N
B. Sum square of all integers from 1 to N
C. Sum square-root of all integers from 1 to N
D. Exponential for (Base^N)
E. Factorial (N!)
F. Sum power of all integers from 1 to N
G. Exit
f
Enter value for N
5
Enter value for power
2
SUM power of N 55
A. Sum of all integers from 1 to N
B. Sum square of all integers from 1 to N
C. Sum square-root of all integers from 1 to N
D. Exponential for (Base^N)
E. Factorial (N!)
F. Sum power of all integers from 1 to N
G. Exit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.