Vous êtes sur la page 1sur 7

/*

* C# Program to Perform Binary to Decimal Conversion

*/

using System;

using System.Collections.Generic;

using System.Text;

namespace Program

class Program

static void Main(string[] args)

int num, binary_val, decimal_val = 0, base_val = 1, rem;

Console.Write("Enter a Binary Number(1s and 0s) : ");

num = int.Parse(Console.ReadLine()); /* maximum five digits */

binary_val = num;

while (num > 0)

rem = num % 10;

decimal_val = decimal_val + rem * base_val;

num = num / 10 ;

base_val = base_val * 2;

Console.Write("The Binary Number is : "+binary_val);

Console.Write("\nIts Decimal Equivalent is : "+decimal_val);

Console.ReadLine();

}
-----------------------------------------------------------------------------
-----------------------------
/*

* C# Program to Convert Decimal to Binary

*/

using System;

class myclass

static void Main()

int num;

Console.Write("Enter a Number : ");

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

int quot;

string rem = "";

while (num >= 1)

quot = num / 2;

rem += (num % 2).ToString();

num = quot;

string bin = "";

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

bin = bin + rem[i];

Console.WriteLine("The Binary format for given number is {0}", bin);

Console.Read();

}
-----------------------------------------------------------------------------
-----------------------------
/*

* C# Program to Convert Decimal to Octal

*/

using System;

class program

public static void Main()

int decimalNumber, quotient, i = 1, j;

int[] octalNumber = new int[100];

Console.WriteLine("Enter a Decimal Number :");

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

quotient = decimalNumber;

while (quotient != 0)

octalNumber[i++] = quotient % 8;

quotient = quotient / 8;

Console.Write("Equivalent Octal Number is ");

for (j = i - 1; j > 0; j--)

Console.Write(octalNumber[j]);

Console.Read();

}
-----------------------------------------------------------------------------
-----------------------------
/*

* C# Program to Perform Celsius to Fahrenheit Conversion

*/

using System;

using System.Collections.Generic;
using System.Linq;

using System.Text;

namespace program

class Program

static void Main(string[] args)

int celsius, faren;

Console.WriteLine("Enter the Temperature in Celsius(°C) : ");

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

faren = (celsius * 9) / 5 + 32;

Console.WriteLine("0Temperature in Fahrenheit is(°F) : " + faren);

Console.ReadLine();

}
-----------------------------------------------------------------------------
-----------------------------
/*

* C# Program to Convert Fahrenheit to Celsius

*/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Celsius

{
class Program

static void Main(string[] args)

double celsius;

Console.Write("Enter Fahrenheit temperature : ");

double fahrenheit = Convert.ToDouble(Console.ReadLine());

celsius = (fahrenheit - 32) * 5 / 9;

Console.WriteLine("The converted Celsius temperature is" +


celsius);

Console.ReadLine();

}
-----------------------------------------------------------------------------
-----------------------------
/*

* C# Program to Create a Gray Code

*/

using System;

public class Gray

public static ulong grayEncode(ulong n)

return n ^ (n >> 1);

public static ulong grayDecode(ulong n)

ulong i = 1 << 8 * 64 - 2; //long is 64-bit

ulong p, b = p = n & i;
while ((i >>= 1) > 0)

b |= p = n & i ^ p >> 1;

return b;

public static void Main(string[] args)

Console.WriteLine("Number\tGray");

for (ulong i = 0; i < 10; i++)

Console.WriteLine(string.Format("{0}\t{1}", i,
Convert.ToString((long)grayEncode(i), 2)));

Console.Read();

}
-----------------------------------------------------------------------------
-----------------------------
/*

* C# Program to Obtain the Character from the User and Convert the Case of the
Character

*/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace casechange

class Program

static void Main(string[] args)


{

char a;

int i;

Console.WriteLine("Enter the Character : ");

a = Convert.ToChar(Console.ReadLine());

i=(int)a;

if (a >= 65 && a <= 90)

Console.WriteLine("The Character is : {0}", char.ToLower(a));

else if (a >= 97 && a <= 122)

Console.WriteLine("The Character is : {0}", char.ToUpper(a));

Console.ReadLine();

Vous aimerez peut-être aussi