C Articles

Page 18 of 96

Swapping numbers using bitwise operator in C

Mandalika
Mandalika
Updated on 15-Mar-2026 22K+ Views

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 More

Write a C program to reduce any fraction to least terms using while loop

Mandalika
Mandalika
Updated on 15-Mar-2026 2K+ Views

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 More

Converting digits to word format using switch case in C language

Mandalika
Mandalika
Updated on 15-Mar-2026 7K+ Views

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 More

Write a C program to find out the largest and smallest number in a series

Mandalika
Mandalika
Updated on 15-Mar-2026 19K+ Views

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 More

Write a C program for time conversions using if and elseif statements

Mandalika
Mandalika
Updated on 15-Mar-2026 1K+ Views

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 More

How to print the stars in Diamond pattern using C language?

Mandalika
Mandalika
Updated on 15-Mar-2026 812 Views

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 More

How to calculate transpose of a matrix using C program?

Mandalika
Mandalika
Updated on 15-Mar-2026 54K+ Views

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 More

Write a program to add two complex numbers using C

Mandalika
Mandalika
Updated on 15-Mar-2026 485 Views

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 More

How to print integers in the form of Pascal triangle using C?

Mandalika
Mandalika
Updated on 15-Mar-2026 420 Views

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 More

How to print Floyd's triangle (of integers) using C program?

Mandalika
Mandalika
Updated on 15-Mar-2026 298 Views

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
Showing 171–180 of 953 articles
« Prev 1 16 17 18 19 20 96 Next »
Advertisements