Curriculum
- 8 Sections
- 119 Lessons
- 25 Weeks
Expand all sectionsCollapse all sections
- Course Overview/*! CSS Used from: Embedded */ *, ::after, ::before { box-sizing: border-box; border-width: 0; border-style: solid; border-color: #e5e7eb; } ::after, ::before { --tw-content: ''; } h2 { font-size: inherit; font-weight: inherit; } a { color: inherit; text-decoration: inherit; } h2, p { margin: 0; } :disabled { cursor: default; } *, ::before, ::after { --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgb(59 130 246 / 0.5); --tw-ring-offset-shadow: 0 0 #0000; --tw-ring-shadow: 0 0 #0000; --tw-shadow: 0 0 #0000; --tw-shadow-colored: 0 0 #0000; } .mx-auto { margin-left: auto; margin-right: auto; } .mb-2 { margin-bottom: 0.5rem; } .mb-4 { margin-bottom: 1rem; } .mb-6 { margin-bottom: 1.5rem; } .mr-2 { margin-right: 0.5rem; } .max-w-screen-sm { max-width: 640px; } .max-w-screen-xl { max-width: 1280px; } .rounded-lg { border-radius: 0.5rem; } .bg-primary-700 { --tw-bg-opacity: 1; background-color: rgb(29 78 216 / var(--tw-bg-opacity)); } .bg-white { --tw-bg-opacity: 1; background-color: rgb(255 255 255 / var(--tw-bg-opacity)); } .px-4 { padding-left: 1rem; padding-right: 1rem; } .px-5 { padding-left: 1.25rem; padding-right: 1.25rem; } .py-2.5 { padding-top: 0.625rem; padding-bottom: 0.625rem; } .py-8 { padding-top: 2rem; padding-bottom: 2rem; } .text-center { text-align: center; } .text-4xl { font-size: 3rem; line-height: 2.5rem; } .text-sm { font-size: 0.875rem; line-height: 1.25rem; } .font-extrabold { font-weight: 800; } .font-light { font-weight: 300; } .font-medium { font-weight: 500; } .leading-tight { line-height: 1.25; } .tracking-tight { letter-spacing: -0.025em; } .text-gray-500 { --tw-text-opacity: 1; color: rgb(107 114 128 / var(--tw-text-opacity)); } .text-gray-900 { --tw-text-opacity: 1; color: rgb(17 24 39 / var(--tw-text-opacity)); } .text-white { --tw-text-opacity: 1; color: rgb(255 255 255 / var(--tw-text-opacity)); } .hover\:bg-primary-800:hover { --tw-bg-opacity: 1; background-color: rgb(30 64 175 / var(--tw-bg-opacity)); } .focus\:outline-none:focus { outline: 2px solid transparent; outline-offset: 2px; } .focus\:ring-4:focus { --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color); --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(4px + var(--tw-ring-offset-width)) var(--tw-ring-color); box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000); } .focus\:ring-primary-300:focus { --tw-ring-opacity: 1; --tw-ring-color: rgb(147 197 253 / var(--tw-ring-opacity)); } @media (min-width: 640px) { .sm\:py-16 { padding-top: 4rem; padding-bottom: 4rem; } } @media (min-width: 768px) { .md\:text-lg { font-size: 1.5rem; line-height: 1.75rem; } } @media (min-width: 1024px) { .lg\:px-6 { padding-left: 1.5rem; padding-right: 1.5rem; } } .imgdata { width: 35% } @media (max-width: 767px) { .imgdata { width: 40% } } .course-payment { display: none !important; } .thim-course-landing-button { display: none !important; }0
- NumericalTCS-NQT Instructions : You will be given 26 Question with a time limit of 40 mins | Questions - 26 | Time - 40 mins48
- 2.1Profit and Loss [ Notes ]
- 2.2Profit & Loss [Formula]
- 2.3Profit and Loss [Practice Problems]
- 2.4Geometry Formula and Concept
- 2.5Geometry Formula and Concept [Practice problem]
- 2.6HCF and LCM [ Handmade Notes ]
- 2.7HCF and LCM [ Formula ]
- 2.8HCF and LCM [ Practice Problems ]
- 2.9Percentage Problem [Formula]
- 2.10Percentage Problem [ Notes ]
- 2.11Percentage Problem [Practice Problems]
- 2.12Alligation and Mixtures [Formula]
- 2.13Alligation and Mixtures [Handmade Notes]
- 2.14Alligation and Mixtures [Practice Problems]
- 2.15Time and Work[Formula]
- 2.16Time and Work [Handmade Notes]
- 2.17Time and Work [Practice Problems]
- 2.18Ages [ Formula ]
- 2.19Ages Problem [Handmade Notes]
- 2.20Ages Problem [Practice Problems]
- 2.21Sequence and Series
- 2.22Sequence and Series [Practice problem ]
- 2.23Divisibility
- 2.24Divisibility [Practice problem ]
- 2.25Ratio and Proportion
- 2.26Ratio and Proportion [Practice problem ]
- 2.27Probability [ Formula and Solved Examples ]
- 2.28Probability [Practice problem ]
- 2.29Perimeter, Area and Volume
- 2.30Perimeter, Area and Volume [Practice problem ]
- 2.31Your Feedback Means Alot to Us ❤️2 Minutes
- 2.32Average Formula and Concept
- 2.33Average Formula and Concept [Practice problem ]
- 2.34Speed Time And Distance
- 2.35Speed Time And Distance [Practice problem ]
- 2.36Mean Median Mode
- 2.37Mean Median Mode [Practice problem ]
- 2.38Decimals and Fractions
- 2.39Decimals and Fractions Formula [Practice problem ]
- 2.40Pie Chart
- 2.41Pie Chart Practice Problem
- 2.42Bar Chart
- 2.43Bar Chart Practice Problem
- 2.44Your Feedback Means Alot to Us ❤️2 Minutes
- 2.45Profit and Loss [Notes]
- 2.46Percentage Problem [Notes]
- 2.47Ratio and Proportion [Notes]
- 2.48Average [Notes]
- VerbalTCS-NQT Instructions : You will be given 24 Question with a time limit of 30 mins | Questions - 24 | Time - 15 mins + 30 mins10
- 3.1Sentence Completion [ Notes + Solved Examples + Practice Problem ]
- 3.2Spotting Erorrs [ Notes + Solved Examples + Practice Problem ]
- 3.3Reading Comprehension [ Notes + Solved Examples + Practice Problem ]
- 3.4Meanings [ Notes + Solved Examples + Practice Problem ]
- 3.5Tenses [ Notes + Solved Examples + Practice Problem ]
- 3.6Idioms And Phrases [ Notes + Solved Examples + Practice Problem ]
- 3.7Formal And Informal Sentences
- 3.8Formal And Informal Sentences [Practice problem]
- 3.9Sentence Arrangement
- 3.10Sentence Arrangement [Practice problem]
- Aptitude Series for Placement [ Free Sample Videos ]Note: TCS NQT Study Material Doesn't Contain videos we provide only Study Materials in this course , these videos are sample videos of our Aptitude Series16
- 4.1📌 Watch this Video to Crack your Aptitude2 Minutes
- 4.2Train Problems Part #119 Minutes
- 4.3Train Problems Part #211 Minutes
- 4.4Train Problems Part #319 Minutes
- 4.5Train Problems [Handmade Notes]
- 4.6📌 Practice Problems [Quiz] – Train Problems
- 4.7Calendar Basics13 Minutes
- 4.8📌 Take the Complete Aptitude Series Now [ Videos Lectures – Quant , Verbal and Reasoning ] 📌
- 4.9Calendar Problem [Handmade Notes]
- 4.10📌 Practice Problems [Quiz] – Calendar
- 4.11Time and Work Part #120 Minutes
- 4.12Time and Work Part #213 Minutes
- 4.13Time and Work Part #312 Minutes
- 4.14Time and Work [Handmade Notes]
- 4.15📌 Practice Problems [Quiz] – Time and Work
- 4.16📌 Take the Complete Aptitude Series Now [ Videos Lectures – Quant , Verbal and Reasoning ] 📌
- ReasoningTCS-NQT Instructions : You will be given 30 Question with a time limit of 50 mins | Questions - 30 | Time - 50 mins21
- 5.1Blood Relation [ Formulas + Notes ]
- 5.2Blood Relation [ Practice Problem ]
- 5.3Number Series [ Formulas + Notes ]
- 5.4Number Series [ Practice Problem ]
- 5.5Seating Arrangement [ Formulas + Notes ]
- 5.6Seating Arrangement [ Practice Problem ]
- 5.7Syllogism [ Formulas + Notes ]
- 5.8Syllogism [ Practice Problem ]
- 5.9Data Sufficiency [ Formulas + Notes ]
- 5.10Data Sufficiency [ Practice Problem ]
- 5.11Clocks [Formula]
- 5.12Clock Problem [Handmade Notes]
- 5.13Clock Problem [Practice Problems]
- 5.14Calendar Problem [Formula]
- 5.15Calendar Problem [Handmade Notes]
- 5.16Calendar Problem [Practice Problems]
- 5.17Number Series [Notes]
- 5.18Syllogism [Notes]
- 5.19Clock Problem [Notes]
- 5.20Coding decoding [Notes]
- 5.21Coding decoding [Practice problem]
- Programming LogicTCS-NQT Instructions : You will be given 10 Question with a time limit of 15 mins | Questions - 10 | Time - 15 mins6
- CodingTCS-NQT Instructions : You will be given 2 Question with a time limit of 15 mins and 30 mins | Questions - 2 | Time - 15 mins + 30 mins16
- 7.1Introduction to java
- 7.2[Programs] Introduction To Java
- 7.3Flow Control
- 7.4[Programs] Flow Control
- 7.5[Programs] Pyramids & Patterns
- 7.6Arrays
- 7.7[Programs] Arrays
- 7.8Java Methods, Class & Object
- 7.9[Programs] Java Methods, Class & Object
- 7.10Strings, Recursion, Modifiers & Keywords
- 7.11[Programs] Strings, Recursion, Modifiers & Keywords
- 7.12Object-Oriented Programming System
- 7.13[Programs] Object Oriented Programming System
- 7.14Exception Handling & Files
- 7.15[Program] Exception Handling & Files
- 7.16TCS-NQT Mock Coding Questions
- Previous Year Paper and Solution2

WAP-Introduction To Java

Print an integer input from user to screen
In this program, we'll learn to print a number entered by the user . The integer is stored in a
variable using System.in, and is displayed on the screen using System.out.
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
// Creates a reader instance which takes
// input from standard input - keyboard
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number: ");
// nextInt() reads the next integer from the keyboard
int number = reader.nextInt();
// println() prints the following line to the output screen
System.out.println("You entered: " + number);
}
}
Output
Enter a number: 10
You entered: 10
In this program, an object of Scanner class, reader, is created to take inputs from standard
input, which is keyboard.
Then, Enter a number prompt is printed to give the user a visual cue as to what they should do
next.
reader.nextInt() then reads all entered integers from the keyboard unless it encounters a new
line character \n (Enter). The entered integers are then saved to the integer variable number.
If you enter any character which is not an integer, the compiler will throw an
InputMismatchException.
Finally, number is printed onto the standard output (System.out) - computer screen using the
function println().

