Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 80 of 151

Space format specifiers in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

For format specifier, import the following package −import java.util.Formatter;Create a formatter object and set space format specifier −Formatter f = new Formatter(); f.format("% d", -50); System.out.println(f); f = new Formatter(); f.format("% d", 90); System.out.println(f); f = new Formatter(); f.format("%10d", -50); System.out.println(f); f = new Formatter(); f.format("% 10d", 90); System.out.println(f);The following is an example displaying different forms of space format specifiers −Exampleimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); f.format("% d", -50); ...

Read More

Add grouping specifiers for large numbers in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 281 Views

For using the Formatter class, import the following package −import java.util.Formatter;We can group specifiers as shown below −Formatter f = new Formatter(); f.format("%,.2f", 38178.9889);The above sets thousands separator and 2 decimal places.The following is an example −Exampleimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); f.format("%d", 50); System.out.println(f); f = new Formatter(); f.format("%,.2f", 38178.9889); System.out.println(f); } }Output50 38,178.99

Read More

Set a Minimum Field Width in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

The minimum field width specifier is what you include between % and the format conversion code.To include a 0 before the field width specifier, pad with 0's. An integer between the % sign and the format conversion code acts as a minimum field width specifier.Let us see an example −Exampleimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); System.out.println(f.format("%08d", 697)); f = new Formatter(); System.out.println(f.format("%10d", 9878)); f = new Formatter(); System.out.println(f.format("%06d", 697)); } }Output00000697 9878 000697

Read More

Ternary Operator in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

A ternary operator uses 3 operands and it can be used to replace the if else statement. This can be done to make the code simpler and more compact.The syntax of the ternary operator is given as follows −Expression ? Statement 1 : Statement 2In the above syntax, the expression is a conditional expression that results in true or false. If the value of the expression is true, then statement 1 is executed otherwise statement 2 is executed.A program that demonstrates the ternary operator in Java is given as follows.Examplepublic class Example {    public static void main(String[] args) { ...

Read More

Java Program to flip a bit in a BigInteger

Samual Sam
Samual Sam
Updated on 11-Mar-2026 247 Views

To flip a bit in a BigInteger in Java, use the flipBit() method. This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped.Exampleimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two;       one = new BigInteger("7");       one = one.flipBit(3);       System.out.println("Result: " +one);    } }OutputResult: 15Let us see another example.Exampleimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger bi1, bi2;       bi1 = new BigInteger("8"); ...

Read More

Shift left in a BigInteger in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 248 Views

To shift left in a BigInteger, use the shiftLeft() method.The java.math.BigInteger.shiftLeft(int n) returns a BigInteger whose value is (this

Read More

Narrowing Conversion in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 3K+ Views

Narrowing conversion is needed when you convert from a larger size type to a smaller size. This is for incompatible data types, wherein automatic conversions cannot be done.Let us see an example wherein we are converting long to integer using Narrowing Conversion.Examplepublic class Demo {     public static void main(String[] args) {        long longVal = 878;        int intVal = (int) longVal;        System.out.println("Long: "+longVal);        System.out.println("Integer: "+intVal);     } }OutputLong: 878 Integer: 878Let us see another example, wherein we are converting double to long using Narrowing Conversion.Examplepublic class Demo {     public static void main(String[] args) {        double doubleVal = 299.89;        long longVal = (long)doubleVal;        System.out.println("Double: "+doubleVal); ...

Read More

Display two-digit year in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

Use the ‘y’ date conversion character to display two-digit year.System.out.printf("Two-digit Year = %TY", d);Above, d is a date object −Date d = new Date();The following is an example −Exampleimport java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; public class Demo {    public static void main(String[] args) throws Exception {       Date d = new Date();       DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");       String format = dateFormat.format(d);       System.out.println("Current date and time = " + format);       System.out.printf("Four-digit Year = %TY", d);       System.out.printf("Two-digit Year = %ty", d);   ...

Read More

Subtract minutes from current time using Calendar.add() method in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 3K+ Views

Import the following package for Calendar class in Java.import java.util.Calendar;Firstly, create a Calendar object and display the current date and time.Calendar calendar = Calendar.getInstance(); System.out.println("Current Date and Time = " + calendar.getTime());Now, let us decrement the minutes using the calendar.add() method and Calendar.MINUTE constant. Set a negative value since you want to decrease the minutes.calendar.add(Calendar.MINUTE, -15);Exampleimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Subtract 15 minutes from current date       calendar.add(Calendar.MINUTE, -15);   ...

Read More

C++ Program to Compute Combinations using Factorials

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

The following is an example to compute combinations using factorials.Example#include using namespace std; int fact(int n) {    if (n == 0 || n == 1)    return 1;    else    return n * fact(n - 1); } int main() {    int n, r, result;    coutn;    coutr;    result = fact(n) / (fact(r) * fact(n-r));    cout

Read More
Showing 791–800 of 1,507 articles
« Prev 1 78 79 80 81 82 151 Next »
Advertisements