1. Home
  2. Docs
  3. C# Programming
  4. Question
  5. Basic Questions

Basic Questions

1. Write a program to print “Hello World” and exit on key press.

using System;

namespace ConsoleApplication
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
            Console.ReadKey();
        }
    }    
}

2. Write a program to print the sum of 2 numbers taken as input from user.

using System;

namespace ConsoleApplication
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Enter the first number");
            var number1 = Convert.ToSingle(Console.ReadLine());

            Console.WriteLine("Enter the first number");
            var number2 = Convert.ToSingle(Console.ReadLine());

            var sum = number1 + number2;
            Console.WriteLine("Sum : {0}", sum);

            Console.ReadKey();
        }
    }    
}

3. Write a C# Sharp program to print the result (quotient and remainder) of dividing two numbers

using System;

namespace ConsoleApplication
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Enter the first number");
            var number1 = Convert.ToSingle(Console.ReadLine());

            Console.WriteLine("Enter the first number");
            var number2 = Convert.ToSingle(Console.ReadLine());

            var quotient = number1 / number2;
            var remainder = number1 % number2;

            Console.WriteLine("Quotient : {0}", quotient);
            Console.WriteLine("Remainder : {0}", quotient);

            Console.ReadKey();
        }
    }    
}

4. Write a C# Sharp program to swap two numbers

Solution 1 : Using a third variable

using System;

namespace ConsoleApplication
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Enter the first number");
            var number1 = Convert.ToSingle(Console.ReadLine());

            Console.WriteLine("Enter the first number");
            var number2 = Convert.ToSingle(Console.ReadLine());

            Console.WriteLine("Before Swapping");
            Console.WriteLine("Number 1 : {0}", number1);
            Console.WriteLine("Number 2 : {0}", number2);
            
            var temp = number1 ;
            number1 = number2;
            number2 = temp;

            Console.WriteLine("After Swapping");
            Console.WriteLine("Number 1 : {0}", number1);
            Console.WriteLine("Number 2 : {0}", number2);

            Console.ReadKey();
        }
    }    
}

Solution 2: Without use of third variable

using System;

namespace ConsoleApplication
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Enter the first number");
            var number1 = Convert.ToSingle(Console.ReadLine());

            Console.WriteLine("Enter the first number");
            var number2 = Convert.ToSingle(Console.ReadLine());

            Console.WriteLine("Before Swapping");
            Console.WriteLine("Number 1 : {0}", number1);
            Console.WriteLine("Number 2 : {0}", number2);

            number1 = number1 + number2;
            number2 = number1 - number2;
            number1 = number1 - number2;

            Console.WriteLine("After Swapping");
            Console.WriteLine("Number 1 : {0}", number1);
            Console.WriteLine("Number 2 : {0}", number2);

            Console.ReadKey();
        }
    }    
}


Was this article helpful to you? Yes No

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *