Top 20 Coding Program Ask In Exam & Interview

Happy New Year 2022

Top 20 Coding Program Ask In Exam & Interview –

There is only one question for 20 minutes.

  1. It has 10 attempts(We can compile only 10 times).
  2. We must start our code from the scratch.
  3. The coding platform is divided into two, one for writing the code and other for output. We should write the whole program.
  4. We can’t use any input functions like scanf(), getch(), getchar().
  5. The input to be provided should be read as command line arguments.

We must only print exact output in Command Line Arguments Questions for TCS
Procedure –

  1. Output must not be re-framed by extra words.
  2. If there is any error, the error will be shown in the output dialog box.
  3. The errors are clearly mentioned.
  4. If there are no errors, a message like “compiled successfully” will be printed.
  5. Along with that they will mention four test cases are ‘passed’ or maybe ‘failed’.
  6. They are indicated like private and public test cases. They have not mentioned what is the test case, which is difficult to understand in TCS Command Line Arguments Programs.

Dont Compile again and again since compiler takes 25 seconds and each time you compile 25 seconds will become lesser in the time you have to code.

1 Command Line Program for Odd Even

#include <stdio.h>
int main(int argc, char *argv[])
{
int number;
number = atol(argv[1]);
if(number % 2 == 0)
printf(“%d is even.”, number);
else
printf(“%d is odd.”, number);
return 0;
}

2 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();
}

3 Command Line Program to Convert Decimal to Octal

This is very smart short and quick program –

#include<stdio.h>
int main(int argc,char *argv[])
{
int n,s=0,b=1,r;
n=atoi(argv[1]);
int c=n;
while(c>0)
{

r=c%8;
s=s+r*b;
c=c/8;
b=b*10;
}
printf(“%d”,s);
getch();
}

4 Command Line Program to check if a year is Leap Year or Not

#include<stdio.h>
void main(int argc,char *argv[])
{
int n;
n=atoi(argv[1]);
if(n%4==0)
{
if(n%100==0)
{
if(n%400==0)
printf(“Leap Year”);
else
printf(“Not Leap Year”);
}
else

printf(“Leap Year”);
}
else
printf(“Not Leap Year”);
getch();
}

5 Command Line Program to find Area of Circle

Write a C program to find the area of a circle with radius provided. The value of radius positive integer passed to the program as the first command line parameter. Write the output to stdout formatted as a floating point number rounded to EXACTLY 2 decimal precision WITHOUT any other additional text.

Scientific format(such as 1.00E+5) should NOT be used while printing the output. You may assume that the inputs will be such that the output will not exceed the largest possible real number that can be stored in a float type variable.

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char * argv[])
{
if(argc==1)
{

printf(“No arguments”);
return 0;
}
else
{
int radius;
float pi=3.14;
float area;
radius=atoi(argv[1]);
area=pi*radius*radius;
printf(“%.2f”,area);
return 0;
}
}
Write a C program to find the area of a circle with radius provided. The value of radius positive integer passed to the program as the first command line parameter. ​ Write the output to stdout formatted as a floating point number rounded to EXACTLY 2 decimal precision WITHOUT any other additional text.

Scientific format(such as 1.00E+5) should NOT be used while printing the output.

6 Command Line Program to find Area of Traingle

#include <stdio.h>
int main(int argc, char *argv[])
{

int base, height;
float area;
base = atol(argv[1]);
height = atol(argv[2]);
area = base*height/2;
printf(“%f”,area);
}

7 Command Line Program to Check if a Number is Palindrome or Not

Palindrome Number Command Line Programming

#include <stdio.h>
int main(int argc, char *argv[])
{
int num, reverse_num=0,remainder,temp;
num = atol(argv[1]);
temp=num;
while(temp!=0)
{
remainder=temp%10;
reverse_num=reverse_num*10+remainder;
temp/=10;
}
if(reverse_num==num)
printf(“%d is a palindrome number”,num);
else
printf(“%d is not a palindrome number”,num);
return 0;

}

8 Command Line Program to Check if a String is Palindrome or Not

#include <stdio.h>
#include <string.h>
void isPalindrome(char str[])
{
int l = 0;
int h = strlen(str) – 1;
while (h > l)
{
if (str[l++] != str[h–])
{
printf(“%s is Not Palindromen”, str);
return;
}
}
printf(“%s is palindromen”, str);
}
int main(int argc, char *argv[])
{
int i,k;
int strsize = 0;
for (i=1; i<argc; i++) {
strsize += strlen(argv[i]);
if (argc > i+1)
strsize++;
}
char *cmdstring;
cmdstring = malloc(strsize);
cmdstring[0] = ‘\0’;
for (k=1; k<argc; k++) {
strcat(cmdstring, argv[k]);
if (argc > k+1)
strcat(cmdstring, ” “);
}
isPalindrome(cmdstring);
}