Add two integers (assume inputs)
In this program, we'll learn to store and add two integer numbers . After addition, the final sum is
displayed on the screen.
class Main {
public static void main(String[] args) {
System.out.println("Enter two numbers");
int first = 10;
int second = 20;
System.out.println(first + " " + second);
// add two numbers
int sum = first + second;
System.out.println("The sum is: " + sum);
}
}
Output:
Enter two numbers
10 20
The sum is: 30
In this program, two integers 10 and 20 are stored in integer variables first and second
respectively.
Then, first and second are added using the + operator, and its result is stored in another
variable sum.
Finally, sum is printed on the screen using println() function.

Multiplication of two floating numbers (assume inputs)
In this program, we'll learn to multiply two floating point numbers , store the result and display it
on the screen.
public class MultiplyTwoNumbers {
public static void main(String[] args) {
float first = 1.5f;
float second = 2.0f;
float product = first * second;
System.out.println("The product is: " + product);
}
}
Output
The product is: 3.0
In the above program, we have two floating-point numbers 1.5f and 2.0f stored in variables first
and second respectively.
Notice, we have used f after the numbers. This ensures the numbers are float, otherwise they
will be assigned - type double.
first and second are then multiplied using the * operator and the result is stored in a new float
variable product.
Finally, the result product is printed on the screen using println() function.

