Friday, August 19

PROBLEM: Make a program that will accept an 'n' number of inputs in Binary Number System and counts the number of '1'. If the number of '1' is prime, it will print the input with its corresponding equivalent in Decimal Number System, else, return nothing.



#include <cstdlib>
#include <iostream>
#include <math.h>

using namespace std;

int main(int argc, char *argv[])
{
    long input,input1,input2;
    long A[100],B[100],I[100];
    long x,y,z,a=0,b,c=0,d=0,e,f,sum=0,i=0,j=0,num;
    cout<<"Number of Inputs:  ";
    cin>>num;
 
    for(;;){
         
            d=0;a=0;c=0;
            sum=0;
            if(j==num){break;}
    cout<<"\t->("<<i+1<<")Enter Binary:  ";
    cin>>input;
    I[i]=input;
    i++;
    for(;;){
            A[a]=input%10;
            b=input/10;
            a++;
            input=b;
            if(b==0){break;}
            }
    for(x=0;x<a;x++){
                      if(A[x]==1){c++;}
                      }
    for(z=c-1;z>1;z--){
                       if(c%z==0){d++;}
                       }
    if(d==0){
             for(f=0;f<a;f++){
             sum+=A[f]*pow(2,f);
             }
             }
             B[j]=sum;
             j++;
             }
    for(e=0;e<num;e++){
                       if(B[e]!=0){
    cout<<"\n\t\t"<<"Input"<<e+1<<": "<<I[e]<<"  |  "<<B[e]<<"\n";
                                     }
             }
           
             cout<<"\n\n\n";
           
    system("PAUSE");
    return EXIT_SUCCESS;
}



/*Program Preview*/


No comments:

Post a Comment