Posts

⓲C Program to Print Right Star Triangle

  Program: Right Star Triangle #include <stdio.h> int main () { int i , j ; // Outer loop controls the number of rows for ( i = 1 ; i <= 5 ; i ++ ) { // Inner loop prints stars in each row for ( j = 1 ; j <= i ; j ++ ) { printf ( "* " ); } // Move to next line after each row printf ( "\n" ); } return 0 ; } Output * * * * * * * * * * * * * * * Explanation 1️⃣ Header File #include <stdio.h> This line allows us to use printf() for printing output. 2️⃣ Main Function int main () The program execution starts from the main() function . 3️⃣ Variable Declaration int i , j ; i → controls rows j → controls number of stars in each row 4️⃣ Outer Loop (Rows) for ( i = 1 ; i <= 5 ; i ++ ) This loop decides how many rows will print . i value Row 1 Row 1 2 Row 2 3 Row 3 4 Row 4 5 Row 5 5️⃣ Inner Loop (Stars) for ( j = 1 ; j ...

⓱C Program to Print a Square Pattern

C Program to Print a Square Pattern #include <stdio.h> int main () { int i , j , n ; printf ( "Enter the size of square: " ); scanf ( "%d" , & n ); for ( i = 1 ; i <= n ; i ++ ) // Outer loop for rows { for ( j = 1 ; j <= n ; j ++ ) // Inner loop for columns { printf ( "* " ); } printf ( "\n" ); // Move to next line after one row } return 0 ; } Explanation of Loops 1️⃣ Outer Loop (Row Loop) for ( i = 1 ; i <= n ; i ++ ) Purpose: Controls the number of rows Example if n = 5 Iteration Row Printed i = 1 First row i = 2 Second row i = 3 Third row i = 4 Fourth row i = 5 Fifth row So the outer loop runs n times . 2️⃣ Inner Loop (Column Loop) for ( j = 1 ; j <= n ; j ++ ) Purpose: Prints stars in each row For every row, the inner loop prints n stars. Example for first row: j value Output j = 1 * j = 2 * j = 3 * j = 4...

⑯Find Largest of Three Numbers (with complete explanation and output examples)

  ✅ Find Largest of Three Numbers ๐Ÿ”น Problem Statement Given three numbers, find which one is the largest. ๐Ÿง  Method 1: Using Simple if-else Conditions (Best Method) ๐Ÿ”ธ Logic Explanation Suppose the numbers are: a b c Steps: Compare a with both b and c . If a is greater than or equal to both → a is largest. Otherwise compare b with a and c . If b is greater than or equal to both → b is largest. Else → c is largest. ๐Ÿ’ป C Program #include <stdio.h> int main () { int a , b , c ; printf ( "Enter three numbers: " ); scanf ( "%d %d %d" , & a , & b , & c ); if ( a >= b && a >= c ) { printf ( "Largest number is: %d" , a ); } else if ( b >= a && b >= c ) { printf ( "Largest number is: %d" , b ); } else { printf ( "Largest number is: %d" , c ); } return 0 ; } ๐Ÿ” Dry Ru...

⓯ Program to Find the Area of a Circle in C (with complete explanation)

๐ŸŸข Program to Find the Area of a Circle in C ๐Ÿ“Œ Problem Statement Write a C program to calculate the area of a circle when the radius is given by the user. ๐Ÿ“˜ Theory The area of a circle is calculated using the formula: Area = ฯ€ × r × r Where: ฯ€ (pi) ≈ 3.14159 r = radius of the circle Since the result may be a decimal value, we use the float data type. ๐Ÿง  Algorithm Start Declare variables radius and area Read the value of radius from the user Calculate area using the formula Display the result End ๐Ÿ’ป C Program # include <stdio.h> int main () { float radius, area; const float PI = 3.14159 ; // Taking input from user printf ( "Enter the radius of the circle: " ); scanf ( "%f" , &radius); // Calculating area area = PI * radius * radius; // Displaying result printf ( "Area of the circle = %.2f" , area); return 0 ; } ๐Ÿ” Explanation of the Code #...

⓮C Program to calculate Simple Interest (With Program and Explanation)

๐Ÿ“Œ Program to calculate Simple Interest   Simple Interest Formula :- Simple Interest (SI) = P × R × T 100​ Where: P = Principal amount R = Rate of interest (per year) T = Time (in years) ๐Ÿงพ C Program to Calculate Simple Interest # include <stdio.h> int main () { float principal, rate, time, simpleInterest; // Taking input from the user printf ( "Enter Principal Amount: " ); scanf ( "%f" , &principal); printf ( "Enter Rate of Interest: " ); scanf ( "%f" , &rate); printf ( "Enter Time (in years): " ); scanf ( "%f" , &time); // Calculating Simple Interest simpleInterest = (principal * rate * time) / 100 ; // Displaying the result printf ( "Simple Interest = %.2f" , simpleInterest); return 0 ; } ๐Ÿง  Explanation of the Program 1️⃣ Header File # include <stdio.h> This header file is used for input and outpu...

⓭C Program to Swap Two Numbers Using a Temporary Variable (With Program and Explanation)

  ๐Ÿ”„ Swap Two Numbers Using a Temporary Variable (C Program) ๐Ÿ“Œ Problem Statement Swapping means exchanging the values of two variables . In this program, we use a temporary variable to store one value during the swap process. ๐Ÿง  Logic / Working Let the two numbers be a and b . Store the value of a in a temporary variable temp Assign the value of b to a Assign the value stored in temp to b This ensures the values are exchanged safely. ๐Ÿ’ป C Program: Swap Two Numbers (Using Temp Variable) # include <stdio.h> int main () { int a, b, temp; // Input two numbers printf ( "Enter first number: " ); scanf ( "%d" , &a); printf ( "Enter second number: " ); scanf ( "%d" , &b); // Swapping using temporary variable temp = a; a = b; b = temp; // Output after swapping printf ( "\nAfter Swapping:\n" ); printf ( "First number = %d\n" , a); ...

⑫ C Program to Swap Two Numbers Without Using a Temporary Variable (With Program and Explanation)

๐Ÿ” C Program to Swap Two Numbers Without Using a Temporary Variable Swapping two numbers means exchanging their values. Usually, we use a temporary variable , but in this program, we’ll swap the numbers without using any extra variable . ๐Ÿ’ก Logic Used We use addition and subtraction to swap the values. Steps: Add both numbers and store the result in the first number. Subtract the second number from the first to get the original first number. Subtract the new first number from the second to get the original second number. ๐Ÿง  Algorithm Start Read two numbers a and b a = a + b b = a - b a = a - b Print swapped values End ๐Ÿ’ป C Program Code # include <stdio.h> int main () { int a, b; // Input from user printf ( "Enter first number: " ); scanf ( "%d" , &a); printf ( " Enter second number: " ); scanf ( "%d" , &b); // Swapping without using temporary variab...