Design and implement a function with no input parameter which reads a number from input (like 123). Only non-decimal numbers are valid (floating points are not valid). The number entered by the user should not be divisible by 10 and if the user enters a number that is divisible by 10 (like 560), it is considered invalid and the application should keep asking until the user enters a valid input. Once the user enters a valid input, the program calculates the reverse of the input number (for 153, the reverse is 351) and prints the result and returns the results.
Hi,
This has been implemented in C# Console Llibrary. Please find the screenshot of the running program and later find the code snippets.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace General
{
class Program
{
static void Main(string[] args)
{
ProcessLogic(true);
}
/// <summary>
/// Get Input from user and Process the logic
/// </summary>
/// <param name="flag"></param>
static void ProcessLogic(bool flag) {
int nInputNumber = 0;
if(flag)
Console.WriteLine("Enter Number");
else
Console.WriteLine("Enter the valid Number");
//Read number and check that its valid or not
bool bFlag = Int32.TryParse(Console.ReadLine(), out nInputNumber);
//Check Validation of number entered by user
if ((nInputNumber <= 0) || ((nInputNumber % 10) == 0))
{
//Case of Invalid number, show error message
Console.WriteLine("Enter the valid Number");
Console.ReadLine();
//Recursive call
ProcessLogic(false);
}
else
{
//Case when user entered the valid number then reverse the number
ReverseNumberLogic(nInputNumber);
}
}
/// <summary>
/// Revers the Input Number
/// </summary>
/// <param name="nInputNumber"></param>
static void ReverseNumberLogic(int nInputNumber) {
int reverse = 0, rem;
//Loop until inputnumber !=0
while (nInputNumber != 0)
{
rem = nInputNumber % 10;
reverse = reverse * 10 + rem;
nInputNumber /= 10;
}
//Print the result
Console.Write("Reverse of the input number is: " + reverse);
Console.ReadLine();
}
}
}
Thanks
Get Answers For Free
Most questions answered within 1 hours.