Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Wednesday, 9 September 2020

Program to find factorial of a number in C#

In this blog, we are going to learn How to find factorial of a positive number or integer in C#. As we know that we can find the factorial of a number in multiple ways, out of which I am going to share two ways i.e. with and without the use of recursion.

Program to find factorial of a number without using recursion:
In mathematics, factorial is the product of all positive numbers or integers less than or equal to the number.
Program:

int number, factorial;

Console.WriteLine("Enter a number to calculate its Factorial");

//Recieve input from user

number = int.Parse(Console.ReadLine());

//set factorial value as we will start loop from number-1

factorial = number;

//Multiply the factorial value till i becomes 1

for (int i=number-1;i>=1;i--) {

    factorial = factorial * i;

}

//Display the output to the user

Console.WriteLine("Factorial of " + number + " is: " + factorial);

//Console.ReadLine() to hold the screen after the execution of the program

Console.ReadLine();

Output:

Program to find the factorial of a number with the use of recursion:
A recursive function is a function that calls itself inside the function.
Program:

class Program

{

    //CalculateFactorial method

    public int CalculateFactorial(int number)

    {

        //If number becomes it will return 1 and recursion will stop

        if (number == 1)

        {

            return 1;

        }

        else

        {

            //Calling CalculateFactorial method (Recursion)

            return number * CalculateFactorial(number - 1);

        }

    }

   

    static void Main(string[] args)

    {

        int number;

        Console.WriteLine("Enter a number to calculate its Factorial");

        //Recieve input from user

        number = int.Parse(Console.ReadLine());

        if (number > 0)

        {

            Program objProgram = new Program();

            //Calling CalculateFactorial method

            int factorial = objProgram.CalculateFactorial(number);

            //Display the output to the user

            Console.WriteLine("Factorial of " + number + " is: " + factorial);

        }

        else

        {

            Console.WriteLine("Number must be greater than zero");

        }

        //Console.ReadLine() to hold the screen after the execution of the program

        Console.ReadLine();

    }

}

Output:

I hope this blog will help you in cracking your interview/exam.
Thanks. 


Monday, 7 September 2020

Program to find the occurrence of the character in a string in C#

In some interviews or exams, a problem is provided to the interviewee or student to find out the occurrence of the character in a string. In this example, I will help you in solving that problem/program with the easiest approach.

Sunday, 6 September 2020

Program to Swap two numbers in C#

Write a program to Swap two numbers in C#

In many Interviews, the Interviewer asks the Interviewee to write a program to swap two numbers. There are multiple methods/ways to swap numbers in C#, out of which two popular methodologies are Swapping two numbers with third/temporary variable and another is swapping two numbers without using a third/temporary variable (using + and – arithmetic operator). Let’s see both approaches in detail. 


Swap two numbers by using third/temporary variable:
As you can see in the below image, it’s very simple to swap two numbers with a third temporary variable.
Program to swap two numbers by using third/temporary variable in C# is provided below:

int num1, num2, temp;

//Get two number from the user to swap

Console.WriteLine("Enter first number");

num1 = int.Parse(Console.ReadLine());

Console.WriteLine("Enter second number");

num2 = int.Parse(Console.ReadLine());

Console.WriteLine("Values before swapping: num1=" + num1 + " and num2=" + num2);

temp = num1;

num1 = num2;

num2 = temp;

Console.WriteLine("Values after swapping: num1=" + num1 + " and num2=" + num2);

//Console.ReadLine() to hold the screen after the execution of the program

Console.ReadLine();

Output:

Swap two numbers without using any third/temporary variable:
As you can see in below image it’s very easy to swap the two numbers without using third variable. 
Program to swap two numbers without using third/temporary variable is provided below:

int num1, num2;

//Get two number from the user to swap

Console.WriteLine("Enter first number");

num1 = int.Parse(Console.ReadLine());

Console.WriteLine("Enter second number");

num2 = int.Parse(Console.ReadLine());

Console.WriteLine("Values before swapping: num1=" + num1 + " and num2=" + num2);

num1 = num1 + num2;

num2 = num1 - num2;

num1 = num1 - num2;

Console.WriteLine("Values after swapping: num1=" + num1 + " and num2=" + num2);

//Console.ReadLine() to hold the screen after the execution of the program

Console.ReadLine();

Output:
I hope this article will help you in your interview preparation.
Thanks



Saturday, 5 September 2020

Top C# Programs for Interview

As I got several emails regarding the help for the Interview, so, I decided to share the programs that are asked during the interview. These programs also help you in an understanding of the problems/algorithms in the easiest way. I will update the programs every week so stay tuned. You can also write an email (or comment in the comment box) to me for any specific program required.

Top C# Program for Interview:

S.No.C# Interview Programs
1Reverse a Number in C#
2Reverse a string in C#
3FizzBuzz in C#
4Program to find Factor of a number in C#
5Program to Swap two numbers in C#
6Program to find the occurrence of the character in a string in C#
7Program to find factorial of a number in C#
8Program to check a string is Palindrome in C#
9Program to check a number is Palindrome in C#
10Program to find the HCF of two numbers in C#
11Program to generate Floyd’s Triangle in C#
12Program to Count Vowels and Consonants in a string in C#
13Program to check whether the entered year is Leap Year or Not in C#
14Program to remove the duplicate character from the string

I hope these programs will help you in cracking your interview.

Thanks

Program to find Factor of a number in C#

Write a program to find the Factor of a number entered by the user. 

The basic approach to solve this problem is to find the number(s) (Factors) which can divide the number entered by the user with no remainder left i.e. 0. In simple words, a factor divides a number completely without leaving any remainder.

Thursday, 20 August 2020

Working up with Google Kubernetes Engine in Google Cloud Platform

In this article, we will learn How to get started with GKE i.e. Google Kubernetes Engine in Google Cloud Platform. GKE provides the easiest and simplest way to set up the Kubernetes cluster. Kubernetes (K8s) is an Open Source system for automating deployment, scaling, and management of the containerized applications.

Wednesday, 12 August 2020

Getting Started with Cloud SQL in Google Cloud Platform

In this article, we will learn How to get started with Cloud SQL in Google Cloud Platform. We will start with the creation of a VM Instance. Then we create a MySQL DB in the Cloud SQL and access that DB in the Web application hosted in the VM Instance created in the Google Cloud Platform. 

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook