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
Programming Articles
Page 935 of 2547
Write a C program to calculate the average word length of a sentence using while loop
In C programming, calculating the average word length of a sentence involves counting the total characters (excluding spaces) and dividing by the number of words. This program demonstrates how to use a while loop to process user input character by character and calculate the average word length. Syntax while(condition) { // Process each character // Count characters and words } Algorithm Read the sentence character by character using a while loop Count total characters (excluding spaces) and words Calculate average by dividing character count by ...
Read MoreWrite a C program to print numbers in words using elseif statements
In C programming, we can convert numbers into their word equivalents using else-if statements instead of switch cases. This approach is particularly useful for handling two-digit numbers by breaking them into tens and units places. Syntax if (condition1) { // statements } else if (condition2) { // statements } else if (condition3) { // statements } else { // default statements } Algorithm The program uses multiple else-if conditions to handle different number ranges − Range ...
Read MoreWrite a C program to print the message in reverse order using for loop in strings
Here we write a program to reverse the sentence without predefined functions. By using for loop, we can easily print statement in reverse order. Syntax for(initialization; condition; increment/decrement) { // Read characters } for(reverse_initialization; reverse_condition; reverse_increment) { // Print characters in reverse } Method 1: Character-by-Character Input Using getchar() This approach reads each character individually using getchar() and stores them in an array. Then it prints the characters in reverse order − #include int main() { char stmt[100]; ...
Read MoreHow to print a one-month calendar of user choice using for loop in C?
A one-month calendar can be printed in C using for loops by positioning the first day correctly and then printing all days in a week-by-week format. The key is to add proper spacing before the first day and break lines after every 7 days. Syntax for(i = 1; i < firstDay; i++) printf(" "); // Add spaces before first day for(i = 1; i
Read MoreGenerate an even squares between 1 to n using for loop in C Programming
Even square numbers are the squares of even integers like 22, 42, 62, 82, which result in 4, 16, 36, 64, 100, and so on. In this tutorial, we will generate even squares between 1 to n using a for loop in C programming. Syntax for (initialization; condition; increment) { // Loop body } Algorithm START Step 1: Declare variables a and n Step 2: Read number n from user Step 3: Use for loop to generate even squares For a = 2; a*a
Read MoreSwapping 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 More