Mohammed Sayer
Mohammed Sayer

Reputation: 25

Triangle number does not display an output in c#

I have written this code in visual studio studio 2013. Its a simple question to find the number with divisor more than 250.i know its a very common question and many of them have already answered it .but i am having a problem i am using the suggested code on stack overflow but its not displaying an output. Like the program runs but it does not display anything on the label. can anyone please help me knowing what am i doing wrong .here is my code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public int Find()
        {
            int number = 0;
            for (int i = 1; ; i++)
            {
                number += i; // number is triangle number i
                if (CountDivisorsOfNumber(number) > 500)
                    return number;
                    lblnum.Text = number.ToString();
            } 
        }


        private static int CountDivisorsOfNumber(int number)
        {
            int count = 0;
            int end = (int)Math.Sqrt(number);
            for (int i = 1; i < end; i++)
            {
                if (number % i == 0)
                    count += 2;
            }
            if (end * end == number) // Perfect square
                count++;
            return count;
        }
       private void Form1_Load(object sender, EventArgs e)
        {
            Find();
        }
    }
}

Upvotes: 2

Views: 84

Answers (2)

Gaspa79
Gaspa79

Reputation: 5690

There's two problems here:

First: You have unreachable code below your "return" statement. The label will not be updated. You need to put braces in your if and switch statements. Like so:

if (CountDivisorsOfNumber(number) > 250)
{
    lblnum.Text = number.ToString();
    return number;
}

Second: you want to show numbers with more than 250 divisions according to your title, but:

 if (CountDivisorsOfNumber(number) > 500)

You are checking for 500.

EDIT: Here's the code I used to test. I got 2162160:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Find();
        }

        private int CountDivisorsOfNumber(int number)
        {
            int count = 0;
            int end = (int)Math.Sqrt(number);
            for (int i = 1; i < end; i++)
            {
                if (number % i == 0)
                    count += 2;
            }
            if (end * end == number) // Perfect square
                count++;
            return count;
        }

        private int Find()
        {
            int number = 0;
            for (int i = 1; ; i++)
            {
                number += i; // number is triangle number i
                if (CountDivisorsOfNumber(number) > 250)
                {
                    lblnum.Text = number.ToString();
                    return number;
                }
            }
        }
    }

Upvotes: 1

Glubus
Glubus

Reputation: 2855

Your return statement is executed before the text is set on the label. Any code after a return statement ( within the same scope ) is ignored.

Change these lines:

    public int Find()
    {
        int number = 0;
        for (int i = 1; ; i++)
        {
            number += i; // number is triangle number i
            if (CountDivisorsOfNumber(number) > 500){
            {
                lblnum.Text = number.ToString();
                return number;
            }
        } 
    }

Upvotes: 0

Related Questions