TCS Doc-3-1
TCS Doc-3-1
a) strcpy()
b)strcat()
c)strptr()
d)strlen()
Explanation:
#include<stdio.h>
int main()
float f=0.1;
if(f==0.1)
printf(“yes”);
else
printf(“no”);
return 0;
a)yes
b)no
4. Output:
int main()
char *str1=”Hello”;
char *str2=”Hello”;
if(str1==str2)
printf(“Same”);
else
printf(“different”);
return 0;
a) runtime error
c) same
d) different
Two pointers pointing to the same string, hence the both pointers point to the
same base address, hence the output is same.
5. Output:
int main()
int i=5;
if(i==5)
i==7;
else
i==10;
printf(“%d”,i);
return 0;
a)5
b)0
c)1
d)10
Output is 5
a) dealloc()
b) release()
c) freemem()
d)free()
b) char* argv[]
c) char **argv
d) char argv;
a) long
b) float
c) double
9 In the below code the program expects the user to enter a word .If the user
enters the word as “ABRACADABRA”, what is the output value printed??
#include<stdio.h>
#define MAX 20
char *fun(char[]);
int main()
char str[MAX];
char *rev;
scanf(“%s”,str);
r=fun(str);
printf(“%s”,r);
return 0;
if(*str)
fun(str+1);
r[i++]=*str;
Answer:
Compilation Error
If the variable is declared the answer would be the reverse of the string .
#include<stdio.h>
int main()
int n;
scanf(“%d”,&n);
printf(“factorial of %d is %ld”,n,fact(n));
return 0;
if(n>=1)
return n*fact(n-1);
else
return 1;
a) Segmentation
b) Iteration
c) Recursion
d) Encapsulation
d) If a function explicitly does not return a value ,then default value of 0 will be
returned
Coding Questions
1. Write a C program that will find the area of a Circle,Given the diameter .
The value D will be given a positive integer number that will be made available
to the program through its first command line parameter .Consider the value
of PI as 3.14. print the output to stdout in floating point format with exactly 2
decimal precision other than the floating point number , no other extra
information should be printed to stdout.
Given
INPUT: D=10
OUTPUT: 78.50
float d;
float area;
if(argc!=2)
exit(1);
r=atoi(argv[1]);
area=3.14*d/2.0*d/2.0;
area=floor((area));
printf(“%7.2f”,area);
return 0;
Explanation:
1. If the argument count is not equal to 2 then exit from the program .
Solution:
if(argc!=2)
exit(1);
int n=atoi(argv[1]);
int i,c=0;
for(i=2;i<=n/2;i++)
if(n%i==0)
c++;
if(c==1)
printf(“not prime”);
else
printf(“prime”);
return 0;
}
3. Write a C program which will convert a given binary number N to its
decimal equivalent, the given binary number n will be passed to the
program using the first command line parameter print the equivalent
decimal number to stdout in integer format other than the decimal
number; no other extra information should be printed to stdout.
INPUT: 111001
OUTPUT: 57
int n,r,sum=0,p=1;
n=atoi(argv[1]);
while(n!=0)
r=n%2;
n=n/2;
sum=sum+p*r;
p=p*10;
printf(“%d”,sum);
return 0;
4. Write a C program that will find the sum of all prime numbers in a given
range .The Range will be specified as command line parameters .The first
command line parameter,N1 which is a positive integer, will contain the lower
bound of the range .The Second command line parameter N2 ,which is also a
positive integer will contain the upper bound of the range .The Program shoud
consider all the prime numbers within the range, excluding the upper bound
and lower bound . Print the output in integer format in stdout. other than the
integer number , no extra information should be printed to stdout
int main(int argc,char *argv[])
int i,j,sum=0,start,end;
if(argc!=3)
exit(1);
start=atoi(argv[1]);
end=atoi(argv[2]);
for(i=start;i<end;i++)
for(j=2;j<=i/2;j++)
if(i%j==0)
break;
if(j>i/2)
sum=sum*i;
printf(“%d”,sum);
return 0;
Explanation:
Other than the word YES or NO, no extra information should be printed to
stdout.
if(argc!=2)
exit(1);
int i=0,c=0,j;
for(i=;argv[1][i]!=‘\0’;i++);
c=i;
for(i=0,j=c-1;i<j;i++,j--)
if(argv[1][i]!=argv[1][j];
printf(“NO”);
exit(1);
else
printf(“YES”);
return 0;
6. Write a C program that will find the length of hypotenuse of a right angle
triangle given the length of other sides S1 and S2. The values of the other 2
sides S1 and S2 will be passed by the program through the first and second
command line parameters. The command line parameters will be of positive
integer type. Print the output to stdout in floating point format with exactly
2decimal precision. Other than the floating point number no extra information
should be printed to stdout.
INPUT: 10, 20
OUTPUT: 22.36
float ans;
int num=atoi(argv[1]);
int num1=atoi(argv[2]);
num=num*num;
num1=num1*num1;
ans=float(sqrt(num+num1));
printf(“%f”,ans);
return 0;
}
float sqrt(float n)
float x=n;
float y =1;
float e=0.000001;
while(x-y>e)
x=(x+y)/2;
y=n/x;
return x;
int num=atoi(argv[1]);
int rem,rev=0,num1,i,count=0;
num=num1;
while(num>0)
rem=num%10;
rev=rem+rev*10;
num=num/10;
count++;
if(count==5)
if(num1==rev)
printf(“YES”);
else
Printf(“NO”);
return 0;