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
C Articles
Page 18 of 96
Swapping numbers using bitwise operator in C
Swapping two numbers using bitwise operators in C is an efficient technique that uses the XOR (^) operator to exchange values without requiring additional memory for a temporary variable. This method works by exploiting the property that XORing a number with itself results in zero. Syntax a = a ^ b; b = a ^ b; a = a ^ b; How XOR Swapping Works The XOR swap algorithm works on the mathematical property that X ^ X = 0 and X ^ 0 = X. When we perform the three XOR operations, the ...
Read MoreWrite a C program to reduce any fraction to least terms using while loop
Reducing a fraction to its lowest terms means finding the simplest form where the numerator and denominator have no common factors other than 1. This is achieved by dividing both numbers by their Greatest Common Divisor (GCD). Syntax while (condition) { // Find GCD and reduce fraction } Method 1: Using Euclidean Algorithm This method uses the Euclidean algorithm to find the GCD efficiently − #include int main() { int numerator, denominator, original_num, original_den; int temp, gcd; ...
Read MoreConverting digits to word format using switch case in C language
Converting digits to word format is a common programming problem that helps understand switch-case statements and number manipulation. In C, we can convert one or two-digit numbers into their English word equivalents using switch statements. Syntax switch(expression) { case value1: // code block break; case value2: // code block break; default: ...
Read MoreWrite a C program to find out the largest and smallest number in a series
In C programming, finding the largest and smallest numbers in a series is a fundamental problem that can be solved using conditional statements or arrays. This involves comparing each number with the current maximum and minimum values to determine the extremes. Syntax if (number > max) max = number; if (number < min) min = number; Method 1: Using Four Variables This approach reads four numbers from the user and compares them to find the largest and smallest − #include int main() ...
Read MoreWrite a C program for time conversions using if and elseif statements
In this tutorial, we will learn how to convert time from 24-hour format (also known as military time) to 12-hour format using C programming with if and else if statements. The 24-hour format uses hours from 00 to 23, while the 12-hour format uses hours from 1 to 12 with AM/PM indicators. Syntax if (condition1) { // statements } else if (condition2) { // statements } else { // statements } Algorithm Start: Step 1: Enter time in 24 hr format ...
Read MoreHow to print the stars in Diamond pattern using C language?
In C programming, printing a diamond pattern with stars is a common pattern printing problem that helps understand nested loops and spacing logic. A diamond pattern consists of an upper triangular half followed by a lower triangular half. Syntax // Upper half pattern for (j = 1; j
Read MoreHow to calculate transpose of a matrix using C program?
The transpose of a matrix is obtained by converting rows to columns and columns to rows. If A is an original matrix and B is its transpose, then element at position A[i][j] becomes B[j][i]. Syntax for (i = 0; i < rows; i++) for (j = 0; j < cols; j++) transpose[j][i] = matrix[i][j]; Example 1: Dynamic Input Matrix This example reads matrix dimensions and elements from user input − #include int main() { ...
Read MoreWrite a program to add two complex numbers using C
In C programming, complex numbers are mathematical entities that consist of real and imaginary parts. A complex number is represented in the form a + bi, where 'a' is the real part and 'b' is the imaginary part multiplied by 'i' (the square root of -1). Syntax struct complexNumber { int real; int imaginary; }; Algorithm To add two complex numbers, we add their real parts separately and imaginary parts separately − Real part: result_real = num1_real + num2_real Imaginary part: result_imaginary = num1_imaginary ...
Read MoreHow to print integers in the form of Pascal triangle using C?
Pascal's triangle is a triangular arrangement of integers where each number is the sum of the two numbers directly above it. This mathematical structure has many applications in combinatorics, algebra, and probability theory. Syntax // Using factorial approach int factorial(int n); int binomialCoefficient = factorial(row) / (factorial(col) * factorial(row - col)); // Using iterative approach int coefficient = 1; coefficient = coefficient * (row - col) / (col + 1); How Pascal Triangle Works Pascal's triangle follows these rules − The first and last elements of each row are ...
Read MoreHow to print Floyd's triangle (of integers) using C program?
Floyd's triangle is a right-angle triangle of consecutive numbers, starting with 1 in the top left corner. Each row contains one more number than the previous row, forming a triangular pattern − 1 2 3 4 5 6 7 8 9 10 Syntax for (i = 1; i = 1; i--) { for (j = 1; j
Read More