atbg09

atbg09

  • NA
  • 1
  • 0

division by zero algorithm

Sep 29 2004 7:30 PM
I'm at the start of learning c# and I am getting unstuck on an algorithm. Basically I want the user to input a 5 digit integer and the program to pick each digit of the integer and print it. I.e. if the user inputs 23556 then the program outputs: 2 3 5 5 6 I have used integral division to do this but it only works if none of the digits are zero or else a division by zero error occurs. Can anyone suggest an alternative algorithm. Thanks matt This is my code: //this program asks the user for a five didgit number //it then uses division and modulus to pick the five digits //and print them sequentially on the screen //all digits must be != 0 using System; class Numbers { static void Main ( string [] args) { int a,b,c,d,e; //defines some variables, each of these represents one of the digits //e.g. 47281 a = 4, b = 7, c = 2 etc int v,w,x,y,z; //defines more variables representing parts of the total number //e.g. 47281 v = 47281, w = 7281, x = 281 etc //Ask the user to input a five digit integer Console.Write ("Please enter a five digit integer:\n"); //Assign this number to the variable v v = Int32.Parse (Console.ReadLine () ); //Calculate the first digit "a" by integral division a = v / 10000; //calculate w by modulus calculation w = v % (10000 * a); //Calculate the second digit "b" by integral division b = w / 1000; //calculate x by modulus calculation x = w % (1000 * b); //Calculate the third digit "c" by integral division c = x / 100; //calculate y by modulus calculation y = x % (100 * c); //calculate fourth digit "d" by modulus d = y / 10; //calculate z which is also e z = y % (10 * d); e = z; //Print results sequentially Console.WriteLine ( "The individual digits are {0} {1} {2} {3} {4}", a,b,c,d,e); } // main method } //end class Numbers

Answers (2)