You are on page 1of 5

PREP INSTA (HTTP://PREPINSTA.

COM/)
Prepare Better for Placement Papers

Login

SignUp

Home (http://prepinsta.com/)

All Companies (http://prepinsta.com/all-companies-dashboard/)

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/) +

TCS Ninja (http://prepinsta.com/tcs-ninja/) +

Success Stories (http://prepinsta.com/stars/)

Contact Us (http://prepinsta.com/contact/)
About Us (http://prepinsta.com/about/)

Refund Policy (http://prepinsta.com/refund-policy/)

Privacy Policy (http://prepinsta.com/privacy-policy/)

Services (http://prepinsta.com/services/)

Disclaimer (http://prepinsta.com/disclaimer/)

Terms and Conditions (http://prepinsta.com/terms-and-conditions/)

 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)

Square Root of Prime Number using


Command Line Argument
The square root of a Prime number by checking first if it is a prime number?

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

Review Date 2017-12-05

Reviewed Item TCS Square Root Program

Author Rating

5 Comments PrepInsta 
1 Login

 Recommend 10 t Tweet f Share Sort by Best

Join the discussion…

LOG IN WITH OR SIGN UP WITH DISQUS ?

Name

Sanjana Bhowmick • 3 months ago


For batch of 2015-19, who are appearing on 2nd September 2018, is programming with
command line argument is necessary and also using functions ?
1△ ▽ • Reply • Share ›

kritika • 3 months ago


on compiling this,i get an error:
/tmp/ccPm1Gg3.o: In function `main
jdoodle.c':(.text+0xa8:): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
Someone plz help me.
△ ▽ • Reply • Share ›

vineel • 3 months ago


can anyone provide me the optimised code for Square Root of Prime Number using
Command Line Argument
Thank you,
△ ▽ • Reply • Share ›

ANIT WAHI > vineel • 3 months ago


Try this
Reply Share ›
HOME (HTTP://PREPINSTA.COM/) ALL COMPANIES
(HTTP://PREPINSTA.COM/ALL-COMPANIES-DASHBOARD/) 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/) TCS NINJA (HTTP://PREPINSTA.COM/TCS-
NINJA/) SUCCESS STORIES (HTTP://PREPINSTA.COM/STARS/) CONTACT US
(HTTP://PREPINSTA.COM/CONTACT/) ABOUT US
(HTTP://PREPINSTA.COM/ABOUT/) REFUND POLICY
(HTTP://PREPINSTA.COM/REFUND-POLICY/) PRIVACY POLICY
(HTTP://PREPINSTA.COM/PRIVACY-POLICY/) SERVICES
(HTTP://PREPINSTA.COM/SERVICES/) DISCLAIMER
(HTTP://PREPINSTA.COM/DISCLAIMER/) TERMS AND CONDITIONS
(HTTP://PREPINSTA.COM/TERMS-AND-CONDITIONS/)

Copyright © 2018 Prep Insta — Escapade WordPress theme by GoDaddy (https://www.godaddy.com/)

You might also like