num
. In ra con số đảo ngược của số người dùng vừa nhập.
Ví dụ: người dùng nhập num=1234
, chương trình in ra kết quả: 4321
reverve=0
Sử dụng vòng lặp while
để duyệt con số:
reverse = reverse * 10
reverse = reverse + (num%10)
num = num / 10
/* C# Program to Get a Number and Display the Number with its Reverse This is a C# Program to get a number and display the number with its reverse. Problem Description This C# Program Gets a Number and Display the Number with its Reverse. Problem Solution Here we obtain a number from the user and display the digits in the reverse order. */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Lession4 { class Program { static void Main(string[] args) { int num, reverse = 0; Console.WriteLine("Enter a Number : "); num = int.Parse(Console.ReadLine()); while (num != 0) { reverse = reverse * 10; reverse = reverse + num % 10; num = num / 10; } Console.WriteLine("Reverse of Entered Number is : " + reverse); Console.ReadLine(); } } }
Cùng nhau học tập, khám phá các kiến thức nền tảng về Lập trình web, mobile, database nhé.
Nền tảng kiến thức - Hành trang tới tương lai hân hạnh phục vụ Quý khách!
Khám phá, trải nghiệm ngay
Vui lòng đăng nhập để gởi bình luận!
Đăng nhậpChưa có bình luận nào!