Csharp/CSharp Tutorial/Data Type/int Calculation

Материал из .Net Framework эксперт
Версия от 12:18, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Bitwise AND of Integer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter an integer:");
            int myInt = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Bitwise AND of Integer and 10 = {0}", myInt & 10);
        }
    }

Comparing integers

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter an integer:");
            int myInt = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Integer less than 10? {0}", myInt < 10);
            Console.WriteLine("Integer between 0 and 5? {0}",(0 <= myInt) && (myInt <= 5));
        }
    }

Comparing the Prefix and Postfix Increment Operators

class IncrementExample
{
  public static void Main()
  {
      int x;
      x = 1;
      System.Console.WriteLine("{0}, {1}, {2}", x++, x++, x);
      System.Console.WriteLine("{0}, {1}, {2}", ++x, ++x, x);
  }
}

integers and arithmetic operators

class MainClass
{
  public static void Main()
  {
    
    System.Console.WriteLine("10 / 3 = " + 10 / 3);
    System.Console.WriteLine("10 % 3 = " + 10 % 3);
    int intValue1 = 10;
    int intValue2 = 3;
    System.Console.WriteLine("intValue1 / intValue2 = " + intValue1 / intValue2);
    System.Console.WriteLine("intValue1 % intValue2 = " + intValue1 % intValue2);
  }
}
10 / 3 = 3
10 % 3 = 1
intValue1 / intValue2 = 3
intValue1 % intValue2 = 1

int variable assignment

using System;
class RightAssocApp
{
    static void Main(string[] args)
    {
        int a = 1;
        int b = 2;
        int c = 3;
   
        Console.WriteLine("a={0} b={1} c={2}", a, b, c);        
        a = b = c;
        Console.WriteLine("After "a=b=c": a={0} b={1} c={2}", a, b, c);
    }
}

Overflowing an Integer Value

public class Program
{
  public static void Main()
  {
      // int.MaxValue equals 2147483647
      int n = int.MaxValue;
      n = n + 1 ;
      System.Console.WriteLine(n);
  }
}

Using Binary Operators

class Division
{
  static void Main()
  {
      int numerator;
      int denominator;
      int quotient;
      int remainder;
      System.Console.Write("Enter the numerator: ");
      numerator = int.Parse(System.Console.ReadLine());
      System.Console.Write("Enter the denominator: ");
      denominator = int.Parse(System.Console.ReadLine());
      quotient = numerator / denominator;
      remainder = numerator % denominator;
      System.Console.WriteLine("{0} / {1} = {2} with remainder {3}",numerator, denominator, quotient, remainder);
  }
}