Ques:Write a C program which will check whether a given number N is prime or not. If the number N is prime, then find its square root and print that value to stdout as floating point number with exactly 2 decimal precision. If the given number N is not prime, then print the value 0.00 to stdout. The given number N will be a positive non Zero integer and it will be passed to the program using the first command line parameter. Other than the floating point result, no other information should be printed to stdout.
           


e>

          Solution 1: without using sqrt() / User Defined


#include<stdio.h>
int main(int argc, char*argv[])
{
    int no,i,count=0;
    float sq=0;
    float j1=0.0001,i1;
    if(argc!=2)
        exit(1);
    no=atoi(argv[1]);
    for(i=1;i<=no;i++)
    {
        if(no%i==0)
            count++;
    }
    if(count==2)
    {       //to calculate square root without sqtr()
            for(i1=0;i1<no;i1=i1+j1)
            {
                if((i1*i1)>no)
                    break;
            }
            sq=i1-j1;
            printf("%.2f",sq);

    }

    else
        printf("%.2f",sq);
    return 0;
}


Solution 2: using sqrt()  


#include<stdio.h>
#include<math.h>
int main(int argc, char*argv[])
{
    int no,i,count=0;
    float sq=0;
    if(argc!=2)
        exit(1);
    no=atoi(argv[1]);
    for(i=1;i<=no;i++)
    {
        if(no%i==0)
            count++;
    }
    if(count==2)
    {       //to calculate square root using sqtr()
            sq=sqrt(no);
            printf("%.2f",sq);

    }
    else
        printf("%.2f",sq);
    return 0;


}