Programming Articles - Page 2652 of 3366

C++ Program to Find the Number of occurrences of a given Number using Binary Search approach

Ravi Ranjan
Updated on 20-Aug-2025 13:43:41

303 Views

In this article, our task is to find the number of occurrences of a given number using binary search. The binary search algorithm works on the divide-and-conquer principle as it keeps dividing the array in half before searching. To search for an element in an array using binary search, it should be sorted. In the sorted array, we find the middle element and compare it with the element that has to be searched, and based on the comparison, we either search in the left or right sub-array or return the middle element. Following are some example scenarios: Scenario ... Read More

Java Program to use Soft Bevel Border in Swing

Daniol Thomas
Updated on 30-Jul-2019 22:30:26

269 Views

Here, we are creating soft beven border on JComboBox:JComboBox comboBox = new JComboBox(list);Now, set bevel border:comboBox.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));The following is an example to use soft bevel border in Swing:Exampleimport java.awt.Font; import java.awt.GridBagLayout; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.BevelBorder; import javax.swing.border.SoftBevelBorder; public class SwingDemo {    static final String list[] = { "One", "Two", "Three", "Four", "Five", "Six" };    public static void main(String[] args) {       JFrame window = new JFrame("ComboBox Example");       JPanel panel = new JPanel();       panel.setLayout(new GridBagLayout());       JComboBox comboBox = new JComboBox(list);       comboBox.setBorder(new ... Read More

Use of bool in C

Samual Sam
Updated on 30-Jul-2019 22:30:26

2K+ Views

In C there is no predefined datatype as bool. We can create bool using enum. One enum will be created as bool, then put the false, and true as the element of the enum. The false will be at the first position, so it will hold 0, and true will be at second position, so it will get value 1. Now we can use this as datatype.Example#include typedef enum {    F, T } boolean; main() {    boolean my_bool1, my_bool2;    my_bool1 = F;    if(my_bool1 == F) {       printf("my_bool1 is false");    } else { ... Read More

Write your own memcpy() in C

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

3K+ Views

Here we will see how to implement memcpy() function in C. The memcpy() function is used to copy a block of data from one location to another. The syntax of the memcpy() is like below −void * memcpy(void * dest, const void * srd, size_t num);To make our own memcpy, we have to typecast the given address to char*, then copy data from source to destination byte by byte. Just go through the following code to get better idea.Example#include #include void custom_memcpy(void *dest, void *src, size_t n) {    int i;    //cast src and dest to char*    char ... Read More

How to leave space with EmptyBorder in Java Swing

Daniol Thomas
Updated on 30-Jul-2019 22:30:26

561 Views

Llet us first create a JPanel and set titled border:JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createTitledBorder("Demo Panel"));Now to create Empty Border:JPanel panel2 = new JPanel(new BorderLayout()); panel2.add(panel, BorderLayout.CENTER); panel2.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));The following is an example to leave space with EmptyBorder in Java Swing:Exampleimport java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JPanel panel = new JPanel();       panel.setBorder(BorderFactory.createTitledBorder("Demo Panel"));       JPanel panel2 = new JPanel(new BorderLayout());       panel2.add(panel, BorderLayout.CENTER);       panel2.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));       ... Read More

How to write a running C code without main()?

Samual Sam
Updated on 30-Jul-2019 22:30:26

8K+ Views

Here we will see, one program can be written without main or not? The answer is yes. We can write program, that has no main() function.In many places, we have seen that the main() is the entry point of a program execution. Just from the programmers perspective this is true. From the system’s perspective it is not true. So the system at first calls the _start(), this sets up the environment, then main is called.To execute this program we have to use this option ‘-nostartfiles’.Example#include extern void _exit(register int); int _start() {    printf("Program without main");       ... Read More

Java Program to create rounded borders in Swing

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

6K+ Views

Let us first create a Frame:JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setUndecorated(true);Now, create rounded borders:double x = 50; double y = 50; frame.setShape(new RoundRectangle2D.Double(x, y, 100, 100, 50, 50));The following is an example to create rounded borders in Swing:Exampleimport java.awt.geom.RoundRectangle2D; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo extends JPanel {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.setUndecorated(true);       double x = 50;       double y = 50;       frame.setShape(new RoundRectangle2D.Double(x, y, 100, 100, 50, 50));     ... Read More

Print a long int in C using putchar() only

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

2K+ Views

Here we will see how to print long int value using the putchar() function in C. We can easily print the value of some variables using printf() in C, but here the restriction is, we cannot use any other function except putchar().As we know that the putchar() is used to print only characters. We can use this function to print each digit of the number. When one numeric value is passed, we have to add character ‘0’ with it to get the ASCII form. Let us see the code to get the better idea.Example#include void print_long(long value) {   ... Read More

Java Program to add Titled Border to Panel in Swing

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

280 Views

To set Titled Border to Panel, let us first create a Panel for our Java Swing Application:JPanel panel = new Jpanel(new BorderLayout());Now, set the titled border:panel.setBorder(new TitledBorder("Displaying Titled Border"));The following is an example to add Titled Border to Panel in Swing:Exampleimport java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.border.TitledBorder; public class SwingDemo {    public static void main(String[] args) {       JPanel panel = new JPanel(new BorderLayout());       panel.setBorder(new TitledBorder("Displaying Titled Border"));       panel.add(new JButton("Demo Button"), BorderLayout.SOUTH);       JOptionPane.showMessageDialog(null, panel);    } }OutputRead More

How to count set bits in a floating point number in C?

Samual Sam
Updated on 30-Jul-2019 22:30:26

542 Views

In this problem, one floating point value is given. We have to find number of set bits in the binary representation of it.For example, if a floating point number is 0.15625, so there are six set bits. A typical C compiler used single precision floating point representation. So it will be look like this.To convert into its bit values, we have to take the number into one pointer variable, then typecast the pointer to char* type data. Then process each byte one by one. Then we can count set bits of each char.Example#include int char_set_bit_count(char number) {    unsigned ... Read More

Advertisements