Professional Documents
Culture Documents
COM/)
Prepare Better for Placement Papers
Login
SignUp
Home (http://prepinsta.com/)
Wheebox (http://prepinsta.com/wheebox/)
TCS (http://prepinsta.com/tcs-placement-papers/) +
Accenture (http://prepinsta.com/accenture-previous-papers/) +
Deloitte (http://prepinsta.com/deloitte/) +
Wipro (http://prepinsta.com/wipro/) +
Capgemini (http://prepinsta.com/capgemini/) +
CoCubes (http://prepinsta.com/cocubes/) +
Cognizant (http://prepinsta.com/cognizant/) +
eLitmus (http://prepinsta.com/elitmus/)
IBM (http://prepinsta.com/ibm/) +
Infosys (http://prepinsta.com/infosys/) +
Mindtree (http://prepinsta.com/mindtree/) +
Mphasis (http://prepinsta.com/mphasis/) +
Contact Us (http://prepinsta.com/contact/)
About Us (http://prepinsta.com/about/)
Services (http://prepinsta.com/services/)
Disclaimer (http://prepinsta.com/disclaimer/)
FACEBOOK (HTTPS://WWW.FACEBOOK.COM/PREPINSTA/)
FOLLOW ON G+ (HTTPS://PLUS.GOOGLE.COM/101604564915439709306)
TWITTER (HTTPS://TWITTER.COM/PREPINSTA)
LINKEDIN (HTTPS://IN.LINKEDIN.COM/COMPANY/PREPINSTA)
Write a C program which will check whether a given number N is a Prime or Not. If
the Number N is a Prime, then find it’s square root and print that value to the
STDOUT as floating point number with exactly 2 decimal precision.
If the number is not Prime, then print the value 0.00 to STDOUT.
The given number will be positive non zero integer and it will be passed to the
program as first command line argument.
Other than floating point No other information should be printed to STDOUT.
Also, you can study other Command Line Programming Questions here on our TCS
Dashboard (http://prepinsta.com/tcs-coding-questions/).
It is highly advisable to go through Command Line Arguments Post
(http://prepinsta.com/solve-command-line-arguments-coding-
questions/#theory) before even looking at the code. Please study this for TCS and
come back to this post later.
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<stdbool.h>
4 #include<math.h>
5 bool isPrime(int n)
6 {
7 if(n<2)
8 return false;
9 int i;
10 for(i=2;i*i<=n;i++)
11 {
12 if(n%i==0)
13 return false;
14 }
15 return true;
16 }
17 int main(int argc, char *argv[])
18 {
19 if(argc==1)
20 {
21 printf("No arguments");
22 return 0;
23 }
24 else
25 {
26 int n;
27 n=atoi(argv[1]);
28 float sq=0;
29 if(isPrime(n))
30 {
31 sq=sqrt(n);
32 printf("%.2f",sq);
33 }
34 else
35 printf("%.2f",sq);
36 return 0;
37 }
38 }
Summary
Reviewer Chinoo
Author Rating
5 Comments PrepInsta
1 Login
Name