Sunday, 27 September 2020

Deployment Manager and Monitoring in Google Cloud Platform

In this article, we will learn How to use the Deployment Manager to deploy VM Instance as well as monitor them in the Google Cloud Platform. If you are new to this series of Google Cloud Platform, I will recommend you to go through the below articles of this series:


Saturday, 19 September 2020

Program to find the HCF of two numbers in C#

In this blog, we will learn How to find the HCF of two numbers in C#. If you are looking for Programs asked in C# Interview, check the below link:


HCF Stands for Highest Common Factor. It is also called the Greatest Common Measure (GCM) and Greatest Common Divisor (GCD). For finding the HCF, we need to find the greatest number which divides two or more numbers.


Check the below C# program, for calculating the HCF of two numbers in C#. I have mentioned comments as well for easy understanding.

int num1, num2, loopCheck, hcf=0;

//Prompt user to add two number for which HCF needs to find

Console.WriteLine("Enter First Number");

num1 = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter Second Number");

num2 = Convert.ToInt32(Console.ReadLine());

//set the min value out of the numbers entered by the user

loopCheck = num2 > num1 ? num1 : num2;

//Loop the data from 1 to the loopCheck value

for (int i = 1; i <= loopCheck; i++)

{

    //If Modulus of both number will be Zero than store it in hcf variable

    if (num1 % i == 0 && num2 % i == 0)

    {

        hcf = i;

    }

}

//Show the output to the User

Console.Write("HCF of {0} and {1} is: {2}", num1, num2, hcf);

//Console.ReadLine() t

Output:

I hope this will help you with your interview preparation or exam.
Thanks

Monday, 14 September 2020

Program to check a number is Palindrome in C#

In this blog, we will learn How to check a number is Palindrome or not in C#. This Is also one of the most popular interview questions among the interviewers. The basic approach is to reverse the number and then compare it with the number added by the user. 

Sunday, 13 September 2020

Program to check a string is Palindrome in C#

In this blog, we will learn How to write a program to check whether a String is a Palindrome or not in C#. A palindrome is a word or phrase or a sequence of characters that reads the same either read from backward or as in forwards e.g. Malayalam, Madam, etc. 

It is also one of the popular and favorite programs asked during the interview/exam. There are multiple ways by which we can check either string is palindrome or not, Out of which two are mentioned here in this blog.
1. Iterate through each character and generate the reverse string
2. Break string to array and reverse the string

1. Iterate through each character and generate reverse string:
In this method, we start reading the character from the last character of the string and store it in another variable as shown in the below image. Once the string is reversed, compare it with the original string to find that string entered by the user is Palindrome or not.
Program in C#:

Console.WriteLine("Enter a string to check either it's Palindrome or not");

string statement = Console.ReadLine();

//variable to hold the reversed statement

string reverseStatement = "";

//Reverse the statement

for (int i=statement.Length-1;i>=0;i--) {

    reverseStatement += statement[i];

}

//Check statement and reverse statement are equal

if (statement.Equals(reverseStatement, StringComparison.OrdinalIgnoreCase))

{

    Console.WriteLine("'{0}' is an example of Palindrome", statement);

}

else {

    Console.WriteLine("'{0}' is not an example of Palindrome", statement);

}

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

Console.ReadLine();

Output:


2. Break string to array and reverse the string
Another and easiest way to check the palindrome is to convert the string to the char array and then reverse the array with Array.Reverse() method. Once the array is reversed, convert it back to the string as shown in the below example. Once the string is reversed, compare it (ignore case while comparing string) with the original string entered by the user.
Program in C#:

Console.WriteLine("Enter a string to check either it's Palindrome or not");

string statement = Console.ReadLine();

//variable to hold the reversed statement

string reverseStatement = "";

//Convert the string to the Char Array

Char[] charArr = statement.ToCharArray();

//Reverse the Array

Array.Reverse(charArr);

//Convert the character array to string

reverseStatement = new string(charArr);

//Check statement and reverse statement are equal

if (statement.Equals(reverseStatement, StringComparison.OrdinalIgnoreCase))

{

    Console.WriteLine("'{0}' is an example of Palindrome", statement);

}

else

{

    Console.WriteLine("'{0}' is not an example of Palindrome", statement);

}

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

Console.ReadLine();

Preview:

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


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



Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Subscribe us on YouTube

Like us on Facebook

Follow us on Google+