Solved

# form displaying input values and percetages

Posted on 2008-10-04
221 Views
I have to create a program that accepts intergers and then displays a message box showing the original values, the total, and the percentage each value contibutes to the total. My question is how do I calculate the percentage each value contibutes to the total?
``````using System;

using System.Collections.Generic;

using System.Windows.Forms;

using System.Text;

namespace Chp7Ex2

{

class Program

{

static void Main(string[] args)

{

string inValue;

int total = 0;

Console.WriteLine("How many values will be entered: ");

int arraySize = Convert.ToInt32(sSize);

int [] input = new int[arraySize];

for (int i=0; i < arraySize; i++)

{

Console.Write("Enter number {0}: ", i + 1);

input[i] = Convert.ToInt32(inValue);

}

for (int i = 0; i < arraySize; i++)

{

total += input[i];

}

MessageBox.Show(total);

}

}

}
``````
0
Question by:98fatboyrider

LVL 11

Assisted Solution

EricTViking earned 50 total points
100 / Total * Value
0

LVL 55

Accepted Solution

Jaime Olivares earned 75 total points
you can show you data as:

// after calculating total
string message = "";
for (int i = 0; i < arraySize; i++)
{
message += string.Format("{0} ({1}%)\r\n", input[i], input[i] * 100 / total);
}
message += string.Format("Total: {0}", total);
MessageBox.Show(message);
0

LVL 26

Assisted Solution

Anurag Thakur earned 75 total points
Jaime's solution should work but if there are 3 values 12,12, 12 the % will just show 33% each time as round is not implemented where as it should show 33.33%

i have extended Jaimes solution further to take care of that too
``````			string inValue;

int total = 0;

Console.WriteLine ("How many values will be entered: ");

int arraySize = Convert.ToInt32 (sSize);

int[] input = new int[arraySize];

for (int i = 0; i < arraySize; i++)

{

Console.Write ("Enter number {0}: ", i + 1);

input[i] = Convert.ToInt32 (inValue);

}

for (int i = 0; i < arraySize; i++)

{

total += input[i];

}

string message = "\r\n\r\n";

for (int i = 0; i < arraySize; i++)

{

message += string.Format ("Input value no {0} : {1} having % contribution ({1}%)\r\n", i, input[i], Math.Round(input[i] * 100.00 / total, 2));

}

message += string.Format ("\r\nTotal: {0}", total);

Console.WriteLine (message);

``````
0

## Featured Post

### Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.