Comparison of Iteration and Recursion in C#

Verified

Added on  2023/06/14

|5
|903
|113
AI Summary
This article compares the implementation of iteration and recursion in C# with examples and test results. It includes code snippets for both methods and explains the differences between them. The test results for each method are also provided with sample inputs. The subject is computer programming, and the course code is B7IT127. The article is relevant for students studying computer science or programming courses in any college or university.
tabler-icon-diamond-filled.svg

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
Assessment 1
Iteration Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace B7IT127Ass1
{
class Program
{
static void Main(string[] args)
{
Int32 n, x, d,i,sum; //variable declaration
try
{
Console.Write("Enter the value of n : "); //get the value
of n, x and d from keyboard
n = Int32.Parse(Console.ReadLine());
Console.Write("\nEnter the value of x : ");
x = Int32.Parse(Console.ReadLine());
Console.Write("\nEnter the value of d : ");
d = Int32.Parse(Console.ReadLine());
Console.WriteLine();
for(i=1; i<=n;i++) //loop starts
{
sum=x+ d * (i-1); //find the value
of sum (series of number)
Console.WriteLine("A("+i+") = "+sum ); //print the
result
}
Console.WriteLine();
Console.ReadKey(); //wait for user press any key
to exit
}
catch(FormatException fex) //if any input data type
error found, it catches and print the error message
{
Console.WriteLine("Error : " + fex.Message);
}
catch (Exception ex) //if any error found in the process
or input, it catches and print the error message
{
Console.WriteLine("Error : " + ex.Message);
}
}
}
}
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
Document Page
Test Results:
If n = 6, x = 3 and d = 4
Explanation:
If n = 6, x = 3 and d = 4
Iteration i Iterative
Condition (i < n)
Sum or A(i)
1 1 1 < 6 (true) 3 + 4 (1-1)=3
2 2 2 < 6 (true) 3 + 4 (2-1)=7
3 3 3 < 6 (true) 3 + 4 (3-1)=11
4 4 4 < 6 (true) 3 + 4 (4-1)=15
5 5 5 < 6 (true) 3 + 4 (5-1)=19
6 6 6 < 6 (true) 3 + 4 (6-1)=23
7 7 7 < 6 (false), loop
ends
Document Page
Recursion Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace B7IT127Ass1Recur
{
class Program
{
static void Main(string[] args)
{
Int32 n, x, d; //variable declaration
try
{
Console.Write("Enter the value of n : "); //get the value
of n, x and d from keyboard
n = Int32.Parse(Console.ReadLine());
Console.Write("\nEnter the value of x : ");
x = Int32.Parse(Console.ReadLine());
Console.Write("\nEnter the value of d : ");
d = Int32.Parse(Console.ReadLine());
Console.WriteLine();
recurSequence(n, x, d, 1); //call the recursion function
Console.WriteLine();
Console.ReadKey(); //wait for user press any key
to exit
}
catch (FormatException fex) //if any input data type
error found, it catches and print the error message
{
Console.WriteLine("Error : " + fex.Message);
}
catch (Exception ex) //if any error found in the process
or input, it catches and print the error message
{
Console.WriteLine("Error : " + ex.Message);
}
}
public static void recurSequence(int n, int x, int d,int i)
//recursion function starts
{
int sum;
if((n-i)<0) //if the series exceed the stop value,
exit from the recursion
{
return ;
}
sum = x + d * (i - 1); //find the value
of sum (series of number)
Console.WriteLine("A(" + i + ") = " + sum); //print the
result
recurSequence(n, x, d,++i);
}
}
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
}
Test Results:
If n = 5, x = 6 and d = 3
Explanation:
If n = 5, x = 6 and d = 3
Iteration Recursion function
(recurSequence(n,x,d,i)
i Iterative Condition ((n-i) <0) Sum or A (i)
1 recurSequence(5,6,3,1) 1 1 < 6 (true) 6 + 3 (1-1)=6
2 recurSequence(5,6,3,2) 2 2 < 6 (true) 6 + 3 (2-1)=9
3 recurSequence(5,6,3,3) 3 3 < 6 (true) 6 + 3 (3-1)=12
4 recurSequence(5,6,3,4) 4 4 < 6 (true) 6 + 3 (4-1)=15
5 recurSequence(5,6,3,5) 5 5 < 6 (true) 6 + 3 (5-1)=18
6 recurSequence(5,6,3,6) 6 6 < 5 (false), loop ends
chevron_up_icon
1 out of 5
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]