The document contains ten C programming examples covering various concepts such as sorting algorithms (Bubble Sort), prime number checking, pattern printing, file handling, function usage, array input/output, pointers, swapping using pointers, recursion for factorial calculation, and dynamic memory allocation. Each program includes a brief description of its purpose and suggestions for implementation. These examples serve as practical exercises for learning fundamental programming techniques in C.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
1 views4 pages
folderlock.txt
The document contains ten C programming examples covering various concepts such as sorting algorithms (Bubble Sort), prime number checking, pattern printing, file handling, function usage, array input/output, pointers, swapping using pointers, recursion for factorial calculation, and dynamic memory allocation. Each program includes a brief description of its purpose and suggestions for implementation. These examples serve as practical exercises for learning fundamental programming techniques in C.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4
// ✅ Program 1: Bubble Sort
// Q: Write a C program to sort an array of integers using Bubble Sort.
// Suggestion: Focus on nested loops and swapping logic. #include <stdio.h> void bubbleSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int main() { int arr[] = {5, 2, 8, 1, 3}; int n = sizeof(arr) / sizeof(arr[0]); bubbleSort(arr, n); printf("Sorted array: "); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }
// ✅ Program 2: Prime Number Checker
// Q: Write a C program to check whether a number is prime. // Suggestion: Use modulo and loop from 2 to sqrt(n). #include <stdio.h> int main() { int num, flag = 1; printf("Enter a number: "); scanf("%d", &num); if (num <= 1) flag = 0; for (int i = 2; i <= num / 2; i++) { if (num % i == 0) { flag = 0; break; } } if (flag) printf("%d is a prime number.\n", num); else printf("%d is not a prime number.\n", num); return 0; }
// ✅ Program 3: Pattern Printing
// Q: Write a C program to print the following pattern: // *\n***\n*****\n*******\n*********\n*******\n*****\n***\n* // Suggestion: Use nested loops and symmetry. #include <stdio.h> int main() { int i, j; for (i = 1; i <= 5; i++) { for (j = 1; j <= 2 * i - 1; j++) printf("*"); printf("\n"); } for (i = 4; i >= 1; i--) { for (j = 1; j <= 2 * i - 1; j++) printf("*"); printf("\n"); } return 0; }
// ✅ Program 4: File Handling
// Q: Write a C program to create and write to a text file. // Suggestion: Use fopen, fprintf, and fclose. #include <stdio.h> int main() { FILE *fptr; fptr = fopen("output.txt", "w"); if (fptr == NULL) { printf("Error opening file!\n"); return 1; } fprintf(fptr, "Hello! This is written to a file.\n"); fclose(fptr); printf("Data written to file successfully.\n"); return 0; }
// ✅ Program 5: Sum using Function
// Q: Write a C function to return sum of two integers. // Suggestion: Use a separate function with return type. #include <stdio.h> int sum(int a, int b) { return a + b; } int main() { int x = 5, y = 7; printf("Sum: %d\n", sum(x, y)); return 0; }
// ✅ Program 6: Array Input and Output
// Q: Write a C program to input and print an array. // Suggestion: Practice scanf and loop traversal. #include <stdio.h> int main() { int arr[5]; printf("Enter 5 numbers: "); for (int i = 0; i < 5; i++) scanf("%d", &arr[i]); printf("Array: "); for (int i = 0; i < 5; i++) printf("%d ", arr[i]); return 0; }
// ✅ Program 7: Pointer Demo
// Q: Write a C program to demonstrate pointer usage. // Suggestion: Declare pointer and access variable. #include <stdio.h> int main() { int a = 10; int *p = &a; printf("Value: %d\n", *p); printf("Address: %p\n", p); return 0; }
// ✅ Program 8: Swap using Pointers
// Q: Swap two numbers using call by reference. // Suggestion: Use pointer parameters. #include <stdio.h> void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } int main() { int a = 5, b = 10; swap(&a, &b); printf("a = %d, b = %d\n", a, b); return 0; }
// ✅ Program 9: Factorial using Recursion
// Q: Write a C program to find factorial using recursion. // Suggestion: Practice recursive functions. #include <stdio.h> int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); } int main() { int n; printf("Enter a number: "); scanf("%d", &n); printf("Factorial: %d\n", factorial(n)); return 0; }
// ✅ Program 10: Dynamic Memory Allocation
// Q: Allocate memory for array dynamically and print values. // Suggestion: Use malloc and free. #include <stdio.h> #include <stdlib.h> int main() { int *arr, n; printf("Enter number of elements: "); scanf("%d", &n); arr = (int *)malloc(n * sizeof(int)); if (arr == NULL) { printf("Memory not allocated.\n"); return 1; } printf("Enter elements: "); for (int i = 0; i < n; i++) scanf("%d", &arr[i]); printf("Elements: "); for (int i = 0; i < n; i++) printf("%d ", arr[i]); free(arr); return 0; }