Plus Minus code in C#

Problem source: https://www.hackerrank.com/challenges/plus-minus

You’re given an array containing integer values. You need to print the fraction of count of positive numbers, negative numbers and zeroes to the total numbers. Print the value of the fractions correct to 3 decimal places.

Input Format

First line contains N, which is the size of the array.
Next line contains N integers A1,A2,A3,,An, separated by space.

Constraints
1N100
100Ai100

Output Format

Output three values on different lines equal to the fraction of count of positive numbers, negative numbers and zeroes to the total numbers respectively correct to 3 decimal places.

Sample Input

6
-4 3 -9 0 4 1          

Sample Output

0.500

0.333

0.167

 

 class Plus_minus_hackerrank
{
static void Main(string[] args)
{
int N = Convert.ToInt16(Console.ReadLine());
decimal positiveNum = 0, negativeNum = 0, zeroNum = 0, temp = (decimal)1 / N;

foreach (string ch in Console.ReadLine().Split(‘ ‘))
{
Int16 num = Convert.ToInt16(ch);
if (num > 0)
{
++positiveNum;
}
else if (num < 0)
{
++negativeNum;
}
else if (num == 0)
{
++zeroNum;
}
}

Console.WriteLine(Math.Round(positiveNum * temp, 3));
Console.WriteLine(Math.Round(negativeNum * temp, 3));
Console.WriteLine(Math.Round(zeroNum * temp, 3));
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s