SlideShare une entreprise Scribd logo
1  sur  2
In this programming assignment you are required to write a tail-recursive method that prints
every alternate integer between two specific integers. Programming: The main() program should
prompt the user to enter the beginning and the end number of the range. It then displays every
3rd integer starling from the beginning number. The end number may or may not be displayed.
For example: if user specifics 2 and 10, then the program displays 2, 5, 8. The next number is 11
that is beyond the last number, so it is not displayed. [Method must be tail-recursive] Use
appropriate print statements indicating the method call point and return of the answer to prove
the tail-recursive nature of the method. A sample output with initial input 2 and 10 could be as
follows: Recursive call not 1 Method call parameters: low = 2 and high = 10 Method returns: 2
Recursive call no: 2 Method call parameters: low = 5 and high = 10 Method returns: 5 Recursive
call no: 3 Method call parameters: low = 8 and high = 10 Method returns: 8 Tail-recursion ends.
Program terminates. Runtime errors should be handled properly. For example, if the user types a
non-numeric character or hits enter without typing a number etc.
Solution
import java.util.Scanner;
public class Tailrecursive {//class starts
static int cnt = 0;//counter
public static void tail_recursive(int b, int e)//takes two parameters begining integer and ending
integer
{
if (b <= e) {
System.out.println(" Recursive call no: " + (cnt + 1));
System.out.println(" Method call parameters: low = " + b + " and high = " + e + " method
returns: " + b);
cnt++;
tail_recursive(b + 3, e);//recursive calling
}
}
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(System.in);//takes input from console
System.out.println("Enter the beginning integer");
int b = s.nextInt();
System.out.println("Enter the ending integer");
int e = s.nextInt();
tail_recursive(b, e);//function calling
System.out.println("Tail recursion ends. program terminates.");
}
}
output:
run:
Enter the beginning integer
2
Enter the ending integer
10
Recursive call no: 1
Method call parameters: low = 2 and high = 10 method returns: 2
Recursive call no: 2
Method call parameters: low = 5 and high = 10 method returns: 5
Recursive call no: 3
Method call parameters: low = 8 and high = 10 method returns: 8
Tail recursion ends. program terminates.
BUILD SUCCESSFUL (total time: 5 seconds)

Contenu connexe

Similaire à In this programming assignment you are required to write a tail-recurs.docx

c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaonyash production
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 solIIUM
 
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docxBottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docxAASTHA76
 
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfC++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfandreaplotner1
 
Lab-11-C-Problems.pptx
Lab-11-C-Problems.pptxLab-11-C-Problems.pptx
Lab-11-C-Problems.pptxShimoFcis
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptxKimVeeL
 

Similaire à In this programming assignment you are required to write a tail-recurs.docx (15)

L03vars
L03varsL03vars
L03vars
 
keyword
keywordkeyword
keyword
 
keyword
keywordkeyword
keyword
 
Pointer
PointerPointer
Pointer
 
Functions in c
Functions in cFunctions in c
Functions in c
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 
Chapter2
Chapter2Chapter2
Chapter2
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
 
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docxBottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
 
07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt
 
Python Manuel-R2021.pdf
Python Manuel-R2021.pdfPython Manuel-R2021.pdf
Python Manuel-R2021.pdf
 
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfC++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
 
Lab-11-C-Problems.pptx
Lab-11-C-Problems.pptxLab-11-C-Problems.pptx
Lab-11-C-Problems.pptx
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 

Plus de olsenlinnea427

Assume that X is a discrete random variable that can take on the follo.docx
Assume that X is a discrete random variable that can take on the follo.docxAssume that X is a discrete random variable that can take on the follo.docx
Assume that X is a discrete random variable that can take on the follo.docxolsenlinnea427
 
Assume that in an economy- people hold $1000 of currency and $4000 of.docx
Assume that in an economy- people hold $1000 of currency and $4000 of.docxAssume that in an economy- people hold $1000 of currency and $4000 of.docx
Assume that in an economy- people hold $1000 of currency and $4000 of.docxolsenlinnea427
 
