Identify whether a binary series or number is divisible by four or not using C or C++ language


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    int table[4][2]={{0,1},{2,3},{0,1},{2,3}};
    int state=0, check=1, i;
    char str[100];
    cout<<"Enter a String : ";
    gets(str);
    for(i=0; i<strlen(str); i++)
    {
        if(str[i]=='0')
            state=table[state][0];
        else if(str[i]=='1')
            state=table[state][1];
        else
            check=0;
    }
    if(state==0 && check==1)
        cout<<"This is a valid string";
    else
        cout<<"This is not a valid string";
    return 0;
}


Sample input: 
Enter a String : 1000

Sample output:
This is a valid string

Post a Comment

Previous Post Next Post