C++ Program to Check if All Digits of Number Are Same or Not using For Loop
Write C++ Program to Check if All Digits of Number Are Same or Not using For Loop
// CPP Program to Check if All Digits of Number Are Same or Not using For Loop
#include <iostream>
#include <string.h>
using namespace std;
char* check_digits(int n)
{
int last_digit = n % 10;
for(; n != 0; )
{
int digit = n % 10;
if(last_digit != digit)
{
return "All Digits are not same";
}
n = n / 10;
}
return "All digits are same";
}
int main()
{
int n;
cout << "Enter the number :--> ";
cin >> n;
cout << check_digits(n);
return 0;
}
Output:
Run 1:
Enter the number :--> 111
Numbers are same
Run 2:
Enter the number :--> 121
Numbers are not same