Programming Articles - Page 1786 of 3366

Sum of two large numbers in C++

sudhir sharma
Updated on 17-Aug-2020 10:40:28

5K+ Views

In this problem, we are given two string that defines two large numbers. Our task is to create a program to find the sum of two large numbers.Let’s take an example to understand the problem, Input: number1 = “341299123919” number2 = “52413424” Output: 341351537343To solve this problem, we will traverse both the string. And add digit by digit and propagate the carry. And store the result digit by digit to sum string.AlgorithmInitialize sum = 0, carry = 0. Step 1: loop from n to 0. Step 1.1: intSum = number1[i] + number2[i] Step 1.2: carry = intSum/10. Sum += intSum ... Read More

Sum of the series Kn + ( K(n-1) * (K-1)1 ) + ( K(n-2) * (K-1)2 ) + ... (K-1)n in C++

sudhir sharma
Updated on 17-Aug-2020 10:22:15

332 Views

In the problem, we are ginen two number k and n of the series K^n + ( K^(n-1) * (K-1)^1 ) + ( K^(n-2) * (K-1)^2 ) + ... (K-1)^n. Our task is to create a program to find the sum of the series.Let’s take an example to understand the problem, Input: n = 3, k = 4 Output: 175 Explanation: Sum of the series is = 4^3 + ( (4^2)*(3^1) ) + ( (4^1)*(3^2) ) + ( (4^0)*(3^3) ) = 64 + 48 + 36 + 27 = 175A simple way to solve the problem, is using a for ... Read More

Sum of XOR of all subarrays in C++

sudhir sharma
Updated on 17-Aug-2020 10:14:33

1K+ Views

In this problem, we are given an array arr[] of n numbers. Our task is to create a program to find the sum of XOR of all subarrays of the array.Here, we need to find all sub-arrays of the given array, and then for each subarray, we will find the xor of element and add the XOR value to the sum variable.Let’s take an example to understand the problem, Input: arr[] = {5, 1, 4} Output: Explanation: XOR of all subarrays for the array : XOR {5} = 5 XOR {1} = 1 XOR {4} = 4 XOR {5, 1} ... Read More

Sum of XOR of all possible subsets in C++

sudhir sharma
Updated on 17-Aug-2020 10:00:52

430 Views

In this problem, we are given an array aar[] of n numbers. Our task is to create a program to find the Sum of XOR of all possible subsets.Here, we will find all subsets of the array. Then for each subset, we will find the XOR of elements of the subset and add them to the sum variable.Let’s take an example to understand the problem, Input: arr[] = {5, 1, 4} Output: 20 Explanation: XOR of all subsets: {5} = 5 {1} = 1 {4} = 4 {5, 1} = 4 {5, 4} = 1 {1, 4} = 5 {5, ... Read More

Sum of XOR of all pairs in an array in C++

sudhir sharma
Updated on 17-Aug-2020 09:58:43

2K+ Views

In this problem, we are given an array arr[] of n integers. Our task is to create a program to find the sum of XOR of all pairs in an array.Let’s take an example to understand the problem, Input: arr[] = {5, 1, 4} Output: 10 Explanation: the sum of all pairs: 5 ^ 1 = 4 1 ^ 4 = 5 5 ^ 4 = 1 sum = 4 + 5 + 1 = 10One simple approach to solve this problem is to run nested loops and find all pairs of numbers. Find XOR of each pair and add ... Read More

File Objects in Java

AmitDiwan
Updated on 17-Aug-2020 09:48:42

1K+ Views

The File object represents the actual file/directory on the disk. Here are the list of constructors to create File Object in Java −Sr.No.Method & Description1File(File parent, String child)This constructor creates a new File instance from a parent abstract pathname and a child pathname string.2File(String pathname)This constructor creates a new File instance by converting the given pathname string into an abstract pathname.3File(String parent, String child)This constructor creates a new File instance from a parent pathname string and a child pathname string.4File(URI uri)This constructor creates a new File instance by converting the given file: URI into an abstract pathname.Assuming an object is ... Read More

Precision Handling in Java

AmitDiwan
Updated on 17-Aug-2020 09:46:45

573 Views

Let us see how precisions are handled in Java −Example Live Demoimport java.io.*; import java.lang.*; public class Demo{    public static void main(String[] args){       double my_val = 34.909;       System.out.println("The formatted value of 34.909 is ");       System.out.println(String.format("%.7f", my_val));       double my_val_2 = 12.56;       System.out.println("The formatted value of 12.56 is ");       System.out.println(String.format("%.9f", my_val_2));    } }OutputThe formatted value of 34.909 is 34.9090000 The formatted value of 12.56 is 12.560000000A class named Demo contains the main function, where a double valued integer is declared, and it is ... Read More

Logical Operators on String in Java

AmitDiwan
Updated on 17-Aug-2020 09:45:22

668 Views

Let us implement logical operators on String in Java −Example Live Demoimport java.io.*; public class Demo{    public static void main(String[] args){       int a = 45, b = 32, c = 87, d = 1;       System.out.println("The first variable is " + a);       System.out.println("The second variable is = " + b);       System.out.println("The third variable is = " + c);       if ((a > b) && (b == c)){          d = a + b + c;          System.out.println("The sum is " + ... Read More

Sum of upper triangle and lower triangle in C++

sudhir sharma
Updated on 17-Aug-2020 09:49:49

1K+ Views

In this problem, we are given a matrix. Our task is to create a program to print the sum of upper triangle and lower triangle.Lower triangleM00                     0             0       …        0 M10                     M11               0       …        0 M20                     M21               M22      …     ... Read More

Complex Numbers in Java

AmitDiwan
Updated on 17-Aug-2020 09:43:26

7K+ Views

Complex numbers are those that have an imaginary part and a real part associated with it. They can be added and subtracted like regular numbers. The real parts and imaginary parts are respectively added or subtracted or even multiplied and divided.Example Live Demopublic class Demo{    double my_real;    double my_imag;    public Demo(double my_real, double my_imag){       this.my_real = my_real;       this.my_imag = my_imag;    }    public static void main(String[] args){       Demo n1 = new Demo(76.8, 24.0),       n2 = new Demo(65.9, 11.23),       temp;       ... Read More

Advertisements