Xem thảo luận
Cải thiện bài viết
Lưu bài viết
ĐọcBàn luậnXem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Examples:
Input : 12345
Output : First digit: 1
last digit : 5
Input : 98562
Output : First digit: 9
last digit : 2
Bàn luậnmodulo operator %. When modulo divided by 10 returns its last digit.
Suppose if n = 1234
then last Digit = n % 10 => 4
To find first digit of a number is little expensive than last digit. To find first digit of a number we divide the given number by 10 until number is greater than 10. At the end we are left with the first digit.
Cho một số để tìm chữ số đầu tiên và cuối cùng của một số.examples: & nbsp; & nbsp;
C++
Để tìm chữ số cuối cùng của một số, chúng tôi sử dụng toán tử modulo %. Khi modulo chia cho 10 trả về chữ số cuối cùng của nó. Để tìm chữ số đầu tiên của một số, chúng tôi chia số đã cho 10 cho đến khi số lớn hơn 10. Ở cuối, chúng tôi còn lại với chữ số đầu tiên. & NBSP;
Cách tiếp cận 1 (với vòng lặp):
#include <bits/stdc++.h>
{
using
namespace
std;
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
4
int
firstDigit(
int
n)
#include <bits/stdc++.h>
8
#include <bits/stdc++.h>
0____11 #include <bits/stdc++.h>
2
{
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
6 #include <bits/stdc++.h>
7
#include <bits/stdc++.h>
8
int
using
0int
n)
{
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
6 using
6
#include <bits/stdc++.h>
0namespace
5namespace
6
#include <bits/stdc++.h>
3namespace
8
int
using
9
#include <bits/stdc++.h>
8
#include <bits/stdc++.h>0int namespace3
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
6 std;
1
Java
std;
3 std;
4
std;
3 std;
6
#include <bits/stdc++.h>
0{
std;
7 std;
8 std;
9
firstDigit(
4firstDigit(
5firstDigit(
2firstDigit(
7
#include <bits/stdc++.h>
0std;
7 int
2 int
firstDigit(
int
n)
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
8
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
1 firstDigit(
1firstDigit(
2firstDigit(
3
#include <bits/stdc++.h>
0{
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
6 #include <bits/stdc++.h>
7
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
8
#include <bits/stdc++.h>
0std;
7 int
2 int
using
0int
n)
#include <bits/stdc++.h>
0{
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
6 n)
4firstDigit(
2n)
6
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
02namespace
6
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
05
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
8
#include <bits/stdc++.h>
8
Python3
#include <bits/stdc++.h>
0std;
7 int
2 {
2 {
3
#include <bits/stdc++.h>
3int
{
8{
9firstDigit(
7
#include <bits/stdc++.h>
09 #include <bits/stdc++.h>
10
____10#include <bits/stdc++.h>
1 #include <bits/stdc++.h>
13#include <bits/stdc++.h>
14 firstDigit(
2#include <bits/stdc++.h>
16
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
18#include <bits/stdc++.h>
14 #include <bits/stdc++.h>
18#include <bits/stdc++.h>
21 firstDigit(
2firstDigit(
7
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
6 int
#include <bits/stdc++.h>
27
#include <bits/stdc++.h>
09 #include <bits/stdc++.h>
29
____10#include <bits/stdc++.h>
6 #include <bits/stdc++.h>
32#include <bits/stdc++.h>
33 firstDigit(
2firstDigit(
3
#include <bits/stdc++.h>
40#include <bits/stdc++.h>
46
C#
#include <bits/stdc++.h>
18#include <bits/stdc++.h>
14 {
9firstDigit(
7
std;
3 std;
4
std;
3 std;
6
#include <bits/stdc++.h>
0{
std;
7 std;
8 std;
9
firstDigit(
4#include <bits/stdc++.h>
4
#include <bits/stdc++.h>
0std;
7 int
2 int
firstDigit(
int
n)
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
8
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
1 firstDigit(
1firstDigit(
2firstDigit(
3
#include <bits/stdc++.h>
0{
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
6 #include <bits/stdc++.h>
7
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
8
#include <bits/stdc++.h>
0std;
7 int
2 int
using
0int
n)
#include <bits/stdc++.h>
0{
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
6 n)
4firstDigit(
2n)
6
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
96namespace
6
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
05
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
8
#include <bits/stdc++.h>
8
#include <bits/stdc++.h>0std;7 int2 {2 {3
using
03
#include <bits/stdc++.h>
3int
{
8{
9firstDigit(
7
{
#include <bits/stdc++.h>
09 #include <bits/stdc++.h>
10
____10#include <bits/stdc++.h>
1 #include <bits/stdc++.h>
13#include <bits/stdc++.h>
14 firstDigit(
2#include <bits/stdc++.h>
16
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
18#include <bits/stdc++.h>
14 #include <bits/stdc++.h>
18#include <bits/stdc++.h>
21 firstDigit(
2firstDigit(
7
#include <bits/stdc++.h>
8
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
6 int
#include <bits/stdc++.h>
27
{
#include <bits/stdc++.h>
09 #include <bits/stdc++.h>
29
#include <bits/stdc++.h>
8
____10#include <bits/stdc++.h>
6 #include <bits/stdc++.h>
32#include <bits/stdc++.h>
33 firstDigit(
2firstDigit(
3
#include <bits/stdc++.h>
18#include <bits/stdc++.h>
14 {
9firstDigit(
7
using
42using
0using
06using
39using
46firstDigit(
7
#include <bits/stdc++.h>40#include <bits/stdc++.h>41#include <bits/stdc++.h>14 namespace6firstDigit(3
using
48
using
#include <bits/stdc++.h>
48
#include <bits/stdc++.h>
0{
std;
7 std;
8 std;
9
#include <bits/stdc++.h>
0std;
7 int
2 int
firstDigit(
int
n)
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
1 firstDigit(
1firstDigit(
2firstDigit(
3
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
8
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
6 #include <bits/stdc++.h>
7
#include <bits/stdc++.h>
0{
#include <bits/stdc++.h>
0std;
7 int
2 int
using
0int
n)
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
8
using
74using
75
#include <bits/stdc++.h>
3using
77namespace
6
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
05
using
81
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
6 n)
4firstDigit(
2n)
6 O(log10n)
Auxiliary Space: O(1)
#include <bits/stdc++.h>
0std;
7 int
2 {
2 {
3
C++
#include <bits/stdc++.h>
Cách tiếp cận 1 (với vòng lặp):
#include <bits/stdc++.h>
{
using
namespace
std;
#include <bits/stdc++.h>
0using
99int
namespace
01namespace
02namespace
03
int
firstDigit(
int
n)
#include <bits/stdc++.h>
8
#include <bits/stdc++.h>
0____11 #include <bits/stdc++.h>
2
{
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
6 #include <bits/stdc++.h>
7
#include <bits/stdc++.h>
8
int
using
0int
n)
{
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
6 using
6
#include <bits/stdc++.h>
0namespace
5namespace
6
using
74namespace
8
int
using
9
#include <bits/stdc++.h>
8
#include <bits/stdc++.h>0int namespace3
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
6 std;
1
Java
std;
3 std;
4
#include <bits/stdc++.h>
0{
std;
3 std;
6
#include <bits/stdc++.h>
3using
99int
namespace
52int
namespace
54firstDigit(
2namespace
56
#include <bits/stdc++.h>
0std;
7 int
2 int
firstDigit(
int
n)
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
8
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
1 firstDigit(
1firstDigit(
2firstDigit(
3
#include <bits/stdc++.h>
0{
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
6 n)
4firstDigit(
2n)
6
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
8
#include <bits/stdc++.h>
0std;
7 int
2 {
2 namespace
81
#include <bits/stdc++.h>
0{
#include <bits/stdc++.h>
3int
{
8{
9firstDigit(
7
#include <bits/stdc++.h>
3namespace
90
namespace
91namespace
6 #include <bits/stdc++.h>
05
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
8
#include <bits/stdc++.h>
8
Python3
std;
3 namespace
98
#include <bits/stdc++.h>
09 #include <bits/stdc++.h>
10
Các
Các
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
6 #include <bits/stdc++.h>
7
#include <bits/stdc++.h>
09 #include <bits/stdc++.h>
29
____10#include <bits/stdc++.h>
6 #include <bits/stdc++.h>
32#include <bits/stdc++.h>
33 firstDigit(
2firstDigit(
3
#include <bits/stdc++.h>
18#include <bits/stdc++.h>
14 {
9firstDigit(
7
#include <bits/stdc++.h>
40#include <bits/stdc++.h>
41#include <bits/stdc++.h>
14 namespace
6firstDigit(
3
#include <bits/stdc++.h>
40#include <bits/stdc++.h>
46
C#
using
#include <bits/stdc++.h>
48
std;
8 namespace
35
#include <bits/stdc++.h>
0int
2 int
firstDigit(
int
n)
#include <bits/stdc++.h>
0{
#include <bits/stdc++.h>
3int
using
93int
std;
56
#include <bits/stdc++.h>
3using
99int
namespace
52int
std;
62
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
6 #include <bits/stdc++.h>
7
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
8
#include <bits/stdc++.h>
0int
2 int
using
0int
n)
#include <bits/stdc++.h>
0{
#include <bits/stdc++.h>
3#include <bits/stdc++.h>
6 using
6
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
8
____10std;
7 int
2 {
2 #include <bits/stdc++.h>
89
#include <bits/stdc++.h>
0{
#include <bits/stdc++.h>
3int
namespace
3
#include <bits/stdc++.h>
3std;
92
std;
93namespace
6 #include <bits/stdc++.h>
05
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
8
#include <bits/stdc++.h>
8
PHP
using
03
using
04 firstDigit(
using
06firstDigit(
3
{
#include <bits/stdc++.h>
0int
06 int
07using
06n)
6
Các
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
6 using
06firstDigit(
7
#include <bits/stdc++.h>
8
using
04 using
0using
06firstDigit(
3
{
____10#include <bits/stdc++.h>
6 using
11using
06 using
32
#include <bits/stdc++.h>
8
using
06 using
35
using
36 firstDigit(
using
06int
38namespace
6int
40
using
42using
0using
06int
44using
46firstDigit(
7
int
47
JavaScript
using
48
using
04 using
51
{
#include <bits/stdc++.h>
0int
53
#include <bits/stdc++.h>
0int
55
#include <bits/stdc++.h>
0#include <bits/stdc++.h>
6 #include <bits/stdc++.h>
7
#include <bits/stdc++.h>
8
#include <bits/stdc++.h>
09 #include <bits/stdc++.h>
29
____10#include <bits/stdc++.h>
6 #include <bits/stdc++.h>
32#include <bits/stdc++.h>
33 firstDigit(
2firstDigit(
3
#include <bits/stdc++.h>
8
using
75
int
67namespace
6firstDigit(
3
int
70int
71firstDigit(
3
using
81
#include <bits/stdc++.h>
18#include <bits/stdc++.h>
14 {
9firstDigit(
7O(1)
Auxiliary Space: O(1)
#include <bits/stdc++.h>
40#include <bits/stdc++.h>
41#include <bits/stdc++.h>
14 namespace
6firstDigit(
3log10() is a mathematical function present in math.h header
file. It returns log base 10 value of the passed parameter to log10() function.
print ("Nhập một số:", end = "") thử: num = int (input ()) đếm = 0 trong khi num! = 0: if Count == 0: last = num % 10 đếm = đếm + 1 rem = num % 10 num = int (num / 10) in ("\ nfirst Digit (", rem, ") + chữ số cuối cùng (", cuối cùng Đầu vào!")num = int(input()) count = 0 while num != 0: if count == 0: last = num % 10 count = count + 1 rem = num % 10 num = int(num / 10) print("\nFirst Digit (", rem, ") + Last Digit (", last, ") =", rem + last) except ValueError: print("\nInvalid Input!")