C Programming Examples: Logic and Data Structures
C Programming Examples: Logic and Data Structures
<h3>1. Comparing Student Marks</h3>
<p>This program compares the marks of two students.</p>
<pre><code>#include <stdio.h>int main() { int studentA, studentB; printf(“Enter marks of Student A: “); scanf(“%d”, &studentA); printf(“Enter marks of Student B: “); scanf(“%d”, &studentB); if (studentA > studentB) { printf(“Student A scored higher with %d marks.\n”, studentA); } else if (studentB > studentA) { printf(“Student B scored higher with %d marks.\n”, studentB); } else { printf(“Both students scored the same: %d marks.\n”, studentA); } return 0; }
<h3>2. Swapping Two Prices</h3>
<p>This program demonstrates swapping the prices of two items using a temporary variable.</p>
<pre><code>#include <stdio.h>int main() { float price1, price2, temp; printf(“Enter price of Item 1: “); scanf(“%f”, &price1); printf(“Enter price of Item 2: “); scanf(“%f”, &price2);
printf("\nBefore swapping:\n");
printf("Item 1 Price = %.2f\n", price1);
printf("Item 2 Price = %.2f\n", price2);
// Swapping logic
temp = price1;
price1 = price2;
price2 = temp;
printf("\nAfter swapping:\n");
printf("Item 1 Price = %.2f\n", price1);
printf("Item 2 Price = %.2f\n", price2);
return 0;}
<h3>3. Palindrome Number Check</h3>
<p>This program checks if an entered integer is a palindrome.</p>
<pre><code>#include <stdio.h>int main() { int num, original, reverse = 0, remainder; printf(“Enter an integer: “); scanf(“%d”, &num); original = num;
while (num != 0) {
remainder = num % 10;
reverse = reverse * 10 + remainder;
num = num / 10;
}
if (original == reverse) {
printf("%d is a Palindrome number.\n", original);
} else {
printf("%d is Not a Palindrome number.\n", original);
}
return 0;}
<h3>4. Binary Search Implementation</h3>
<p>This code performs a binary search on a sorted array.</p>
<pre><code>#include <stdio.h>int main() { int a[100], n, i, key, low, mid, high, found = 0; printf(“Enter the number of elements:\n”); scanf(“%d”, &n); printf(“Enter %d elements in ascending order:\n”, n); for (i = 0; i < n; i++) { scanf(“%d”, &a[i]); } printf(“Enter an element to search:\n”); scanf(“%d”, &key);
low = 0;
high = n - 1;
while (low <= high && !found) {
mid = (low + high) / 2;
if (key == a[mid])
found = 1;
else if (key < a[mid])
high = mid - 1;
else
low = mid + 1;
}
if (found == 1)
printf("Key found at position %d\n", mid + 1);
else
printf("Key not found\n");
return 0;}
<h3>5. Matrix Multiplication</h3>
<p>Program to multiply two matrices, checking compatibility first.</p>
<pre><code>#include <stdio.h>int main() { int A[10][10], B[10][10], C[10][10]; int m, n, p, q, i, j, k;
printf("Enter the size of matrix A (rows columns): ");
scanf("%d%d", &m, &n);
printf("Enter the size of matrix B (rows columns): ");
scanf("%d%d", &p, &q);
if (n != p) {
printf("Matrix multiplication is not possible\n");
} else {
printf("Enter elements of matrix A:\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &A[i][j]);
printf("Enter elements of matrix B:\n");
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
scanf("%d", &B[i][j]);
// Multiplication
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
C[i][j] = 0;
for (k = 0; k < n; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
printf("The resultant matrix C is:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
printf("%5d", C[i][j]);
}
printf("\n");
}
}
return 0;}
<h3>6. Array Sorting (Bubble Sort)</h3>
<p>Implementation of the Bubble Sort algorithm for an array.</p>
<pre><code>#include <stdio.h>int main() { int a[100], n, i, j, temp; printf(“Enter the number of elements:\n”); scanf(“%d”, &n); printf(“Enter the elements of array:\n”); for (i = 0; i < n; i++) { scanf(“%d”, &a[i]); }
// Bubble Sort
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf("The sorted array is:\n");
for (i = 0; i < n; i++) {
printf("%d\n", a[i]);
}
return 0;}
<h3>7. Employee Record Management</h3>
<p>Structure definition and basic input/output for employee records.</p>
<pre><code>#include <stdio.h>// Definition of an Employee structure struct employee { int eid, eage; char ename[20]; float esal; };
int main() { struct employee e[50]; int n, i;
printf("Enter the number of Employees: ");
scanf("%d", &n);
printf("\n");
// Read employee details
for (i = 0; i < n; i++)
{
printf("Enter the details of Employee : %d\n", i + 1);
printf("Enter eid:");
scanf("%d", &e[i].eid);
printf("Enter ename:");
scanf("%s", e[i].ename);
printf("Enter eage:");
scanf("%d", &e[i].eage);
printf("Enter esal:");
scanf("%f", &e[i].esal);
printf("\n");
}
// Display employee details
printf("\n****************************************************\n");
printf("Details of %d Employees are as follows\n", n);
printf("****************************************************\n");
printf("Empid\tEmpname\tEmpage\tEmpsal\n");
printf("----------------------------------------------------“); for (i = 0; i < n; i++) { printf(“%d\t%s\t%d\t%.2f\n”, e[i].eid, e[i].ename, e[i].eage, e[i].esal); } printf(“—————————————————- “); return 0; }
<h3>8. String Concatenation</h3>
<p>Concatenates two strings, inserting a space between them. <em>Note: Using <code>gets()</code> is unsafe; <code>fgets()</code> is preferred in modern C.</em></p>
<pre><code>#include <stdio.h>int main() { char STR1[100], STR2[100], STR3[100]; int i = 0, j = 0, count = 0;
// Reading strings STR1 and STR2
printf("Enter the String 1\n");
gets(STR1);
printf("Enter the String 2\n");
gets(STR2);
// Copy string STR1 to STR3
while (STR1[i] != '\0')
{
STR3[count] = STR1[i];
count++;
i++;
}
// Insert blank space between STR1 and STR2
STR3[count++] = ' ';
// Copy string STR2 to STR3
while (STR2[j] != '\0')
{
STR3[count] = STR2[j];
count++;
j++;
}
// End of string
STR3[count] = '\0';
// Printing strings
printf("\nString STR1 =\t");
puts(STR1);
printf("\nString STR2 =\t");
puts(STR2);
printf("\nString STR3 =\t");
puts(STR3);
return 0;}
<h3>9. Prime Number Finder in a Range</h3>
<p>Finds all prime numbers within a specified range using a helper function.</p>
<pre><code>#include <stdio.h>// Function prototype int isprime(int);
int main() { int n1, n2, r, i; printf(“Enter the range n1 and n2 to find prime numbers\n”); scanf(“%d%d”, &n1, &n2);
printf("The prime numbers between %d and %d are:\n", n1, n2);
for (i = n1; i <= n2; i++)
{
r = isprime(i); // function call
if (r == 1)
printf("%d\t", i);
}
printf("\n");
return 0;}
// Function definition int isprime(int x) { int i, c = 0; for (i = 1; i <= x; i++) { if (x % i == 0) c++; } // A prime number is divisible only by 1 and itself (count == 2) if (c == 2) return 1; else return 0; }
