Division problems

How to use division with integer type

Page 1 of 1

8 Replies - 9243 Views - Last Post: 23 August 2007 - 12:49 AM Rate Topic: -----

#1 MrIce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 06

Division problems

Posted 22 August 2007 - 02:55 AM

Hello.

I'm back... from long "vacations". :)

If the "oldies" don't remember, i was asking about c++ (and also about html) some time ago. Well, in between I had problems with my computer, so I couldn't be on the internet so much and the priorities changed. I think I'll get a brand new comp for Christmass, so I will soon be on the forum more times, because my knowledge has spread , so I can help and also I need help in new topics.

SO, rather talk about my problem. I started working on some "homework" C# examples. Because I'm new to C#, I'm strugling with the coding. The object orientation is somehow now logical to me and I can writte simple stuff like in Java.

I'm learning C# by coding such examples, don't have any book, only the Internet is sometimes the help. :)

Well, I have two problems.

The first one is this:

using System;
using System.Collections.Generic;
using System.Text;

namespace Naloga5
{
	class Program
	{
		static void Main(string[] args) 
		{
			int number;
			int divided;
			Console.WriteLine("Vpisi stevilo:");
			number = int.Parse(Console.ReadLine());
			Console.WriteLine();

			for (int i = 0; i < 100; i++)
			{

				divided = number % 10;
				number = number / 10;

				if (divided != 0) Console.WriteLine(divided); 
			}

			Console.ReadLine();
		}
	}
}



Here I'd like to make that each digit of the number is shown in it's own line. But my example works only with numbers which don't have the zero (0) in it. How could I fix this?

But the second problem is confusing me. I'd like to find out with wich numbers can another number be divided. F.e.:
15 can be divided with 1,3,5,15. And I don't know how to find this out.

So far, I have this code:

using System;
using System.Collections.Generic;
using System.Text;

namespace Naloga6
{
	class Program
	{
		static void Main(string[] args)
		{

			int number, number1;
			Console.WriteLine("Vpisi stevilo:");
			number = int.Parse(Console.ReadLine());
			for (int i = 1; i< number; i++)
			{
				if ((number/i) == 0) Console.WriteLine(i);
			}

			Console.ReadLine();
		}
	}
}



I don't know how to writte the IF statement(to ask) if a number can be divided. Some help would be appreciated. ;)

Is This A Good Question/Topic? 0
  • +

Replies To: Division problems

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Division problems

Posted 22 August 2007 - 04:39 AM

You example works with numbers that dont have 0's because of the line if (divide != 0) Console.WriteLine(divided);. This is telling C# to only write lines that arent 0. Try using it this way:

using System;
using System.Collections.Generic;
using System.Text;

namespace Naloga5
{
	class Program
	{
		static void Main(string[] args)
		{
			int number;
			int divided;
			Console.WriteLine("Vpisi stevilo:");
			number = int.Parse(Console.ReadLine());
			Console.WriteLine();

			for (int i = 0; i < 100; i++)
			{

				divided = number % 10;
				number = number / 10;
			   Console.WriteLine(divided);
			}

			Console.ReadLine();
		}
	}
}



The second one is a little more tricky. As you go through your loop you should have a local variable to hold which numbers divide into the input number, so you can display them once the calculations are complete. Something like:

using System;
using System.Collections.Generic;
using System.Text;

namespace Naloga6
{
	class Program
	{
		static void Main(string[] args)
		{

			int number, number1;
		string divisor;
			Console.WriteLine("Vpisi stevilo:");
			number = int.Parse(Console.ReadLine());
			for (int i = 1; i< number; i++)
			{
				if ((number/i) == 0)
				{
					
				   divisor += i;
				  if(i > 1)
				  {
					  divisor += ",";
				  }
			   }
			  }
		Console.WriteLine(number + " is divisible by " + divisor);
			Console.ReadLine();
		}
	}
}



Hope this at leasr gets you down the right path :)

Happy Coding!

