Find Angles of a Quadrilateral in C++

Ayush Gupta
Updated on 16-Sep-2020 17:31:28

269 Views

In this problem, we are given a value d, which is the common difference of AP. This AP is all the angles of a quadrilateral. Our task is to create a program to find the angles of a quadrilateral in C++.Problem Description − Here, the angles of the quadrilateral are in the form of an AP with common difference d. And we need to find the angles.Let’s take an example to understand the problemInputd = 15Output67.5, 82.5, 97.5, 112.5ExplanationFirst angle is x Second angle is x + 15 Third angle is x + 30 Four angle is x + 45Sum ... Read More

Find Area and Perimeter of a Semicircle in C++

Ayush Gupta
Updated on 16-Sep-2020 17:30:28

335 Views

In this problem, we are given a value that denotes the radius of a semicircle. Our task is to create a program to find the Area and Perimeter of a Semicircle in C++.SemiCircle is a closed figure that is half of a circle.Let’s take an example to understand the problem, InputR = 5Outputarea = 39.25 perimeter = 15.7Solution ApproachTo solve the problem, we will use the mathematical formula for the area and perimeter of a semi-circle which is derived by dividing the area of circle by 2.Area of semicircle, A= $½(\prod^*a^2)=1.571^*a^2$Perimeter of semicircle, P =(π*a)Area of semicircle, area = $½(π^*a^2)$Program ... Read More

Find the Primorial of Numbers in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:41:24

338 Views

The primorial of a number n is equal to the product of first n prime numbers.For example, if n = 4Then, the output primorial(n) is, 2*3*5*7 = 210We are required to write a JavaScript function that takes in a number and returns its primordial.ExampleFollowing is the code −const num = 4; const isPrime = n => {    if (n===1){       return false;    }else if(n === 2){       return true;    }else{       for(let x = 2; x < n; x++){          if(n % x === 0){       ... Read More

Count the Number of Data Types in an Array in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:39:44

234 Views

We are required to write a JavaScript function that takes in an array that contains elements of different data types and the function should return a map representing the frequency of each data type.Let’s say the following is our array −const arr = [23, 'df', undefined, null, 12, {    name: 'Rajesh' }, [2, 4, 7], 'dfd', null, Symbol('*'), 8];ExampleFollowing is the code −const arr = [23, 'df', undefined, null, 12, {    name: 'Rajesh'},    [2, 4, 7], 'dfd', null, Symbol('*'), 8]; const countDataTypes = arr => {    return arr.reduce((acc, val) => {       const dataType ... Read More

Check If Two Arrays Can Form a Sequence in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:36:02

214 Views

We are required to write a JavaScript function that takes in two arrays of numbers.And the function should return true if the two arrays upon combining and shuffling can form a consecutive sequence, false otherwise.For example − If the arrays are −const arr1 = [4, 6, 2, 9, 3]; const arr2 = [1, 5, 8, 7];Then the output should be true.ExampleFollowing is the code −const arr2 = [1, 5, 8, 7]; const canFormSequence = (arr1, arr2) => {    const combined = [...arr1, ...arr2];    if(combined.length < 2){       return true;    };    combined.sort((a, b) => a-b); ... Read More

Finding Second Smallest Word in a String using JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:33:56

317 Views

We are required to write a JavaScript function that takes in a string sentence as first and the only argument. The function should return the length of the second smallest word from the string.For example: If the string is −const str = 'This is a sample string';Then the output should be 2.ExampleFollowing is the code −const str = 'This is a sample string'; const secondSmallest = str => {    const strArr = str.split(' ');    if(strArr.length < 2){       return false;    }    for(let i = 0; i < strArr.length; i++){       strArr[i] = ... Read More

Expressing Numbers in Expanded Form using JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:32:51

841 Views

Suppose we are given a number 124 and are required to write a function that takes this number as input and returns its expanded form as a string.The expanded form of 124 is −'100+20+4'ExampleFollowing is the code −const num = 125; const expandedForm = num => {    const numStr = String(num);    let res = '';    for(let i = 0; i < numStr.length; i++){       const placeValue = +(numStr[i]) * Math.pow(10, (numStr.length - 1 - i));       if(numStr.length - i > 1){          res += `${placeValue}+`       }else{          res += placeValue;       };    };    return res; }; console.log(expandedForm(num));OutputFollowing is the output in the console −100+20+5

Replace Every Nth Instance of Characters in a String - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:31:16

983 Views

We are required to write a JavaScript function that takes in a string as the first argument, a number, say n, as the second argument and a character, say c, as the third argument. The function should replace the nth appearance of any character with the character provided as the third argument and return the new string.ExampleFollowing is the code −const str = 'This is a sample string'; const num = 2; const char = '*'; const replaceNthAppearance = (str, num, char) => {    const creds = str.split('').reduce((acc, val, ind, arr) => {       let { res, ... Read More

Find Element Larger Than All Elements on Right in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:28:58

180 Views

We are required to write a JavaScript function that takes in an array of numbers and returns a subarray that contains all the element from the original array that are larger than all the elements on their right.ExampleFollowing is the code −const arr = [12, 45, 6, 4, 23, 23, 21, 1]; const largerThanRight = (arr = []) => {    const creds = arr.reduceRight((acc, val) => {       let { largest, res } = acc;       if(val > largest){          res.push(val);          largest = val;       };       return { largest, res };    }, {       largest: -Infinity,       res: []    });    return creds.res; }; console.log(largerThanRight(arr));OutputFollowing is the output in the console −[ 1, 21, 23, 45 ]

Check for Valid Hex Code in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:27:29

712 Views

A string can be considered as a valid hex code if it contains no characters other than the 0-9 and a-f alphabetsFor example −'3423ad' is a valid hex code '4234es' is an invalid hex codeWe are required to write a JavaScript function that takes in a string and checks whether its a valid hex code or not.ExampleFollowing is the code −const str1 = '4234es'; const str2 = '3423ad'; const isHexValid = str => {    const legend = '0123456789abcdef';    for(let i = 0; i < str.length; i++){       if(legend.includes(str[i])){          continue;       ... Read More

Advertisements