C Format Display of Format like C
program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cformatdisplay
{
class Program
{
static void Main(string[] args)
{
int a = 10;
double b = 45.76;
char c = 'A';
Console.WriteLine("Integer
value is {2} \nAgain {2}", c, b, a);
}
}
}
13 Numeric Format Specifier
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace numericformatspecifiers
{
class Program
{
static void Main(string[] args)
{
double rate = 1.29876;
int a = 123;
double b = 123456754.678;
Console.WriteLine("The
value is {0:######.##}", a);
Console.WriteLine("The
value is {0:0###.##}", a);
Console.WriteLine("The
value is {0:#######.##}", b);
Console.WriteLine("The
value is {0:#######.00} {1:#######.00}", a,b);
Console.WriteLine("The
value is {0:#,####,#####.##}", b);
Console.WriteLine("The
value is {0:c}", b);
Console.WriteLine("The
value is {0:p}", rate);
}
}
}
14 Explicit Data Conversion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace explicitdataconversion
{
class Program
{
static void Main(string[] args)
{
Double a,
b, c;
Console.Write("Enter
No 1 ");
a = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter
No 2 ");
b = Convert.ToDouble(Console.ReadLine());
c = a + b;
Console.WriteLine("The
Sum is " + c);
}
}
}
15 DateTime Data Type
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace datedatatype
{
class Program
{
static void Main(string[] args)
{
DateTime curr,
dob;
TimeSpan diff;
curr = DateTime.Now;
dob = new
DateTime(1987, 10, 5,
12, 0, 0);
diff = curr - dob;
Console.WriteLine("Age
is {0:##}", diff.TotalDays / 365);
}
}
}
16 Ternary Operator Use
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ternaryopuse
{
class Program
{
static void Main(string[] args)
{
int a, b, c,
d;
a = 10;
b = 20;
c = 30;
d = (a < b) ?
((a < c) ? a : c) : ((b <
c) ? b : c);
Console.WriteLine(d);
d = (a < b) ?
b - a : a - b;
Console.WriteLine(d);
d = ((a < b) ?
(b - a) : (a - b)) - 20
+ 45;
Console.WriteLine(d);
if ((a < b)
? a == 10 : b == 45)
{
Console.WriteLine("Within
IF");
}
}
}
}
17 Nullable Data Type
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace nullabledatatype
{
class Program
{
static void Main(string[] args)
{
int? age;
age = 10;
age = null;
if(age==null)
Console.WriteLine("Age
not applicable");
Console.WriteLine(age);
}
}
}
18 Date Time Properties
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace datetimeproperties
{
class Program
{
static void Main(string[] args)
{
DateTime dt
= DateTime.Now;
Console.WriteLine(dt.Date);
Console.WriteLine(dt.Day);
Console.WriteLine(dt.Month);
Console.WriteLine(dt.Year);
Console.WriteLine(dt.Hour);
Console.WriteLine(dt.Minute);
Console.WriteLine(dt.Second);
Console.WriteLine(dt.Millisecond);
Console.WriteLine(dt.DayOfWeek);
Console.WriteLine(dt.DayOfYear);
Console.WriteLine(dt.ToLongDateString());
Console.WriteLine(dt.ToLongTimeString());
Console.WriteLine(dt.ToShortDateString());
Console.WriteLine(dt.ToShortTimeString());
Console.WriteLine(dt.AddDays(-2));
Console.WriteLine(dt.Millisecond+" "+dt.AddTicks(10000).Millisecond);
}
}
}
19 Date Time Format Specifier
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace datetimeformtsp
{
class Program
{
static void Main(string[] args)
{
DateTime dt
= DateTime.Now;
Console.WriteLine("{0:d}",
dt);
Console.WriteLine("{0:dd}",
dt);
Console.WriteLine("{0:ddd}",
dt);
Console.WriteLine("{0:dddd}",
dt);
Console.WriteLine("{0:m}",
dt);
Console.WriteLine("{0:MM}",
dt);
Console.WriteLine("{0:MMM}",
dt.AddMonths(1));
Console.WriteLine("{0:MMMM}",
dt.AddMonths(1));
Console.WriteLine("{0:hh}",
dt);
Console.WriteLine("{0:HH}",
dt);
Console.WriteLine("{0:mm}",
dt);
Console.WriteLine("{0:ss}",
dt);
}
}
}
20 Static functions of factorial
functions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace statcfunctions
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Factorial
of 6 is {0:####.00}", factorial(6));
}
static int factorial(int num)
{
if (num == 0)
return (1);
else
return (num
* factorial(num - 1));
}
}
}
21 Static Functions of Prime Number
Checking
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace staticfunctionuse2
{
class Program
{
static void Main(string[] args)
{
int num;
Console.Write("Enter
a value ");
num = int.Parse(Console.ReadLine());
if (isPrime(num,2))
{
Console.WriteLine("Sq
of num is " + num * num);
}
else
{
Console.WriteLine("Cube
of num is " + num * num*num);
}
}
private static bool isPrime(int num,int
x)
{
if (x == num)
return (true);
else if (num
% x == 0)
return (false);
else
return (isPrime(num,
x + 1));
}
}
}
No comments:
Post a Comment