SUM OF SOME NUMBERS USING FOR LOOP......
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter limit");
int limit = Convert.ToInt16(Console.ReadLine());
int sum = 0;
for (int i = 0; i <= limit; i++)
{
sum = sum + i;
}
Console.WriteLine("The sum is :" + sum.ToString());
Console.WriteLine("end");
Console.ReadLine();
}
}
}
SELECTION OF OPERATIONS.....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the first number.");
int a = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter the second number.");
int b = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("enter an operation to be performed (+,-,*,/)");
string o = Console.ReadLine();
using if condition
if (o.Trim() == "+")
{
Console.WriteLine(Convert.ToString(a+b));
}
else if (o.Trim() == "-")
{
Console.WriteLine(Convert.ToString(a - b));
}
else if (o.Trim() == "*")
{
Console.WriteLine(Convert.ToString(a * b));
}
else if (o.Trim() == "/")
{
Console.WriteLine(Convert.ToString(a / b));
}
Console.WriteLine("end");
Console.ReadLine();
}
}
}
Using switch condition
switch (o.Trim())
{
case "+":
Console.WriteLine(Convert.ToString(a + b));
break;
case "-":
Console.WriteLine(Convert.ToString(a- b));
break;
case "*":
Console.WriteLine(Convert.ToString(a * b));
break;
case "/":
Console.WriteLine(Convert.ToString(a / b));
break;
}
Console.WriteLine("end");
Console.ReadLine();
Category: