Programming Articles

Page 31 of 2546

Legal and illegal declaration and initializations in C

Bhanu Priya
Bhanu Priya
Updated on 15-Mar-2026 2K+ Views

In C programming, variable declaration and initialization must follow specific rules. Understanding legal and illegal practices helps write error-free code and avoid compilation issues. Syntax // Variable Declaration datatype variable1, variable2, ..., variableN; // Variable Initialization datatype variable = value; Variable Declaration Variables can be declared in two scopes − Global declaration: Variables declared outside any function, accessible throughout the program. Local declaration: Variables declared inside a function, accessible only within that function. #include int globalVar; /* global declaration */ int main() { ...

Read More

How to calculate sum of random numbers in between 0 to 100 using files in C Programming?

Bhanu Priya
Bhanu Priya
Updated on 15-Mar-2026 2K+ Views

In this program, we generate random numbers between 0 and 100, calculate their sum, and store the result in a file. The sum will be different on each execution because random numbers are generated based on the current time. Syntax FILE *fptr = fopen("filename.txt", "w"); fprintf(fptr, "format_string", value); fclose(fptr); Algorithm The steps to calculate the sum of random numbers and store in a file are − Generate 100 random numbers between 1 and 100 Calculate the sum of all generated numbers Open a file in write mode Write the sum to the ...

Read More

Write a C program to maintain cricketer's information in tabular form using structures

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

In C programming, we can use structures to organize and store cricketer information such as name, age, number of matches, and average runs. This program demonstrates how to input multiple cricketer records and display them in a sorted tabular format based on their average runs. Syntax struct structure_name { data_type member1; data_type member2; // ... more members }; Approach We will create a structure to hold cricketer information, input data for multiple cricketers, sort them based on average runs using bubble sort, ...

Read More

Write a C program to calculate the average word length of a sentence using while loop

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

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 More

Write a C program to print numbers in words using elseif statements

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

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 More

Write a C program to print the message in reverse order using for loop in strings

Mandalika
Mandalika
Updated on 15-Mar-2026 858 Views

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 More

How to print a one-month calendar of user choice using for loop in C?

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

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 More

Generate an even squares between 1 to n using for loop in C Programming

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

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 More

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
Showing 301–310 of 25,452 articles
« Prev 1 29 30 31 32 33 2546 Next »
Advertisements