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 Arnab Chakraborty
Page 173 of 377
C program to convert digit to words
In C programming, converting a digit to its corresponding word representation is a common task. This involves taking a single digit (0-9) and converting it to its English word equivalent like "Zero", "One", "Two", etc. Syntax void digitToWord(int digit); Method 1: Using If-Else Statements This approach uses a series of if-else statements to check each digit and print the corresponding word − #include void digitToWord(int d) { if (d < 0 || d > 9) { printf("Beyond range ...
Read MoreC program to find sum and difference using pointers in function
In C programming, functions can return only one value at a time. However, when we need to calculate multiple results like sum and difference of two numbers, we can use pointers as function parameters. This technique allows us to modify the original variables directly through their memory addresses. Syntax void functionName(int *ptr1, int *ptr2); // Call the function functionName(&variable1, &variable2); Algorithm To solve this problem, we will follow these steps − Define a function that takes addresses of two variables as parameters Store the sum in a temporary variable Calculate the difference ...
Read MoreC program to find maximum of four integers by defining function
In C programming, we can find the maximum of four integers by defining our own function. We create a max() function that compares two numbers and returns the larger one, then use it repeatedly to find the maximum among all four numbers. So, if the input is like a = 5, b = 8, c = 2, d = 3, then the output will be 8. Syntax int max(int x, int y); Algorithm To solve this, we will follow these steps − Define a function max() that takes two integers x ...
Read MoreC program to find sum and difference of two numbers
In C programming, finding the sum and difference of two numbers is a fundamental operation. This program demonstrates how to perform arithmetic operations on different data types and handle the output formatting correctly based on the operands' types. Syntax // For integer arithmetic int result = num1 + num2; int result = num1 - num2; // For floating-point arithmetic float result = num1 + num2; float result = num1 - num2; Example 1: Sum and Difference of Integer Numbers This example shows basic arithmetic operations on integer values − ...
Read MoreC Program to read and write character string and sentence
In C programming, reading different types of input requires different functions. Reading a single character uses scanf() with %c, strings without spaces use scanf() with %s, and sentences with spaces require fgets() for proper handling. Syntax // Character input scanf("%c", &character); // String input (no spaces) scanf("%s", string); // Sentence input (with spaces) fgets(sentence, size, stdin); Example This program demonstrates reading a character, string, and sentence from user input − #include int main() { char character; char string[500]; ...
Read MoreFind a permutation that causes worst case of Merge Sort in C
Merge sort has a consistent O(n log n) time complexity, but in practice, certain input permutations require more comparisons during the merge operations. The worst case occurs when the merge operation at each level compares every element from both subarrays before completing. Syntax void generateWorstCase(int arr[], int left, int right); Understanding the Worst Case The worst case for merge sort happens when at each merge operation, elements from left and right subarrays are alternately selected. This forces maximum comparisons since we cannot skip any elements during merging. Algorithm To generate the worst ...
Read Morefillpoly() function in C
The fillpoly() function in C is part of the graphics.h library and is used to draw and fill a polygon with the current fill pattern and color. It takes the same arguments as drawpoly() but additionally fills the interior of the polygon. Syntax void fillpoly(int numpoints, int *polypoints); Parameters numpoints − Specifies the number of points in the polygon polypoints − Pointer to an array containing x, y coordinates of each point The polypoints array should contain numpoints * 2 integers, where each pair represents the x and y coordinates of ...
Read MoreFlood fill algorithm using C graphics
The flood fill algorithm is a technique used to fill a connected region with a specific color. It starts from a seed pixel and recursively fills all adjacent pixels that have the same color as the original pixel. Syntax void floodFill(int x, int y, int newColor, int oldColor); Note: This example requires Turbo C/C++ or a compatible graphics library with graphics.h support. Modern compilers may need additional setup for graphics functions. Algorithm Steps The flood fill algorithm follows these steps − Check if the current pixel coordinates are within ...
Read MoreFind the Longest Substring Containing Vowels in Even Counts in C++
The problem of finding the longest substring with vowels appearing an even number of times can be solved efficiently using bit manipulation and prefix sums. We need to track the parity (odd/even count) of each vowel ('a', 'e', 'i', 'o', 'u') as we traverse the string. Syntax int findLongestSubstring(char* s); Algorithm Steps The approach uses a bitmask to represent the parity state of vowels − Use a 5-bit mask where each bit represents parity of vowels a, e, i, o, u Store the first occurrence of each mask state When a mask ...
Read MoreInteger to Roman in C
Given a decimal number n, we have to convert this into Roman numeral. The value n lies in the range 1 to 4000. Roman numerals use specific symbols and follow subtraction rules for certain combinations. Number Numeral 1 I 4 IV 5 V 9 IX 10 X 40 XL 50 L 90 XC 100 C 400 CD 500 D 900 CM 1000 M 4000 MMMM ...
Read More