Programming Articles - Page 2529 of 3366

C++ program to read file word by word?

Arnab Chakraborty
Updated on 13-Aug-2019 10:28:27

5K+ Views

In this section we will see how we can read file content word by word using C++. The task is very simple. we have to use the file input stream to read file contents. The file stream will open the file by using file name, then using FileStream, load each word and store it into a variable called word. Then print each word one by one.Algorithmread_word_by_word(filename)begin    file = open file using filename    while file has new word, do       print the word into the console    done endFile Content (test_file.txt)This is a test file. There are ... Read More

C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! + …… n/n!

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

2K+ Views

Here we will see how we can get the sum of the given series. The value of n will be given by user. We can solve this problem by making a factorial function, and get factorial in each step in the loop. But factorial calculation is costlier task than normal addition. We will use the previous factorial term in the next one. Like 3! is (3 * 2 * 1), and 4! is 4 * 3!. So if we store 3! into some variable, we can use that and add the next number only to get the next factorial easily.Algorithmsum_series_fact(n)begin ... Read More

C++ Program to count Vowels in a string using Pointer?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

588 Views

To get the vowels from a string, we have to iterate through each character of the string. Here we have to use pointers to move through the string. For this we need C style strings. If the string is pointed by str, then *str will hold the first character at the beginning. Then if str is increased, the *str will point next character and so on. If the character is in [a, e, i, o, u] or [A, E, I, O, U] then it is vowel. So we will increase the countAlgorithmcountVowels(str)begin    count := 0    for each character ... Read More

C++ program to concatenate a string given number of times?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

607 Views

Here we will see how we can concatenate a string n number of times. The value of n is given by the user. This problem is very simple. In C++ we can use + operator for concatenation. Please go through the code to get the idea.AlgorithmconcatStrNTimes(str, n)begin    res := empty string    for i in range 1 to n, do       res := concatenate res and res    done    return res endExample Live Demo#include using namespace std; main() {    string myStr, res = "";    int n;    cout > myStr;    cout > n;    for(int i= 0; i < n; i++) {       res += myStr;    }    cout

How can we add/insert a JButton to JTable cell in Java?

raja
Updated on 10-Feb-2020 11:10:47

8K+ Views

A JTable is a subclass of JComponent class and it can be used to create a table with information displayed in multiple rows and columns. When a value is selected from a JTable, a TableModelEvent is generated, which is handled by implementing a TableModelListener interface. We can add or insert a JButton to JTable cell by customizing the code either in DefaultTableModel or AbstractTableModel and we can also customize the code by implementing TableCellRenderer interface and need to override getTableCellRendererComponent() method.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class JTableButtonTest extends JFrame {    private JTable table;    private JScrollPane scrollPane;    public JTableButtonTest() {     ... Read More

How to read an input value from a JTextField and add to a JList in Java?

Alshifa Hasnain
Updated on 15-May-2025 19:38:52

2K+ Views

In this article, we will learn to read an input value from a JTextField and add it to a JList in Java. The Swing's two commonly used components are JTextField for text input and JList for displaying a list of items for building graphical user interfaces. JList A JList is a subclass of the JComponent class that allows the user to choose either a single selection or multiple selections. The JList class itself does not support a scrollbar. In order to add a scrollbar, we have to use the JScrollPane class together with the JList class. The JScrollPane then manages ... Read More

How can we set the margin to a JButton in Java?

Alshifa Hasnain
Updated on 12-May-2025 12:50:14

4K+ Views

While developing Java Swing applications, you may have cases when you need to modify the space around JButtons to develop attractive applications. In this article, we will learn to set the margin of a JButton in Java. What is a JButton? A JButton is a subclass of AbstractButton, and it can be used for adding platform-independent buttons to a Java Swing application. A Button can generate an ActionListener interface when the button is pressed or clicked, it can also generate the MouseListener and KeyListener interfaces. Syntax The following is the syntax for JButton initialization: JButton button = new JButton("Button"); We ... Read More

C++ Program for Sum of squares of first n natural numbers?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

629 Views

In this problem we will see how we can get the sum of squares of first n natural numbers. Here we are using one for loop, that runs from 1 to n. In each step we are calculating square of the term and then add it to the sum. This program takes O(n) time to complete. But if we want to solve this in O(1) or constant time, we can use this series formula −AlgorithmsquareNNatural(n)begin    sum := 0    for i in range 1 to n, do       sum := sum + i^2    done    return ... Read More

C++ program for Solving Cryptarithmetic Puzzles

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

1K+ Views

In the crypt-arithmetic problem, some letters are used to assign digits to it. Like ten different letters are holding digit values from 0 to 9 to perform arithmetic operations correctly. There are two words are given and another word is given as answer of addition for those two words. As an example we can say that two words ‘BASE’ and ‘BALL’, and the result is ‘GAMES’. Now if we try to add BASE and BALL by their symbolic digits, we will get the answer GAMES.NOTE − There must be ten letters maximum, otherwise it cannot be solved.InputThis algorithm will take ... Read More

C++ Program for Smallest K digit number divisible by X?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

271 Views

In this problem we will try to find smallest K-digit number, that will be divisible by X. To do this task we will take the smallest K digit number by this formula (10^(k-1)). Then check whether the number is divisible by X or not, if not, we will get the exact number by using this formula.(min+ 𝑋)−((min+ 𝑋) 𝑚𝑜𝑑 𝑋)One example is like a 5-digit number, that is divisible by 29. So the smallest 5-digit number is 10000. This is not divisible by 29. Now by applying the formula we will get −(10000+ 29)−((10000+29) 𝑚𝑜𝑑 29)=10029−24=10005The number 10005 is divisible ... Read More

Advertisements