Sunday, 22 October 2017

TCS - C Coding Question

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.
           


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;


}

16 comments:

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

    ReplyDelete
    Replies
    1. Yes there are some set of questions ...This Ques was one of them

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

    ReplyDelete
    Replies
    1. No...u can also use sqtr()
      Both the way are correct ☺️

      Delete
  3. Replies
    1. Yes you can but if u are asked to write without using math.h then try in this way

      Delete
  4. #include
    #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;
    }

    ReplyDelete
    Replies
    1. 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
  5. #include
    #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");
    }

    ReplyDelete
    Replies
    1. Your code will not pass the test case if it is not prime
      U can check your ans according to question in any compiler

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

      Delete
    3. As 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.

      Delete
  6. how to know to wright our program using simple c or command line argument in question they do not mention

    ReplyDelete
    Replies
    1. It 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

Featured Post

TCS Latest Test Patern ( For On-Campus Drives )

Tata Consultancy Services is software services consulting company whose  headquarter is situated in Mumbai, India. It is the Asia’s larges...