Written by Siddartha Thota on March 2, 2013 in C# > Programming

C# Code to check Percentage of contribution a number does to the total

GET ALERTS:

Get our Latest updates delivered to your mailbox!

In this program, we are going to check out how to create a console C# application that allows us to find out the percentage that a number is contributing to the total list. This is one of the basic application, that helps you to know the percentage of contribution that a number is doing to the total.

The concept behind this program is that, you need to consider an array that which accepts the total number of values you are going to consider in order to add them and then you need to enter the individual values to get the sum of all the numbers, then we just use a simple logic that helps us to find out the percentage of contribution that the numbers you’ve entered.

Its like if you choose 5, you got to enter 5 numbers and this application will add them up, and finally it will display the percentage of contribution that the entered numbers does.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace percentage
{
    class percentage
    {
        static void Main(string[] args)
        {

            int totVals = 0;
            double tot = 0;
            double val = 0;
            int n = 0;
            double[] myVals = null;
            Console.WriteLine("Enter the number of values you want to make use of!");
            if (Int32.TryParse(Console.ReadLine(), out totVals))
            {
                myVals = new double[totVals];
                for (n = 1; n <= totVals; n++)
                {
                    Console.WriteLine("Enter the value to check the percentage of it's cotribution{0}:", n);
                    if (double.TryParse(Console.ReadLine(), out val))
                    {

                        tot = tot + val;
                        myVals[n - 1] = val;
                    }
                    else
                    {
                        Console.WriteLine("You need to enter the valid number, make sure you entered only numbers of Int type.");
                        n--;
                    }
                }
            }
            else
            {
                Console.WriteLine("Enter the valid Int type number in order to continue");

            }
            Console.WriteLine("The total of numbers that you've entered is {0} \n", tot);

            Console.WriteLine("The Final Result of the percentage of the cotribution each number does :");
            for (int j = 0; j < myVals.Length; j++)
            {
                Console.WriteLine("The value is {0}, the percentage the number {1} contributes to the list is", myVals[j], (myVals[j] / tot) * 100);
            }
        }
    }
}

Output :
output of percentage of the contribution

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: