- Get link
- X
- Other Apps
π C Programming Practice Programs
π Covers: Operators | Decision Making | Loops | Nested Loops | Break | Continue
✍ Written by Krishna Popat
π Introduction
After completing the basics of C programming, students must practice logic-based programs to strengthen their understanding.
This blog contains 10 moderate to hard level C programs.
Each program includes:
-
Understanding before the code
-
Comment on every single line of code
-
Output after execution
This makes it perfect for lab submission, exams, and blogging.
✅ PROGRAM 1: ARMSTRONG NUMBER CHECK
π Understanding
An Armstrong number is a number whose sum of the cubes of its digits equals the number itself.
Example: 153 = 1³ + 5³ + 3³
#include <stdio.h> // Include standard input-output header file
int main() // Main function starts program execution
{
int num; // Variable to store the input number
int originalNum; // Variable to store the original number
int digit; // Variable to store each extracted digit
int sum = 0; // Variable to store sum of cubes of digits
printf("Enter a number: "); // Prompt user to enter a number
scanf("%d", &num); // Read number from user
originalNum = num; // Store original number for comparison
while(num > 0) // Loop until number becomes zero
{
digit = num % 10; // Extract the last digit
sum = sum + digit*digit*digit; // Add cube of digit to sum
num = num / 10; // Remove last digit from number
}
if(sum == originalNum) // Check Armstrong condition
printf("Armstrong Number"); // Print if condition is true
else
printf("Not an Armstrong Number"); // Print if condition is false
return 0; // End of program
}
π₯ Output
Enter a number: 153
Armstrong Number
✅ PROGRAM 2: PERFECT NUMBER CHECK
π Understanding
A perfect number is a number whose sum of its proper divisors equals the number itself.
Example: 6 = 1 + 2 + 3
#include <stdio.h> // Include input-output header
int main() // Main function
{
int num; // Variable to store the number
int i; // Loop counter variable
int sum = 0; // Variable to store sum of divisors
printf("Enter a number: "); // Ask user for input
scanf("%d", &num); // Read the number
for(i = 1; i <= num/2; i++) // Loop from 1 to half of number
{
if(num % i == 0) // Check if i is a divisor
sum = sum + i; // Add divisor to sum
}
if(sum == num) // Check perfect number condition
printf("Perfect Number"); // Print if true
else
printf("Not a Perfect Number"); // Print if false
return 0; // End program
}
π₯ Output
Enter a number: 6
Perfect Number
✅ PROGRAM 3: PRIME NUMBERS BETWEEN TWO NUMBERS
π Understanding
This program prints prime numbers in a given range using nested loops, break, and continue.
#include <stdio.h> // Include input-output library
int main() // Main function
{
int start, end; // Variables to store range
int i, j; // Loop counters
int isPrime; // Flag variable for prime check
printf("Enter start and end: "); // Ask user for range
scanf("%d %d", &start, &end); // Read range values
for(i = start; i <= end; i++) // Outer loop through range
{
if(i <= 1) // Check for invalid numbers
continue; // Skip to next iteration
isPrime = 1; // Assume number is prime
for(j = 2; j <= i/2; j++) // Inner loop for divisibility check
{
if(i % j == 0) // If divisible
{
isPrime = 0; // Mark as not prime
break; // Exit inner loop
}
}
if(isPrime == 1) // If number is prime
printf("%d ", i); // Print the prime number
}
return 0; // End program
}
π₯ Output
Enter start and end: 10 20
11 13 17 19
✅ PROGRAM 4: HCF (GCD) OF TWO NUMBERS
π Understanding
HCF is the greatest number that divides both numbers exactly.
#include <stdio.h> // Include standard library
int main() // Main function
{
int a, b; // Variables for two numbers
int i; // Loop counter
int hcf = 1; // Variable to store HCF
printf("Enter two numbers: "); // Ask user for input
scanf("%d %d", &a, &b); // Read both numbers
for(i = 1; i <= a && i <= b; i++) // Loop till smaller number
{
if(a % i == 0 && b % i == 0) // Check common divisor
hcf = i; // Store divisor
}
printf("HCF = %d", hcf); // Print HCF
return 0; // End program
}
π₯ Output
Enter two numbers: 12 18 HCF = 6
✅ PROGRAM 5: DECIMAL TO BINARY (NO ARRAY)
π Understanding
Binary number is formed by dividing the decimal number by 2 repeatedly and storing remainders.
#include <stdio.h> // Include input-output library
int main() // Main function
{
int num; // Variable to store decimal number
int binary = 0; // Variable to store binary result
int place = 1; // Variable to store place value
int remainder; // Variable to store remainder
printf("Enter decimal number: "); // Ask user input
scanf("%d", &num); // Read decimal number
while(num > 0) // Loop until number becomes zero
{
remainder = num % 2; // Get remainder
binary = binary + remainder * place; // Build binary number
place = place * 10; // Increase place value
num = num / 2; // Divide number by 2
}
printf("Binary = %d", binary); // Print binary number
return 0; // End program
}
π₯ Output
Enter decimal number: 13
Binary = 1101
✅ PROGRAM 6: REVERSE NUMBER & PALINDROME CHECK
π Understanding
A palindrome number remains the same when reversed.
Example: 121 → 121 (Palindrome), 123 → 321 (Not Palindrome)
Logic:
Extract digits using % 10
Build reverse using multiplication by 10
Compare reversed number with original
#include <stdio.h> // Include input-output library
int main() // Main function
{
int num; // Variable to store input number
int originalNum; // Variable to store original number
int reverse = 0; // Variable to store reversed number
int digit; // Variable to store extracted digit
printf("Enter a number: "); // Ask user for input
scanf("%d", &num); // Read number from user
originalNum = num; // Store original number
while(num > 0) // Loop until number becomes zero
{
digit = num % 10; // Extract last digit
reverse = reverse * 10 + digit; // Build reversed number
num = num / 10; // Remove last digit
}
if(reverse == originalNum) // Compare reversed and original
printf("Palindrome Number"); // If same
else
printf("Not a Palindrome Number"); // If different
return 0; // End program
}
π₯ Output
Enter a number: 121
Palindrome Number
✅ PROGRAM 7: FLOYD’S TRIANGLE (Nested Loops)
π Understanding
Floyd’s Triangle prints consecutive numbers in triangle form using nested loops.
#include <stdio.h> // Include standard library
int main() // Main function
{
int rows; // Variable to store number of rows
int i, j; // Loop counters
int num = 1; // Variable to print numbers
printf("Enter number of rows: "); // Ask user input
scanf("%d", &rows); // Read rows
for(i = 1; i <= rows; i++) // Outer loop for rows
{
for(j = 1; j <= i; j++) // Inner loop for columns
{
printf("%d ", num); // Print current number
num++; // Increment number
}
printf("\n"); // Move to next line
}
return 0; // End program
}
π₯ Output
1
2 3
4 5 6
7 8 9 10
✅ PROGRAM 8: FREQUENCY OF EACH DIGIT IN A NUMBER
π Understanding
This program counts how many times each digit (0–9) appears in a number.
#include <stdio.h> // Include input-output library
int main() // Main function
{
int num; // Variable to store number
int temp; // Temporary copy of number
int digit; // Variable to store extracted digit
int i; // Loop variable for digits 0–9
int count; // Counter variable
printf("Enter a number: "); // Ask user input
scanf("%d", &num); // Read number
for(i = 0; i <= 9; i++) // Loop for digits 0 to 9
{
temp = num; // Copy original number
count = 0; // Reset count
while(temp > 0) // Loop through digits
{
digit = temp % 10; // Extract digit
if(digit == i) // Compare digit
count++; // Increase count
temp = temp / 10; // Remove digit
}
if(count > 0) // Print only if digit exists
printf("Digit %d occurs %d times\n", i, count);
}
return 0; // End program
}
π₯ Output
Enter a number: 122333
Digit 1 occurs 1 times
Digit 2 occurs 2 times
Digit 3 occurs 3 times
✅ PROGRAM 9: HARMONIC SERIES
π Understanding
Calculates the sum:
1 + 1/2 + 1/3 + ... + 1/n
#include <stdio.h> // Include standard library
int main() // Main function
{
int n; // Variable for number of terms
int i; // Loop variable
float sum = 0.0; // Variable to store sum
printf("Enter value of n: "); // Ask user input
scanf("%d", &n); // Read n
for(i = 1; i <= n; i++) // Loop from 1 to n
{
sum = sum + (1.0 / i); // Add term to sum
}
printf("Sum = %.2f", sum); // Print result
return 0; // End program
}
π₯ Output
Enter value of n: 5
Sum = 2.28
✅ PROGRAM 10: MENU-DRIVEN CALCULATOR (Switch + Break)
π Understanding
Performs arithmetic operations based on user choice using switch.
#include <stdio.h> // Include input-output library
int main() // Main function
{
int choice; // Variable for menu choice
float a, b; // Variables for numbers
printf("1. Add\n"); // Menu option
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Divide\n");
printf("Enter your choice: "); // Ask user choice
scanf("%d", &choice); // Read choice
printf("Enter two numbers: "); // Ask for numbers
scanf("%f %f", &a, &b); // Read numbers
switch(choice) // Switch statement
{
case 1: // Addition case
printf("Result = %.2f", a + b);
break;
case 2: // Subtraction case
printf("Result = %.2f", a - b);
break;
case 3: // Multiplication case
printf("Result = %.2f", a * b);
break;
case 4: // Division case
if(b != 0)
printf("Result = %.2f", a / b);
else
printf("Division by zero not allowed");
break;
default: // Invalid choice
printf("Invalid choice");
}
return 0; // End program
}
π₯ Output
Enter your choice: 1
Enter two numbers: 10 5
Result = 15.00
π― FINAL ACADEMIC CONCLUSION
After completing these 10 structured programs:
✔ Loop concepts are strong
✔ Nested loop logic is clear
✔ Decision-making is confident
✔ Break & Continue are understood
✔ Logical thinking is improved
This collection represents a complete moderate-to-hard practice set suitable for:
Laboratory submission
University internal assessment
Exam preparation
Strong foundation for Functions in C
π Part of the C Programming Beginner Series
π Blog: LEARNING GROWTH HUB
✍️ Author: Krishna Popat
- Get link
- X
- Other Apps
Comments
Post a Comment