skip to main |
skip to sidebar
Jun 1, 2010
PPP: The Sum of Digits
A Programing Practice Problem where the user provides a positive integer to receive back the total of the digits in the given number.
Datalinks:
Programming Practice Problem
Subscribe to:
Post Comments (Atom)
Datalinks
- Artificial Intelligence (5)
- Batch (2)
- Board Game (1)
- Dream (3)
- Future Possibility (2)
- Game (5)
- Humanity (1)
- Programming (9)
- Programming Practice Problem (6)
- Quality Assurance (3)
- Story (2)
- Tip (8)


2 comments:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace SumOfInput
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblTitle.Text = "Welcome to The Sum of Digits";
lblInstructions.Text = "Input an integer of any length and press 'Get Sum!' to find the sum of all digits entered.";
lblResult.Text = "";
}
protected void btnGetSum_Click(object sender, EventArgs e)
{
int iDigit, iCount, iLen;
string sub, input, result;
input = txtInput.Text;
iLen = input.Length;
iDigit = 1;
int[] digits;
digits = new int[iLen];
for (iCount = 0; iCount < iLen; iCount++)
{
sub = input.Substring(iCount, iDigit);
digits[iCount] = int.Parse(sub);
}
result = digits.Sum().ToString();
lblResult.Text = "The sum of your digits is " + result;
}
protected void btnClear_Click(object sender, EventArgs e)
{
txtInput.Text = "";
lblResult.Text = "";
}
}
}
By the way, I did not program any validation checking for input.
Post a Comment