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 4 of 81
Product of N with its largest odd digit in C
In C programming, we need to find the product of a given number N with its largest odd digit. If the number contains no odd digits, we return -1. Syntax int largestOddDigit(int n); int findProduct(int n); Algorithm Extract each digit of the number using modulo operation Check if the digit is odd and compare with the current largest odd digit Keep track of the maximum odd digit found Multiply the original number with the largest odd digit Return -1 if no odd digit exists Example 1: Number with Odd Digits ...
Read MoreProduct of maximum in first array and minimum in second in C
Given two arrays, we need to find the product of the maximum element from the first array and the minimum element from the second array. This is a common programming problem that demonstrates array traversal and basic mathematical operations. Syntax int findMaxElement(int arr[], int size); int findMinElement(int arr[], int size); int calculateProduct(int arr1[], int arr2[], int n1, int n2); Method 1: Using Sorting Approach In this approach, we sort both arrays and then multiply the last element of the first array with the first element of the second array − #include ...
Read MoreMaximum number of threads that can be created within a process in C
In this article, we will explore how to determine the maximum number of threads that can be created within a process in C. Understanding thread limits is crucial for developing efficient multi-threaded applications. A thread is a lightweight process that can be independently managed by the scheduler. Multiple threads can be associated with a single process, and they require less time for context switching compared to processes. Threads share memory with their peer threads and require fewer resources for creation and termination. Syntax int pthread_create(pthread_t *thread, const pthread_attr_t *attr, ...
Read MoreCount minimum bits to flip such that XOR of A and B equal to C in C++
We are given three binary sequences A, B and C of length N. Each sequence represents a binary number. We have to find the number of bit flips required in A and B such that XOR of A and B results in C. Syntax int flipCount(int A[], int B[], int C[], int n); First, let us understand the XOR operation truth table − X ...
Read Morembrtowc() function in C/C++ program
The mbrtowc() function is used to convert a multibyte character sequence to a wide character. This function is part of the C standard library and is defined in the header file. It provides a safe way to convert multibyte characters (like UTF-8) to wide character representation. Syntax size_t mbrtowc(wchar_t* pwc, const char* s, size_t n, mbstate_t* ps); Parameters The function accepts the following parameters − pwc − Pointer to the location where the resulting wide character will be stored s − Pointer to the multibyte character string to be converted n ...
Read Moreputwchar() function in C/C++
The putwchar() function in C is used to write a wide character to the standard output (stdout). It is the wide character equivalent of the putchar() function and is defined in the header file. Syntax wint_t putwchar(wchar_t wc); Parameters wc − The wide character to be written to stdout Return Value On success: Returns the wide character that was written On failure: Returns WEOF and sets an error indicator Example 1: Writing Single Wide Character This example demonstrates writing a single wide character to stdout ...
Read MoreSystem() Function in C/C++
The system() function is a part of the C standard library that executes system commands. It is used to pass commands that can be executed in the command processor or terminal of the operating system, and returns the command's exit status after completion. Note: To use the system() function, include header file. Syntax int system(const char *command); Parameters command − A pointer to a null-terminated string containing the command to be executed. If NULL, checks if command processor is available. Return Value Returns the exit ...
Read MoreProblem with scanf() when there is fgets()/gets()/scanf() after it in C
In C programming, a common issue occurs when using scanf() followed by fgets(), gets(), or another scanf() for character input. The problem arises because scanf() leaves a newline character in the input buffer, which interferes with subsequent input functions. Syntax scanf("format_specifier", &variable); fgets(string, size, stdin); Problem 1: scanf() Followed by fgets() When scanf() reads an integer and is followed by fgets(), the newline character left by scanf() is immediately consumed by fgets() − #include int main() { int x; char str[100]; ...
Read MoreProcess Synchronization in C/C++
Process synchronization is the technique to overcome the problem of concurrent access to shared data which can result in data inconsistency. A cooperating process is the one which can affect or be affected by other process which will lead to inconsistency in processes data therefore Process synchronization is required for consistency of data. Syntax /* General structure for process synchronization */ do { entry_section(); critical_section(); exit_section(); remainder_section(); } while(1); The Critical-Section Problem Every process has a reserved segment ...
Read MorePrinting source of a C program itself
In C programming, a self-printing program (also called a quine) is a program that prints its own source code as output. This can be achieved using the __FILE__ macro along with file I/O operations to read and display the current source file. Syntax FILE *fopen(__FILE__, "r"); The __FILE__ macro is a preprocessor macro that returns the name of the current source file as a string literal. This allows us to open and read our own source code at runtime. Example: Understanding __FILE__ Macro First, let's see how the __FILE__ macro works − ...
Read More