Thursday, 12 February 2015

C# Program - to Check Leap Year or Not

This C# Program Checks Whether the Entered Year is a Leap Year or Not.When A year is divided by 4. If remainder becomes 0 then the year is called a leap year..

namespace Program
    {
        class leapyear
        {
            static void Main(string[] args)
            {
                leapyear obj = new leapyear();
                obj.readdata();
                obj.leap();
            }
            int y;
            public void readdata()
            {
                Console.WriteLine("Enter the Year in Four Digits : ");
                y = Convert.ToInt32(Console.ReadLine());
            }
            public void leap()
            {
                if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
                {
                    Console.WriteLine("{0} is a Leap Year", y);
                }
                else
                {
                    Console.WriteLine("{0} is not a Leap Year", y);
                }
                Console.ReadLine();
            }
        }

     }
Continue Reading →

C# Program - to Check Perfect Number or Not

This C# Program Checks Whether the Entered Number is a Perfect Number or Not. A perfect number is a positive integer that is equal to the sum of its proper divisors.

class Program
    {
        static void Main(string[] args)
        {
            int number, sum = 0, n;
            Console.Write("enter the Number");
            number = int.Parse(Console.ReadLine());
            n = number;
            for (int i = 1; i < number; i++)
            {
                if (number % i == 0)
                {
                    sum = sum + i;
                }
            }
            if (sum == n)
            {
                Console.WriteLine("\n Entered number is a perfect number"); //6
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("\n Entered number is not a perfect number");
                Console.ReadLine();
            }
        }

    }
}


Continue Reading →

C# Program - Armstrong Number

This C# Program is written to Check Whether the Entered Number is an Armstrong Number or Not . An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.

    class programme
    {
        static void Main(string[] args)
        {
            int number, remainder, sum = 0;
            Console.Write("Enter the Number: ");
            number = int.Parse(Console.ReadLine());
            for (int i = number; i > 0; i = i / 10)
            {
                remainder = i % 10;
                sum = sum + remainder * remainder * remainder;

            }
            if (sum == number)
            {
                Console.Write("Entered Number is an Armstrong Number");
            }
            else
                Console.Write("Entered Number is not an Armstrong Number");
            Console.ReadLine();
        }
    }


Continue Reading →

Program to Check Whether a String is Palindrome or not

It contains a program to reverse a string and to check whether a Entered string is palindrome or not.
using System;
namespace palindrome
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string s,revs="";
            
Console.WriteLine(" Enter string");
            s = 
Console.ReadLine();
            
for (int i = s.Length-1; i >=0; i--) //String Reverse
            {
                revs += s[i].ToString();
            }
            
if (revs == s) // Checking whether string is palindrome or not
            {
                
Console.WriteLine("String is Palindrome \n Entered String Was {0} and reverse string is {1}", s, revs);
            }
            
else
            {
                
Console.WriteLine("String is not Palindrome \n Entered String Was {0} and reverse string is {1}", s, revs);
            }
            
Console.ReadKey();
        }
    }
}


Continue Reading →

Coding Test: Prime Numbers in C#

What is a prime number?
A prime number is a number that can only be divided by itself and 1 without remainders.
What are the prime numbers from 1 to 100?
The prime numbers from 1 to 100 are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97.
Why is 1 not a prime number?
1 is not a prime number because it has only one factor, namely 1. Prime numbers need to have exactly two factors.
Why is 2 a prime number?
2 is a prime number because its only factors are 1 and itself.
Is 51 a prime number?
51 is not a prime number because it has 3 and 17 as divisors, as well as itself and 1. In other words, 51 has four factors.

Below is a simple C# program to check if a number is prime.

Prime Number Program in C#

using System;
class Program
{
    static void Main()
    {
        // Get the number from the user
        Console.Write("Enter a number: ");
        int number = Convert.ToInt32(Console.ReadLine());

        // Check if the number is prime
        if (IsPrime(number))
        {
            Console.WriteLine($"{number} is a prime number.");
        }
        else
        {
            Console.WriteLine($"{number} is not a prime number.");
        }
    }

    // Method to check if a number is prime
    static bool IsPrime(int num)
    {
        if (num <= 1) return false; // Numbers less than or equal to 1 are not prime
        if (num == 2) return true;  // 2 is a prime number

        // Check divisibility from 2 to the square root of the number
        for (int i = 2; i <= Math.Sqrt(num); i++)
        {
            if (num % i == 0)
                return false; // If divisible by any number, it's not prime
        }

        return true; // The number is prime
    }
}

Write code to find prime numbers from 1 to 100 
using System;
class Program
{  static void Main(string[] args)
  {
      for (int i = 1; i <= 100; i++)
      {
          // Check if the number is prime
          if (IsPrime(i))
          {
              Console.WriteLine(i);
          }                
      }
  }
// Method to check if a number is prime
    static bool IsPrime(int num)
    {
        if (num <= 1) return false; // Numbers less than or equal to 1 are not prime
        if (num == 2) return true;  // 2 is a prime number

        // Check divisibility from 2 to the square root of the number
        for (int i = 2; i <= Math.Sqrt(num); i++)
        {
            if (num % i == 0)
                return false; // If divisible by any number, it's not prime
        }

        return true; // The number is prime
    }
}
Output:


