Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Sunidhi Bansal
Page 8 of 81
Products of ranges in an array in C
The product of ranges in an array problem involves calculating the product of elements between given left (L) and right (R) indices under modulo P. This is useful in competitive programming to handle large products efficiently. Syntax int calculateProduct(int A[], int L, int R, int P); Parameters A[] − The input array of integers L − Left index (1-based) R − Right index (1-based) P − Prime modulo value Algorithm Step 1: Convert 1-based indices to 0-based (L-1, R-1) Step 2: Initialize result as 1 Step 3: ...
Read MoreC Program for diamond pattern with different layers
A diamond pattern in C is a symmetric arrangement of numbers that forms a diamond shape. The pattern starts with a single digit at the top, expands to show increasing sequences in the middle, and then contracts back to a single digit at the bottom. Syntax void print_pattern(int n); Approach The algorithm for creating a diamond pattern with n layers follows these steps − The pattern has (2 * n + 1) total rows First and last rows contain only "0" with proper spacing For rows 1 to n-1: numbers increase from ...
Read MoreC Program to add two fractions
Given with the input as fraction i.e. a/b and c/d where a, b, c and d can be any integer values other than 0 and the task is to add these two fraction to generate their final sum. Fractions are represented by − a / b, where a is known as numerator and b is known as denominator. a and b can have any numeric values but b can have any numeric value other than 0. Sum of two fractions is represented as a / b + c / d, and the rule for adding the two ...
Read MoreC program to calculate distance between two points
Given two points with coordinates, we need to calculate the distance between them. In a two-dimensional plane, if we have points A and B with coordinates (x1, y1) and (x2, y2) respectively, we can find the distance using the Euclidean distance formula. Syntax distance = sqrt((x2 - x1)² + (y2 - y1)²) Distance Formula The distance between two points is calculated using the formula − ...
Read MoreC program to calculate distance between three points in 3D
In C programming, calculating the distance between two points in 3D space is a common geometric problem. The distance between two points in three-dimensional space can be calculated using the Euclidean distance formula. Syntax distance = sqrt(pow(x2-x1, 2) + pow(y2-y1, 2) + pow(z2-z1, 2)); Where (x1, y1, z1) and (x2, y2, z2) are the coordinates of the two points in 3D space. Formula The mathematical formula for calculating distance between two points in 3D space is − Distance = √[(x2-x1)² + (y2-y1)² + (z2-z1)²] ...
Read MoreC program to calculate age
Calculating age is a common programming task that requires handling date arithmetic carefully. This involves computing the difference between two dates while accounting for varying month lengths and leap years. Syntax void calculateAge(int currentDay, int currentMonth, int currentYear, int birthDay, int birthMonth, int birthYear); Algorithm The age calculation follows these steps − If the current day is less than birth day, borrow days from the previous month If the current month is less ...
Read MoreC program to Calculate Body Mass Index (BMI)
Body Mass Index (BMI) is a health metric used to assess whether a person has a healthy body weight relative to their height. In C programming, we can calculate BMI using a simple formula. Syntax BMI = weight / (height * height) Where weight is in kilograms and height is in meters. Example: BMI Calculator This example demonstrates how to calculate BMI using a function − #include // Function to calculate BMI float calculateBMI(float weight, float height) { return weight / (height * height); } ...
Read MoreProgram to convert given number of days in terms of Years, Weeks and Days in C
You are given a number of days, and the task is to convert the given number of days in terms of years, weeks and days. Let us assume the number of days in a year = 365 (non-leap year). Syntax years = total_days / 365 weeks = (total_days % 365) / 7 days = (total_days % 365) % 7 Formula Explanation Number of years = (number of days) / 365 − The quotient obtained by dividing the given number of days with 365 Number of weeks = (number of days % 365) ...
Read MoreC Program for Armstrong Numbers
An Armstrong number (also known as narcissistic number) is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 1³ + 5³ + 3³ = 1 + 125 + 27 = 153. Syntax int armstrong(int number); Formula For an n-digit number with digits d₁, d₂, ..., dₙ − Armstrong Number = d₁ⁿ + d₂ⁿ + ... + dₙⁿ Example: 153 (3-digit number) 1³ + 5³ + ...
Read MoreC Program for Area And Perimeter Of Rectangle
Given a length and breadth of a rectangle we have to find its area and perimeter. Rectangle is a 2-D figure containing four sides and four angles of 90 degrees each. All the sides of rectangle are not equal − only the opposite sides of a rectangle are equal. The diagonals in a rectangle also are of the same length. Syntax Area = Length × Breadth Perimeter = 2 × (Length + Breadth) Algorithm Length Breadth ...
Read More