Find Star Number in C++

Ayush Gupta
Updated on 17-Sep-2020 04:47:24

160 Views

In this problem, we are given a number n. Our task is to create a program to find Star number in C++.Star Number is a special number that represents a centered hexagram (sixpoint star).Some start numbers are 1, 13, 37, 73, 121.Let’s take an example to understand the problemInputn = 5Output121Solution ApproachTo find the nth star number we will use the formula.Let’s see the general formula for the star number.n = 2 -> 13 = 12 + 1 = 6(2) + 1 n = 3 -> 37 = 36 + 1 = 6(6) + 1 n = 4 -> 73 ... Read More

Sum of 1 + x^2 + x^3 + ... + x^n in C++

Ayush Gupta
Updated on 17-Sep-2020 04:45:55

302 Views

In this problem, we are given two values x and n that corresponds to the given series. Our task is to create a program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++.Problem description − we need to find the sum of series based on the given values of x and n. In the series, every other term differs from the previous term by x/i for ith term.Let’s take an example to understand the problemInputx = 6, n = 4Output29.8ExplanationThe sum of the series is1 + 6/2 + 36/6 + 216/24 + 1296/120 = 29.8Solution ApproachTo find ... Read More

Find Sum of a Series in C++

Ayush Gupta
Updated on 16-Sep-2020 17:48:57

610 Views

In this problem, we are given two numbers a and n. Our task is to create a program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n! in C++.Problem description − The problem is to find the sum of the given series using the given values of a and n. The series is a special series in which each term is the multiple of the last term with a/i, i -> 1 to n.Let’s take an example to understand the problemInputa = 3, n = 4Output15.375Explanationsum of series is(3^1)/1! + (3^2)/2! + (3^3)/3! + ... Read More

Find Sum of Elements in a Given Array in C++

Ayush Gupta
Updated on 16-Sep-2020 17:47:42

1K+ Views

In this problem, we are given an array arr[] of n integer values. Our task is to create a Program to find sum of elements in a given array in C++.Program Description − For the given array, we will add up all elements and return the sum.Let’s take an example to understand the problemInputarr[] = {3, 1, 7, 2, 9, 10}Output32ExplanationSum = 3 + 1 + 7 + 2 + 9 + 10 = 32Solution ApproachTo find the sum of elements of the array, we will traverse the array and extract each element of the array and add them to ... Read More

Find Sum of Harmonic Series in C++

Ayush Gupta
Updated on 16-Sep-2020 17:42:44

970 Views

In this problem, we are given three numbers a, d, and n. Our task is to create a program to find sum of harmonic series in C++.Harmonic progression is a series whose inverse will be an arithmetic progression. I.e. if for a harmonic progression A1, A2, A3.. An, there is an arithmetic progression 1/A1, 1/A2, 1/A3.So, a general HP is1/a, 1/(a+d), 1/(a+2d), … 1/(a + nd)Where 1/a is the first term. And d is the common difference of the reversed AP.Problem Description − Here, we will be given the first term a, common difference d, and the number of terms ... Read More

Find Sum of Prime Numbers Between 1 to N in C++

Ayush Gupta
Updated on 16-Sep-2020 17:41:06

4K+ Views

In this problem, we are given a number n. Our task is to create a program to find sum of prime numbers between 1 to n in C++.Prime Numbers are those numbers that have only two factors. They are the number and 1.Let’s take an example to understand the problemInputn = 15Output41ExplanationPrime numbers between 1 to 15 are 2, 3, 5, 7, 11, 13. The sum is 41.Solution ApproachA simple way to solve the problem is by using a loop and checking if each number is a prime number or not and add all those which are prime.To check if ... Read More

Find Sum of Series 1 + 1/2 + 1/3 + 1/4 + ... + 1/n in C++

Ayush Gupta
Updated on 16-Sep-2020 17:39:13

4K+ Views

In this problem, we are given a number n. Our task is to create a program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n in C++.Code description − Here, we will find the sum of the series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n till nth term. The series is a harmonic progression series.Harmonic progression is a series whose inverse will be an arithmetic progression. I.e. if for a harmonic progression A1, A2, A3... An, there is an arithmetic progression 1/A1, 1/A2, 1/A3.First, let’s take an example to ... Read More

Find Angles of a Quadrilateral in C++

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

263 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

318 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

330 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

Advertisements