Assume a Poisson distribution with -4-5- Find the following probabilit.docx
Assume a Poisson distribution with -4-5- Find the following probabilit.docxAssume a Poisson distribution with -4-5- Find the following probabilit.docx
Assume a Poisson distribution with -4-5- Find the following probabilit.docxolsenlinnea427
 
Assume a member is selected at random trom the population represented.docx
Assume a member is selected at random trom the population represented.docxAssume a member is selected at random trom the population represented.docx
Assume a member is selected at random trom the population represented.docxolsenlinnea427
 
Assume a list has the following element- write a function to interch.docx
Assume a list has the following element-   write a function to interch.docxAssume a list has the following element-   write a function to interch.docx
Assume a list has the following element- write a function to interch.docxolsenlinnea427
 
Assume a company had no jobs in progress at the beginning of July and.docx
Assume a company had no jobs in progress at the beginning of July and.docxAssume a company had no jobs in progress at the beginning of July and.docx
Assume a company had no jobs in progress at the beginning of July and.docxolsenlinnea427
 
Assignment- Peg Game Create a web page that allows the user to play th.docx
Assignment- Peg Game Create a web page that allows the user to play th.docxAssignment- Peg Game Create a web page that allows the user to play th.docx
Assignment- Peg Game Create a web page that allows the user to play th.docxolsenlinnea427
 
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docxAssignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docxolsenlinnea427
 
As part of measures for containing the Covid-19 pandemic- vaccination.docx
As part of measures for containing the Covid-19 pandemic- vaccination.docxAs part of measures for containing the Covid-19 pandemic- vaccination.docx
As part of measures for containing the Covid-19 pandemic- vaccination.docxolsenlinnea427
 
As hematopoietic stem cells (HSC) mature and become differentiated the.docx
As hematopoietic stem cells (HSC) mature and become differentiated the.docxAs hematopoietic stem cells (HSC) mature and become differentiated the.docx
As hematopoietic stem cells (HSC) mature and become differentiated the.docxolsenlinnea427
 
As human populations & ecological footprints approach earth's carrying.docx
As human populations & ecological footprints approach earth's carrying.docxAs human populations & ecological footprints approach earth's carrying.docx
As human populations & ecological footprints approach earth's carrying.docxolsenlinnea427
 
Aries' description of an attitude toward death he called -tame death-.docx
Aries' description of an attitude toward death he called -tame death-.docxAries' description of an attitude toward death he called -tame death-.docx
Aries' description of an attitude toward death he called -tame death-.docxolsenlinnea427
 
Apple Inc- received $350-000 as dividends last year from its investmen.docx
Apple Inc- received $350-000 as dividends last year from its investmen.docxApple Inc- received $350-000 as dividends last year from its investmen.docx
Apple Inc- received $350-000 as dividends last year from its investmen.docxolsenlinnea427
 
Anthony has been living in Western WA for 3 years- After moving from C.docx
Anthony has been living in Western WA for 3 years- After moving from C.docxAnthony has been living in Western WA for 3 years- After moving from C.docx
Anthony has been living in Western WA for 3 years- After moving from C.docxolsenlinnea427
 
answer the questions in Blockchain list 5 point for all of the followi.docx
answer the questions in Blockchain list 5 point for all of the followi.docxanswer the questions in Blockchain list 5 point for all of the followi.docx
answer the questions in Blockchain list 5 point for all of the followi.docxolsenlinnea427
 
Answer the following questions- Question 1- You have implemented a sec.docx
Answer the following questions- Question 1- You have implemented a sec.docxAnswer the following questions- Question 1- You have implemented a sec.docx
Answer the following questions- Question 1- You have implemented a sec.docxolsenlinnea427
 
Answer the following based on a Blockchain and Biometrics I- What t.docx
Answer the following based on a Blockchain and Biometrics    I- What t.docxAnswer the following based on a Blockchain and Biometrics    I- What t.docx
Answer the following based on a Blockchain and Biometrics I- What t.docxolsenlinnea427
 
