C++ Program to Check if All Digits of Number Are Same or Not using While Loop

Write C++ Program to Check if All Digits of Number Are Same or Not using While Loop

// CPP Program to Check if All Digits of Number Are Same or Not using While Loop

#include <iostream>
#include <string.h>

using namespace std;

char* check_digits(int n)
{
	int last_digit = n % 10;
	while(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

≪ Previous | Next ≫