TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Piyush chhabra
NA
83
16.9k
c# datatypes..!!
Jun 23 2014 1:18 AM
hello every one..
i am trying to make a console programe in c# in which i want to calculate the product of divisors of a number.
for example the number is 12 then its divisors are 2, 3, 4 ,6. then output should be 2*3*4*6=144.
Here is my code..
using System;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.Clear();
ulong b=0; ulong c = 1;
b = Convert.ToUInt32(Console.ReadLine());
for (ulong i = 2; i <=b/2; i++)
{
if (b % i == 0)
{
c = c * i;
}
}
Console.WriteLine(c);
}
}
}
Now my problem is that i want to run this programe for large numeric numbrs like 10000 or 100000 or 2-3 lakhs may be.
but it gives correct output only for small numbers. it does not give correct output for large number like 10000 whose product of divisors will be very large.
i tried to use high data types but its not giving correct output.
what is going wrong...
Please programersss Help me...........!!!
Reply
Answers (
1
)
1
Vulpes
0
96k
2.5m
Jun 23 2014 10:50 AM
If you need to deal with numbers bigger than ulong and you're using .NET 4.0 or later, then I'd use the System.Numerics.BigInteger type to hold the multiplied value.
You'll need to add a reference to System.Numerics.dll for the following to compile.
Notice also that ulong is actually a C# alias for UInt64 not UInt32:
using System;
using System.Text;
using System.Numerics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.Clear();
ulong b = Convert.ToUInt64(Console.ReadLine());
BigInteger c = 1;
for (ulong i = 2; i <= b/2; i++)
{
if (b % i == 0)
{
c = c * i;
}
}
Console.WriteLine(c);
}
}
}
For an input of 10000, the output is now:
10000000000000000000000000000000000000000000000
Accepted Answer
C# is a type safe
Automatic Garbage Collection