Anglin Company- a manufacturing firred has supplicd the following info.docx
Anglin Company- a manufacturing firred has supplicd the following info.docxAnglin Company- a manufacturing firred has supplicd the following info.docx
Anglin Company- a manufacturing firred has supplicd the following info.docxolsenlinnea427
 
Angela Lopez owns and manages a consulting firm called Metrixi which b.docx
Angela Lopez owns and manages a consulting firm called Metrixi which b.docxAngela Lopez owns and manages a consulting firm called Metrixi which b.docx
Angela Lopez owns and manages a consulting firm called Metrixi which b.docxolsenlinnea427
 
Andrew finds that on his way to work his wait time for the bus is roug.docx
Andrew finds that on his way to work his wait time for the bus is roug.docxAndrew finds that on his way to work his wait time for the bus is roug.docx
Andrew finds that on his way to work his wait time for the bus is roug.docxolsenlinnea427
 

Plus de olsenlinnea427 (20)

Assume that X is a discrete random variable that can take on the follo.docx
Assume that X is a discrete random variable that can take on the follo.docxAssume that X is a discrete random variable that can take on the follo.docx
Assume that X is a discrete random variable that can take on the follo.docx
 
Assume that in an economy- people hold $1000 of currency and $4000 of.docx
Assume that in an economy- people hold $1000 of currency and $4000 of.docxAssume that in an economy- people hold $1000 of currency and $4000 of.docx
Assume that in an economy- people hold $1000 of currency and $4000 of.docx
 
Assume a Poisson distribution with -4-5- Find the following probabilit.docx
Assume a Poisson distribution with -4-5- Find the following probabilit.docxAssume a Poisson distribution with -4-5- Find the following probabilit.docx
Assume a Poisson distribution with -4-5- Find the following probabilit.docx
 
Assume a member is selected at random trom the population represented.docx
Assume a member is selected at random trom the population represented.docxAssume a member is selected at random trom the population represented.docx
Assume a member is selected at random trom the population represented.docx
 
Assume a list has the following element- write a function to interch.docx
Assume a list has the following element-   write a function to interch.docxAssume a list has the following element-   write a function to interch.docx
Assume a list has the following element- write a function to interch.docx
 
Assume a company had no jobs in progress at the beginning of July and.docx
Assume a company had no jobs in progress at the beginning of July and.docxAssume a company had no jobs in progress at the beginning of July and.docx
Assume a company had no jobs in progress at the beginning of July and.docx
 
Assignment- Peg Game Create a web page that allows the user to play th.docx
Assignment- Peg Game Create a web page that allows the user to play th.docxAssignment- Peg Game Create a web page that allows the user to play th.docx
Assignment- Peg Game Create a web page that allows the user to play th.docx
 
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docxAssignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
 
As part of measures for containing the Covid-19 pandemic- vaccination.docx
As part of measures for containing the Covid-19 pandemic- vaccination.docxAs part of measures for containing the Covid-19 pandemic- vaccination.docx
As part of measures for containing the Covid-19 pandemic- vaccination.docx
 
As hematopoietic stem cells (HSC) mature and become differentiated the.docx
As hematopoietic stem cells (HSC) mature and become differentiated the.docxAs hematopoietic stem cells (HSC) mature and become differentiated the.docx
As hematopoietic stem cells (HSC) mature and become differentiated the.docx
 
As human populations & ecological footprints approach earth's carrying.docx
As human populations & ecological footprints approach earth's carrying.docxAs human populations & ecological footprints approach earth's carrying.docx
As human populations & ecological footprints approach earth's carrying.docx
 
Aries' description of an attitude toward death he called -tame death-.docx
Aries' description of an attitude toward death he called -tame death-.docxAries' description of an attitude toward death he called -tame death-.docx
Aries' description of an attitude toward death he called -tame death-.docx
 
Apple Inc- received $350-000 as dividends last year from its investmen.docx
Apple Inc- received $350-000 as dividends last year from its investmen.docxApple Inc- received $350-000 as dividends last year from its investmen.docx
Apple Inc- received $350-000 as dividends last year from its investmen.docx
 