ASCII equivalent of a character
In this program, we'll learn to find and display the ASCII value of a character . This is done using
type-casting and normal variable assignment operations.
public class AsciiValue {
public static void main(String[] args) {
char ch = 'a';
int ascii = ch;
// You can also cast char to int
int castAscii = (int) ch;
System.out.println("The ASCII value of " + ch + " is: " + ascii);
System.out.println("The ASCII value of " + ch + " is: " +
castAscii);
}
}
Output
The ASCII value of a is: 97
The ASCII value of a is: 97
In the above program, character a is stored in a char variable, ch. Like, double quotes (" ") are
used to declare strings, we use single quotes (' ') to declare characters.
Now, to find the ASCII value of ch, we just assign ch to an int variable ascii. Internally, Java
converts the character value to an ASCII value.
We can also cast the character ch to an integer using (int). In simple terms, casting is
converting a variable from one type to another, here char variable ch is converted to an int
variable castAscii.
Finally, we print the ascii value using the println() function.

Division of integers (quotient & remainder) with no decimal places
In this program, we'll learn to compute quotient and remainder from the given dividend and
divisor in Java.
public class QuotientRemainder {
public static void main(String[] args) {
int dividend = 25, divisor = 4;
int quotient = dividend / divisor;
int remainder = dividend % divisor;
System.out.println("Quotient = " + quotient);
System.out.println("Remainder = " + remainder);
}
}
Output:
Quotient = 6
Remainder = 1
In the above program, we have created two variables dividend and divisor. Here, we are
calculating the quotient and remainder by dividing 25 by 4.
To find the quotient, we have used the / operator. We have divided dividend (25) by divisor (4).
Since both dividend and divisor are integers, the result will also be integer.
25 / 4 // results 6.5
// convert 6.5 to integer
// output will be 6
Likewise, to find the remainder we use the % operator. Here, the dividend is divided by the
divisor and the remainder is returned by the % operator.
25 % 4 // results 1
Finally, quotient and remainder are printed on the screen using println() function.