This post has been edited by PsychoCoder: 22 August 2007 - 04:39 AM

Was This Post Helpful? 0
  • +
  • -

#3 MrIce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 06

Re: Division problems

Posted 22 August 2007 - 05:15 AM

I used the If line in the first code, because of the for loop. It would be better if the for loop would go only so far as the number is long. That is the problem, but I don't know how to handle this. I can't use the "number.Length()" function, because the Length function comes only with Strings. (if i remember correct)

When I leave it working without the if line, it throws out true the rest of the screen zeroes till the end of the loop. :angry: That's why I used the if line.

In the second problem. If I c/p your example, it returns an Error: Use of unassigned local variable 'divisor'.
But I think the problem lies in the if statement. To ask the right question in the if statement. I wrotte
if ((number/i) == 0) Console.WriteLine(i); 
only because I tried something out. This if has to be rephrased, as I remember to code such example in c++. If I only had the examples from the c++, when i was working. But I deleted them, when I finished learning it. :ohno:
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Division problems

Posted 22 August 2007 - 05:25 AM

You dont have to use number.length(), this is what I get for not reading how your for loop was written, all you need is

using System;
using System.Collections.Generic;
using System.Text;

namespace Naloga5
{
	class Program
	{
		static void Main(string[] args)
		{
			int number;
			int divided;
			Console.WriteLine("Vpisi stevilo:");
			number = int.Parse(Console.ReadLine());
			Console.WriteLine();

			for (int i = 0; i <= number; i++)
			{

				divided = number % 10;
				number = number / 10;
				If(i <= number)
				{
					 Console.WriteLine(divided);
				 }			   
			}

			Console.ReadLine();
		}
	}
}



Replace the 100 in your for loop with the variable number (duh @ me, sorry for not catching that). Now as for it displaying all zeros I dont understand that, unless it has something to do with the divided = number % 10; line (but I dont see that happening either). Give the above code a try and see what happens.

As far as the other issue, I need to find out why its saying theres an unused variable so Ill get back to you on that one :)
Was This Post Helpful? 0
  • +
  • -

#5 MrIce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 06

Re: Division problems

Posted 22 August 2007 - 05:56 AM

Ah, sill me. This is integer, hehe. Forgot about this.
I fixed the code, changed the code to this:

for (int i = 0; i <number; i++)
			{

				divided = number % 10;
				number = number / 10;

			   // if (divided != 0) 
				Console.WriteLine(divided); 
			}

			Console.WriteLine(number);
			Console.ReadLine();



But now I got the problem, when inserting a number with zero or if the number is 1.

I get the number + one zero in the end on the screen. Other numbers work fine (I suposse), because I tried it out.

Now (in this few seconds) I fixed this strange output, using this:
if (number != 0) Console.WriteLine(number);
on the end. But I think, it can be fixed more elegant. Or not?
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Division problems

Posted 22 August 2007 - 06:02 AM

First, try chaning your < sign to a >= such as

for (int i = 0; i >= number; i++)
			{

				divided = number % 10;
				number = number / 10;

			   // if (divided != 0)
				Console.WriteLine(divided);
			}

			Console.WriteLine(number);
			Console.ReadLine();



See if that fixes the extra zero being displayed.
Was This Post Helpful? 0
  • +
  • -

#7 MrIce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 06

Re: Division problems

Posted 22 August 2007 - 06:21 AM

Hmm... it strangly does fix it. Now it displays the number in one line. Before it so that it was displaying digits in their own lines. :blink:
Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Division problems

Posted 22 August 2007 - 07:43 AM

When you write Console.WriteLine(divided); you need to add a line break to the end of it so it knows to write to a new line.
Was This Post Helpful? 0
  • +
  • -

#9 MrIce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 06

Re: Division problems

Posted 23 August 2007 - 12:49 AM

Maybe yesterday was something wrong in VS. When I tried today, it was working ok. :blink:

What about the second problem? I didn't had any time to check that out on my own...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1