C++ 程序查找数字的最后一位

编写一个 C++ 程序,通过示例查找数字的最后一位。任何数字对十取模都将得到该数字的最后一位,在本程序中,我们使用了相同的方法。

#include<iostream>

using namespace std;

int main()
{
	int number, lastDigit;
	
	cout << "\nPlease Enter Any Number to find Last Digit =  ";
	cin >> number;
  	
  	lastDigit = number % 10;
  	
	cout << "\nThe Last Digit in a Given Number " << number << " = " << lastDigit; 
		
 	return 0;
}
Please Enter Any Number to find Last Digit =  5789

The Last Digit in a Given Number 5789 = 9

使用函数的 C++ 程序查找数字的最后一位

#include<iostream>

using namespace std;

int lastDigitofNumber(int num)
{
	return num % 10;
}

int main()
{
	int number, lastDigit;
	
	cout << "\nPlease Enter Any Number to find Last Digit =  ";
	cin >> number;
  	
  	lastDigit = lastDigitofNumber(number);
  	
	cout << "\nThe Last Digit in a Given Number " << number << " = " << lastDigit; 
		
 	return 0;
}
C++ Program to find the Last Digit of a Number