For More Problems Click On The bellow link
What is
the output of this C code?
#include<stdio.h>
void main()
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **p); }
void main()
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **p); }
5 5 5
2) 5 5 junk
3) 5 junk junk
4) Compile time error2)
2) 5 5 junk
3) 5 junk junk
4) Compile time error2)
Given the below statements about C programming language.
1)
main() function should always be the first function present in a C program file
2) all the elements of a union share their memory location
3) A void pointer can hold the address of any type and can be typecasted to any type
4) A static variable holds random junk value if it is not initialized
2) all the elements of a union share their memory location
3) A void pointer can hold the address of any type and can be typecasted to any type
4) A static variable holds random junk value if it is not initialized
Which
of the above are correct statements?
1)
2,3
2) 1,2
3) 1,2,3
4) 1,2,3,4
2) 1,2
3) 1,2,3
4) 1,2,3,4
Solution: Option 3
TCS Ninja placement papers – Coding section
This is
the most important section of all. If you fail to implement the code,
then very likely that you would not make to the next round. Hence focus on
this round is very important. TCS coding questions in TCS previous year
placement papers will give you an idea about the kind of questions asked.
1) Write
a C program to calculate the factorial of a non-negative integer N. The
factorial of a number N is defined as the product of all integers from 1 up to
N. Factorial of 0 is defined to be 1. The number N is a non-negative integer
that will be passed to the program as the first command line parameter. Write
the output to stdout formatted as an integer WITHOUT any other additional text.
You may assume that the input integer will be such that the output will not
exceed the largest possible integer that can be stored in an int type variable.
Kindly
note that certain keywords
like “scanf, getc, getch, getchar” cannot be used
while solving this problem.
#include<stdio.h>
int main(int a, char *b[]) //command line arguments
{
Int x,y,f=1;
x=atoi(b[1]); //atoi function is to convert a character to integer
for(i=1;i<=x;i++)
{
f=f*i;
}
printf("%d",f);
return 0; }
Nth Fibonacci Number using Command Line
Arguments
Using Command
Line Arguments
[code
language=”cpp”]
#include<stdio.h>
#include<stdlib.h>
int fib(int n)
{
int a=0,b=1,c,i;
if(n==0) return a;
for(i=2;i<=n;i++)
{
c=a+b;
a=b;
b=c;
}
return b;
}
int main(int argc, char * argv[])
{
if(argc==1)
{
printf("No arguments");
return 0;
}
else
{
int n;
n=atoi(argv[1]);
printf("%d",fib(n));
return 0;
}
}
#include<stdio.h>
#include<stdlib.h>
int fib(int n)
{
int a=0,b=1,c,i;
if(n==0) return a;
for(i=2;i<=n;i++)
{
c=a+b;
a=b;
b=c;
}
return b;
}
int main(int argc, char * argv[])
{
if(argc==1)
{
printf("No arguments");
return 0;
}
else
{
int n;
n=atoi(argv[1]);
printf("%d",fib(n));
return 0;
}
}
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.
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.
It is highly
advisable to go through Command Line
Arguments Post before even looking at the code. Please
study this for TCS and come back to this post later.
[code
language=”cpp”]
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<math.h>
bool isPrime(int n)
{
if(n<2)
return false;
int i;
for(i=2;i*i<=n;i++)
{
if(n%i==0)
return false;
}
return true;
}
int main(int argc, char *argv[])
{
if(argc==1)
{
printf("No arguments");
return 0;
}
else
{
int n;
n=atoi(argv[1]);
float sq=0;
if(isPrime(n))
{
sq=sqrt(n);
printf("%.2f",sq);
}
else
printf("%.2f",sq);
return 0;
}
}
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<math.h>
bool isPrime(int n)
{
if(n<2)
return false;
int i;
for(i=2;i*i<=n;i++)
{
if(n%i==0)
return false;
}
return true;
}
int main(int argc, char *argv[])
{
if(argc==1)
{
printf("No arguments");
return 0;
}
else
{
int n;
n=atoi(argv[1]);
float sq=0;
if(isPrime(n))
{
sq=sqrt(n);
printf("%.2f",sq);
}
else
printf("%.2f",sq);
return 0;
}
}
Command Line program to find Armstrong number using Command line
arguments
A number is called Armstrong number if the Sum of the
cubes of its digits is equal to the number itself.
C program to find Armstrong number using Command line arguments
The following is a C program to check whether the given number
is Armstrong number or not using command line arguments.
It is
highly advisable to go through Command Line
Arguments Post before even looking at the code. Please
study this for TCS and come back to this post later
[code
language=”cpp”]
#include<stdio.h>
#include<stdlib.h>
#include<stdlib.h>
int main(int
argc,char *argv[])
{
int Given_number= atoi(argv[1]);
int num;
for(num=1; num<=Given_number; num++)
{
int a=num;
int s=0;
int r=0;
{
int Given_number= atoi(argv[1]);
int num;
for(num=1; num<=Given_number; num++)
{
int a=num;
int s=0;
int r=0;
while(a>0)
{
s=a%10;
r=r+(s*s*s);
a=a/10;
}
if(r==num)
printf(" %d isarmstrong no \n", num);
}
}
{
s=a%10;
r=r+(s*s*s);
a=a/10;
}
if(r==num)
printf(" %d isarmstrong no \n", num);
}
}
[/code]
Code – 2
[code
language=”cpp”]
#include<stdio.h>
void main(int
argc, char * argv[])
{
int
num,num1,arms=0,rem;
if ( argc !=
2 )
{
printf("Enter
the number:\n");
scanf("%d",&num);
}
else
{
num =
atoi(argv[1]);
}
num1=num;
while(num>0)
{
rem=num%10;
arms=arms+rem*rem*rem;
num=num/10;
}
if(num1==arms)
{
printf("
\n%d is an Armstrong number",num1);
}
else
{
printf("\n%d
is NOT an Armstrong number",num1);
}
}
Command Line Program to Convert Binary to Octal
This is a very
smart program very short and quick method –
#include<stdio.h>
void main(int argc,char *argv[])
{
long int n,r,c,b=1,s=0;
n=atoi(argv[1]);
c=n;
while(c!=0)
{
r=c%10;
s=s+r*b;
c=c/10;
b=b*2;
}
printf("%lo",s);
getch();
}
Ques. Write
a code to check whether no is prime or not. Condition use function check() to
find whether entered no is positive or negative ,if negative then enter the
no, And if yes pas no as a parameter to prime() and check whether no is
prime or not?
(TCS Ninja – June 2019 Slot 2)
#include<bits/stdc++.h> using namespace std; void prime(int num){ int count=0; for(int i=2;i<num;i++){ if(num%i==0){ count++; break; } } if(count==0){ cout<<"prime"<<endl; } else{ cout<<"Not Prime"<<endl; } } int main(){ int n; cout<<"Enter the number: "; cin>>n; if(n>0){ prime(n); } else{ cout<<"negative number.Please enter a postive number"<<endl; } return 0; }
Ques. Find the 15th term
of the series?
0,0,7,6,14,12,21,18,
28
#include int a1(int x); int a2(int y); void main() { int n; scanf("%d",&n); if(n%2==0) a1(n/2); else a2(n/2+1); } int a1(int x) { int s=0; for(int i=0;i<x-1;i++) { s=s+6; } printf("%d",s); } int a2(int x) { int s=0; for(int i=0;i<x-1;i++) { s=s+7; } printf("%d",s); }
No comments:
Post a Comment