1.sum of array:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[] arr = { 1, 2, 3, 4, 5 };
int sum = 0;
for(int i=0;i<arr.Length;i++)
{
sum += arr[i];
}
Console.WriteLine("The sum of array:" + sum);
Console.ReadLine();
}
}
}
...............................................................................................................................................................
2. Count space
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a String:");
string str = Console.ReadLine();
int spaceCount = CountSpaces(str);
Console.WriteLine("The number of spaces: " + spaceCount);
Console.ReadLine();
}
static int CountSpaces(string input)
{
int count = 0;
foreach (char c in input)
{
if (c == ' ')
{
count++;
}
}
return count;
}
}
}
......................................................................................................................................................................
Comments
Post a Comment