Posts

Showing posts from January, 2026

⓯ 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...

⑪ C Program to Count Digits in a Number (With Program and Explanation)

  ๐Ÿ”ข C Program to Count Digits in a Number ๐Ÿ“Œ Problem Statement Write a C program to count the total number of digits in a given integer number. ๐Ÿง  Understanding the Logic To count digits in a number: Take a number as input. Repeatedly divide the number by 10 . Each division removes the last digit . Count how many times the division happens until the number becomes 0 . ๐Ÿ‘‰ Each division means one digit . ✨ Special Case If the number is 0 , the digit count is 1 . ๐Ÿงพ Algorithm (Step by Step) Start Read the number n If n == 0 , set count = 1 Else While n > 0 count++ n = n / 10 Print count End ๐Ÿ’ป C Program to Count Digits # include <stdio.h> int main () { int num, count = 0 ; printf ( "Enter a number: " ); scanf ( "%d" , &num); // Special case when number is 0 if (num == 0 ) { count = 1 ; } else { while (num != 0 ) { count++; ...

๐Ÿ”ŸC Program to Find the Largest of Three Numbers (With Program and Explanation)

  ๐Ÿ”ข C Program to Find the Largest of Three Numbers ๐Ÿ“Œ Problem Statement Write a C program that takes three numbers as input and determines which one is the largest . ๐Ÿง  Logic / Explanation Read three numbers from the user: a , b , and c . Compare: If a is greater than both b and c , then a is the largest. Else if b is greater than both a and c , then b is the largest. Otherwise, c is the largest. Print the largest number. ๐Ÿ‘‰ We use if–else if–else statements to make comparisons. ๐Ÿ’ป C Program Code # include <stdio.h> int main () { int a, b, c; // Input three numbers printf ( "Enter three numbers: " ); scanf ( "%d %d %d" , &a, &b, &c); // Compare the numbers if (a >= b && a >= c) { printf ( "Largest number is: %d" , a); } else if (b >= a && b >= c) { printf ( "Largest number is: %d" , b); } els...

9️⃣C Program to Find LCM of Two Numbers (With Program and Explanation)

  ๐Ÿ”ข C Program to Find LCM of Two Numbers ๐Ÿ“Œ What is LCM? LCM (Least Common Multiple) of two numbers is the smallest positive number that is divisible by both numbers . Example: LCM of 6 and 8 = 24 LCM of 4 and 5 = 20 ๐Ÿง  Logic Used To find LCM efficiently, we use this formula: LCM = Number1 × Number2 GCD​ So first: Find GCD (Greatest Common Divisor) Apply the formula ๐Ÿ’ป C Program to Find LCM of Two Numbers # include <stdio.h> int main () { int num1, num2, i, gcd, lcm; printf ( "Enter two numbers: " ); scanf ( "%d %d" , &num1, &num2); // Finding GCD for (i = 1 ; i <= num1 && i <= num2; i++) { if (num1 % i == 0 && num2 % i == 0 ) { gcd = i; } } // Calculating LCM lcm = (num1 * num2) / gcd; printf ( "LCM of %d and %d is %d" , num1, num2, lcm); return 0 ; } ๐Ÿ“ Explanation of the Program ๐Ÿ”น Step 1: Taking Input s...

8️⃣C Program to Find GCD of Two Numbers

  ✅ C Program to Find GCD of Two Numbers ๐Ÿ“Œ What is GCD? GCD (Greatest Common Divisor) is the largest positive number that divides two numbers without leaving a remainder . ๐Ÿ”น Example: Numbers: 24 and 36 Common divisors: 1, 2, 3, 4, 6, 12 Greatest among them = 12 ๐Ÿ‘‰ So, GCD of 24 and 36 is 12 ๐Ÿง  Method Used: Euclidean Algorithm The Euclidean Algorithm is the most efficient way to find GCD. Rule: GCD ( a , b ) = GCD ( b , a % b ) Repeat until b becomes 0. The remaining value of a is the GCD. ๐Ÿ’ป C Program to Find GCD of Two Numbers # include <stdio.h> int main () { int a, b, temp; printf ( "Enter two numbers: " ); scanf ( "%d %d" , &a, &b); while (b != 0 ) { temp = b; b = a % b; a = temp; } printf ( "GCD is: %d" , a); return 0 ; } ๐Ÿงช Example Output Input: Enter two numbers: 24 36 Output: GCD is : 12 ๐Ÿ” Step-by-Step Explanation Let...

7️⃣C Program to Find the Sum of Digits of a Number (With Program and Explanation)

  ๐Ÿ”ข C Program to Find the Sum of Digits of a Number ๐Ÿ“Œ Problem Statement Write a C program to find the sum of digits of a given number . Example If the input number is 456 , then the sum of digits = 4 + 5 + 6 = 15 ๐Ÿ’ก Concept Used In C, we use: % 10 → to extract the last digit of a number / 10 → to remove the last digit We repeat this process using a while loop until the number becomes 0 . ๐Ÿง  Algorithm Start Read an integer number Initialize sum = 0 While the number is greater than 0 Find last digit using num % 10 Add digit to sum Remove last digit using num / 10 Print the sum End ๐Ÿ’ป C Program: Sum of Digits of a Number # include <stdio.h> int main () { int num, digit, sum = 0 ; printf ( "Enter a number: " ); scanf ( "%d" , &num); while (num > 0 ) { digit = num % 10 ; // Extract last digit sum = sum + digit; // Add digit to sum num = num / 10 ;...