Programming Articles

Page 176 of 2544

How to sum up elements of a C++ vector?

Vishesh Raina
Vishesh Raina
Updated on 01-Aug-2024 27K+ Views

In this article, we will understand how to sum the elements present inside a vector in C++. A vector is a dynamically allocated array with variable size. The sum of elements of a vector can be calculated in a number of ways, and we will discuss two such ways. Problem Statement Given a non-empty vector of integers, calculate the sum of all elements of the vector using multiple approaches in C++. Examples The following examples give the input and output of some test cases : Input vector vec={1, 3, 4, 5, 2, 3} Output ...

Read More

Java Program to convert string to byte

Shriansh Kumar
Shriansh Kumar
Updated on 01-Aug-2024 1K+ Views

Suppose you are given a String named "str". Now, your task is to write a Java program to convert the given string to Byte. String is a class in Java which stores sequence of characters within double quotes and Byte is a wrapper class of java.lang package which wraps value of byte datatype. Example Scenario: Input: String str = "65"; Output: res = 65 Using Byte.valueOf() Method The valueOf() method of Java Byte class is used to convert given string into its corresponding Byte object. It accepts a single numeric string as a parameter value and returns it as ...

Read More

Java Program to Check Whether a Number is Prime or Not

Shriansh Kumar
Shriansh Kumar
Updated on 01-Aug-2024 28K+ Views

Given a number, let's say N, write a Java program to check whether the given number is prime or not. Prime numbers are special numbers with only two factors 1 and that number itself, they cannot be divided by any other number. Example Scenario 1 Input: num = 1; Output: 1 is not a prime number Example Scenario 2 Input: num2 = 5; Output: 5 is a prime number 5 is divisible by 1 and 5 only Checking Prime Numbers using Factorization The naive approach to check a given number is prime or not is factorization. ...

Read More

Java Program to Check whether the input number is a Neon Number

Shriansh Kumar
Shriansh Kumar
Updated on 01-Aug-2024 1K+ Views

In this article, we will understand how to check whether the given input number is a neon number. A neon number is such a number whose sum of digits of square of the number is equal to the number itself. Example Scenario: Input: num = 9; Output: The given input is a neon number How to Check Neon Number in Java? To check a given input is a neon number in Java, use the below approaches − Using for loop Using while loop Using recursion Using ...

Read More

Java Program to compare dates if a date is after another date

Shriansh Kumar
Shriansh Kumar
Updated on 01-Aug-2024 1K+ Views

In Java, there are a few scenarios where we are required to work with the date and time such as while developing a calendar application, attendance management system in Java and checking age of two persons. Also, the date is a way of keeping track of our time as it is an integral part of our daily lives. Therefore, Java provides classes like Date and LocalDate to work with date and time. In this article, we will discuss how to compare and check if a date is after another or not. Example Scenario 1 Input: dateOne = "2023-01-20"; ...

Read More

Java String Concatenation with Other Data Types.

Shriansh Kumar
Shriansh Kumar
Updated on 01-Aug-2024 2K+ Views

In Java, string concatenation is the operation of joining two or more strings together. However, string concatenation can be performed with various primitive data types, not just with other strings. Two strings can be concatenated using concat() method of String class, but, to concatenate strings with other primitive data type you need to use the ‘+’ operator. The given data type will automatically converted to its string representation. Example Scenario: Input: String res = "Age: " + 45; Output: result = Age: 45 String Concatenation with int The int is a primitive datatype in Java which represents numeric data ...

Read More

Java Program to return the elements at the odd position in a list

Shriansh Kumar
Shriansh Kumar
Updated on 01-Aug-2024 972 Views

What is the Odd Position in a List? In Java, a List does not have predefined odd and even positions. However, if one considers the first position as index 0, then an odd position in the given List would be any index that is not divisible by 2. To determine if a position is odd, use the modulo operator (%).   How to Find Elements at Odd Position in a List? Below approaches are followed to find out the elements at the odd position in a list − By dividing index with 2 ...

Read More

Java Program to Find the Perimeter of a Rectangle

Shriansh Kumar
Shriansh Kumar
Updated on 01-Aug-2024 2K+ Views

For a given rectangle of length l and width w, write a Java program to find its perimeter. The Perimeter of a rectangle is calculated by adding the lengths of all the sides of the rectangle. Below is a demonstration of a rectangle, a quadrilateral with four right angles (90°). The perimeter of a rectangle is the total length of the two lengths and two widths of the rectangle − Example Scenario: Input: length = 5, 8, 5, 8; Output: Perimeter = 26 On adding all the sides of rectangle, you will get perimeter. 5+8+5+8 = 26 Steps ...

Read More

Java Program to Check if JVM is 32 or 64 bit

Shriansh Kumar
Shriansh Kumar
Updated on 01-Aug-2024 954 Views

In this article, we will learn how to determine whether the installed JVM in your system is 32 bit or 64 bit. For this operation, we use the getProperty() method of System class, which helps in retrieving system property of specified argument. In computer architecture, 32 bit and 64 bit refers to those components that operate on data in 32-bit units and 64-bit units respectively. 64 bit machines are said to be faster and more secure compare to the 32 bit machines. What is JVM? JVM is Java Virtual Machine that is responsible for executing the byte code. It is ...

Read More

Java Program To Find all the Subsets of a String

Shriansh Kumar
Shriansh Kumar
Updated on 01-Aug-2024 2K+ Views

In this article, we will understand how to find all the subsets of a string. In Java, String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). A part or a subset of string is called substring. Example Scenario: Input: string = JVM; Output: The subsets of the string are: J, JV, JVM, V, VM, M Finding all Subsets of a String in Java Use the following approaches to find all subsets of a String in Java − Using nested for Loop ...

Read More
Showing 1751–1760 of 25,433 articles
« Prev 1 174 175 176 177 178 2544 Next »
Advertisements