a. Variables and Data Types
b. Object-Based Manipulation
c. Conditional Logic
d. Loops
e. Methods.
a] Variables and Data Types:
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespace practical1
{
Class Program
{
Public static void Main (string []args)
{
Console.Write("\nEnetr the amount of celcious:");
intcelcious = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nKelvin={0}", celcious + 273);
Console.WriteLine("\nFahrenheit={0}", celcious * 18 / 10 % 32);
Console.Read();
}
}
}
output:
b] Object-Based Manipulation:
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespacecircle_area_and_perimeter_
{
classProgram
{
Public staticvoid Main(string[] args)
{
Double r, per_cer, area;
Double PI = 3.14;
Console.Write("Enter the radious of the circle:");
r = Convert.ToDouble(Console.ReadLine());
per_cer = 2 * PI * r;
area = PI * r * r;
Console.WriteLine("Perimeter of the Circle:{0}", per_cer);
Console.WriteLine("Area of the Circle:{0}", area);
Console.Read();
}
}
}
output:
c] Conditional Logic:
[1]
using System;
public class Exercise2
{
void Main()
{
int num1, rem1;
Console.Write("\n\n ");
Console.Write("Check whether a number is even or odd:\n");
Console.Write(" ---------------------------------------");
Console.Write("\n\n ");
Console.Write(" Input an integer:");
num1 = Convert.ToInt32(Console.ReadLine());
rem1 = num1 % 2;
if (rem1 == 0)
Console.WriteLine(" is an even integer.\n ",num1);
else
Console.WriteLine("is an odd integer.\n ",num1);
Console.Read();}
}
output:
[2]
using System;
public class Exercise8
{
void Main()
{
int num1, num2, num3;
Console.Write("\n\n");
Console.Write("Find the largest of three numbers:\n");
Console.Write("------------------------------------");
Console.Write("\n\n");
Console.Write("Input the 1st number :");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the 2nd number :");
num2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the 3rd number :");
num3 = Convert.ToInt32(Console.ReadLine());
if (num1 > num2)
{
if (num1 > num3)
{
Console.Write("The 1st Number is the greatest among three. \n\n");
}
else
{
Console.Write("The 3rd Number is the greatest among three. \n\n");
}
}
else if (num2 > num3)
Console.Write("The 2nd Number is the greatest among three \n\n");
else
Console.Write("The 3rd Number is the greatest among three \n\n");
Console.Read();
}
}
OUTPUT:
[3]
using System;
public class SwitchExample
{
void Main(string[] args)
{
Console.WriteLine("Enter a number:");
int num = Convert.ToInt32(Console.ReadLine());
switch (num)
{
case 10: Console.WriteLine("It is 10"); break;
case 20: Console.WriteLine("It is 20"); break;
case 30: Console.WriteLine("It is 30"); break;
default: Console.WriteLine("Not 10, 20 or 30"); break;
Console.Read();
}
}
}
output:
[4]
using System;
public class DoWhileExample
{
public static void Main(string[] args)
{
int i=1;
do
{
Console.WriteLine(i);
i++;
}
while(i<=10);
Console.Read();
}
}
output:
d] Loops:
[1]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication12
{
class Exercise2
{
static void Main(string[] args)
{
int j, sum = 0;
Console.Write("\n\n");
Console.Write("Find the sum of first 10 natural number :\n");
Console.Write("--------------------------------------------------");
Console.Write("\n\n");
Console.Write("The first 10 natural number are :\n");
for(j=1;j<=10;j++)
{
sum=sum+j;
Console.Write("{0}",j);
}
Console.Write("\n The sum is :{0}\n",sum);
Console.Read();
}
}
}
output:
[2]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication12
{
class program
{
static void Main(string[] args)
{
int a = 10;
while(a<20)
{
Console.WriteLine("The value of a: {0}", a);
a++;
}
Console.ReadLine();
}
}
}
output:
e] Methods:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication12
{
class NumberManipulation
{
public int Findmax(int num1,int num2)
{
int result;
if (num1 > num2)
{
result = num1;
}
else
{
result=num2;
}
return result;
}
static void Main(string[] args)
{
int a = 100;
int b = 200;
int ret;
NumberManipulation n = new NumberManipulation();
ret = n.Findmax(a, b);
Console.WriteLine("Max value is :{0}", ret);
Console.ReadLine();
}
}
output:
0 Comments