Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. /*Write a program in C# Sharp to display the individual digits of a given number using recursion.
  2. Input any number : 1234
  3. The digits in the number 1234 are : 1 2 3 4*/
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace ConsoleApplication4
  12. {
  13.     class Program
  14.     {
  15.         public static int GetUserNumber()
  16.         {
  17.             Console.Write("Input any number : ");
  18.             int userNumber = Convert.ToInt32(Console.ReadLine());
  19.             return userNumber;
  20.         }
  21.  
  22.         public static void DisplayNumbers(int userNum) //Вывод результатов зацикливается, не доходя до 1
  23.         {
  24.             if (userNum % 10 >= 1)
  25.             {                
  26.                 userNum /= 10;
  27.                 DisplayNumbers(userNum);
  28.                 Console.Write(" " + userNum % 10);
  29.             }
  30.             else Console.Write("");
  31.         }
  32.  
  33.         static void Main(string[] args)
  34.         {
  35.             int userNum = GetUserNumber();
  36.             DisplayNumbers(userNum);            
  37.             Console.ReadKey();
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement