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

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

Write a Program to Check if All Digits of Number Are Same or Not using For Loop

// C Program to Check if All Digits of Number Are Same or Not using For Loop

#include <stdio.h>
#include <string.h>

int 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;
	printf("Enter the number :--> ");
	scanf("%d", &n);

	printf("%s ",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