Skip to main content

Posts

Showing posts with the label Java,Languages

Java Program to Convert a Decimal Number to Binary Number using Arrays as Stacks

Given an Integer number convert into Binary Number using arrays as a stack. Example: Input : 10 Output: 1010 Input : 16 Output: 10000 Approach: Divide the number by 2 and store the remainder of the number in the array. Divide the number by 2. Repeat the process until the number becomes zero. Print the array in reverse order.   Java // Java Program to Convert a Decimal Number // to Binary Number using Arrays as Stacks   import java.util.*; public class DecimalToBinary      static int arr[] = new int [ 1000 ];        // maintaining count variable      // as the top of the stack      static int count;        // push at the count index and increment the count      public static void push( int n)        arr[count++] = n;             // pop all the elements starting      // from count-1 till 0      public static void pop()               for ( int i = count - 1 ; i >= 0 ; i--)              System.out.print(...

StringBuffer substring() method in Java with Examples

In StringBuffer class, there are two types of substring method depending upon the parameters passed to it. substring(int start) The substring(int start) method of StringBuffer class is the inbuilt method used to return a substring start from index start and extends to end of this sequence. The string returned by this method contains all character from index start to end of the old sequence. Syntax: public String substring(int start) Parameters: This method accepts only one parameter start which is Integer type value refers to the start index of substring. Return Value: This method returns the substring lie in the range start to end of old sequence. Exception: This method throws StringIndexOutOfBoundsException if start is less than zero, or greater than the length of this object. Below programs illustrate the StringBuffer substring() method: Example 1: // Java program to demonstrate // the substring() Method.     class GFG      public ...