Question fusing 2 codes?


New member
Nov 30, 2023
Programming Experience
I made these to codes but idk how to fuse them into 1 , would be nice if someone could help me ^^

using System;

class Program
    static double CalculateDistance(double echoTime)
        double speedOfSound = 0.0343;
        double distance = (echoTime * speedOfSound) / 2;
        return distance;

    static double CalculateEchoTime(double distance)
        double speedOfSound = 0.0343;
        double echoTime = (distance * 2) / speedOfSound;
        return echoTime;

    static void Main()
        Console.WriteLine("Ultrasonic Sensor Program");
        Console.WriteLine("1. Calculate distance to object");
        Console.WriteLine("2. Calculate echo time");

        Console.Write("Choose an option (1 or 2): ");
        int choice = Convert.ToInt32(Console.ReadLine());

        if (choice == 1)
            Console.Write("Enter the echo time in microseconds: ");
            double echoTime = Convert.ToDouble(Console.ReadLine());
            double distance = CalculateDistance(echoTime);
            Console.WriteLine($"The distance to the object is {distance} cm.");
        else if (choice == 2)
            Console.Write("Enter the distance to the object in cm: ");
            double distance = Convert.ToDouble(Console.ReadLine());
            double echoTime = CalculateEchoTime(distance);
            Console.WriteLine($"The echo time is {echoTime} microseconds.");
            Console.WriteLine("Invalid choice. Please choose 1 or 2.");


  • Task.txt
    1.5 KB · Views: 11
Last edited by a moderator:
Thank you for attaching the code, but it would have been better if you had just posted the code in your message in code tags. I have updated your original post to show the contents of your attachment.
So what do you exactly mean by fusing them together? Right now it looks like you have a single program. Do you want the output of one method to be fed in as input for the other? Which one will be the input of the other?
i want to transfer the function that te user is only able to put in numbers , and e into the other programm.

would be nice if you can help me with that
You didn't share the code for the "function that te user is only able to put in numbers". Perhaps post that code as text in code tags when you reply?

In general, if you write the method so that it only returns valid numbers, and prompts the user to enter numeric values, then you can call that method from your existing calculation methods.

As an aside, code that C/C++ typically calls "functions" are called "methods" in C#.

Latest posts

Top Bottom