Basic Program in C#
1> How to find Ascii Values in C#
-------------------------------------------------------------------------------------------------
3> To SWAP two numbers C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class SWAP
{
static void Main(string[] args)
{
int a;
int b;
int c;//refrence variable
Console.WriteLine("To SWAP the two numbers");
Console.WriteLine("enter the first number");
a= int.Parse( Console.ReadLine());
Console.WriteLine("enter the second number");
b = int.Parse(Console.ReadLine());
Console.WriteLine("two number are {0} {1}",a,b);
//SWAP Logic
c = a;
a = b;
b = c;
Console.WriteLine("Swaped number are {0} {1}", a,b);
Console.ReadLine();
}
}
}
output:-
--------------------------------------------------------------------------------------------
4> To check string is Palindrome or not in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class Palindrome
{
static void Main(string[] args)
{
string text, revers="";
Console.WriteLine("Enter the letter ");
text = Console.ReadLine();
// to revese the string value
for( int i = text.Length-1; i>=0; i--)
{
revers+= text[i].ToString();
}
if (text == revers)
{
Console.WriteLine("Letter are same , so is Palindrome {0} {1}", text, revers);
}
else
{
Console.WriteLine("Letter are not same so is not Palindrome {0} {1}", text, revers);
}
Console.ReadLine();
}
}
}
--------------------------------------------------------------------------------
5>To check whether number is Even or Odd in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class EvenOrOdd
{
static void Main(string[] args)
{
int s;
Console.WriteLine("Enter the number");
s =int.Parse( Console.ReadLine());
if (s % 2 == 0)
{
Console.WriteLine("Is Even number {0}", s);
}
else
{
Console.WriteLine("Is Odd number {0}", s);
}
Console.ReadLine();
}
}
}
output:-
--------------------------------------------------------------------------------
6> To check which number is greater than 3 digit number in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class Program
{
static void Main(string[] args)
{
int f;
int s;
int t;
Console.WriteLine("enter the first number");
f =int.Parse( Console.ReadLine());
Console.WriteLine("enter the second number");
s = int.Parse(Console.ReadLine());
Console.WriteLine("enter the third number");
t = int.Parse(Console.ReadLine());
if (f > s && f>t)
{
Console.WriteLine("first number is greater :-{0}",f);
}
else if (s > t && s>f )
{
Console.WriteLine("second number is greater :-{0}",s);
}
else if (t > f && t> s)
{
Console.WriteLine("third number is greater :-{0}",t);
}
else
{
Console.WriteLine("equal");
}
Console.ReadLine();
}
}
}
output:-
--------------------------------------------------------------------------------
7>How to check year is leap year or not in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class Program
{
static void Main(string[] args)
{
int year;
Console.WriteLine("Enter the year");
year = int.Parse(Console.ReadLine());
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
{
Console.WriteLine(year + " is leap year");
}
else
{
Console.WriteLine(year + " is not leap year");
}
}
else
{
Console.WriteLine(year + " is leap year");
}
}
else
{
Console.WriteLine(year+" is not leap year");
}
Console.ReadLine();
}
}
}
--------------------------------------------------------------------------------
8>How to Check , the number is negative or positve in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class Program
{
static void Main(string[] args)
{
double number;
Console.WriteLine("Enter the number.");
number = double.Parse(Console.ReadLine());
if (number >= 0)
{
Console.WriteLine("Enter the number is positve.");
}
else
{
if (number <= 0.0)
{
if (number == 0.0)
{
Console.WriteLine("Enter the number is positve.");
}
else
{
Console.WriteLine("Enter the number is negative.");
}
}
}
Console.ReadLine();
}
}
}
-----------------------------------------------------------------
9> To sum of natural number in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class Program
{
static void Main(string[] args)
{
int n,m=0,i;
Console.WriteLine("Enter the number.");
n = int.Parse(Console.ReadLine());
for(i=1; i<=n;i++)
{
m += i;
}
Console.WriteLine("sum of natural number is {0}",m);
Console.ReadLine();
}
}
1> How to find Ascii Values in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Aciivalue
{
class Program
{
static void Main(string[] args)
{
string s;
Console.WriteLine(" Enter the letter:- ");
s = Console.ReadLine();
foreach (char c in s)
{
Console.WriteLine((int)c);
Console.ReadLine();
}
}
}
}
output:-
Enter the letter:- AB EF
ACII value:- 65 66 32 69 70 note here 32 is for space value between AB EF
--------------------------------------------------------------------------------------------------
2> How to get the size of data type in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sizevalue
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Size of int "+sizeof(int));
Console.WriteLine("Size of Int32 " + sizeof(Int32));
Console.WriteLine("Size of float " + sizeof(float));
Console.WriteLine("Size of decimal " + sizeof(decimal));
Console.WriteLine("Size of bool " + sizeof(bool));
Console.WriteLine("Size of Byte " + sizeof(Byte));
Console.WriteLine("Size of DateTimeKind " + sizeof(DateTimeKind));
Console.WriteLine("Size of Double " + sizeof(Double));
Console.WriteLine("Size of double " + sizeof(double));
Console.ReadLine();
}
}
}
output here:-
--------------------------------------------------------------------------------------------------
2> How to get the size of data type in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sizevalue
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Size of int "+sizeof(int));
Console.WriteLine("Size of Int32 " + sizeof(Int32));
Console.WriteLine("Size of float " + sizeof(float));
Console.WriteLine("Size of decimal " + sizeof(decimal));
Console.WriteLine("Size of bool " + sizeof(bool));
Console.WriteLine("Size of Byte " + sizeof(Byte));
Console.WriteLine("Size of DateTimeKind " + sizeof(DateTimeKind));
Console.WriteLine("Size of Double " + sizeof(Double));
Console.WriteLine("Size of double " + sizeof(double));
Console.ReadLine();
}
}
}
output here:-
-------------------------------------------------------------------------------------------------
3> To SWAP two numbers C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class SWAP
{
static void Main(string[] args)
{
int a;
int b;
int c;//refrence variable
Console.WriteLine("To SWAP the two numbers");
Console.WriteLine("enter the first number");
a= int.Parse( Console.ReadLine());
Console.WriteLine("enter the second number");
b = int.Parse(Console.ReadLine());
Console.WriteLine("two number are {0} {1}",a,b);
//SWAP Logic
c = a;
a = b;
b = c;
Console.WriteLine("Swaped number are {0} {1}", a,b);
Console.ReadLine();
}
}
}
output:-
--------------------------------------------------------------------------------------------
4> To check string is Palindrome or not in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class Palindrome
{
static void Main(string[] args)
{
string text, revers="";
Console.WriteLine("Enter the letter ");
text = Console.ReadLine();
// to revese the string value
for( int i = text.Length-1; i>=0; i--)
{
revers+= text[i].ToString();
}
if (text == revers)
{
Console.WriteLine("Letter are same , so is Palindrome {0} {1}", text, revers);
}
else
{
Console.WriteLine("Letter are not same so is not Palindrome {0} {1}", text, revers);
}
Console.ReadLine();
}
}
}
output:-
--------------------------------------------------------------------------------
5>To check whether number is Even or Odd in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class EvenOrOdd
{
static void Main(string[] args)
{
int s;
Console.WriteLine("Enter the number");
s =int.Parse( Console.ReadLine());
if (s % 2 == 0)
{
Console.WriteLine("Is Even number {0}", s);
}
else
{
Console.WriteLine("Is Odd number {0}", s);
}
Console.ReadLine();
}
}
}
output:-
--------------------------------------------------------------------------------
6> To check which number is greater than 3 digit number in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class Program
{
static void Main(string[] args)
{
int f;
int s;
int t;
Console.WriteLine("enter the first number");
f =int.Parse( Console.ReadLine());
Console.WriteLine("enter the second number");
s = int.Parse(Console.ReadLine());
Console.WriteLine("enter the third number");
t = int.Parse(Console.ReadLine());
if (f > s && f>t)
{
Console.WriteLine("first number is greater :-{0}",f);
}
else if (s > t && s>f )
{
Console.WriteLine("second number is greater :-{0}",s);
}
else if (t > f && t> s)
{
Console.WriteLine("third number is greater :-{0}",t);
}
else
{
Console.WriteLine("equal");
}
Console.ReadLine();
}
}
}
output:-
--------------------------------------------------------------------------------
7>How to check year is leap year or not in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class Program
{
static void Main(string[] args)
{
int year;
Console.WriteLine("Enter the year");
year = int.Parse(Console.ReadLine());
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
{
Console.WriteLine(year + " is leap year");
}
else
{
Console.WriteLine(year + " is not leap year");
}
}
else
{
Console.WriteLine(year + " is leap year");
}
}
else
{
Console.WriteLine(year+" is not leap year");
}
Console.ReadLine();
}
}
}
output:-
--------------------------------------------------------------------------------
8>How to Check , the number is negative or positve in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class Program
{
static void Main(string[] args)
{
double number;
Console.WriteLine("Enter the number.");
number = double.Parse(Console.ReadLine());
if (number >= 0)
{
Console.WriteLine("Enter the number is positve.");
}
else
{
if (number <= 0.0)
{
if (number == 0.0)
{
Console.WriteLine("Enter the number is positve.");
}
else
{
Console.WriteLine("Enter the number is negative.");
}
}
}
Console.ReadLine();
}
}
}
output:-
-----------------------------------------------------------------
9> To sum of natural number in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class Program
{
static void Main(string[] args)
{
int n,m=0,i;
Console.WriteLine("Enter the number.");
n = int.Parse(Console.ReadLine());
for(i=1; i<=n;i++)
{
m += i;
}
Console.WriteLine("sum of natural number is {0}",m);
Console.ReadLine();
}
}
}
output :-
----------------------------------------------------------------
10> Multiplication table of the number in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class Program
{
static void Main(string[] args)
{
int n,i;
Console.WriteLine("Enter the number.");
n = int.Parse(Console.ReadLine());
for(i=1; i<=10;i++)
{
Console.WriteLine(" {0} * {1} = {2}",n,i,n*i );
}
Console.ReadLine();
}
}
}
output:-
--------------------------------------------------------------------
11>Fibonacci sequence in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class Program
{
static void Main(string[] args)
{
int n,i,t=0,q=1,tot=0;
Console.WriteLine("Enter the number.");
n = int.Parse(Console.ReadLine());
Console.WriteLine("fibonacci series {0}",t);
Console.WriteLine("fibonacci series {0}",q);
for (i=3; i<=n;++i)
{
tot = t + q;
t = q;
q = tot;
Console.WriteLine("Fibonacci number \n {0} ",tot);
}
Console.ReadLine();
}
}
}
10> Multiplication table of the number in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class Program
{
static void Main(string[] args)
{
int n,i;
Console.WriteLine("Enter the number.");
n = int.Parse(Console.ReadLine());
for(i=1; i<=10;i++)
{
Console.WriteLine(" {0} * {1} = {2}",n,i,n*i );
}
Console.ReadLine();
}
}
}
output:-
--------------------------------------------------------------------
11>Fibonacci sequence in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace value
{
class Program
{
static void Main(string[] args)
{
int n,i,t=0,q=1,tot=0;
Console.WriteLine("Enter the number.");
n = int.Parse(Console.ReadLine());
Console.WriteLine("fibonacci series {0}",t);
Console.WriteLine("fibonacci series {0}",q);
for (i=3; i<=n;++i)
{
tot = t + q;
t = q;
q = tot;
Console.WriteLine("Fibonacci number \n {0} ",tot);
}
Console.ReadLine();
}
}
}
output:- 0 1 1 2 3 5 8 13 21 34
1 comments:
comments(y) Good work, It will help to beginners...
Reply