Friday, 20 December 2013

CPP Code to check for a Prime Number(in Assembly like structure).

#include<iostream>
using namespace std;

int main()
{
    int n,i,flag,tempn,divisor,sum,rem;
    cout<<"Enter N : ";
    cin>>n;
    divisor=2;
    flag=0;
    i=0;
    tempn=n;

//    calculating divison by 2    i=n/2    Start;

here1:
    if(tempn<=0)
        goto there1;
    tempn=tempn-divisor;
    i=i+1;
    goto here1;
there1:
    if(tempn!=0)
        i--;
//    calculating divison by 2    i=n/2    End;

here:
    divisor=i;
    if(i-2<0)
        goto there;
    sum=0;
here2:
    if(n-sum<=0)
        goto there2;
    sum=sum+divisor;
    goto here2;
there2:   
    if(sum-n!=0)
        sum=sum-divisor;
    rem=n-sum;

   
    if(rem==0)
    {
        flag=1;
        goto there;
    }
    i=i-1;
    goto here;
there:
    if(flag==1)
        cout<<"\n"<<n<<"   NA its not prime\n\n";
    else
        cout<<"\n"<<n<<"   Yes its prime\n\n";
   
    return 0;
}

No comments:

Post a Comment