Wednesday 6 June 2018

C program to print a number if next number has the same unit digit

Next Number Same Unit Digit

An array of N integers are given as input. The program must print the integers only if the unit digit
of the current integer and the unit digit of the next integer are same

Boundary Condition
2 <= N <= 1000

Program
***********

#include <stdio.h>

int main()
{
    int N,a[1000],i,j;

    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%d",&a[i]);
    }    
    if (N>=2 && N<=1000)
    {
        for(i=0;i<N-1;i++)
        {
            j=i+1;
           if((a[i]%10)==(a[j]%10))
           {
               printf("%d\t",a[i]);
           }    
        
        }
    }
        return 0;
}

Example
Input
6
12 834 94 485 285 905

Output
834 485 285

No comments:

Post a Comment