Average of First N Odd Natural Numbers

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

292 Views

Here we will see how to get the average of n odd natural numbers. The n is given by the user. To get the ith odd number we can use this formula 2*i+1. Here also we are using this formula. Let us see the algorithm to get the clear idea.AlgorithmavgOddNaturalNumber(n)Begin    sum := 0    for i in range 0 to n-1, do       sum := sum + (2i + 1)    done    return sum/n EndExample Live Demo#include using namespace std; float asciiAverage(string str){    int sum = 0;    for(int i = 0; i str;    cout

Handling Large Numbers in C++

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

12K+ Views

In C++, we can use large numbers by using the boost library. This C++ boost library is widely used 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 ... Read More

POP3 Protocol Client in Python

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

710 Views

The poolib module from Python's standard library defines POP3 and POP3_SSL classes. POP3 class encapsulates a connection to a POP3 server and implements the protocol as defined in RFC 1939. POP3_SSL classsupports POP3 servers that use SSL as an underlying protocol layer.POP3 protocolis obsolescent as its implementation quality of POP3 servers is quite poor. If your mailserver supports IMAP, it is recommended to use the imaplib.IMAP4 class.Both classes have following methods defined −getwelcome()Returns the greeting string sent by the POP3 server.user(username)Send user command, response should indicate that a password is required.pass_(password)Send password.Stat()Get mailbox status. The result contains 2 integers: (message ... Read More

Create Android Notification with Expiration Date

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

864 Views

This example demonstrate about How to create an Android notification with expiration dateStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.package app.tutorialspoint.com.notifyme ; import android.app.AlarmManager ; import android.app.DatePickerDialog ; import android.app.Notification ; import android.app.PendingIntent ; import android.content.Context ; import android.content.Intent ; import android.os.Bundle ; import android.support.v4.app.NotificationCompat ; import android.support.v7.app.AppCompatActivity ; import android.view.View ; import android.widget.Button ; import android.widget.DatePicker ; import java.text.SimpleDateFormat ; import ... Read More

The Adaptive Tree Walk Protocol

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

8K+ Views

Adaptive Tree Walk Protocol is a technique for transmitting data over shared channels that combines the advantages of collision based protocols and collision free protocols.In computer networks, when more than one station tries to transmit simultaneously via a shared channel, the transmitted data is garbled, an event called collision. In collision based protocols like ALOHA, all stations are permitted to transmit a frame without trying to detect whether the transmission channel is idle or busy. This works very good under light loads. Under heavy loads, collision free protocols are suitable, since channel access is resolved in the contention period that ... Read More

Set Palette Layer for JDesktopPane in Java

Chandu yadav
Updated on 30-Jul-2019 22:30:26

240 Views

At first, create a JDesktopPane −JDesktopPane desktopPane = new JDesktopPane();Now, create an Internal Frame −JInternalFrame intFrame = new JInternalFrame("Our Frame", true, true, true, true); intFrame.setBounds(50, 90, 200, 250);Set Pallette layer for JDesktopPane and add the Internal Frame to JDesktopPane −intFrame.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE); desktopPane.add(intFrame, JDesktopPane.PALETTE_LAYER);The following is an example to set palette layer for JDesktopPane −package my; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; public class SwingDemo {    public static void main(final String[] args) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JDesktopPane desktopPane = new JDesktopPane();   ... Read More

Specify Return Format in MongoDB to Return Values as an Array

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

235 Views

Use aggregation for this and add the values to an array using the $group and $addToSet operatorLet us first create a collection with documents −> dbspecifyReturnFormatDemoinsertOne({"Subject":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefd364ef71edecf6a1f6c0") } > dbspecifyReturnFormatDemoinsertOne({"Subject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefd369ef71edecf6a1f6c1") } > dbspecifyReturnFormatDemoinsertOne({"Subject":"SQL Server"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefd36fef71edecf6a1f6c2") }Following is the query to display all documents from a collection with the help of find() method −> dbspecifyReturnFormatDemofind();Output{ "_id" : ObjectId("5cefd364ef71edecf6a1f6c0"), "Subject" : "MongoDB" } { "_id" : ObjectId("5cefd369ef71edecf6a1f6c1"), "Subject" : "MySQL" } { "_id" : ObjectId("5cefd36fef71edecf6a1f6c2"), "Subject" : ... Read More

Retrieve DataLink Object from a Table Using JDBC

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

228 Views

A DATALINK object represents an URL value which refers to an external resource (outside the current database/data source), which can be a file, directory etc..You can retrieve a DATALINK object an SQL table using the getURL() method of the PreparedStatement interface. This method accepts an integer value representing an index of the column in the ResultSet and returns the URL object in the specified index.ExampleLet us create a table with name tutorials_data in MySQL database using CREATE statement as shown below −CREATE TABLE tutorials_data (    tutorial_id INT PRIMARY KEY AUTO_INCREMENT,    tutorial_title VARCHAR(100),    tutorial_author VARCHAR(40),    submission_date date, ... Read More

Program for Printing Array in Pendulum Arrangement

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

857 Views

To arrange elements of the array following pendulum arrangement.Sort the given array, create an empty array to store the result.Store the 0th element in a variable say temp.Store the element at index 1 in the sorted array in (mid+1)st position of the resultant array, and the next element int the (mid-1)st position and the next element in (mid+2)nd element and so on.Example Live Demoimport java.util.Arrays; import java.util.Scanner; public class ArrayinPendulumArrangement {    public static int[] swap(int origPos, int newPos, int[] array){       origPos = 1;       newPos = 4;       int temp = array[origPos];   ... Read More

Get Time in Milliseconds Using C++ on Linux

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

5K+ Views

Here we will see how to get time (the elapsed time for the program or any other kind of time).Here we are using linux library for C++. There is a structure called timeval. This timeval stores the time in seconds, milliseconds. We can create two time for start and end, then find the difference from them.Example#include #include #include using namespace std; main() {    struct timeval start_time, end_time;    long milli_time, seconds, useconds;    gettimeofday(&start_time, NULL);    cout > ch;    gettimeofday(&end_time, NULL);    seconds = end_time.tv_sec - start_time.tv_sec; //seconds    useconds = end_time.tv_usec - start_time.tv_usec; //milliseconds ... Read More

Advertisements