**Ques:Write a C program which will chech 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.**

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;

}

Its a great help. IS these questions have been asked in tcs campus drive?

ReplyDeleteYes there are some set of questions ...This Ques was one of them

Deleteis it necessary for this question to print the square root without using sqrt() function? Since it is not said in question.

ReplyDeleteNo...u can also use sqtr()

DeleteBoth the way are correct ☺️

Thanks for your help

DeleteCan we use math.h ?

ReplyDeleteYes you can but if u are asked to write without using math.h then try in this way

Delete#include

ReplyDelete#include

#include

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;

}

Thats a bit okk, but first print statement is totally illogical according to question. Your code can be improved in some places then only its okk

Delete#include

ReplyDelete#include

int main(int argc,char *argv[])

{

int a,b,i,flag=0;

a=atoi(argv[1]);

for(i=2;i<a/2;++i)

{

if(a%i==0)

{

flag=1;

break;

}

}

if(flag==0)

printf("Root is %.2f",sqrt(a));

else

printf("Result is 0");

}

Your code will not pass the test case if it is not prime

DeleteU can check your ans according to question in any compiler

Can you tell me why if(count==2) is used here

DeleteAs Its a program for Prime number and we know that prime no. have only two factors i.e, 1 and itself. So the value of count is 2. In the given question it is mentioned to calculate squareroot only if its a prime. So we first check whether its prime or not and if its a prime then we will print its Square root.

DeleteYes you are right,, thanx

ReplyDeletehow to know to wright our program using simple c or command line argument in question they do not mention

ReplyDeleteIt is mentioned in question, u need to understand where it has been written. In the above question, it is mentioned in 3rd last line

Delete