Sunteți pe pagina 1din 55

TCS Coding Practice Question | Checking

Leap Year
Given a number N, the task is to check if N is a Leap Year or not using Command Line Arguments.

// C program to check if N is a leap year


// using command line arguments

#include <stdio.h>

#include <stdlib.h> /* atoi */

// Function to check

// if year is a leap year or not

int isLeapYear(int year)


{

// Return 1 if year is a multiple


// 0f 4 and not multiple of 100.
// OR year is multiple of 400.
if (((year % 4 == 0)

1
&& (year % 100 != 0))
|| (year % 400 == 0))
return 1;
else
return 0;
}

// Driver code
int main(int argc, char* argv[])
{

int n;

// Check if the length of args array is 1


if (argc == 1)

printf("No command line arguments found.\n");

else {

// Get the command line argument and


// Convert it from string type to integer
type
// using function "atoi( argument)"
n = atoi(argv[1]);

// Check if n is a leap year


if (isLeapYear(n) == 1)
printf("Yes\n");
else
printf("No\n");
}

return 0;
}
================================================================

// Java program to check if N is a leap year


// using command line arguments

class GFG {
2
// Function to check
// if year is a leap year or not
public static int isLeapYear(int year)
{

// Return 1 if year is a multiple


// 0f 4 and not multiple of 100.
// OR year is multiple of 400.
if (((year % 4 == 0)
&& (year % 100 != 0))
|| (year % 400 == 0))
return 1;
else
return 0;
}

// Driver code
public static void main(String[] args)
{

// Check if length of args array is


// greater than 0
if (args.length > 0) {

// Get the command line argumentand


// Convert it from string type to
integer type
int n = Integer.parseInt(args[0]);

// Check if n is a leap year


if (isLeapYear(n) == 1)
System.out.println("Yes");
else
System.out.println("No");
}
else

3
System.out.println("No command line +
"arguments found.");
}
}
===============================================

TCS Coding Practice Question | Checking


Prime Number
Given a number N, the task is to check if N is a Prime Number or not using Command Line
Arguments.

// C program to find
// the Prime Numbers from 1 to N
// using command line arguments

#include <stdio.h>

#include <stdlib.h> /* atoi */

4
// Function to check if x is prime

int isPrime(int x)
{
int i;

// Loop to check if x has any factor


// other than 1 and x itself
for (i = 2; i < x / 2 + 1; i++)
{
if (x % i == 0)
{
// Since i is a factor of x
// x is not prime
return 0;
}
}

// x is prime
return 1;
}