Swapping of two numbers
In this program, we'll learn two techniques to swap two numbers in Java. The first one uses a
temporary variable for swapping, while the second one doesn't use any temporary variables.
Using temporary variable
public class SwapNumbers {
public static void main(String[] args) {
float first = 1.20f, second = 2.45f;
System.out.println("--Before swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
// Value of first is assigned to temporary
float temporary = first;
// Value of second is assigned to first
first = second;
// Value of temporary (which contains the initial value of first)
is assigned to second
second = temporary;
System.out.println("--After swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
}
}
Output:
--Before swap--
First number = 1.2
Second number = 2.45
--After swap--
First number = 2.45
Second number = 1.2

In the above program, two numbers 1.20f and 2.45f which are to be swapped are stored in
variables: first and second respectively.
The variables are printed before swapping using println() to see the results clearly after
swapping is done.
1. First, the value of first is stored in variable temporary (temporary = 1.20f).
2. Then, value of second is stored in first (first = 2.45f).
3. And, finally value of temporary is stored in second (second = 1.20f).
This completes the swapping process and the variables are printed on the screen.
Remember, the only use of temporary is to hold the value of first before swapping. You can also
swap the numbers without using temporary.
without using temporary variable
public class SwapNumbers {
public static void main(String[] args) {
float first = 12.0f, second = 24.5f;
System.out.println("--Before swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
first = first - second;
second = first + second;
first = second - first;
System.out.println("--After swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
}
}
Output:
--Before swap--
First number = 12.0
Second number = 24.5

--After swap--
First number = 24.5
Second number = 12.0
In the above program, instead of using temporary variable, we use simple mathematics to swap
the numbers.
For the operation, storing (first - second) is important. This is stored in variable first.
first = first - second;
first = 12.0f - 24.5f
Then, we just add second (24.5f) to this number - calculated first (12.0f - 24.5f) to swap the
number.
second = first + second;
second = (12.0f - 24.5f) + 24.5f = 12.0f
Now, second holds 12.0f (which was initially value of first). So, we subtract calculated first (12.0f
- 24.5f) from swapped second (12.0f) to get the other swapped number.
first = second - first;
first = 12.0f - (12.0f - 24.5f) = 24.5f
The swapped numbers are printed on the screen using println().

Odd or Even Integer
In this program, you'll learn to check if a number entered by a user is even or odd. This will be
done using if...else statement and ternary operator in Java.
Using conditional branching (if-else)
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = reader.nextInt();
if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
Output
Enter a number: 12
12 is even
In the above program, a Scanner object, reader is created to read a number from the user's
keyboard. The entered number is then stored in a variable num.
Now, to check whether num is even or odd, we calculate its remainder using % operator and
check if it is divisible by 2 or not.
For this, we use if...else statement in Java. If num is divisible by 2, we print num is even. Else,
we print num is odd.
We can also check if num is even or odd by using ternary operator in Java.
Using ternary operator
import java.util.Scanner;

public class EvenOdd {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = reader.nextInt();
String evenOdd = (num % 2 == 0) ? "even" : "odd";
System.out.println(num + " is " + evenOdd);
}
}
Output
Enter a number: 13
13 is odd
In the above program, we've replaced if...else statement with ternary operator (? :).
Here, if num is divisible by 2, "even" is returned. Else, "odd" is returned. The returned value is
saved in a string variable evenOdd.
Then, the result is printed on the screen using string concatenation.

Alphabets are vowels or consonants
In this program, you'll learn to check whether an alphabet is a vowel or a consonant using
if..else and switch statements in Java.
Using if..else statement
public class VowelConsonant {
public static void main(String[] args) {
char ch = 'i';
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' )
System.out.println(ch + " is vowel");
else
System.out.println(ch + " is consonant");
}
}
Output
i is vowel
In the above program, 'i' is stored in a char variable ch. In Java, you use double quotes (" ") for
strings and single quotes (' ') for characters.
Now, to check whether ch is vowel or not, we check if ch is any of: ('a', 'e', 'i', 'o', 'u'). This is
done using a simple if..else statement.
Using switch statement
public class VowelConsonant {
public static void main(String[] args) {
char ch = 'z';
switch (ch) {
case 'a':
case 'e':
case 'i':

case 'o':
case 'u':
System.out.println(ch + " is vowel");
break;
default:
System.out.println(ch + " is consonant");
}
}
}
Output
z is consonant
In the above program, instead of using a long if condition, we replace it with a switch case
statement.
If ch is either of cases: ('a', 'e', 'i', 'o', 'u'), vowel is printed. Else, default case is executed and
consonant is printed on the screen.
Largest among three integers
In this program, you'll learn to find the largest among three numbers using if else and nested
if..else statement in Java.
Using if..else statement
public class Largest {
public static void main(String[] args) {
double n1 = -4.5, n2 = 3.9, n3 = 2.5;
if( n1 >= n2 && n1 >= n3)
System.out.println(n1 + " is the largest number.");
else if (n2 >= n1 && n2 >= n3)
System.out.println(n2 + " is the largest number.");
else
System.out.println(n3 + " is the largest number.");
}

}
Output
3.9 is the largest number.
In the above program, three numbers -4.5, 3.9 and 2.5 are stored in variables n1, n2 and n3
respectively.
Then, to find the largest, the following conditions are checked using if else statements
1. If n1 is greater or equals to both n2 and n3, n1 is the greatest.
2. If n2 is greater or equals to both n1 and n3, n2 is the greatest.
3. Else, n3 is the greatest.
Using nested if..else statement
public class Largest {
public static void main(String[] args) {
double n1 = -4.5, n2 = 3.9, n3 = 5.5;
if(n1 >= n2) {
if(n1 >= n3)
System.out.println(n1 + " is the largest number.");
else
System.out.println(n3 + " is the largest number.");
} else {
if(n2 >= n3)
System.out.println(n2 + " is the largest number.");
else
System.out.println(n3 + " is the largest number.");
}
}
}
Output
5.5 is the largest number.
In the above program, instead of checking for two conditions in a single if statement, we use
nested if to find the greatest.

Then, to find the largest, the following conditions are checked using if else statements
1. If n1 is greater or equals to n2,
a. and if n1 is greater or equals to n3, n1 is the greatest.
b. else, n3 is the greatest.
2. Else,
a. if n2 is greater or equals to both n3, n2 is the greatest.
b. else, n3 is the greatest.
All roots of a quadratic equation
In this program, you'll learn to find all roots of a quadratic equation and print them using format()
in Java.
The standard form of a quadratic equation is:
ax2 + bx + c = 0
Here, a, b, and c are real numbers and a can't be equal to 0.
We can calculate the root of a quadratic by using the formula:
x = (-b ± √(b2-4ac)) / (2a)
The ± sign indicates that there will be two roots:
root1 = (-b + √(b2-4ac)) / (2a)
root1 = (-b - √(b2-4ac)) / (2a)
The term b2-4ac is known as the determinant of a quadratic equation. It specifies the nature of
roots. That is,
● if determinant > 0, roots are real and different
● if determinant == 0, roots are real and equal
● if determinant < 0, roots are complex complex and different
public class Main {
public static void main(String[] args) {
// value a, b, and c
double a = 2.3, b = 4, c = 5.6;
double root1, root2;

// calculate the determinant (b2 - 4ac)
double determinant = b * b - 4 * a * c;
// check if determinant is greater than 0
if (determinant > 0) {
// two real and distinct roots
root1 = (-b + Math.sqrt(determinant)) / (2 * a);
root2 = (-b - Math.sqrt(determinant)) / (2 * a);
System.out.format("root1 = %.2f and root2 = %.2f", root1, root2);
}
// check if determinant is equal to 0
else if (determinant == 0) {
// two real and equal roots
// determinant is equal to 0
// so -b + 0 == -b
root1 = root2 = -b / (2 * a);
System.out.format("root1 = root2 = %.2f;", root1);
}
// if determinant is less than zero
else {
// roots are complex number and distinct
double real = -b / (2 * a);
double imaginary = Math.sqrt(-determinant) / (2 * a);
System.out.format("root1 = %.2f+%.2fi", real, imaginary);
System.out.format("\nroot2 = %.2f-%.2fi", real, imaginary);
}
}
}
Output
root1 = -0.87+1.30i and root2 = -0.87-1.30i
In the above program, the coefficients a, b, and c are set to 2.3, 4, and 5.6 respectively. Then,
the determinant is calculated as b2 - 4ac.

Based on the value of the determinant, the roots are calculated as given in the formula above.
Notice we've used library function Math.sqrt() to calculate the square root of a number.
We have used the format() method to print the calculated roots.
The format() function can also be replaced by printf() as:
System.out.printf("root1 = root2 = %.2f;", root1);
Frequency of character
In this program, you'll learn to find the occurence (frequency) of a character in a given string.
public class Frequency {
public static void main(String[] args) {
String str = "This website is awesome.";
char ch = 'e';
int frequency = 0;
for(int i = 0; i < str.length(); i++) {
if(ch == str.charAt(i)) {
++frequency;
}
}
System.out.println("Frequency of " + ch + " = " + frequency);
}
}
Output
Frequency of e = 4
In the above program, the length of the given string, str, is found using the string method
length().
We loop through each character in the string using charAt() function which takes the index (i)
and returns the character in the given index.
We compare each character to the given character ch. If it's a match, we increase the value of
frequency by 1.

In the end, we get the total occurrence of a character stored in frequency and print it.
Remove whitespaces from a string
In this program, you'll learn to remove all whitespaces in a given string using regular
expressions in Java.
public class Whitespaces {
public static void main(String[] args) {
String sentence = "T
his is b ett
er.";
System.out.println("Original sentence: " + sentence);
sentence = sentence.replaceAll("\\s", "");
System.out.println("After replacement: " + sentence);
}
}
Output
Original sentence: T
his is b ett
er.
After replacement: Thisisbetter.
In the above program, we use String's replaceAll() method to remove and replace all
whitespaces in the string sentence.
We've used regular expression \\s that finds all white space characters (tabs, spaces, new line
character, etc.) in the string. Then, we replace it with "" (empty string literal).
Take string from user
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// create an object of Scanner
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string");

// take the input
String input = sc.nextLine();
System.out.println("Original String: " + input);
// remove white spaces
input = input.replaceAll("\\s", "");
System.out.println("Final String: " + input);
sc.close();
}
}
Output
Enter the string
J av a- P rog ram m ing
Original String: J av a- P rog ram m ing
Final String: Java-Programming
Here, the replaceAll() method replaces all the white spaces from the string.
Round a Number to n Decimal Places
In this program, you'll learn to round a given number to n decimal places in Java.
Using format
public class Decimal {
public static void main(String[] args) {
double num = 1.34567;
System.out.format("%.4f", num);
}
}
Output
1.3457

