Essential C Programs: Examples & Code
Essential C Programs with Examples
1. Largest of Three Numbers
This program finds the largest among three numbers using the ternary operator.
largest = (x > y) ? (x > z ? x : z) : (y > z ? y : z);
2. Quadratic Equation Solver
This program solves a quadratic equation using the quadratic formula.
#include
#includeint main() {
double a, b, c, discriminant, root1, root2;printf("Enter coefficients a, b, c: ");
scanf("%lf %lf %lf", &a, &b, &c);discriminant = b * b - 4 * a * c;if (discriminant < 0) {
printf("No real roots\n");
} else if (discriminant == 0) {
printf("One real root: %.2lf\n", -b / (2 * a));
} else {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Two real roots: %.2lf and %.2lf\n", root1, root2);
}return 0;
}
3. Menu-Driven Program
This program presents a menu to the user with options to perform various operations on a number.
#include
#includeint main() {
int choice;
float num;while (1) {
printf("Enter a number: ");
scanf("%f", &num);printf("\nMenu:\n");
printf("1. Check even or odd\n");
printf("2. Positive or negative\n");
printf("3. Square\n");
printf("4. Square root\n");
printf("5. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);switch (choice) {
case 1:
printf("%.2f is %s.\n", num, (int)num % 2 == 0 ? "even" : "odd");
break;
case 2:
printf("%.2f is %s.\n", num, num > 0 ? "positive" : (num < 0 ? "negative" : "zero"));
break;
case 3:
printf("Square of %.2f is %.2f.\n", num, num * num);
break;
case 4:
printf(num >= 0 ? "Square root: %.2f\n" : "Invalid for negative.\n", sqrt(num));
break;
case 5:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice.\n");
}
}
}
4. Sum and Average of Numbers Divisible by 7
This program calculates the sum and average of numbers divisible by 7 within a given range (1-300).
#includeint main() {
int i, sum = 0, count = 0;printf("Numbers divisible by 7 (1-300): \n");for (i = 1; i <= 300; i++) {
if (i % 7 == 0) {
printf("%d ", i);
sum += i;
count++;
}
}printf("\nSum: %d\n", sum);
printf("Average: %.2f\n", (float)sum / count);return 0;
}
5. ASCII Table
This program prints the ASCII table for printable characters (ASCII values 32 to 126).
#includeint main() {
for (int i = 32; i <= 126; i++) {
printf("ASCII %d: %c\n", i, (char)i);
}
return 0;
}
6. Armstrong & Prime Checker
This program checks if a number is both an Armstrong number and a prime number.
#include
#includeint isPrime(int n) {
if (n <= 1) return 0;
for (int i = 2; i <= sqrt(n); i++)
if (n % i == 0) return 0;
return 1;
}int isArmstrong(int n) {
int sum = 0, temp = n, digits = 0;
while (temp) digits++, temp /= 10;
temp = n;
while (temp) sum += pow(temp % 10, digits), temp /= 10;
return sum == n;
}int main() {
int num;
printf("Enter number: ");
scanf("%d", &num);
printf("%d is %s prime.\n", num, isPrime(num) ? "" : "not");
printf("%d is %s an Armstrong number.\n", num, isArmstrong(num) ? "" : "not");
return 0;
}
7. Range of Prime and Armstrong Numbers
This program prints all prime and Armstrong numbers within a given range (100-1000).
#include
#includeint isPrime(int n) {
if (n <= 1) return 0;
for (int i = 2; i <= sqrt(n); i++)
if (n % i == 0) return 0;
return 1;
}int isArmstrong(int n) {
int sum = 0, temp = n, digits = 0;
while (temp) digits++, temp /= 10;
temp = n;
while (temp) sum += pow(temp % 10, digits), temp /= 10;
return sum == n;
}int main() {
printf("Prime and Armstrong numbers between 100 and 1000:\n");
for (int i = 100; i <= 1000; i++) {
if (isPrime(i) || isArmstrong(i)) {
printf("%d\n", i);
}
}
return 0;
}
8. Delete Element from Array
This program deletes an element from an array at a specified position.
#includeint main() {
int arr[50], pos, i, num;printf("Enter number of elements: ");
scanf("%d", &num);for (i = 0; i < num; i++) {
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}printf("Enter position to delete: ");
scanf("%d", &pos);if (pos > num || pos <=0) {
printf("Invalid position.\n");
} else {
for (i = pos - 1; i < num - 1; i++) {
arr[i] = arr[i + 1];
}printf("Array after deletion:\n");
for (i = 0; i < num - 1; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
}return 0;
}
9. Matrix Operations
This program performs matrix addition, subtraction, multiplication, and transpose.
#includeint main() {
int ch, r1, c1, r2, c2, i, j, k, m1[10][10], m2[10][10], m3[10][10];printf("1. Add\n2. Subtract\n3. Multiply\n4. Transpose\n5. Exit\nChoice: ");
scanf("%d", &ch);switch (ch) {
case 1:
case 2:
printf("Enter order of 2 matrices: ");
scanf("%d%d%d%d", &r1, &c1, &r2, &c2);if (r1 != r2 || c1 != c2) {
printf("Invalid order\n");
} else {
printf("Enter elements of matrix 1:\n");
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
scanf("%d", &m1[i][j]);printf("Enter elements of matrix 2:\n");
for (i = 0; i < r2; i++)
for (j = 0; j < c2; j++)
scanf("%d", &m2[i][j]);for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
m3[i][j] = (ch == 1) ? m1[i][j] + m2[i][j] : m1[i][j] - m2[i][j];printf(ch == 1 ? "Sum:\n" : "Difference:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c1; j++)
printf("%d\t", m3[i][j]);
printf("\n");
}
}
break;case 3:
printf("Enter order of matrices: ");
scanf("%d%d%d%d", &r1, &c1, &r2, &c2);if (c1 != r2) {
printf("Multiplication not possible.\n");
} else {
printf("Enter elements of matrix 1:\n");
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
scanf("%d", &m1[i][j]);printf("Enter elements of matrix 2:\n");
for (i = 0; i < r2; i++)
for (j = 0; j < c2; j++)
scanf("%d", &m2[i][j]);for (i = 0; i < r1; i++)
for (j = 0; j < c2; j++) {
m3[i][j] = 0;
for (k = 0; k < c1; k++)
m3[i][j] += m1[i][k] * m2[k][j];
}printf("Product:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++)
printf("%d\t", m3[i][j]);
printf("\n");
}
}
break;case 4:
printf("Enter order: ");
scanf("%d%d", &r1, &c1);printf("Enter elements of matrix:\n");
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
scanf("%d", &m1[i][j]);printf("Transpose:\n");
for (i = 0; i < c1; i++) {
for (j = 0; j < r1; j++)
printf("%d\t", m1[j][i]);
printf("\n");
}
break;case 5:
printf("Exiting...\n");
return 0;default:
printf("Invalid choice.\n");
}return 0;
}