C 1 Int Params Define a method that takes a params int Insid

C#

1. Int Params

Define a method that takes a params int[].

Inside the method, print out the count of integers and the sum of the integers.

From Main, call the method with no arguments, again with 1 number, and again with 4 numbers.

2. Recursion

Create a recursive method that calculates the sum of integers larger than 5.

For example S(10) => 10 + 9 + 8 + 7 + 6 = 40

Call the method from main and print the result.

Solution

1)


using System;

class Program

{

static int sum1(int[] arr)

{

int sum=0;

for(int i=0;i<arr.size();i++)

{

sum=sum+arr[i];

}

return sum;

}

static void Main() {
sum1( {12,3});

}

2)
using System;

class Program {

static int sum(int n) {

if(n==5)return n;

if(n>5)

return n+sum(n-1);

}

static void Main() {

int total = sum(10);

Console.WriteLine(total);

}

}

C# 1. Int Params Define a method that takes a params int[]. Inside the method, print out the count of integers and the sum of the integers. From Main, call the
C# 1. Int Params Define a method that takes a params int[]. Inside the method, print out the count of integers and the sum of the integers. From Main, call the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site