Continue Reading →

Monday, 9 February 2015

The difference between == and === in jQuery/JavaScript

In this post I would like to explain the difference between double (==) and triple (===) equals in JavaScript/jQuery. A quick and short explanation is that == does type coercion (conversion) before checking for equality; and === does strict equation which requires values to have the same type as well.
Here is what it means:
0 == false     // true
0 === false    // false, because they have different types (int, bool)
1 == "1"       // true
1 === "1"      // false, because they have different types (int, string)
null == undefined // true
null === undefined // false
Detailed (longer) explanation
When you use == the javascript interpreter will convert one side of the comparison according to these rules:
  • When comparing a number and a string, the string is converted to a number value.
  • When comparing a boolean, the boolean is converted to 1 if it is true and +0 if it is false.
  • When object is compared with a number or a string, the object is converted to its’ default value (.valueOf(), .toString()). If these methods are missing a runtime error is thrown.
  • When comparing an object and another object, no type conversion is made. They are equal only if they refer the same object.
When you use === the javascript interpreter will not convert values to primitive types and make sure that the operands have the same value as well as the same type.
"foo" === "foo"  // true, both operands are of type String
new String("foo") === new String("foo")  // false, 2 Objects refer diff object instances
Detailed comparison information can be found on Mozilla Developer Network page. Here is the summary:
  • Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions
  • Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another
  • Two Boolean operands are strictly equal if both are true or both are false
  • Two distinct objects are never equal for either strictly or abstract comparisons
  • An expression comparing Objects is only true if the operands reference the same Object
  • Null and Undefined Types are == (but not ===)
Continue Reading →

Static Members vs Instance Members

Introduction
Static and Instance declaration was something that I found difficult to get my mind around when I first began programming and, after some searching, realized that there are not many places on the Internet that explain it in depth (or at least not to the point that makes any sense!).

Background
I recently had the opportunity to do some Technical Editing for one of Wrox Publishing's upcoming books and had placed quite a lengthy comment in the manuscript related to Static vs Instance members. Unfortunately it was much too long for a side note and, at the suggestion of the author, I have decided to place that comment here, albeit expanded.

Using the Code
This code is functional in the aspect that it gets the point across of using Static and Instance members but it is up to you to expand upon this theory to make the code useful for anything. First, we need to understand the difference between static and instance members:
  1. Static: Static members are shared with all objects of the class.
  2. Instance: Instance members belong to the object that they are instantiated from.
  • In Non Static class, we can also have static constructor, static member and static functions. 
  • In Static class, we can have only static constructor, static member and static functions. 
While this may sound somewhat cryptic, it will all become much clearer with the following code:

public class CloneGenerator

   {
       //the total number of clones that we create with this class
       private static int numberOfClones;

       //each clone will have a name
       private string cloneName;

       //constructor
       public CloneGenerator(string s)
       {
           cloneName = s;
           numberOfClones++;
           return;
       }

       // return this clones name
       public string getName()
       {
           return cloneName;
       }

       //count Clones
       public static int countClones()
       {
           return numberOfClones;
       }

   }

   public class GenerateClones
   {
       public void main()
       {
           //Instantiate our class and create a clone
           CloneGenerator c1 = new CloneGenerator("Bob");
           CloneGenerator c2 = new CloneGenerator("Jon");
           CloneGenerator c3 = new CloneGenerator("Jim");

           //this string will contain the name Bob
           string clone1 = c1.getName();

           //this string will contain the name Jon
           string clone2 = c2.getName();

           //this string will contain the name Jim
           string clone3 = c3.getName();

           //Call the Static function
           //iCloneCount will hold the value of 3 after this assignment
           int iCloneCount = CloneGenerator.countClones();
       }
   }

As you can see, we instantiate three CloneGenerator objects and pass in a string parameter, which contains the clone's name, and then assign those values to local string variables by calling [objectName].getName()which returns the name of that cloneGenerator object. In contrast, we populate the value of iCloneCountby calling our static function directly: CloneGenerator.countClones() which will return 3. 

To further explain, if you were to do something such as this...

string name = CloneGenerator.getName();

... you will receive a run time error to the effect of "Method call requires an instance of an object" since getNameis an instance member. One thing to quickly note is that you CAN access static members inside of Instance members (in the above example, we reference the static member numberOfClones inside the instance member CloneGenerator(string s)) but you can NOT access Instance members inside of static members.

So, in summary, an Instance member is a member that belongs to an Instance of an object (in our example, the objects are c1, c2, and c3) whereas a static member belongs to the class itself and does not require an instance of an object as was demonstrated by making a call to CloneGenerator.countClones().
Continue Reading →

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (55) CD (1) CI (2) CloudComputing (2) Coding (10) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (20) Entity Framework (5) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) jwtToken (4) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives