Write a program in C++ to find the first and last digit of a number

  • برمجة سي بلس بلس
  • برمجة

Write a program in C++ to find the first and last digit of a number

Sample Output:

 Find the first and last digit of a number:                            
-----------------------------------------------                        
 Input any number: 5679                                                
 The first digit of 5679 is: 5                                         
 The last digit of 5679 is: 9

الأجوبة

#include <iostream>
using namespace std;

int main()
{
   int n,first,last;
    cout << "\n\n Find the first and last digit of a number:\n";
    cout << "-----------------------------------------------\n";
    cout << " Input any number: ";
    cin >> n;
    first = n;
	last=n % 10;
	for(first=n;first>=10;first=first/10);
    cout<<" The first digit of "<<n<<" is: "<<first<<endl;
    cout<<" The last digit of "<<n<<" is: "<<last<<endl;
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...