Anthony has been living in Western WA for 3 years- After moving from C.docx
Anthony has been living in Western WA for 3 years- After moving from C.docxAnthony has been living in Western WA for 3 years- After moving from C.docx
Anthony has been living in Western WA for 3 years- After moving from C.docx
 
answer the questions in Blockchain list 5 point for all of the followi.docx
answer the questions in Blockchain list 5 point for all of the followi.docxanswer the questions in Blockchain list 5 point for all of the followi.docx
answer the questions in Blockchain list 5 point for all of the followi.docx
 
Answer the following questions- Question 1- You have implemented a sec.docx
Answer the following questions- Question 1- You have implemented a sec.docxAnswer the following questions- Question 1- You have implemented a sec.docx
Answer the following questions- Question 1- You have implemented a sec.docx
 
Answer the following based on a Blockchain and Biometrics I- What t.docx
Answer the following based on a Blockchain and Biometrics    I- What t.docxAnswer the following based on a Blockchain and Biometrics    I- What t.docx
Answer the following based on a Blockchain and Biometrics I- What t.docx
 
Anglin Company- a manufacturing firred has supplicd the following info.docx
Anglin Company- a manufacturing firred has supplicd the following info.docxAnglin Company- a manufacturing firred has supplicd the following info.docx
Anglin Company- a manufacturing firred has supplicd the following info.docx
 
Angela Lopez owns and manages a consulting firm called Metrixi which b.docx
Angela Lopez owns and manages a consulting firm called Metrixi which b.docxAngela Lopez owns and manages a consulting firm called Metrixi which b.docx
Angela Lopez owns and manages a consulting firm called Metrixi which b.docx
 
Andrew finds that on his way to work his wait time for the bus is roug.docx
Andrew finds that on his way to work his wait time for the bus is roug.docxAndrew finds that on his way to work his wait time for the bus is roug.docx
Andrew finds that on his way to work his wait time for the bus is roug.docx
 

Dernier

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 

Dernier (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 

In this programming assignment you are required to write a tail-recurs.docx

  • 1. In this programming assignment you are required to write a tail-recursive method that prints every alternate integer between two specific integers. Programming: The main() program should prompt the user to enter the beginning and the end number of the range. It then displays every 3rd integer starling from the beginning number. The end number may or may not be displayed. For example: if user specifics 2 and 10, then the program displays 2, 5, 8. The next number is 11 that is beyond the last number, so it is not displayed. [Method must be tail-recursive] Use appropriate print statements indicating the method call point and return of the answer to prove the tail-recursive nature of the method. A sample output with initial input 2 and 10 could be as follows: Recursive call not 1 Method call parameters: low = 2 and high = 10 Method returns: 2 Recursive call no: 2 Method call parameters: low = 5 and high = 10 Method returns: 5 Recursive call no: 3 Method call parameters: low = 8 and high = 10 Method returns: 8 Tail-recursion ends. Program terminates. Runtime errors should be handled properly. For example, if the user types a non-numeric character or hits enter without typing a number etc. Solution import java.util.Scanner; public class Tailrecursive {//class starts static int cnt = 0;//counter public static void tail_recursive(int b, int e)//takes two parameters begining integer and ending integer { if (b <= e) { System.out.println(" Recursive call no: " + (cnt + 1)); System.out.println(" Method call parameters: low = " + b + " and high = " + e + " method returns: " + b); cnt++; tail_recursive(b + 3, e);//recursive calling } } public static void main(String[] args) throws Exception { Scanner s = new Scanner(System.in);//takes input from console System.out.println("Enter the beginning integer");
  • 2. int b = s.nextInt(); System.out.println("Enter the ending integer"); int e = s.nextInt(); tail_recursive(b, e);//function calling System.out.println("Tail recursion ends. program terminates."); } } output: run: Enter the beginning integer 2 Enter the ending integer 10 Recursive call no: 1 Method call parameters: low = 2 and high = 10 method returns: 2 Recursive call no: 2 Method call parameters: low = 5 and high = 10 method returns: 5 Recursive call no: 3 Method call parameters: low = 8 and high = 10 method returns: 8 Tail recursion ends. program terminates. BUILD SUCCESSFUL (total time: 5 seconds)