In: Computer Science
IN C# WITH SCREENSHOTS OF THE CODE
RECURSION
Objectives
• Learn the basics of recursion.
Background
There are many problems that loops simplify, such as displaying
every pixel to a screen or
receiving repetitive input. However, some situations that can be
simplified with looping are not
easily solvable using loops. This includes problems that require
back tracking and being able to
use information from previous iterations, which would normally be
lost when using an iterative
loop. In those cases, it is much easier to use recursion to
logically solve the problem and it may
even reduce the amount of code that needs to be written.
Submission Guidelines:
You will turn in a Program code – 2 program files (one for each lab
problem)
Tasks
This lab has two parts:
1. Write a recursive method CalculateExponent( ) that takes in
two integer parameters, x
and y, and implements xy (x raised to the power y), where x and y
are integers and y > 0.
Write a driver program that calls this method from the Main
program.
2. Write a recursive method IntegerMultipy ( ) that takes in two
integer parameters i, j, and
implements i * j (integer multiplication), where i > 0. Define
the multiplication process in
terms of integer addition. For example, 4 * 7 is equal to 7 added
to itself 4 times. Write a
driver program that calls this method from the Main program.
Sample Output:
LAB 1:
Please enter a value for x
4
Please enter a value for y
2
Page 2 of 2
16
// Your program should check if y is a positive, non-zero
integer.
LAB 2:
Please enter a value for i
4
Please enter a value for j
7
28
Grading:
● 100 %: Attempted lab and submitted fully functioning lab
exercises with complete
headers and clear I/O statements and
● 95 %: Attempted lab and submitted fully functioning lab exercises
but incomplete
headers and/or unclear I/O statements before due date.
● 90%: All but one item are correct
● 80%: At least two more items are correct
● 70%: Program compiles and methods are correct
● 0%: Did not attempt lab or did not submit it before the due
date
Lab1 program:
using System;
public class Program
{
//recursive function to calculate Exponent values
private static int CalculateExponent( int x, int y){
if (y>0){
return x* CalculateExponent(x, y-1);
}else
return 1;
}
//Driver code
public static void Main()
{
Console.WriteLine("Please enter value of x:");
int x,y;
string input = Console.ReadLine();
Int32.TryParse(input, out x);
Console.WriteLine("Please enter value of y:");
input = Console.ReadLine();
Int32.TryParse(input, out y);
Console.Write("x^y=");
Console.WriteLine(CalculateExponent(x,y));
}
}
Lab 1 program output:
Lab 2 program:
using System;
public class Program
{
//recursive function to calculate multiplication
private static int IntegerMultiply(int i, int j){
if(i>0 && j>0){
return i + IntegerMultiply(i,j-1);
}
else
return 0;
}
//Driver code
public static void Main()
{
Console.WriteLine("Please enter value of i:");
int i,j;
string input = Console.ReadLine();
Int32.TryParse(input, out i);
Console.WriteLine("Please enter value of j:");
input = Console.ReadLine();
Int32.TryParse(input, out j);
Console.Write("i*j=");
Console.WriteLine(IntegerMultiply(i,j));
}
}
Lab 2 program output: