C# Program that outputs all perfect numbers less than 1000 ?

Posted by Nav234 under C# on 9/22/2010 | Points: 10 | Views : 17435 | Status : [Member] | Replies : 2
Hi all,

C# Program that outputs all perfect numbers less than 1000.

The Proper divisors of a positive integer 'n' are those integers,excluding 'n' itself,that divide 'n' exactly.
For Example : Proper divisor of 12 are 1,2,3,4 and 6.

A perfect number is the Positive Integer that is equal to Sum of its proper divisors.
For Example : 6 is a perfect number because 6 = 1 + 2 + 3 .

Can any one help on this issue ?

Thanks in advance

S.Naveen...


Responses

Posted by: Kannaiah.chinni1329 on: 10/21/2010 [Member] Starter | Points: 25

Up
0
Down
hi

am sending logic for generating perfect numbers below 1000,
int n=1000;
int sum=0;
for(int i=1;i<n;i++)
{
sum=0;
for(int j=1;j<i;j++)
{
if(i%j ==0) {
sum=sum+j;
}
}
if(sum== i)
System.out.println(i);
}


output \6
28
496

a


from
karunakar
senior programmer

karunakar

Nav234, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Brianzh on: 10/24/2010 [Member] Starter | Points: 25

Up
0
Down
using System;
public class PerfectNumbers
{
public static void Main()
{

int max = 0;
const int MIN = 1;
int minNumber = MIN;
int runTotal = 0;
int num = 0;
int holdNum = 0;
int mod = 0;
int divNum = 0;
string inputString;
int maxValue;

Console.WriteLine("+++++++++++++++++++++++++++++++++++++");
Console.WriteLine("WELCOME TO THE PERFECT NUMBER PROGRAM");
Console.WriteLine(" ");
Console.WriteLine("+++++++++++++++++++++++++++++++++++++");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" Please enter a maximum value");
Console.WriteLine(" The program will find perfect numbers");
Console.WriteLine(" from 1 to the value selected");
inputString = Console.ReadLine();
maxValue = Convert.ToInt32(inputString);
max = maxValue;

while (minNumber <= max)
{

if (minNumber == 1)
{
holdNum = minNumber;
divNum = num - 1;
if (divNum != 0)
mod = holdNum % divNum;
if (mod == 0)
{
runTotal += divNum; //accumulate totals
}
}
else
{
for (num = minNumber; num >= 1; --num)
{

holdNum = minNumber;
divNum = num - 1;
if (divNum != 0)
mod = holdNum % divNum;
if (mod == 0) //accumulate totals
{
runTotal += divNum;
//Console.WriteLine("runtotal is {0}", runTotal);
}
}
if (runTotal == holdNum)
{
Console.WriteLine(" The perfect numbers from 1 to {0} are: {1} ", max, holdNum);

}
//Console.WriteLine("holdNum is {0}, divNum is {1}", holdNum, divNum);

holdNum = 0; // reset variables
divNum = 0; // reset variables
runTotal = 0; // reset variables
}

++minNumber;

}
Console.WriteLine(" ");
Console.WriteLine(" BYE!!!");
Console.ReadLine();//pause statement
}

}

Nav234, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response