Construct a list of the divisors of a number,
using System;
using System.Collections.Generic;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int number = 143; // number input
List<int> divs = new List<int>(); // the list of divisors
for (int i = 1; i <= number; i++)
{
if (number % i == 0)
{
divs.Add(i);
}
}
if (divs.Count == 2) Console.WriteLine("{0} is prime!", number);
for (int i = 0; i < divs.Count; i++)
{
Console.WriteLine("A divisor of {0} is : {1}", number, divs[i]);
}
Console.ReadKey();
}
}
}
Select the second largest one.
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
Yes Medalgod I also assumed it but the question of the OP was : but i cannot find anything considering the second largest divisor of a number. so that was what I responded to;)
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
To use generic List you have to include the following line: using System.Collections.Generic;
The reason I put in int number = 143; is to keep it simple. Change 143 to anything you like and recompile, see the code sample below how using ReaLline from the console makes it more "complicated".
You could also have number input from a file or a TextBox in a Forms application.
If you want to use the results of your algorithm you will have to put them in some kind of datastructure anyway, in this code example without a list I have to use WriteLine. Don't see any other way to show the results to the user.
using System;
using System.Collections.Generic;//not needed here, but needed with List!!!
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int number = 0;
Console.Write("Input a number please:");
string numstr = Console.ReadLine();
bool OK = int.TryParse(numstr, out number);
if (OK)
{
for (int i = 1; i <= number; i++)
{
if (number % i == 0)
{
Console.WriteLine("A divisor of {0} is : {1}", number, i);
}
}
}
else
{
Console.WriteLine(">>>ERROR in input");
}
Console.ReadKey();
}
}
}
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
When you say you want to find the second largest divisor, are you taking the number itself to be the first? I'm assuming you are, but if not i'll show you the changes you need :)
class Program
{
static void Main(string[] args)
{
int input = 268; //you can get this value from user input/database/file etc
int result;
result = SecondLargestDivisor.Calculate(input);
}
}
class SecondLargestDivisor
{
public static int Calculate(int number)
{
for (int i = number - 1; i > 0; i--)
{
if (number % i == 0)
{
return i;
}
}
return 0;
}
}
I have placed the method in a class and declared it as static so it can be called without instantiation. I have used a Console App but you could call it the same way from a Winform. Also, i have used a preset number for input but you can pass any value you like as the input parameter.
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
@Ryshad : A very nice improvement on what I and medalgod came up with.
And completely what the OP asked I guess!
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
07-ozo, welcome here at daniweb.
If you have a question, please post it in a new thread.
Do not resurrect old threads please. Read the rules of this site.
A search may already provide the answer to your question.
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661