9 Command Line Program to Check if a Number is Prime or Not

#include <stdio.h>
int main(int argc, char *argv[])
{
int n, i, flag = 0;
n = atol(argv[1]);
for(i=2; i<=n/2; ++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf(“%d is a prime number.”,n);
else
printf(“%d is not a prime number.”,n);
return 0;
}

10 Command Line Program to Reverse a Number

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
if(argc==1)
{
printf(“No Arguments”);
return 0;
}
else
{
int n,reverseNumber,temp,rem;
n=atoi(argv[1]);
temp=n;
reverseNumber=0;
while(temp)
{
rem=temp%10;
reverseNumber=reverseNumber*10+rem;
temp=temp/10;
}
printf(“%d”,reverseNumber);
return 0;
}
}

Top 20 Coding Program Ask In Exam & Interview

11 Calculate Square Root without using Math.h sqrt Function

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
if(argc==1)
{
printf(“No arguments”);
return 0;
}
else
{
int n;
n=atoi(argv[1]);
float i=0.00;
while(i*i<=n)
{
i=i+0.001;
}
i=i-0.001;
printf(“%.2f”,i);
}
}

12 Square Root of Prime Number using Command Line Argument

#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;
}
}

13 Nth Fibonacci Number

#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;
}
}

14 Decimal to Binary

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
if(argc==1)
{
printf(“No Arguments “);
return 0;
}
else
{
int n;
n=atoi(argv[1]);
int binaryN[64];
int i=0;int j;
while(n>0)
{
//storing in binary array remainder of number
binaryN[i]=n%2;
n=n/2;
i++;
}
//printing reverse array
while(i)
{
printf(“%d”,binaryN[–i]);
}
return 0;
}
}

Top 20 Coding Program Ask In Exam & Interview

15 Binary to decimal using Command Line Language

#include <stdio.h>
int main(int argc, char *argv[])
{
int num,binary,decimal=0,rem,base=1;
num=atoi(argv[1]);
binary=num;
while(num>0)
{
rem=num%2;
decimal =rem*base;
num=num/10;
base=base*2;
}
printf(“%d”,decimal);
return 0;
}

16 Sum of all the digits of a number using Command Line Language

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
long num, temp, digit, sum = 0;
if(argc == 1 || argc > 2)
{
printf(“Enter the number\n”);
exit(1);
}
num = atoi (argv[1]) ;
temp = num;
while (num > 0)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}
printf(“Sum of the digits of %ld = %ld\n”, temp, sum);
}

17 Average of Two Numbers using Command Line Language

#include <stdio.h>
int main(int argc, char * argv[])
{
int sum = 0,i = 1,count = 0;
if(argc == 1)
{
printf(“Enter the number \n”);
exit(1);
}
count = argc – 1;
while (i <= count )
{
sum += atoi (argv[i]) ;
i++;
}
printf(“Avg of the numbers.%d\n”, sum/count);
}

18 LCM of Two Numbers using Command Line Language

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
int n1,n2,x,y;
if (argc == 1 || argc > 3)
{
printf(“Enter Two Number\r\n”);
exit(0);
}
x=atoi(argv[1]);
y=atoi(argv[2]);
n1 = x; n2 = y;
while(n1!=n2){
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
printf(“L.C.M of %d & %d = %d \r\n”,x,y,x*y/n1);
return 0;
}

19 Command Line Programming For Greatest of Two numbers

#include <stdio.h>
int main(int argc, char *argv[])
{
int c[10];
int i,temp,j,greatest;
j = 0;
for(i=1; i<argc; i++)
{
temp = atoi(argv[i]);
c[j] = temp;
j++;
}
greatest = c[0];
for (i = 0; i < 10; i++) {
if (c[i] > greatest) {
greatest = c[i];
}
}
printf(“Greatest of ten numbers is %d”, greatest);
return 0;
}

20 Command Line Program for Swapping two Numbers

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main(int argc, char * argv[])
{
if(argc==1){33
printf(“No command line argument present, add them first”);
return 0;
}
double firstNumber, secondNumber, temporaryVariable;
firstNumber = atoi(argv[1]);
secondNumber = atoi(argv[2]);
temporaryVariable = firstNumber;
firstNumber = secondNumber;
secondNumber = temporaryVariable;
printf(“\nAfter swapping, firstNumber = %.2lf\n”, firstNumber);
printf(“After swapping, secondNumber = %.2lf”, secondNumber);
return 0;

  • }

Top 20 Coding Program Ask In Exam & Interview

Hope You Like This Blog And You Get Information For Your Exam And Interview Also Thank YoU and For More Content Like This Click Here Click Here

 

Related posts

Leave a Comment