2 Replies - 11130 Views - Last Post: 27 November 2009 - 11:19 AM Rate Topic: -----

#1 redriller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-November 09

Check the Armstrong number

Posted 27 November 2009 - 08:39 AM

 private void btnKiemTra_Click(object sender, EventArgs e)
		{
			int k = int.Parse(txtN.Text);
			int Luuk = k;
			double LgthOfNum = txtN.ToString().Length;
			double num = 0;
			double sum = 0;
			while (k > 0)
			{
				num = k % 10;
				sum = sum + Math.Pow(num, LgthOfNum);
				k /= 10;
			}
			if (sum == Luuk)
			{
				MessageBox.Show("It is an armstrong num.");
			}
			else
			{
				MessageBox.Show("It is not an armstrong num.");
			}

		}


There is something wrong with my code. When I tried 3 and 153, it went wrong. In fact, 153 and 3 are Armstrong numbers.

Is This A Good Question/Topic? 0
  • +

Replies To: Check the Armstrong number

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Check the Armstrong number

Posted 27 November 2009 - 10:30 AM

double LgthOfNum = txtN.ToString().Length;

That's your problem. txtN.ToString() is returning "System.Windows.Form.TextBox". Try taking the length of the Text in the field, not the length of the name of the object type.

And I will say this again, stop converting Strings to Strings. There is no need to use ToString() on a text field.
Was This Post Helpful? 1
  • +
  • -

#3 StarBP  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 06-May 09

Re: Check the Armstrong number

Posted 27 November 2009 - 11:19 AM

Try

double LgthOfNum = txtN.Text.Length;


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1