1
Hello World
using System;
namespace HelloWorldApp {
class Hello {
static void Main(string[] args) {
Console.WriteLine("Hello World!");
}
}
}
2
Even or Odd
* C# Program to Check whether the Entered Number is Even or Odd
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace check1
{
class Program
{
static void Main(string[] args)
{
int i;
Console.Write("Enter a Number : ");
i = int.Parse(Console.ReadLine());
if (i % 2 == 0)
{
Console.Write("Entered Number is an Even Number");
Console.Read();
}
else
{
Console.Write("Entered Number is an Odd Number");
Console.Read();
}
}
}
}
3
power exponent value
/*
* C# Program to Caluculate the power exponent value
*/
using System;
using System.Collections.Generic;
using System.Linq;
class program
{
static void Main(string[] args)
{
IEnumerable oddNums =
Enumerable.Range(20, 20).Where(x => x % 2 != 0);
foreach (int n in oddNums)
{
Console.WriteLine(n);
}
Console.ReadLine();
}
}
4
Positive Number
/*
* C# Program to Accept a Number from the user and Display it
* if it is Positive
*/
using System;
class program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
if (number > 0)
{
Console.WriteLine("Number is positive");
}
else if (number == 0)
{
Console.WriteLine("Number is 0");
}
else
{
Console.WriteLine("Number is negative");
}
Console.ReadLine();
}
}
5
Greatest among 2 numbers
/*
* C# Program to Find Greatest among 2 numbers
*/
using System;
class prog
{
public static void Main()
{
int a, b;
Console.WriteLine("Enter the Two Numbers : ");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
if (a > b)
{
Console.WriteLine("{0} is the Greatest Number", a);
}
else
{
Console.WriteLine("{0} is the Greatest Number ", b);
}
Console.ReadLine();
}
}
6
Swap two Numbers
/*
* C# Program to Swap two Numbers
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
int num1, num2, temp;
Console.Write("\nEnter the First Number : ");
num1 = int.Parse(Console.ReadLine());
Console.Write("\nEnter the Second Number : ");
num2 = int.Parse(Console.ReadLine());
temp = num1;
num1 = num2;
num2 = temp;
Console.Write("\nAfter Swapping : ");
Console.Write("\nFirst Number : "+num1);
Console.Write("\nSecond Number : "+num2);
Console.Read();
}
}
}
7
Divisible by 2
/*
* C# Program to Find whether the Number is Divisible by 2
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication16
{
class Program
{
static void Main(string[] args)
{
int n;
Console.WriteLine("Enter the Number :");
n = int.Parse(Console.ReadLine());
if (n % 2 == 0)
{
Console.WriteLine("Entered Number is Divisible by 2 ");
}
else
{
Console.WriteLine("Entered Number is Not Divisible by 2");
}
Console.ReadLine();
}
}
}
8
Multiples of 3 and 5
/*
*C# Program to Print the Sum of all the Multiples of 3 and 5
*/
using System;
class program
{
public static void Main()
{
int a, b, i, Sum = 0;
for (i = 1; i < 100; i++)
{
a = i % 3;
b = i % 5;
if (a == 0 || b == 0)
{
Console.Write("{0}\t", i);
Sum = Sum + i;
}
}
Console.WriteLine("\nThe Sum of all the Multiples of 3 or 5 Below 100 : {0}",
Sum);
Console.Read();
}
}
9
Multiples of 17
/*
* C# Program to Print all the Multiples of 17 which are Less than 100
*/
using System;
class program
{
public static void Main()
{
int a,i;
Console.WriteLine("Multiples of 17 are : ");
for (i = 1; i < 100; i++)
{
a = i % 17;
if (a == 0)
{
Console.WriteLine(i);
}
}
Console.Read();
}
}
10
Sum of the Digits
/*
* C# Program to Get a Number and Display the Sum of the Digits
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
int num, sum = 0, r;
Console.WriteLine("Enter a Number : ");
num = int.Parse(Console.ReadLine());
while (num != 0)
{
r = num % 10;
num = num / 10;
sum = sum + r;
}
Console.WriteLine("Sum of Digits of the Number : "+sum);
Console.ReadLine();
}
}
}
11
Sum of Digits using Recursion
/*
* C# Program to Find Sum of Digits of a Number using Recursion
*/
using System;
class program
{
public static void Main()
{
int num, result;
pro pg = new pro();
Console.WriteLine("Enter the Number : ");
num=int.Parse(Console.ReadLine());
result =pg.sum(num);
Console.WriteLine("Sum of Digits in {0} is {1}", num, result);
Console.ReadLine();
}
}
class pro
{
public int sum(int num)
{
if (num != 0)
{
return (num % 10 + sum(num / 10));
}
else
{
return 0;
}
}
12
Reverse
/*
* C# Program to Get a Number and Display the Number with its Reverse
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
int num, reverse = 0;
Console.WriteLine("Enter a Number : ");
num = int.Parse(Console.ReadLine());
while (num != 0)
{
reverse = reverse * 10;
reverse = reverse + num % 10;
num = num / 10;
}
Console.WriteLine("Reverse of Entered Number is : "+reverse);
Console.ReadLine();
}
}
}
13
Palindrome
/*
* C# Program to Reverse a Number & Check if it is a Palindrome
*/
using System;
class program
{
public static void Main()
{
int num, temp, remainder, reverse = 0;
Console.WriteLine("Enter an integer \n");
num = int.Parse(Console.ReadLine());
temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
Console.WriteLine("Given number is = {0}", temp);
Console.WriteLine("Its reverse is = {0}", reverse);
if (temp == reverse)
Console.WriteLine("Number is a palindrome \n");
else
Console.WriteLine("Number is not a palindrome \n");
Console.ReadLine();
}
}
14
Sum of two Binary Numbers
/*
* C# Program to Find the Sum of two Binary Numbers */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
int b1, b2;
int i = 0, rem = 0;
int[] sum = new int[20];
Console.WriteLine("Enter the first binary number: ");
b1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the second binary number: ");
b2 = int.Parse(Console.ReadLine());
while (b1 != 0 || b2 != 0)
{
sum[i++] = (b1 % 10 + b2 % 10 + rem) % 2;
rem = (b1 % 10 + b2 % 10 + rem) / 2;
b1 = b1 / 10;
b2 = b2 / 10;
}
if (rem != 0)
sum[i++] = rem;
--i;
Console.WriteLine("Sum of two binary numbers: ");
while (i >= 0)
Console.Write("{0}", sum[i--]);
Console.ReadLine();
}
}
}
15
Multiplication of two Binary Numbers
/*
* C# Program to Find Multiplication of two Binary Numbers
*/
using System;
class program
{
public static void Main()
{
int binary1, binary2, multiply = 0;
int digit, factor = 1;
prog pg = new prog();
Console.WriteLine("Enter the first binary number: ");
binary1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the second binary number: ");
binary2 = int.Parse(Console.ReadLine());
while (binary2 != 0)
{
digit = binary2 % 10;
if (digit == 1)
{
binary1 = binary1 * factor;
multiply = pg.binaryproduct(binary1, multiply);
}
else {
binary1 = binary1 * factor;
binary2 = binary2 / 10;
factor = 10;
}
Console.WriteLine("Product of two binary numbers: {0}", multiply);
Console.ReadLine();
}
}
class prog
{
public int binaryproduct(int binary1, int binary2)
{
int i = 0, remainder = 0;
int[] sum = new int[20];
int binaryprod = 0;
while (binary1 != 0 || binary2 != 0)
{
sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (remainder != 0)
sum[i++] = remainder;
--i;
while (i >= 0)
binaryprod = binaryprod * 10 + sum[i--];
return binaryprod;
}
}
16
Arithmetic Operations
/*
* C# Program to Perform all Basic Arithmetic Operations
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
int Num1, Num2, result;
char option;
Console.Write("Enter the First Number : ");
Num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the Second Number : ");
Num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Main Menu");
Console.WriteLine("1. Addition");
Console.WriteLine("2. Subtraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");
Console.Write("Enter the Operation you want to perform : ");
option = Convert.ToChar(Console.ReadLine());
switch (option)
{
case '1':
result = Num1 + Num2;
Console.WriteLine("The result of Addition is : {0}", result);
break;
case '2':
result = Num1 - Num2;
Console.WriteLine("The result of Subtraction is : {0}", result);
break;
case '3':
result = Num1 * Num2;
Console.WriteLine("The result of Multiplication is : {0}", result);
break;
case '4':
result = Num1 / Num2;
Console.WriteLine("The result of Division is : {0}", result);
break;
default:
Console.WriteLine("Invalid Option");
break;
}
Console.ReadLine();
}
}
}
17
Multiplication of Exponents
/*
* C# Program to Perform Multiplication of Exponents of Same Base
*/
using System;
class Program
{
static void Main()
{
Console.WriteLine("Enter the Base : ");
double num = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the First Exponent :");
double exp1 = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the Second Exponent :");
double exp2 = double.Parse(Console.ReadLine());
double mul;
mul = exp1 + exp2;
Console.WriteLine("Result is : {0}^{1} : {2}", num, mul, Math.Pow(num, mul));
Console.ReadLine();
}
}
18
Division of Exponents
/*
* C# Program to Perform Division of Exponents of Same Base
*/
using System;
class Program
{
static void Main()
{
Console.WriteLine("Enter the Base : ");
double num = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the First Exponent :");
double exp1 = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the Second Exponent :");
double exp2 = double.Parse(Console.ReadLine());
double div;
div = exp1 - exp2;
Console.WriteLine("Result is : {0}^{1} : {2}", num, div, Math.Pow(num, div));
Console.ReadLine();
}
}
19
Division of Exponents
/*
* C# Program to Perform Division of Exponents of Same Base
*/
using System;
class Program
{
static void Main()
{
Console.WriteLine("Enter the Base : ");
double num = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the First Exponent :");
double exp1 = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the Second Exponent :");
double exp2 = double.Parse(Console.ReadLine());
double div;
div = exp1 - exp2;
Console.WriteLine("Result is : {0}^{1} : {2}", num, div, Math.Pow(num, div));
Console.ReadLine();
}
}
20
Multiplication Table
/*
* C# Program to Find and display the Multiplication Table
*/
using System;
class Multipication
{
static void Main()
{
int no;
Console.Write("Enter a no : ");
no = Convert.ToInt32(Console.ReadLine());
while (no <= 0)
{
Console.WriteLine("You entered an invalid no");
Console.Write("Enter a no great than 0: ");
no = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Multiplication Table :");
for (int i = 1; i <= no; i++)
{
Console.WriteLine("\n");
for (int j = 1; j <= no; j++)
{
Console.Write("{0,6}", i * j);
}
}
Console.Read();
}
}
21
display the Multiplication Table
/*
* C# Program to Find and display the Multiplication Table
*/
using System;
class Multipication
{
static void Main()
{
int no;
Console.Write("Enter a no : ");
no = Convert.ToInt32(Console.ReadLine());
while (no <= 0)
{
Console.WriteLine("You entered an invalid no");
Console.Write("Enter a no great than 0: ");
no = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Multiplication Table :");
for (int i = 1; i <= no; i++)
{
Console.WriteLine("\n");
for (int j = 1; j <= no; j++)
{
Console.Write("{0,6}", i * j);
}
}
Console.Read();
}
}
22
Convert the Case of the Character
/*
* 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();
}
}
23
Categorize as Tall, Dwarf or Average
/*
* C# Program to Accept the Height of a Person & Categorize as
* Tall, Dwarf or Average
*/
using System;
class program
{
public static void Main()
{
float height;
Console.WriteLine("Enter the Height (in centimeters) \n");
height = int.Parse(Console.ReadLine());
if (height < 150.0)
Console.WriteLine("Dwarf \n");
else if ((height >= 150.0) && (height <= 165.0))
Console.WriteLine(" Average Height \n");
else if ((height >= 165.0) && (height <= 195.0))
Console.WriteLine("Taller \n");
else
Console.WriteLine("Abnormal height \n");
}
}
24
Minimum Range of Values
/*
* C# Program to Find the Minimum Range of Values for Decimal,
* Float and Double Datatype
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace maxdatatype
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("The Minimum Range of the Decimal Data " +
"Type is : {0} ",Decimal.MinValue);
Console.WriteLine("The Minimum Range of the Float Data " +
"Type is : {0} ",Single.MinValue);
Console.WriteLine("The Minimum Range of the Decimal Data " +
"Type is : {0} ",Double.MinValue);
Console.WriteLine("Exponent Form : The Minimum Range of Decimal " +
"Data Type is : {0:E}", Decimal.MinValue);
Console.WriteLine("Exponent Form : The Minimum Range of Float " +
"Data Type is : {0:E}", Single.MinValue);
Console.WriteLine("Exponent Form : The Minimum Range of Double " +
"Data Type is : {0:E}", Double.MinValue);
Console.ReadLine();
}
}
}
25
Maximum Range of Values
/*
* C# Program to Find the Maximum Range of Values for Decimal
* Float and Double Datatype
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace maxdatatype
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("The Maximum Range of the Decimal " +
"Data Type is : {0} ", Decimal.MaxValue);
Console.WriteLine("The Maximum Range of the Float " +
"Data Type is : {0} ", Single.MaxValue);
Console.WriteLine("The Maximum Range of the Decimal " +
"Data Type is : {0} ", Double.MaxValue);
Console.WriteLine("Exponent Form : The Maximum Range of Decimal " +
"Data Type is : {0:E}", Decimal.MaxValue);
Console.WriteLine("Exponent Form : The Maximum Range of Float " +
"Data Type is : {0:E}", Single.MaxValue);
Console.WriteLine("Exponent Form : The Maximum Range of Double " +
"Data Type is : {0:E}", Double.MaxValue);
Console.ReadLine();
}
}
}
26
Basic Arithmetic Operations
/*
* C# Program to Perform all Basic Arithmetic Operations
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
int Num1, Num2, result;
char option;
Console.Write("Enter the First Number : ");
Num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the Second Number : ");
Num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Main Menu");
Console.WriteLine("1. Addition");
Console.WriteLine("2. Subtraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");
Console.Write("Enter the Operation you want to perform : ");
option = Convert.ToChar(Console.ReadLine());
switch (option)
{
case '1':
result = Num1 + Num2;
Console.WriteLine("The result of Addition is : {0}", result);
break;
case '2':
result = Num1 - Num2;
Console.WriteLine("The result of Subtraction is : {0}", result);
break;
case '3':
result = Num1 * Num2;
Console.WriteLine("The result of Multiplication is : {0}", result);
break;
case '4':
result = Num1 / Num2;
Console.WriteLine("The result of Division is : {0}", result);
break;
default:
Console.WriteLine("Invalid Option");
break;
}
Console.ReadLine();
}
}
}
27
Logical Operator
/*
* C# Program to Illustrate the use of Conditional Logical Operator
*/
using System;
public class Program
{
static void Main()
{
int age;
Console.WriteLine("Enter the Age :");
age=int.Parse(Console.ReadLine());
bool adult = age >= 18 ? true : false;
Console.WriteLine("Adult : {0}", adult);
Console.Read();
}
}
28
LeftShift Operations
/*
* C# Program to Illustrate LeftShift Operations
*/
using System;
class sample
{
public static void Main()
{
int x = 1024 * 1024 * 1024;
uint p = 1024 * 1024 * 1024;
int y = -42;
Console.WriteLine("LEFT SHIFT OPERATIONS :");
Console.WriteLine("{0},{1},{2}", x, x * 2, x << 1);
Console.WriteLine("{0},{1},{2}", p, p * 2, p << 1);
Console.WriteLine("{0},{1},{2}", x, x * 4, x << 2);
Console.WriteLine("{0},{1},{2}", p, p * 4, p << 2);
Console.WriteLine("{0},{1},{2}", y, y * 1024 * 1024 * 64, x << 26);
Console.ReadLine();
}
}
29
Boxing Operations
/*
* C# Program to Demonstrate Boxing Operations
*/
using System;
class sample
{
int x = 10;
object obj;
void boxmethod()
{
sample s= new sample();
bool b;
object ob="CSHARP";
b=s.obj is int;
Console.WriteLine(b);
s.obj = x;
b = s.obj is int;
Console.WriteLine("{0},{1},{2}",s.obj,s.x,b);
s.x = (int)s.obj;
s.x = 20;
b = s.obj is int;
Console.WriteLine("{0},{1},{2}", s.obj, s.x, b);
s.obj="CSHARP";
b=s.obj is int;
Console.WriteLine("{0},{1},{2}",s.obj,s.x,b);
Console.ReadLine();
}
public static void Main()
{
sample s=new sample();
s.boxmethod();
}
}
30
Unboxing Operation
/*
* C# Program to Perform Unboxing Operation
*/
using System;
class sample
{
int data;
void insert(object x)
{
data = (int)x * 5;
}
object delete()
{
data=0;
return (object)data;
}
public static void Main()
{
sample s = new sample();
s.insert(10);
Console.WriteLine("Data : {0}", s.data);
Console.WriteLine("Data : {0}", s.delete());
Console.ReadLine();
}
}
31
Fibonacci Series
/*
* C# Program to Generate Fibonacci Series
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace fibonaci
{
class Program
{
static void Main(string[] args)
{
int i, count, f1 = 0, f2 = 1, f3 = 0;
Console.Write("Enter the Limit : ");
count = int.Parse(Console.ReadLine());
Console.WriteLine(f1);
Console.WriteLine(f2);
for (i = 0; i <= count; i++)
{
f3 = f1 + f2;
Console.WriteLine(f3);
f1 = f2;
f2 = f3;
}
Console.ReadLine();
}
}
}
32
Factorial of Given Number
/*
* C# Program to Generate the Factorial of Given Number
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace factorial
{
class Program
{
static void Main(string[] args)
{
int i, number, fact;
Console.WriteLine("Enter the Number");
number = int.Parse(Console.ReadLine());
fact = number;
for (i = number - 1; i >= 1; i--)
{
fact = fact * i;
}
Console.WriteLine("\nFactorial of Given Number is: "+fact);
Console.ReadLine();
}
}
}
33
Prime Numbers
/*
* C# Program to Display All the Prime Numbers Between 1 to 100
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PrimeNumber
{
class Program
{
static void Main(string[] args)
{
bool isPrime = true;
Console.WriteLine("Prime Numbers : ");
for (int i = 2; i <= 100; i++)
{
for (int j = 2; j <= 100; j++)
{
if (i != j && i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
Console.Write("\t" +i);
}
isPrime = true;
}
Console.ReadKey();
}
}
}
34
Prime Numbers Display its Largest Factor
/*
* C# Program to Check Whether the Given Number is a Prime number if so then
* Display its Largest Factor
*/
using System;
namespace example
{
class prime
{
public static void Main()
{
Console.Write("Enter a Number : ");
int num;
num = Convert.ToInt32(Console.ReadLine());
int k;
k = 0;
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
{
k++;
}
}
if (k == 2)
{
Console.WriteLine("Entered Number is a Prime Number and " +
"the Largest Factor is {0}",num);
}
else
{
Console.WriteLine("Not a Prime Number");
}
Console.ReadLine();
}
}
}
35
Perfect Number or Not
/*
* C# Program to Check Whether the Entered Number is a Perfect Number or Not
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
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");
Console.ReadLine();
}
else
{
Console.WriteLine("\n Entered number is not a perfect number");
Console.ReadLine();
}
}
}
}
36
Armstrong Number
/*
* C# Program to Check Whether the Entered Number is an Armstrong Number or Not
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6
{
class Program
{
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();
}
}
}
37
Armstrong Numbers from 1 to 1000
/*
* C# Program to Print all the Armstrong Numbers from 1 to 1000
*/
using System;
class Program
{
static void Main()
{
int a, b, c, d;
for (int i = 1; i <= 1000; i++)
{
a = i / 100;
b = (i - a * 100) / 10;
c = (i - a * 100 - b * 10);
d = a * a * a + b * b * b + c * c * c;
if (i == d)
{
System.Console.WriteLine("{0}", i);
}
}
Console.Read();
}
}
38
Sum of N Numbers
/*
* C# Program to Generate the Sum of N Numbers
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace program
{
class Program
{
static void Main(string[] args)
{
int i, sum = 0,n;
Console.Write("Enter the Nth Number : ");
n = int.Parse(Console.ReadLine());
for (i = 0; i <= n; i++)
{
sum = sum + i;
}
Console.WriteLine("\nSum of N Numbers : " + sum);
Console.ReadLine();
}
}
}
39
first 50 Natural Numbers
/*
* C# Program to Find the Sum of first 50 Natural Numbers
* using For Loop
*/
using System;
class program
{
public static void Main()
{
int num, sum = 0;
for (num = 1; num <= 50; num++)
{
sum = sum + num;
}
Console.WriteLine("Sum = {0}", sum);
Console.ReadLine();
}
}
40
Factors of the Entered Number
/*
* C# Program to Display the Factors of the Entered Number
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
int num, x;
Console.WriteLine("Enter the Number ");
num = int.Parse(Console.ReadLine());
Console.WriteLine("The Factors are : ");
for (x = 1; x <= num; x++)
{
if (num % x == 0)
{
Console.WriteLine(x);
}
}
Console.ReadLine();
}
}
}
41
Absolute value
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace example
{
internal class Program
{
private static void Main(string[] args)
{
int num;
Console.Write("Enter a number:");
num = Convert.ToInt32(Console.ReadLine());
if (num < 0)
{
num = num * -1;
}
Console.WriteLine("Absolute value : " + num);
Console.ReadLine();
}
}
}
42
Random Numbers
/*
* C# Program to Generate Random Numbers
*/
using System;
class Program
{
static void Main()
{
Console.WriteLine("Some Random Numbers that are generated are : ");
for (int i = 1; i < 10; i++)
{
Randfunc();
}
}
static Random r = new Random();
static void Randfunc()
{
int n = r.Next();
Console.WriteLine(n);
Console.ReadLine();
}
}
43
Amicable Number or Not
/*
* C# Program Checks Whether the Entered Number is a Amicable Number or Not
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class Program
{
public static void Main(String[] args)
{
int num1, num2, sum1 = 0, sum2 = 0, i;
Console.WriteLine("Enter First Number : ");
num1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Second Number : ");
num2 = int.Parse(Console.ReadLine());
for (i = 1; i < num1; i++)
{
if (num1 % i == 0)
{
sum1 = sum1 + i;
}
}
for (i = 1; i < num2; i++)
{
if (num2 % i == 0)
{
sum2 = sum2 + i;
}
}
if (num1 == sum2 && num2 == sum1)
{
Console.WriteLine("They are a Pair of Amicable Numbers");
Console.ReadLine();
}
else
{
Console.WriteLine("They are not Amicable Numbers");
Console.ReadLine();
}
}
}
}
44
Square Root
/*
* C# Program to Find Square Root of a Given Number
*/
using System;
using System.Text;
using System.Collections;
using System.Data;
namespace Cons
{
public class squareroot
{
public static void Main()
{
Console.WriteLine("Enter a Number : ");
int Number = Convert.ToInt16(Console.ReadLine());
double SqrtNumber = Math.Sqrt(Number);
Console.WriteLine("Square root of {0} is: {1}", Number, SqrtNumber);
Console.ReadLine();
}
}
}
45
Cube Root
/*
* C# Program to Find the Cube Root of a Given Number
*/
using System;
class CubeRoot
{
public static void Main()
{
double num, res;
Console.Write("Enter the Number : ");
num = double.Parse(Console.ReadLine());
res = Math.Ceiling(Math.Pow(num, (double)1 / 3));
Console.Write("Cube Root : " + res);
}
}
46
Add 2 Complex Numbers
/*
* C# Program to Add 2 Complex Numbers
*/
using System;
public struct Complex
{
public int real;
public int imaginary;
public Complex(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
public override string ToString()
{
return (String.Format("{0} + {1}i", real, imaginary));
}
}
47
Fractional Powers
/*
* C# Program to Calculate Fractional Powers
*/
using System;
class Program
{
static void Main()
{
double value1 = Math.Pow(2, 2.1);
double value2 = Math.Pow(Math.E, 2);
double value3 = Math.Pow(Math.PI, 1);
Console.WriteLine("Result : {0}", value1);
Console.WriteLine("Result : {0}", value2);
Console.WriteLine("Result : {0}", value3);
Console.ReadLine();
}
}
48
Calculate Power of Three
/*
* C# Program to Calculate Power of Three
*/
using System;
class Program
{
static void Main(string[] args)
{
new GeneratePowers().RaiseToPower
(5, // 4 values per table
3);
Console.ReadLine();// power to raise each value
}
}
public class GeneratePowers
{
public void RaiseToPower(int maxIterations, int power)
{
Console.WriteLine("{0,8}{1,16}",
"Number", "Power of " + power);
for (int i = 1; i <= maxIterations; ++i)
{
Console.Write("{0,5}{1,15}\n", i,
Math.Pow(i, power));
}
}
}
49
power exponent value
/*
* C# Program to Calculate the power exponent value
*/
using System;
class Program
{
static void Main()
{
double m, n;
Console.WriteLine("Enter the Number : ");
m = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the Exponent : ");
n = double.Parse(Console.ReadLine());
double value1 = Math.Pow(m, n);
Console.WriteLine("Result : {0}", value1);
Console.ReadLine();
}
}
50
Edge Values in Power Function
/*
* C# Program to Check the Edge Values in Power Function
*/
using System;
class Program
{
static void Main()
{
double value1 = Math.Pow(double.MinValue, double.MaxValue);
double value2 = Math.Pow(double.MinValue, 0);
double value3 = Math.Pow(double.NaN, 2);
double value4 = Math.Pow(double.PositiveInfinity, 2);
double value5 = Math.Pow(double.NegativeInfinity, 2);
Console.WriteLine("Result : {0}", value1);
Console.WriteLine("Result : {0}", value2);
Console.WriteLine("Result : {0}", value3);
Console.WriteLine("Result : {0}", value4);
Console.WriteLine("Result : {0}", value5);
Console.ReadLine();
}
}
51
Check the Edge Values
/*
* C# Program to Check the Edge Values in Power Function
*/
using System;
class Program
{
static void Main()
{
double value1 = Math.Pow(double.MinValue, double.MaxValue);
double value2 = Math.Pow(double.MinValue, 0);
double value3 = Math.Pow(double.NaN, 2);
double value4 = Math.Pow(double.PositiveInfinity, 2);
double value5 = Math.Pow(double.NegativeInfinity, 2);
Console.WriteLine("Result : {0}", value1);
Console.WriteLine("Result : {0}", value2);
Console.WriteLine("Result : {0}", value3);
Console.WriteLine("Result : {0}", value4);
Console.WriteLine("Result : {0}", value5);
Console.ReadLine();
}
}
52
find Product of 2 Numbers
/*
* C# Program to find Product of 2 Numbers using Recursion
*/
using System;
class program
{
public static void Main()
{
int a, b, result;
Console.WriteLine("Enter two numbers to find their product: ");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
prog pg = new prog();
result = pg.product(a, b);
Console.WriteLine("Product of {0} and {1} is {2}",a, b, result);
Console.ReadLine();
}
}
class prog
{
public int product(int a, int b)
{
if (a < b)
{
return product(b, a);
}
else if (b != 0)
{
return (a + product(a, b - 1));
}
else
{
return 0;
}
}
}
53
the Number with its Reverse
/*
* C# Program to Get a Number and Display the Number with its Reverse
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
int num, reverse = 0;
Console.WriteLine("Enter a Number : ");
num = int.Parse(Console.ReadLine());
while (num != 0)
{
reverse = reverse * 10;
reverse = reverse + num % 10;
num = num / 10;
}
Console.WriteLine("Reverse of Entered Number is : "+reverse);
Console.ReadLine();
}
}
}
54
Local Time
/*
* C# Program to get the Local Time
*/
using System;
class Program
{
static void Main()
{
TimeZone zone = TimeZone.CurrentTimeZone;
DateTime local = zone.ToLocalTime(DateTime.Now);
Console.WriteLine("The Local Time is : {0}",local);
Console.ReadLine();
}
}
55
Universal Time
/*
* C# Program to get the Universal Time
*/
using System;
class Program
{
static void Main()
{
TimeZone zone = TimeZone.CurrentTimeZone;
DateTime univ = zone.ToUniversalTime(DateTime.Now);
Console.WriteLine("Universal Time is {0}",univ);
Console.Read();
}
}
56
Date in Various Formats
/*
* C# Program to Display the Date in Various Formats
*/
using System;
namespace DateAndTime
{
class Program
{
static int Main()
{
DateTime date = new DateTime(2013,6, 23);
Console.WriteLine("Some Date Formats : ");
Console.WriteLine("Date and Time: {0}", date);
Console.WriteLine(date.ToString("yyyy-MM-dd"));
Console.WriteLine(date.ToString("dd-MMM-yy"));
Console.WriteLine(date.ToString("M/d/yyyy"));
Console.WriteLine(date.ToString("M/d/yy"));
Console.WriteLine(date.ToString("MM/dd/yyyy"));
Console.WriteLine(date.ToString("MM/dd/yy"));
Console.WriteLine(date.ToString("yy/MM/dd"));
Console.Read();
return 0;
}
}
}
57
Add Two Dates
/*
* C# Program to Add Two Dates
*/
using System;
namespace DateAndTime
{
class Program
{
static int Main()
{
DateTime SDate = new DateTime(2010, 10, 7);
Console.WriteLine("Starting Date : {0}", SDate);
DateTime EDate = startDate.AddDays(10);
Console.WriteLine("Ending Date : {0}\n", EDate);
Console.ReadLine();
return 0;
}
}
}
58
Campare Two Dates
/*
* C# Program to Campare Two Dates
*/
using System;
namespace DateAndTime
{
class Program
{
static int Main()
{
DateTime sd = new DateTime(2010, 10, 12);
Console.WriteLine("Starting Date : {0}", sd);
DateTime ed = sd.AddDays(10);
Console.WriteLine("Ending Date : {0}", ed);
if (sd < ed)
Console.WriteLine("{0} Occurs Before {1}", sd, ed);
Console.Read();
return 0;
}
}
}
59
Leap Year or Not
/*
* C# Program to Check Whether the Entered Year is a Leap Year or Not
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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();
}
}
}
60
Convert Number in Years, Weeks & Days
/*
* C# Program to Convert a Given Number of Days in terms of
* Years, Weeks & Days
*/
using System;
class program
{
public static void Main()
{
int ndays, year, week, days, DAYSINWEEK=7;
Console.WriteLine("Enter the number of days");
ndays = int.Parse(Console.ReadLine());
year = ndays / 365;
week = (ndays % 365) / DAYSINWEEK;
days = (ndays % 365) % DAYSINWEEK;
Console.WriteLine("{0} is equivalent to {1}years, {2}weeks and {3}days",
ndays, year, week, days);
Console.ReadLine();
}
}
61
DayLight Saving Information
/*
* C# Program to Get the DayLight Saving Information
*/
using System;
using System.Globalization;
class Program
{
static void Main()
{
TimeZone z = TimeZone.CurrentTimeZone;
DaylightTime t = z.GetDaylightChanges(DateTime.Today.Year);
Console.WriteLine("Start Time: {0}", t.Start);
Console.WriteLine("Delta Time: {0}", t.Delta);
Console.WriteLine("End Time: {0}", t.End);
Console.ReadLine();
}
}
62
Binary to Decimal Conversion
/*
* 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();
}
}
}