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

Tuesday, 5 June 2018

Program to count the number of alphabets occurring in all the given N strings

Common Alphabets in N strings

N strings are passed as inputs to the program. Each string will contain only the alphabets a-z in lower case. A given alphabet may be repeated any number of times. The program must print the count C of the alphabets that are present in all the N string values.

Boundary conditions:
2 <= N <= 500
Maximum length of each string = 1000

Program:

********
#include <stdio.h>

#include <string.h>


int main()
{
    char a[500][1000],test[500];
    int i,N,count=0,j,k,len;
    
    scanf("%d",&N);
    if (N>=2 && N<=500)
    {
        for(i=0;i<N;i++)
            scanf("%s",a[i]);
        i=0; j=0;
        len=strlen(a[0]);
       
        while (a[0][i]!='\0')
        {
            for(k=0;k<j;k++)
            {
                if (a[0][i]!=test[k])
                    continue;
                else
                    break;
            }
            if (k>=j)
                test[j++]=a[0][i];
            ++i;
        }
        test[j]='\0';
        printf("%s",test);
        i=0; count=0;
        while (test[i]!='\0')
        {
            for(j=1;j<N;j++)
            {
                if (strchr(a[j],test[i]))
                 continue;
                else
                  break;
            }
            if (j>=N)
                ++count;
            ++i;
        }
        printf("%d",count);
    }
            
   return 0;
}

Example Input/Output:
3
mnppqqr
ajkmnnm
poormanagement

Output:
2

Explanation:
Only two alphabets m and n are present in all the three strings.