// Driver code
int main(int argc, char* argv[])
{

int n;

// Check if the length of args array is 1


if (argc == 1)

printf("No command line arguments


found.\n");
else {

// Get the command line argument and

5
// Convert it from string type to
integer type
// using function "atoi( argument)"
n = atoi(argv[1]);

// Check if n is prime
if (isPrime(n) == 1)
printf("Yes\n");
else
printf("No\n");
}

return 0;
}

// Java program to reverse a string


// using command line arguments

class GFG {

// Function to check if x is prime


public static int isPrime(int x)
{
int i;

// Loop to check if x has any factor


// other than 1 and x itself
for (i = 2; i < x / 2 + 1; i++) {
if (x % i == 0) {
// Since i is a factor of x
// x is not prime
return 0;
}
}

// x is prime
return 1;

6
}

// Driver code
public static void main(String[] args)
{

// Check if length of args array is


// greater than 0
if (args.length > 0) {

// Get the command line argument


and
// Convert it from string type to
integer type
int n = Integer.parseInt(args[0]);

// Check if n is prime
if (isPrime(n) == 1)
System.out.println("Yes");
else
System.out.println("No");
}
else
System.out.println("No command line
"
+ "arguments
found.");
}
}

TCS Coding Practice Question | Prime


Numbers upto N
Given a number N, the task is to find the Prime Numbers from 1 to N using Command Line
Arguments.

7
// C program to find
// the Prime Numbers from 1 to N
// using command line arguments

#include <stdio.h>

#include <stdlib.h> /* atoi */

// Function to check if x is prime


int isPrime(int x)
{
int i;

// Loop to check if x has any factor


// other than 1 and x itself
for (i = 2; i < x / 2 + 1; i++) {
if (x % i == 0) {
// Since i is a factor of x
// x is not prime
return 0;
}
}

// x is prime
return 1;
}

// Function to find prime numbers up to n


void findPrimes(int n)
{
int i;
8
// Loop from 2 to n
// to find all prime numbers in between
for (i = 2; i <= n; i++) {

// Check if i is prime
// If yes then print it
// else continue to next number
if (isPrime(i) == 1)
printf("%d, ", i);
}

printf("\n");
}

// Driver code
int main(int argc, char* argv[])
{

int n;

// Check if the length of args array is 1


if (argc == 1)
printf("No command line arguments
found.\n");
else {

// Get the command line argument and


// Convert it from string type to
integer type
// using function "atoi( argument)"
n = atoi(argv[1]);

// Find all prime numbers upto n


findPrimes(n);
}
return 0;

9
}

// Java program to find


// the Prime Numbers from 1 to N
// using command line arguments

class GFG {

// Function to check if x is prime


public static int isPrime(int x)
{
int i;

// Loop to check if x has any factor


// other than 1 and x itself
for (i = 2; i < x / 2 + 1; i++) {
if (x % i == 0) {
// Since i is a factor of x
// x is not prime
return 0;
}
}

// x is prime
return 1;
}

// Function to find prime numbers up to n


public static void findPrimes(int n)
{
int i;

// Loop from 2 to n
// to find all prime numbers in between
for (i = 2; i <= n; i++) {

// Check if i is prime
10
// If yes then print it
// else continue to next number
if (isPrime(i) == 1)
System.out.print(i + ", ");
}

System.out.println();
}

// Driver code
public static void main(String[] args)
{

// Check if length of args array is


// greater than 0
if (args.length > 0) {

// Get the command line argument


and
// Convert it from string type to
integer type
int n = Integer.parseInt(args[0]);

// Find all prime numbers upto n


findPrimes(n);
}
else
System.out.println("No command line
"
+ "arguments
found.");
}
}

TCS Coding Practice Question | LCM of 2


Numbers
11
Given two numbers, the task is to find the LCM of two numbers using Command Line Arguments.
LCM (Least Common Multiple) of two numbers is the smallest number which can be divided by both
numbers.
For example, LCM of 15 and 20 is 60 and LCM of 5 and 7 is 35.

// C program to compute the LCM of two numbers


12
// using command line arguments

#include <stdio.h>
#include <stdlib.h> /* atoi */

// Function to compute the GCD of two numbers


int GCD(int a, int b)
{
if (b == 0)
return a;

return GCD(b, a % b);


}

// Function to find the LCM


int LCM(int a, int b)
{
return (a * b) / GCD(a, b);
}

// Driver code
int main(int argc, char* argv[])
{

int num1, num2;

// Check if the length of args array is 1


if (argc == 1)
printf("No command line arguments
found.\n");

else {

// Get the command line argument and


// Convert it from string type to
integer type
// using function "atoi( argument)"

13
num1 = atoi(argv[1]);
num2 = atoi(argv[2]);

// Find the LCM and print it


printf("%d\n", LCM(num1, num2));
}
return 0;
}
// Java program to compute the LCM of two
numbers
// using command line arguments

class GFG {

// Function to compute the GCD of two


numbers
static int GCD(int a, int b)
{
if (b == 0)
return a;

return GCD(b, a % b);


}

// Function to find the LCM


static int LCM(int a, int b)
{
return (a * b) / GCD(a, b);
}

// Driver code
public static void main(String[] args)
{

// Check if length of args array is


// greater than 0
if (args.length > 0) {

14
// Get the command line argument
and
// Convert it from string type to
integer type
int num1 =
Integer.parseInt(args[0]);
int num2 =
Integer.parseInt(args[1]);

// Find the LCM


int res = LCM(num1, num2);

// Print the LCM


System.out.println(res);
}
else
System.out.println("No command line
"
+ "arguments
found.");
}
}

TCS Coding Practice Question | HCF or


GCD of 2 Numbers
Given two numbers, the task is to find the HCF of two numbers using Command Line Arguments.
GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest
number that divides both of them.

15
// C program to compute the HCF of two numbers
// using command line arguments

#include <stdio.h>
#include <stdlib.h> /* atoi */

// Function to compute the HCF of two numbers


int HCF(int a, int b)
{
if (b == 0)
return a;

return HCF(b, a % b);


}

// Driver code
int main(int argc, char* argv[])
{

int num1, num2;

16
// Check if the length of args array is 1
if (argc == 1)
printf("No command line arguments
found.\n");

else {

// Get the command line argument and


// Convert it from string type to
integer type
// using function "atoi( argument)"
num1 = atoi(argv[1]);
num2 = atoi(argv[2]);

// Find the HCF and print it


printf("%d\n", HCF(num1, num2));
}
return 0;
}
// Java program to compute the HCF of two
numbers
// using command line arguments

class GFG {

// Function to compute the HCF of two


numbers
static int HCF(int a, int b)
{
if (b == 0)
return a;

return HCF(b, a % b);


}

// Driver code
public static void main(String[] args)

17
{

// Check if length of args array is


// greater than 0
if (args.length > 0) {

// Get the command line argument


and
// Convert it from string type to
integer type
int num1 =
Integer.parseInt(args[0]);
int num2 =
Integer.parseInt(args[1]);

// Find the HCF


int res = HCF(num1, num2);

// Print the HCF


System.out.println(res);
}
else
System.out.println("No command line
"
+ "arguments
found.");
}
}

TCS Coding Practice Question | Swap two


Numbers
Given two numbers, the task is to swap the two numbers using Command Line Arguments.

18
// C program to swap the two numbers
// using command line arguments

#include <stdio.h>
#include <stdlib.h> /* atoi */

// Function to swap the two numbers


void swap(int x, int y)
{
// Code to swap ‘x’ and ‘y’
19
// x now becomes x+y
x = x + y;

// y becomes x
y = x - y;

// x becomes y
x = x - y;

printf("%d %d\n", x, y);


}

// Driver code
int main(int argc, char* argv[])
{

int num1, num2;

// Check if the length of args array is 1


if (argc == 1)
printf("No command line arguments
found.\n");

else {

// Get the command line argument and


// Convert it from string type to
integer type
// using function "atoi( argument)"
num1 = atoi(argv[1]);
num2 = atoi(argv[2]);

// Swap the numbers and print it


swap(num1, num2);
}
return 0;

20
}
// Java program to swap the two numbers
// using command line arguments

class GFG {

// Function to swap the two numbers


static void swap(int x, int y)
{
// Code to swap ‘x’ and ‘y’

// x now becomes x+y


x = x + y;

// y becomes x
y = x - y;

// x becomes y
x = x - y;

System.out.println(x + " " + y);


}

// Driver code
public static void main(String[] args)
{

// Check if length of args array is


// greater than 0
if (args.length > 0) {

// Get the command line argument


and
// Convert it from string type to
integer type
int num1=Integer.parseInt(args[0]);
int num2=Integer.parseInt(args[1]);

21
// Swap the numbers
swap(num1, num2);
}
Else

System.out.println("No command line


"+ "arguments found.");
}
}

TCS Coding Practice Question | Average of


2 Numbers
Given two numbers, the task is to find the average of two numbers using Command Line Arguments.

// C program to compute the average of two


numbers
// using command line arguments

#include <stdio.h>
#include <stdlib.h> /* atoi */

22
// Function to compute the average of two
numbers
int average(int a, int b)
{
return (a + b) / 2;
}

// Driver code
int main(int argc, char* argv[])
{

int num1, num2;

// Check if the length of args array is 1


if (argc == 1)
printf("No command line arguments
found.\n");

else {

// Get the command line argument and


// Convert it from string type to
integer type
// using function "atoi( argument)"
num1 = atoi(argv[1]);
num2 = atoi(argv[2]);

// Find the average and print it


printf("%d\n", average(num1, num2));
}
return 0;
}
// Java program to compute the average of two
numbers
// using command line arguments

class GFG {

23
// Function to compute the average of two
numbers
static int average(int a, int b)
{
return (a + b) / 2;
}

// Driver code
public static void main(String[] args)
{

// Check if length of args array is


// greater than 0
if (args.length > 0) {

// Get the command line argument


and
// Convert it from string type to
integer type
int num1=Integer.parseInt(args[0]);
int num2=Integer.parseInt(args[1]);

// Find the average


int res = average(num1, num2);

// Print the average


System.out.println(res);
}
else
System.out.println("No command line
"+ "arguments found.");
}
}

TCS Coding Practice Question | Greatest of


3 Numbers
24
Given three numbers, the task is to find the greatest of three numbers using Command Line
Arguments.

// C program to compute the greatest of three


numbers
// using command line arguments

#include <stdio.h>
25
#include <stdlib.h> /* atoi */

// Function to compute the greatest of three


numbers
int greatest(int A, int B, int C)
{

if (A >= B && A >= C)


return A;

if (B >= A && B >= C)


return B;

return C;
}

// Driver code
int main(int argc, char* argv[])
{

int num1, num2, num3;

// Check if the length of args array is 1


if (argc == 1)
printf("No command line arguments
found.\n");

else {

// Get the command line argument and


// Convert it from string type to
integer type
// using function "atoi( argument)"
num1 = atoi(argv[1]);
num2 = atoi(argv[2]);
num3 = atoi(argv[3]);

// Find the greatest and print it

26
printf("%d\n", greatest(num1, num2,
num3));
}
return 0;
}

// Java program to compute the greatest of three numbers


// using command line arguments

class GFG {

// Function to compute the greatest of three numbers


static int greatest(int A, int B, int C)
{

if (A >= B && A >= C)


return A;

if (B >= A && B >= C)


return B;

return C;
}

// Driver code
public static void main(String[] args)
{

// Check if length of args array is


// greater than 0
if (args.length > 0) {

// Get the command line argument and


// Convert it from string type to integer type
int num1 = Integer.parseInt(args[0]);
int num2 = Integer.parseInt(args[1]);
int num3 = Integer.parseInt(args[2]);

// Find the greatest


int res = greatest(num1, num2, num3);

// Print the greatest


System.out.println(res);
}
else
System.out.println("No command line "+ "arguments found.");
}
}

27
TCS Coding Practice Question | Check Odd
or Even
Given a number, the task is to check if this number is Odd or Even using Command Line Arguments.
A number is called even if the number is divisible by 2 and is called odd if it is not divisible by 2.

28
C PROGRAM:-

Java Program:-

29
TCS Coding Practice Question |
Palindrome String
Given a string, the task is to check if this String is Palindrome or not using Command Line Arguments.

30
31
TCS Coding Practice Question | Sum of
Digits of a number
32
Given a number, the task is to find the Sum of Digits of this number using Command Line Arguments

C PROGRAM:-

33
34
35
TCS Coding Practice Question | Check
Palindrome Number
Given a number, the task is to check if this number is Palindrome or not using Command Line
Arguments.

36
37
38
TCS Coding Practice Question | Reverse a
String
Given a string, the task is to reverse this String using Command Line Arguments.

39
40
41
42
TCS Coding Practice Question | Fibonacci
Series
Given a number ‘n’, the task is to print the the Fibonacci series using Command Line Arguments. The
Fibonacci numbers are the numbers in the following integer sequence.

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

43
with seed values

Example:-

Approach:

44
45
TCS Coding Practice Question | Factorial
of a Number
Given a number, the task is to find the Factorial of this number using Command Line Arguments.
Factorial of a non-negative integer is the multiplication of all integers smaller than or equal to n

46
Example:-

47
48
TCS Coding Practice Question | Reverse a
Number

49
Given a number, the task is to reverse this number using Command Line Arguments.

50
51
Check whether the given character is in
upper case, lower case or non alphabetic
character
52
Given a character, the task is to check whether the given character is in upper case, lower case or
non-alphabetic character

53
Check input character is alphabet, digit or
special character

54
55

S-ar putea să vă placă și