Divisible by 5 Solution

Write a program which accepts a sequence of comma separated 4 digit binary numbers as its input and then check whether they are divisible by 5 or not. The numbers that are divisible by 5 are to be printed in a comma separated sequence.

Example: Input: 0100,0011,1010,1001 Output: 1010

Input Format

The input line consists comma separated binary digits

Constraints

No constraints

Output Format

The output line displays the numbers that are divisible by 5 in a comma separated sequence

Sample Input 0

0100,0011,1010,1001

Sample Output 0

1010





Program :

 #include<stdio.h>

int btd(int n)

{

    int num=n;

    int dec_value=0;

    int base=1;

    int temp=num;

    while(temp)

    {

        int last_digit=temp%10;

        temp=temp/10;

        dec_value+=last_digit*base;

        base=base*2;

    }

    return dec_value;

}

int main()

{

    int n[100],i;

    for(i=0;i<4;i++)

        scanf("%d,",&n[i]);

    for(i=0;i<4;i++)

    {

        if(btd(n[i])%5==0&&btd(n[i+1])%5==0)

            printf("%d,",n[i]);

        else if(btd(n[i])%5==0)

            printf("%d",n[i]);

    }

}

Post a Comment

0 Comments