In the above program, we've used the format() method to print the given floating-point number
num to 4 decimal places. The 4 decimal places are given by the format .4f.
This means, print only up to 4 places after the dot (decimal places), and f means to print the
floating-point number.
Using DecimalFormat
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class Decimal {
public static void main(String[] args) {
double num = 1.34567;
DecimalFormat df = new DecimalFormat("#.###");
df.setRoundingMode(RoundingMode.CEILING);
System.out.println(df.format(num));
}
}
Output
1.346
In the above program, we've used DecimalFormat class to round a given number num.
We declare the format using the # patterns #.###. This means we want num up to 3 decimal
places. We also set the rounding mode to Ceiling, this causes the last given place to be rounded
to its next number.
So, 1.34567 rounded to 3 decimal places prints 1.346, 6 is the next number for 3rd place
decimal 5.

String is Empty or Null
In this program, you'll learn to check if a string is empty or null using a method and the if-else
statement in Java.
class Main {
public static void main(String[] args) {
// create null, empty, and regular strings
String str1 = null;
String str2 = "";
String str3 = " ";
// check if str1 is null or empty
System.out.println("str1 is " + isNullEmpty(str1));
// check if str2 is null or empty
System.out.println("str2 is " + isNullEmpty(str2));
// check if str3 is null or empty
System.out.println("str3 is " + isNullEmpty(str3));
}
// method check if string is null or empty
public static String isNullEmpty(String str) {
// check if string is null
if (str == null) {
return "NULL";
}
// check if string is empty
else if(str.isEmpty()){
return "EMPTY";
}
else {
return "neither NULL nor EMPTY";
}
}
}
Output

str1 is NULL
str2 is EMPTY
str3 is neither NULL nor EMPTY
In the above program, we have created
● a null string str1
● an empty string str2
● a string with white spaces str3
● method isNullEmpty() to check if a string is null or empty
Here, str3 only consists of empty spaces. However, the program doesn't consider it an empty
string.
This is because white spaces are treated as characters in Java and the string with white spaces
is a regular string.
Now, if we want the program to consider strings with white spaces as empty strings, we can use
the trim() method. The method removes all the white spaces present in a string.
String with spaces is Empty or Null
class Main {
public static void main(String[] args) {
// create a string with white spaces
String str = "
";
// check if str1 is null or empty
System.out.println("str is " + isNullEmpty(str));
}
// method check if string is null or empty
public static String isNullEmpty(String str) {
// check if string is null
if (str == null) {
return "NULL";
}
// check if string is empty
else if (str.trim().isEmpty()){
return "EMPTY";
}

else {
return "neither NULL nor EMPTY";
}
}
}
Output
str is EMPTY
In the above example, notice the condition to check empty string
else if (str.trim().isEmpty())
Here, we have used the trim() method before isEmpty(). This will
1. remove all the white spaces present inside the string
2. check if the string is empty
Hence, we get str is EMPTY as output.