C/C++ Program for Triangular Matchstick Number?

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

81 Views

Here we will see how to count number of matchsticks are required to make the pyramid-like below. The base of the pyramid is given. So if the base is 1, it will take 3 matchsticks to make a pyramid, for base 2, 9 matchsticks are needed, for base size 3, it will take 18 matchsticks.To solve this problem, we have to use this formula −Example Live Demo#include using namespace std; int main(){    int x;    cout > x;    int count = 3*x*(x+1)/2;    cout

Advanced C++ with boost library

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

1K+ Views

C++ boost libraries are widely useful library. This is used for different sections. It has large domain of applications. For example, using boost, we can use large number like 264 in C++.Here we will see some examples of boost library. We can use big integer datatype. We can use different datatypes like int128_t, int256_t, int1024_t etc. By using this we can get precision up to 1024 easily.At first we are multiplying two huge number using boost library.Example#include #include using namespace boost::multiprecision; using namespace std; int128_t large_product(long long n1, long long n2) {    int128_t ans = (int128_t) n1 * ... Read More

What is a null-terminated string in C/C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

9K+ Views

In C the strings are basically array of characters. In C++ the std::string is an advancement of that array. There are some additional features with the traditional character array. The null terminated strings are basically a sequence of characters, and the last element is one null character (denoted by ‘\0’). When we write some string using double quotes (“…”), then it is converted into null terminated strings by the compiler.The size of the string may smaller than the array size, but if there are some null character inside that array, that will be treated as the end of that string.See ... Read More

How to establish a connection with the database using the properties file in JDBC?

Rishi Raj
Updated on 30-Jul-2019 22:30:26

1K+ Views

One of the variant of the getConnection() method of the DriverManager class accepts url of the database, (String format) a properties file and establishes connection with the database.Connection con = DriverManager.getConnection(url, properties);To establish a connection with a database using this method −Set the Driver class name as a system property −System.setProperty("Jdbc.drivers", "com.mysql.jdbc.Driver");Create a properties object as −Properties properties = new Properties();Add the user name and password to the above created Properties object as −properties.put("user", "root"); properties.put("password", "password");Finally invoke the getConnection() method of the DriverManager class by passing the URL and, properties object as parameters.//Getting the connection String url = "jdbc:mysql://localhost/mydatabase"; Connection con = ... Read More

Querying null value in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

268 Views

To query null value in MongoDB, use $ne operator. Let us first create a collection with documents −> db.queryingNullDemo.insertOne( ...    { ...       "StudentName":"Larry", ...       "StudentDetails": ...       { ...          "StudentAge":21, ...          "StudentSubject":"MongoDB" ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd00bec588d4a6447b2e05f") } > db.queryingNullDemo.insertOne( ...    { ...       "StudentName":"Sam", ...       "StudentDetails": ...       { ...          "StudentAge":23, ...         ... Read More

How to enable row selection in a JTable with Java

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

1K+ Views

To enable row selection, use the setRowSelectionAllowed () method and set it to TRUE −table.setCell setRowSelectionAllowed(true);The following is an example to enable row selection in a JTable −Examplepackage my; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.TitledBorder; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       JPanel panel = new JPanel();       panel.setBorder(BorderFactory.createTitledBorder(          BorderFactory.createEtchedBorder(), "ODI Rankings", TitledBorder.CENTER, TitledBorder.TOP));       String[][] rec = {          { "1", "Steve", "AUS" },   ... Read More

Get MAX() on column in two MySQL tables?

George John
Updated on 30-Jul-2019 22:30:26

367 Views

Use GREATEST() to find the maximum. Let us first create a table −mysql> create table DemoTable1    (    Number int    ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(80); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable1 values(229); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(575); Query OK, 1 row affected (0.24 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;Output+--------+ | Number | +--------+ | 80 | | 229 ... Read More

Differentiate between the prefix and postfix forms of the ++ operator in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

3K+ Views

Java provides two operators namely ++ and --, to increment and decrement values by 1 respectively.There are two variants of these operators −Pre-increment/decrement − This form, increments/decrements the value first, and then performs the specified operation.ExampleIn the following example, the initial value of the variable i is 5. We are printing the incremented value of it using the pre increment operator.Since we are using the pre increment operator, the value of i is incremented then printed. Live Demopublic class ForLoopExample {    public static void main(String args[]) {       int i = 5;       System.out.println(++i);     ... Read More

C/C++ Program to Count Inversions in an array using Merge Sort?

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

598 Views

The inversions of an array indicate; how many changes are required to convert the array into its sorted form. When an array is already sorted, it needs 0 inversions, and in other case, the number of inversions will be maximum, if the array is reversed.To solve this problem, we will follow the Merge sort approach to reduce the time complexity, and make it in Divide and Conquer algorithm.InputA sequence of numbers. (1, 5, 6, 4, 20).OutputThe number of inversions required to arrange the numbers into ascending order.Here the number of inversions are 2. First inversion: (1, 5, 4, 6, 20) ... Read More

How to display row count in Java Swing JList

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

353 Views

Use JList getVisibleRowCount() method to display the row count in a JList:String sports[]= {"Tennis", "Archery", "Football", "Fencing", "Cricket", "Squash", "Hockey", "Rugby"}; Jlist list = new JList(sports); int rowCount = list.getVisibleRowCount(); System.out.println("Row Count = "+rowCount);The following is an example to display row count in JList:Exampleimport java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame {    static JFrame frame;    static JList list;    public static void main(String[] args) {       frame = new JFrame("JList Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       String sports[]= {"Tennis", Archery", "Football", "Fencing", "Cricket", "Squash", "Hockey", "Rugby"};       list = ... Read More

Advertisements