Sequence filter in array in C

Filter the elements (having sequence) from the given array in C.

#include <stdio.h>
int main()
{
	int arrIN[]={1111,2222,3331,44555,7777}; // input array
	int arrOUT[5]; // output array
	
	int i,num;
	int outIndex=0; // index for output array
	int refV; // lastDigit to check
	int flag;
	
 for(i=0;i<5;i++)
 {
    num = arrIN[i]; // get the current array element
    refV = num%10; // get the last digit
    flag = 1; 

  	while(num>0)
	 {
	 	if(num%10!=refV) // checking the sequence 
		 {
		   flag = 0; 
		   break; // break the loop if sequence is not matched
		 }
		num = num/10; 
	 }
	
	if(flag) // if flag is true add current number to output array.
	{
	  arrOUT[outIndex] = arrIN[i];
	  outIndex++;
	}
 }

// print the output
for(i=0;i<outIndex;i++)
{
	printf("%d \n",arrOUT[i]);
}
	
	
	return 0;
}
1111
2222
7777

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.