Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.

Program Discussion :: Basics
Home > Programs > Basics

310. 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.

Answer:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
  int n, i, flag = 0;
  double x;

  if (argc == 1 || argc > 2)
  {
    printf("Enter String \r\n");
    exit(0);
  }

    n = atoi(argv[1]);
    x = n;
    for(i=2; i<= n/2; ++i)
    {
        if(n%i == 0)
        {
            flag=1;
            break;
        }
    }
    if (flag==0)
        printf("%.2lf \r\n", sqrt (x) );
    else
        printf("%.2lf \r\n",0.00);
return 0;
}

Post Your Answer Here:

Name *
Email

Language:

Post Your Reply Here: