Function Multiply & Multiplyr - C# Programming Exercise

In this exercise in C#, you will need to write two functions called "Multiply" and "MultiplyR" to calculate the product of two numbers using sums. The first version must be iterative, and the second one must be recursive. This exercise will help you understand how multiplication can be performed through repeated addition, and also understand the differences between iterative and recursive solutions in C#. By completing this exercise, you will gain experience in how to apply loops in one version and manage recursive calls in the other. The iterative version will use a loop to perform the repetitive multiplication, while the recursive version will rely on function calls to achieve the same result, allowing you to dive deeper into the use of recursion in programming. This exercise will also teach you how to properly handle input and output parameters in C#.

By completing this exercise, you will be able to implement and compare iterative and recursive solutions to solve mathematical problems in your C# projects.

 Category

Functions

 Exercise

Function Multiply & Multiplyr

 Objective

Write two C# functions, Multiply and MultiplyR, to calculate the product of two numbers using sums. T he first version must be iterative, and the second one must be recursive.

 Write Your C# Exercise

// Import the System namespace to use basic classes like Console
using System;

class Program
{
    // Function to multiply two numbers iteratively using addition
    public static int Multiply(int a, int b)
    {
        int product = 0;  // Variable to store the product

        // Add 'a' to itself 'b' times to simulate multiplication
        for (int i = 0; i < Math.Abs(b); i++)
        {
            product += a;  // Add 'a' repeatedly
        }

        // If b is negative, the product should be negative
        if (b < 0)
        {
            product = -product;  // Negate the product
        }

        return product;
    }

    // Function to multiply two numbers recursively using addition
    public static int MultiplyR(int a, int b)
    {
        // Base case: if b is 0, the product is 0
        if (b == 0)
        {
            return 0;
        }
        else
        {
            // Recursive case: add 'a' recursively
            return a + MultiplyR(a, b - 1);
        }
    }

    // Main function to test the Multiply and MultiplyR functions
    public static void Main()
    {
        // Test the Multiply function
        Console.WriteLine("Iterative Multiply:");
        Console.WriteLine("3 * 4 = " + Multiply(3, 4));  // Expected output: 12
        Console.WriteLine("-3 * 4 = " + Multiply(-3, 4));  // Expected output: -12
        Console.WriteLine("3 * -4 = " + Multiply(3, -4));  // Expected output: -12
        Console.WriteLine("0 * 4 = " + Multiply(0, 4));  // Expected output: 0

        // Test the MultiplyR function
        Console.WriteLine("\nRecursive Multiply:");
        Console.WriteLine("3 * 4 = " + MultiplyR(3, 4));  // Expected output: 12
        Console.WriteLine("-3 * 4 = " + MultiplyR(-3, 4));  // Expected output: -12
        Console.WriteLine("3 * -4 = " + MultiplyR(3, -4));  // Expected output: -12
        Console.WriteLine("0 * 4 = " + MultiplyR(0, 4));  // Expected output: 0
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Functions: greeting + farewell

    In this C# exercise, you are asked to write a program where the main structure is the Main method. Inside this method, two functions should be called: SayHello...

  •  Function with parameters

    In this C# exercise, you are asked to write a program where the Main method must be like this:public static void Main(){ SayHello("John"...

  •  Function returning a value

    In this C# exercise, you are asked to write a program where the Main method must be like this:public static void Main(){ int x = 3;

  •  Function returning a value V2

    In this C# exercise, you are asked to write a program where the Main method must be like this:public static void Main(){ Console.WriteLi...

  •  Function write centered

    In this C# exercise, you are asked to write a function that displays the given text centered on the screen (assuming a screen width of 80 characters). The code should...

  •  Function write underlined

    In this C# exercise, you are asked to write a function that displays the given text centered on the screen (assuming a screen width of 80 characters) and then underli...