Unique C Programs for Problem Solving: From Basics to Advanced
Last Updated on: 30th Jul 2025 00:24:17 AM
Welcome to an exciting journey through unique and practical C programs designed for beginer students .These programs cover a wide range of topics—basics, arrays, strings, pointers, structures, file handling, and dynamic memory allocation—progressing from beginner-friendly to advanced, real-life applications. Each program is interactive (using scanf and fgets), includes a description, code, sample output, and a clear explanation, making learning fun and engaging. Whether you’re calculating tips, managing a library, or simulating a bank, these programs will spark creativity and sharpen your C skills. Let’s dive in and solve some problems!
Table of Contents
-
Basic Programs (1–10)
-
Arrays and Strings (11–20)
-
Pointers (21–30)
-
Structures and Unions (31–40)
-
File Handling (41–45)
-
Dynamic Memory Allocation (46–50)
1. Basic Programs
1. Calculate Restaurant Tip
Description: Calculate a restaurant bill’s tip based on user input for the bill amount and tip percentage.
#include <stdio.h>
int main() {
float bill, tip_percent, tip;
printf("Enter bill amount: $");
scanf("%f", &bill);
printf("Enter tip percentage: ");
scanf("%f", &tip_percent);
tip = bill * (tip_percent / 100);
printf("Tip: $%.2f\n", tip);
printf("Total: $%.2f\n", bill + tip);
return 0;
}
Output (Input: 50, 15):
Enter bill amount: $50
Enter tip percentage: 15
Tip: $7.50
Total: $57.50
Explanation: Takes bill and tip percentage, calculates the tip, and adds it to the bill. Uses scanf for input and %.2f for formatted output.
2. Check Leap Year
Description: Determine if a user-entered year is a leap year.
#include <stdio.h>
int main() {
int year;
printf("Enter year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
Output (Input: 2024):
Enter year: 2024
2024 is a leap year.
Explanation: Checks if the year is divisible by 4 and not 100, or by 400, to determine leap year status.
3. Swap Two Numbers
Description: Swap two user-entered numbers using a temporary variable.
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Before swap: a = %d, b = %d\n", a, b);
temp = a;
a = b;
b = temp;
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}
Output (Input: 5, 10):
Enter two numbers: 5 10
Before swap: a = 5, b = 10
After swap: a = 10, b = 5
Explanation: Uses a temporary variable to swap two numbers, demonstrating basic variable manipulation.
4. Factorial of a Number
Description: Calculate the factorial of a user-entered number.
#include <stdio.h>
int main() {
int n;
unsigned long fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
if (n < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
for (int i = 1; i <= n; i++) {
fact *= i;
}
printf("Factorial of %d = %lu\n", n, fact);
}
return 0;
}
Output (Input: 5):
Enter a number: 5
Factorial of 5 = 120
Explanation: Uses a loop to multiply numbers from 1 to n. Handles negative input with a check.
5. Fibonacci Sequence
Description: Print the Fibonacci sequence up to a user-specified number of terms.
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci Sequence: ");
for (int i = 0; i < n; i++) {
if (i <= 1) {
next = i;
} else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
printf("\n");
return 0;
}
Output (Input: 6):
Enter number of terms: 6
Fibonacci Sequence: 0 1 1 2 3 5
Explanation: Generates Fibonacci numbers by adding the previous two terms, printing n terms.
6. Check Prime Number
Description: Check if a user-entered number is prime.
#include <stdio.h>
int main() {
int n, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &n);
if (n <= 1) {
isPrime = 0;
} else {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
isPrime = 0;
break;
}
}
}
printf("%d is %sprime.\n", n, isPrime ? "" : "not ");
return 0;
}
Output (Input: 17):
Enter a number: 17
17 is prime.
Explanation: Checks divisibility up to the square root of n to determine if it’s prime.
7. Reverse a Number
Description: Reverse the digits of a user-entered number.
#include <stdio.h>
int main() {
int n, reversed = 0,remainder;
printf("Enter a number: ");
scanf("%d", &n);
while (n > 0) {
remainder = n % 10; // Get the last digit
reverse = reverse * 10 + remainder;
n /= 10;
}
printf("Reversed number: %d\n", reversed);
return 0;
}
Output (Input: 1234):
Enter a number: 1234
Reversed number: 4321
Explanation: Extracts digits using modulo and builds the reversed number.
8. Palindrome Number
Description: Check if a user-entered number is a palindrome.
#include <stdio.h>
int main() {
int num, reverse = 0, remainder, original;
// Taking input for the number
printf("Enter a number: ");
scanf("%d", &num);
// Store the original number for comparison later
original = num;
// Reverse the number
while (num != 0) {
remainder = num % 10; // Get the last digit
reverse = reverse * 10 + remainder; // Build the reversed number
num /= 10; // Remove the last digit
}
// Check if the original number is equal to the reversed number
if (original == reverse) {
printf("%d is a palindrome.\n", original);
} else {
printf("%d is not a palindrome.\n", original);
}
return 0;
}
Output (Input: 1221):
Enter a number: 1221
1221 is a palindrome
Explanation: Reverses the number and compares it with the original to check for palindrome.
9. Sum of Digits
Description: Calculate the sum of digits of a user-entered number.
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
while (n > 0) {
sum += n % 10;
n /= 10;
}
printf("Sum of digits: %d\n", sum);
return 0;
}
Output (Input: 123):
Enter a number: 123
Sum of digits: 6
Explanation: Extracts each digit using modulo and adds to the sum.
10. Simple Interest Calculator
Description: Calculate simple interest based on principal, rate, and time.
#include <stdio.h>
int main() {
float principal, rate, time, interest;
printf("Enter principal, rate, time: ");
scanf("%f %f %f", &principal, &rate, &time);
interest = (principal * rate * time) / 100;
printf("Simple Interest: $%.2f\n", interest);
return 0;
}
Output (Input: 1000, 5, 2):
Enter principal, rate, time: 1000 5 2
Simple Interest: $100.00
Explanation: Applies the simple interest formula: (principal * rate * time) / 100.