C++ Articles - Page 293 of 719

Count numbers upto N which are both perfect square and perfect cube in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:19:15

5K+ Views

We are given a number N. The goal is to count the numbers upto N that are perfect squares as well as perfect cubes. For example, 1, 64 are both perfect squares and perfect cubes.We will use sqrt() to calculate square root and cbrt() to calculate cube root of a number.Let’s understand with examples.Input − N=100Output − Count of numbers that are perfect squares and cubes − 2Explanation − 1 and 64 are only numbers from 1 to 100 that are both perfect squares and cubes.Input − N=5000Output −Count of numbers that are perfect squares and cubes − 3Explanation − ... Read More

Count numbers with unit digit k in given range in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:16:53

559 Views

We are given an interval [first, last]. The goal is to find the count of numbers that have a unit digit k and lie between range [first, last].We will do this by traversing from i=first to i=last. For each number i compare its unit digit with the k, if they are the same increment the count.Let’s understand with examples.Input − first=8 last=40 , k=8Output − Count of numbers with unit digit k − 4Explanation −Numbers between 8 and 40 with unit digit = 8 8, 18, 28, 38Input − first=100 last=200 , k=9Output − Count of numbers with unit digit ... Read More

Count of m digit integers that are divisible by an integer n in C++

Sunidhi Bansal
Updated on 29-Aug-2020 12:05:47

284 Views

We are given two integers m and n. The goal is to count m digit numbers that are divisible by n.If m=1, then numbers are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and n=3 then numbers divisible by 3=0, 3, 6, 9 count=4.Let’s understand with examples.Input − m=2, n=9Output − Count of m digit numbers divisible by n − 10Explanation − between 10 and 99 numbers divisible by 9 are −18, 27, 36, 45, 54, 63, 72, 81, 90, 99Input m=3, n=300Output − Count of m digit numbers divisible by n: 3Explanation − between 100 and 999 ... Read More

Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++

Sunidhi Bansal
Updated on 29-Aug-2020 12:03:46

722 Views

We are given an array of n positive numbers.The goal is to count the ordered pairs (i,j) such that arr[i]*arr[j] > arr[i]+arr[j] and 0sum.Traverse array using two for loops for each element of the pair.Outer Loop from 0

Count number of ordered pairs with Even and Odd Sums in C++

Sunidhi Bansal
Updated on 29-Aug-2020 12:01:45

377 Views

We are given an array of n positive numbers.The goal is to count the ordered pairs (arr[x], arr[y]) with the sum of arr[x] and arr[y] is even or odd. Pair ( arr[i], arr[j] ) and ( arr[j], arr[i] are counted as separate.We will traverse the array using two for loops for each number of pairs. Now calculate sum, if it is even increment count by 2 for even sums else increment count by 2 for odd sums.Let’s understand with examples.Input− Arr[]= { 1, 1, 2, 3 } N=4Output− Count of even product sums − 6 Count of odd sum pairs ... Read More

Count number of ordered pairs with Even and Odd Product in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:59:47

221 Views

We are given an array of n positive numbers.The goal is to count the ordered pairs (arr[x], arr[y]) with the product of arr[x] and arr[y] is even or odd. Pair ( arr[i], arr[j] ) and ( arr[j], arr[i] are counted as separate.We will traverse the array using two for loops for each number of pairs. Now calculate product, if it is even increment count by 2 for even products else increment count by 2 for odd products.Let’s understand with examples.Input− Arr[]= { 1, 1, 2, 3 } N=4Output − Count of even product pairs: 6 Count of odd product pairs: ... Read More

Count numbers with same first and last digits in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:57:28

490 Views

We are given an interval [first, last]. The goal is to find the count of numbers that have the same first and last digit within this interval. For example, 232 has the same first and last digit as 2.We will do this by traversing from i=first to i=last. For each number I compare its first digit with the last digit, if they are the same increment the count.Let’s understand with examples.Input − first=8 last=40Output − Count of numbers with same first and last digits − 5Explanation − Numbers between 8 and 40 with same first and last digit −8, 9, ... Read More

Count the number of objects using Static member function in C++ Program

Sunidhi Bansal
Updated on 02-Dec-2024 00:23:52

2K+ Views

The goal here is to count the number of objects of a class that are being created using a static member function. A static data member is shared by all objects of the class commonly. If no value is given, a static data member is always initialized with 0. A static member function can only use static data members of that class. We are using a class Student here. We will declare a static data member count which will store the count of objects. A static member function rollCall(void) will display the count of objects as roll no.s of students ... Read More

Count the number of operations required to reduce the given number in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:53:16

317 Views

We are given with a positive integer K and an array Ops[] which contains integers. The goal is to find the number of operations required to reduce K such that it becomes less than 0. Operations are −First operation is K + Ops[0], first element added to KAfter 1. Add Ops[i] to K until K

Count the number of pairs that have column sum greater than row sum in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:51:02

192 Views

We are given a matrix of size NXN. The goal is to find the count of all valid pairs of indexes (i, j) such that the sum elements of column j is greater than the sum of elements of row i.We will do this by traversing the matrix and calculate sums of elements of each row and column.Store sum of elements of each in rowsum[N] and sum of elements of each column in colsum[N].Now make pairs of rowsum[i] and colsum[j] and check if colsum[j]>rowsum[i]. If true increment count for such pairs.Let’s understand with examples.Input-: matrix= {    { 1, 2, ... Read More

Advertisements