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 7 of 96
C program to compare if the two matrices are equal or not
In C programming, comparing two matrices for equality involves checking both their dimensions and corresponding elements. Two matrices are considered equal if they have the same dimensions (rows and columns) and all corresponding elements are identical. Syntax // Matrix comparison logic if (rows1 == rows2 && cols1 == cols2) { // Compare each element for (int i = 0; i < rows1; i++) { for (int j = 0; j < cols1; j++) { ...
Read MoreC program to sort all columns and rows of matrix
In C programming, sorting matrix rows and columns involves rearranging elements within each row in ascending order and elements within each column in descending order. This operation requires nested loops to traverse and compare matrix elements. Syntax /* Row sorting (ascending) */ for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { for (k = j + 1; k < cols; k++) { if (matrix[i][j] ...
Read MoreC program to interchange the diagonal elements in given matrix
In C programming, interchanging the diagonal elements of a matrix involves swapping the main diagonal elements with the secondary (anti) diagonal elements. This operation is only possible with square matrices where the number of rows equals the number of columns. Syntax /* Logic to interchange diagonal elements */ for (i = 0; i < n; i++) { temp = matrix[i][i]; /* Store main diagonal element */ matrix[i][i] = matrix[i][n-i-1]; /* Assign ...
Read MoreC program to perform operations on two halves' in a single array
In C programming, we can perform different operations on two halves of a single array. This approach is useful when you need to apply distinct sorting criteria to different portions of your data. Let's explore how to split an array into two halves and sort the first half in ascending order while sorting the second half in descending order. Syntax // Splitting array into two halves int mid = n / 2; // where n is array size // Bubble sort for ascending order (first half) for (i = 0; i < mid; i++) { ...
Read MoreC program to generate the value of x power n using recursive function
In C programming, calculating xn (x raised to the power n) can be efficiently done using recursive functions. This approach breaks down the problem into smaller subproblems, making the solution elegant and mathematically intuitive. Syntax long int power(int x, int n); Algorithm The recursive algorithm uses the following mathematical properties − If n = 1, then xn = x (base case) If n is even, then xn = (xn/2)2 If n is odd, then xn = x × xn-1 Example: Recursive Power Function Here's a complete C program that ...
Read MoreWhat are different variations of for loop iterations?
The for loop in C offers several variations beyond its standard form, making it a versatile control structure. Each variation serves specific programming needs, from multiple variable initialization to infinite loops. Syntax for (initialization; condition; increment/decrement) { // statements } The basic components are − Initialization − Sets the initial value of loop control variable(s) Condition − Tests whether to continue the loop execution Operation − Updates the loop variable after each iteration Variation 1: Multiple Variable Initialization The comma operator allows initializing multiple variables in ...
Read MoreWhat is exit() function in C language?
The exit() function in C is used to terminate a program immediately. This function causes the program to stop execution and return control to the operating system. Unlike a normal return from main(), exit() can be called from anywhere in the program. Syntax void exit(int status); Parameters status − An integer value returned to the operating system. Conventionally, 0 indicates successful termination, and non-zero values indicate errors. Return Value The exit() function does not return any value as it terminates the program immediately. Note: To use the exit() ...
Read MoreC program to print four powers of numbers 1 to 9 using nested for loop
In C programming, nested loops are powerful constructs where one loop is placed inside another loop. This technique is particularly useful for creating tables, matrices, and complex patterns. Here we'll use nested for loops to generate a table showing the first four powers of numbers 1 to 9. Syntax for (initialization; condition; increment) { for (initialization; condition; increment) { // Inner loop statements } // Outer loop statements } How Nested Loops Work In ...
Read MoreWhat are reading and writing characters in C language?
In C programming language, reading and writing characters are fundamental I/O operations that allow programs to interact with users through the console. Syntax int getchar(void); int putchar(int c); Character Input/Output Functions C provides several functions for character I/O operations − getchar() − Reads a character from standard input (keyboard) putchar() − Writes a character to standard output (screen) getche() − Reads a character with echo (non-standard, conio.h) getch() − Reads a character without echo (non-standard, conio.h) Example: Basic Character Reading and Writing This example demonstrates reading characters and converting ...
Read MoreWhat are printf conversion characters and their types?
In C programming, printf() conversion characters (also called format specifiers) are special codes that tell printf() how to format and display different data types. These characters are preceded by a % symbol and specify the type and format of the variable to be printed. Syntax printf("format string with %specifier", variable); Common Printf Conversion Characters Here is a list of the most commonly used conversion characters in printf() − %d − signed decimal integer %u − unsigned decimal integer %x − hexadecimal integer (lowercase) %X − hexadecimal integer (uppercase) %o − octal integer ...
Read More