0% found this document useful (0 votes)
2 views5 pages

Tcs Nqt Java Basic Programs

The document contains basic Java programs that cover various fundamental concepts such as reversing a string, checking for palindromes, finding the maximum element in an array, counting vowels, summing digits, generating a Fibonacci series, identifying prime numbers, removing duplicates from a string, finding the second largest element, checking for anagrams, determining leap years, and calculating the GCD of two numbers. Each program is presented with its respective class and main method implementation. These examples are useful for TCS NQT preparation.

Uploaded by

ajayajayak007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Tcs Nqt Java Basic Programs

The document contains basic Java programs that cover various fundamental concepts such as reversing a string, checking for palindromes, finding the maximum element in an array, counting vowels, summing digits, generating a Fibonacci series, identifying prime numbers, removing duplicates from a string, finding the second largest element, checking for anagrams, determining leap years, and calculating the GCD of two numbers. Each program is presented with its respective class and main method implementation. These examples are useful for TCS NQT preparation.

Uploaded by

ajayajayak007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Basic Java Programs for TCS NQT

Reverse a String
public class ReverseString {
public static void main(String[] args) {
String str = "hello";
String rev = "";

for(int i = [Link]()-1; i >= 0; i--){


rev = rev + [Link](i);
}

[Link]("Reversed String: " + rev);


}
}

Check Palindrome
public class Palindrome {
public static void main(String[] args) {
String str = "madam";
String rev = "";

for(int i = [Link]()-1; i >= 0; i--){


rev = rev + [Link](i);
}

if([Link](rev))
[Link]("Palindrome");
else
[Link]("Not Palindrome");
}
}

Maximum Element in Array


public class MaxArray {
public static void main(String[] args) {
int arr[] = {5, 10, 3, 20, 8};
int max = arr[0];

for(int i = 1; i < [Link]; i++){


if(arr[i] > max)
max = arr[i];
}

[Link]("Maximum: " + max);


}
}
Count Vowels in String
public class VowelsCount {
public static void main(String[] args) {
String str = "education";
int count = 0;

for(int i=0;i<[Link]();i++){
char ch = [Link](i);

if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u' ||
ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'){
count++;
}
}

[Link]("Vowels: " + count);


}
}

Sum of Digits
public class SumDigits {
public static void main(String[] args) {
int num = 1234;
int sum = 0;

while(num > 0){


sum += num % 10;
num /= 10;
}

[Link]("Sum of digits: " + sum);


}
}

Fibonacci Series
public class Fibonacci {
public static void main(String[] args) {
int n = 10;
int a = 0, b = 1;

for(int i = 1; i <= n; i++){


[Link](a + " ");
int next = a + b;
a = b;
b = next;
}
}
}
Prime Numbers up to N
public class PrimeNumbers {
public static void main(String[] args) {
int n = 20;

for(int i=2;i<=n;i++){
boolean prime = true;

for(int j=2;j<=i/2;j++){
if(i % j == 0){
prime = false;
break;
}
}

if(prime)
[Link](i + " ");
}
}
}

Remove Duplicates from String


public class RemoveDuplicates {
public static void main(String[] args) {
String str = "programming";
String result = "";

for(int i=0;i<[Link]();i++){
char ch = [Link](i);

if([Link](ch) == -1){
result += ch;
}
}

[Link](result);
}
}

Second Largest Element


public class SecondLargest {
public static void main(String[] args) {
int arr[] = {10, 20, 5, 8, 25};

int first = Integer.MIN_VALUE;


int second = Integer.MIN_VALUE;

for(int num : arr){


if(num > first){
second = first;
first = num;
}
else if(num > second && num != first){
second = num;
}
}

[Link]("Second Largest: " + second);


}
}

Check Anagram
import [Link];

public class Anagram {


public static void main(String[] args) {
String s1 = "listen";
String s2 = "silent";

char a[] = [Link]();


char b[] = [Link]();

[Link](a);
[Link](b);

if([Link](a,b))
[Link]("Anagram");
else
[Link]("Not Anagram");
}
}

Leap Year
public class LeapYear {
public static void main(String[] args) {
int year = 2024;

if((year%4==0 && year%100!=0) || (year%400==0))


[Link]("Leap Year");
else
[Link]("Not Leap Year");
}
}

GCD of Two Numbers


public class GCD {
public static void main(String[] args) {
int a = 36, b = 60;

while(b != 0){
int temp = b;
b = a % b;
a = temp;
}

[Link]("GCD: " + a);


}
}

You might also like