• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Monday, August 10, 2020

Reverse Number Program in C# with Examples

 Admin     August 10, 2020     C#, Interview Question, Programming, Programs     No comments   

In this article, I am going to discuss the Reverse Number Program in C# with some examples. Please read our previous article where we discussed the Palindrome Program in C# with some examples. This is one of the most frequently asked interview questions in C#. As part of this article, we are going to discuss the following pointers.
  1. How to reverse a given number in C#?
  2. How to reverse a string in C#?
How to reverse a given number in C#?
In the following program, we take the input number from the console and then reverse that number.
using System;
namespace LogicalPrograms
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a Number : ");
            int number = int.Parse(Console.ReadLine());
            int reminder, reverse = 0;
            while (number > 0)
            {
                //Get the remainder by dividing the number with 10  
                reminder = number % 10;
                //multiply the sum with 10 and then add the reminder
                reverse = (reverse * 10) + reminder;
                //Get the quotient by dividing the number with 10 
                number = number / 10;
            }
            Console.WriteLine($"The Reverse order is : {reverse}");
            Console.ReadKey();
        }
    }
}
Output:
How to reverse a given number in C#?

How to reverse a string in C#?
In the following program, we take the string as an input from the console. Then we reverse the string using for loop.
using System;
namespace LogicalPrograms
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string name = Console.ReadLine();
            string reverse = string.Empty;
            for (int i = name.Length - 1; i >= 0; i--)
            {
                reverse += name[i];
            }
            Console.WriteLine($"The Reverse string is : {reverse}");
            Console.ReadKey();
        }
    }
}
Output:
How to reverse a string in C#?

Reverse a string Using Foreach loop in C#:
Let us see how to reverse a string using for each loop in C#.
using System;
namespace LogicalPrograms
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string name = Console.ReadLine();
            string reverse = string.Empty;
            foreach (char c in name)
            {
                reverse = c + reverse;
            }
            
            Console.WriteLine($"The Reverse string is : {reverse}");
            Console.ReadKey();
        }
    }
}
Output:
Reverse a string Using Foreach loop in C#:

Reverse a string using Array.Reverse Method in C#:
In the following example, we take a string as an input from the console and then convert that string to a character array. Then we use the Array class Reverse method to reverse the elements of the character array. Once we reverse the elements of the character array, then we create a string from that character array.
using System;
namespace LogicalPrograms
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string name = Console.ReadLine();
            char[] nameArray = name.ToCharArray();
            Array.Reverse(nameArray);
            string reverse = new string(nameArray);
            
            Console.WriteLine($"The Reverse string is : {reverse}");
            Console.ReadKey();
        }
    }
}
Output:
Reverse a string using Array.Reverse Method in C#

In the next article, I am going to discuss the Armstrong Number Program in C# with some examples. Here, in this article, I try to explain the different ways to reverse a number and a string using C# with some examples. I hope you understand the Reverse Number Program in C# with examples.

Summary:
I hope this post will be helpful to write a Reverse Number Program in C# with Examples
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • Web API Versioning using Query String Parameter
    In this article, I am going to discuss Web API Versioning using the Query String Parameter with an example. Please read our previous artic...
  • Static Files Middleware in ASP.NET Core
    In this article, I am going to discuss how to serve static files using Static Files Middleware in ASP.NET Core Application. Please read ou...
  • Cross-Origin Resource Sharing in WEB API
    In this article, I am going to discuss how to enable Cross-Origin Resource Sharing in Web API which allows cross-domain AJAX calls. Pleas...
  • Custom Method Names in Web API
    In this article, I am going to discuss how to create Custom Method Names in Web API application. Please read our previous article before p...
  • Web API Versioning Using Accept Header
    In this article, I am going to discuss Web API Versioning Using Accept Header with an example. This is a continuation part of our previous...
  • Authentication and Authorization in Web API
    In this article, I am going to discuss the Authentication and Authorization in Web API . Here I will give you an overview of Authentication...
  • Parameter Binding in ASP.NET Web API
    In this article, I am going to discuss one of the most important concepts i.e. Parameter Binding in ASP.NET Web API Application . Please re...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape