Recursion in C#

Decided to try my hand at writing a recursive function today, and was able to do so, and have it work without a lot of work.

using System;

namespace Recursion
{
    class Program
    {
        private static void recursiveLoop(int startNumber,int endNumber)
        {
        if (startNumber <= endNumber)
            {
                Console.WriteLine(startNumber);
                startNumber++;
                recursiveLoop(startNumber,endNumber);
            }
        }
        static void Main(string[] args)
        {
            recursiveLoop(1,10000);
        }
    }
}
Code language: C# (cs)

C# Methods (functions) in separate files

Decided to sit down tonight and work on getting used to working with methods in C#, and how to work with them in external files, to help organize the code. Need to be comfortable doing so for when I start doing more OOP style stuff with classes and such.

//Program.cs

using System;

namespace TryingStuff2
{
    class Program
    {
        static void Main(string[] args)
        {
            int numberOne = 5;
            int numberTwo = 7;

            Console.WriteLine(MathFunctions.AddNumbers(numberOne, numberTwo));
            Console.WriteLine(MathFunctions.SubNumbers(numberOne, numberTwo));
            Console.WriteLine(MathFunctions.MulNumbers(numberOne, numberTwo));
            Console.WriteLine(MathFunctions.DivNumbers(numberOne, numberTwo));
        }
    }
}
Code language: C# (cs)
//MathFunctions.cs

namespace TryingStuff2
{
    public class MathFunctions
    {
        public static int AddNumbers(int numberOne, int numberTwo)
        {
            return (numberOne + numberTwo);
        }

        public static int SubNumbers(int numberOne, int numberTwo)
        {
            return (numberOne - numberTwo);
        }
        public static int MulNumbers(int numberOne, int numberTwo)
        {
            return (numberOne * numberTwo);
        }

        public static int DivNumbers(int numberOne, int numberTwo)
        {
            return (numberOne / numberTwo);
        }
    }
}
Code language: C# (cs)

Porting from Python to C#

Decided to work on porting at least part of the Python program I had done with the video on Youtube into C#, and am really happy with getting the original core part working.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TryingStuff1
{
    class Program
    {
        static void Main(string[] args)
        {
            string user_input= "";

            while (user_input != "quit")
            {
                int calculation_to_units = 24;
                string name_of_unit = "hours";
                
                Console.Write("Enter the number of days.  Enter 'quit' to exit the program: ");
                user_input = Console.ReadLine();
                Console.Clear();
                if (user_input.ToLower() == "quit")
                {
                    System.Environment.Exit(0);
                }
                else if (int.TryParse(user_input, out int number))
                {
                    int user_input_int;
                    if (int.Parse(user_input) <= 0)
                    {
                        Console.WriteLine("Please enter a positive integer.");
                    }
                    else
                    {
                        user_input_int = int.Parse(user_input);
                        int total_time = user_input_int * calculation_to_units;
                        string conversion_output = String.Format("{0} days are {1} {2}", user_input_int, total_time, name_of_unit);
                        Console.WriteLine(conversion_output);
                        continue;
                    }
                }
                else
                    Console.WriteLine("Please enter an integer or 'quit'.");
            }
        }
    }
}

Code language: C# (cs)

The C# code does not have the functions like the Python code, or the sets being used, but is still doing almost all of the same work.

#! /bin/python

calculation_to_units = 24
name_of_unit = "hours"

def days_to_units(number_of_days):
    if number_of_days > 0:
        return f"{number_of_days} days are {number_of_days * calculation_to_units} {name_of_unit}"
    else:
        return "You did not enter a positive integer..."

def validate_and_execute(validated_days):
    try:
        print(days_to_units(int(validated_days)))
    except ValueError:
        print("Please enter a positive integer value...")

user_input = 0
while user_input != "quit":
    user_input = input("""Enter the number of days (comma separated lists are accepted (e.g. 10, 20, 30).\nEnter 'quit' to exit the program: """)
    if user_input.lower() == "quit":
        quit()
    list_of_days = user_input.split(", ")
    for num_of_days in set(list_of_days):
        validate_and_execute(num_of_days)
Code language: Python (python)

More learning the differences…

Decided to start doing small projects to see the differences in how C, C++, C# and Python handle doing the same thing. Started off with a simple enter your name, and then print Hello and the name, since that is one of the more basic things that everyone should know how to do.

// C
#include <stdio.h>
int main()
{
    char name[40];
    printf("Enter your name please: ");
    scanf("%s",name);
    printf("Hello %s.\n", name);
    return 0;
}
Code language: C++ (cpp)
// C++
#include <iostream>
using namespace std;

int main() 
{
    string name;
    cout << "Enter your name please: ";
    cin >> name;
    cout << "Hello " << name << ".\n";
    return 0;
}
Code language: C++ (cpp)
// C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace print_enter
{
    class Program
    {
        static void Main(string[] args)
        {
            string name;
            Console.Write("Enter your name please: ");
            name = Console.ReadLine();
            Console.WriteLine("Hello {0}.\n", name);
        }
    }
}
Code language: C# (cs)
# Python
username = input("Enter your name please: ")
print("Hello " + username + ".")
Code language: Python (python)