编写一个 C++ 程序,用示例查找数字的首位和末位数字之和。这里,我们首先找到给定数字的首位和末位数字。接下来,我们对它们进行算术加法。
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int number, firstDigit, lastDigit, sum, count;
cout << "\nPlease Enter Number to find Sum of First and Last Digit = ";
cin >> number;
count = log10(number);
firstDigit = number / pow(10, count);
lastDigit = number % 10;
sum = firstDigit + lastDigit;
cout << "\nTotal Number of Digit in a Given Number " << number << " = " << count + 1;
cout << "\nThe First Digit in a Given Number " << number << " = " << firstDigit;
cout << "\nThe Last Digit in a Given Number " << number << " = " << lastDigit;
cout << "\nThe Sum of First and Last Digit of " << number << " = " << sum;
return 0;
}

C++ 程序查找数字的首位和末位数字之和 示例 2
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int number, firstDigit, lastDigit, sum;
cout << "\nPlease Enter Number to find Sum of First and Last Digit = ";
cin >> number;
for(firstDigit = number; firstDigit >= 10; firstDigit = firstDigit/10);
lastDigit = number % 10;
sum = firstDigit + lastDigit;
cout << "\nThe First Digit in a Given Number " << number << " = " << firstDigit;
cout << "\nThe Last Digit in a Given Number " << number << " = " << lastDigit;
cout << "\nThe Sum of First and Last Digit of " << number << " = " << sum;
return 0;
}
Please Enter Number to find Sum of First and Last Digit = 476492
The First Digit in a Given Number 476492 = 4
The Last Digit in a Given Number 476492 = 2
The Sum of First and Last Digit of 476492 = 6
在这个示例中,它查找数字的首位和末位数字之和,我们使用 while 循环来获取首位数字。
#include<iostream>
using namespace std;
int main()
{
int number, firstDigit, lastDigit, sum;
cout << "\nPlease Enter Number to find Sum of First and Last Digit = ";
cin >> number;
firstDigit = number;
while(firstDigit >= 10)
{
firstDigit = firstDigit/10;
}
lastDigit = number % 10;
sum = firstDigit + lastDigit;
cout << "\nThe First Digit in a Given Number " << number << " = " << firstDigit;
cout << "\nThe Last Digit in a Given Number " << number << " = " << lastDigit;
cout << "\nThe Sum of First and Last Digit of " << number << " = " << sum;
return 0;
}
Please Enter Number to find Sum of First and Last Digit = 98548
The First Digit in a Given Number 98548 = 9
The Last Digit in a Given Number 98548 = 8
The Sum of First and Last Digit of 98548 = 17
使用函数查找数字的首位和末位数字之和的程序
#include<iostream>
using namespace std;
int firstDigitofNumber(int num)
{
while(num >= 10 )
{
num = num / 10;
}
return num;
}
int lastDigitofNumber(int num)
{
return num % 10;
}
int main()
{
int number, firstDigit, lastDigit, sum;
cout << "\nPlease Enter Number to find Sum of First and Last Digit = ";
cin >> number;
firstDigit = firstDigitofNumber(number);
lastDigit = lastDigit = lastDigitofNumber(number);
sum = firstDigit + lastDigit;
cout << "\nThe First Digit in a Given Number " << number << " = " << firstDigit;
cout << "\nThe Last Digit in a Given Number " << number << " = " << lastDigit;
cout << "\nThe Sum of First and Last Digit of " << number << " = " << sum;
return 0;
}
Please Enter Number to find Sum of First and Last Digit = 178965
The First Digit in a Given Number 178965 = 1
The Last Digit in a Given Number 178965 = 5
The Sum of First and Last Digit of 178965 = 6