C++ Program to Print First and Last Digit of Number using For Loop
Write C++ Program to Print First and Last Digit of Number using For Loop
// CPP Program to Print First and Last Digit of Number using For Loop
#include <iostream>
using namespace std;
int main()
{
int n, first_digit,last_digit;
cout << "Enter the Number :--> ";
cin >> n;
last_digit = n % 10; //last digit of a number
for(; n > 0; )
{
first_digit = n % 10; //first digit of a number
n = n / 10;
}
cout << "The first digit of the number is : " << first_digit;
cout << "\nThe last digit of the number id : " << last_digit;
return 0;
}
Output:
Enter the Number :--> 98745
The first digit of the number is : 9
The last digit of the number id : 5