B7IT127 - C# Program: Iterative and Recursive Arithmetic Sequence

Verified

Added on  2023/06/14

|5
|903
|113
Practical Assignment
AI Summary
This assignment solution provides a C# program that calculates the sum of numbers in an arithmetic sequence using both iterative and recursive methods. The program takes inputs for 'n' (number of terms), 'x' (starting number), and 'd' (common difference). The iterative approach uses a 'for' loop to calculate and print each term in the sequence, while the recursive approach defines a function that calls itself until the desired number of terms is reached. The code includes error handling for invalid input types. Test results and explanations are provided for both the iterative and recursive implementations, demonstrating the step-by-step calculations and the conditions for loop termination and recursion exit. Desklib offers this assignment solution as well as other solved assignments and past papers to aid students in their studies.
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

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
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

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
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]