1. Display Even Numbers from Array
#includeint main() { int n, arr[50]; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); for(int i = 0; i < n; i++) scanf("%d", &arr[i]); printf("Even numbers: "); for(int i = 0; i < n; i++) if(arr[i] % 2 == 0) printf("%d ", arr[i]); return 0; }
Output:
Enter number of elements: 5
Enter 5 elements: 1 2 3 4 5
Even numbers: 2 4
2. Find Total and Average of Array Elements
#includeint main() { int n, arr[50], sum = 0; float avg; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); for(int i = 0; i < n; i++) { scanf("%d", &arr[i]); sum += arr[i]; } avg = (float)sum / n; printf("Total: %d\nAverage: %.2f", sum, avg); return 0; }
Output:
Enter number of elements: 4
Enter 4 elements: 10 20 30 40
Total: 100
Average: 25.00
3. Display Array in Reverse Order
#includeint main() { int n, arr[50]; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); for(int i = 0; i < n; i++) scanf("%d", &arr[i]); printf("Array in reverse order: "); for(int i = n-1; i >= 0; i--) printf("%d ", arr[i]); return 0; }
Output:
Enter number of elements: 5
Enter 5 elements: 1 2 3 4 5
Array in reverse order: 5 4 3 2 1
4. Count Positive, Negative, Even, and Odd Numbers
#includeint main() { int n, arr[50], pos = 0, neg = 0, even = 0, odd = 0; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); for(int i = 0; i < n; i++) { scanf("%d", &arr[i]); if(arr[i] > 0) pos++; else if(arr[i] < 0) neg++; if(arr[i] % 2 == 0) even++; else odd++; } printf("Positive: %d\nNegative: %d\nEven: %d\nOdd: %d", pos, neg, even, odd); return 0; }
Output:
Enter number of elements: 6
Enter 6 elements: 1 -2 3 -4 5 6
Positive: 4
Negative: 2
Even: 3
Odd: 3
5. Copy Array Elements to Another Array
#includeint main() { int n, arr1[50], arr2[50]; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); for(int i = 0; i < n; i++) scanf("%d", &arr1[i]); for(int i = 0; i < n; i++) arr2[i] = arr1[i]; printf("Copied array: "); for(int i = 0; i < n; i++) printf("%d ", arr2[i]); return 0; }
Output:
Enter number of elements: 4
Enter 4 elements: 10 20 30 40
Copied array: 10 20 30 40
6. Add Two Matrices
#includeint main() { int r, c, a[10][10], b[10][10], sum[10][10]; printf("Enter rows and columns: "); scanf("%d %d", &r, &c); printf("Enter first matrix elements:\n"); for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) scanf("%d", &a[i][j]); printf("Enter second matrix elements:\n"); for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) scanf("%d", &b[i][j]); for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) sum[i][j] = a[i][j] + b[i][j]; printf("Sum of matrices:\n"); for(int i = 0; i < r; i++) { for(int j = 0; j < c; j++) printf("%d ", sum[i][j]); printf("\n"); } return 0; }
Output:
Enter rows and columns: 2 2
Enter first matrix elements:
1 2
3 4
Enter second matrix elements:
5 6
7 8
Sum of matrices:
6 8
10 12
7. Subtract Two Matrices
#includeint main() { int r, c, a[10][10], b[10][10], diff[10][10]; printf("Enter rows and columns: "); scanf("%d %d", &r, &c); printf("Enter first matrix elements:\n"); for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) scanf("%d", &a[i][j]); printf("Enter second matrix elements:\n"); for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) scanf("%d", &b[i][j]); for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) diff[i][j] = a[i][j] - b[i][j]; printf("Difference of matrices:\n"); for(int i = 0; i < r; i++) { for(int j = 0; j < c; j++) printf("%d ", diff[i][j]); printf("\n"); } return 0; }
Output:
Enter rows and columns: 2 2
Enter first matrix elements:
5 6
7 8
Enter second matrix elements:
1 2
3 4
Difference of matrices:
4 4
4 4
8. Convert Positive to Negative and Vice Versa
#includeint main() { int n, arr[50]; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); for(int i = 0; i < n; i++) scanf("%d", &arr[i]); printf("Converted array: "); for(int i = 0; i < n; i++) { arr[i] = -arr[i]; printf("%d ", arr[i]); } return 0; }
Output:
Enter number of elements: 5
Enter 5 elements: 1 -2 3 -4 5
Converted array: -1 2 -3 4 -5
9. Multiply Two Matrices
#includeint main() { int r1, c1, r2, c2, a[10][10], b[10][10], prod[10][10]; printf("Enter rows and columns for first matrix: "); scanf("%d %d", &r1, &c1); printf("Enter rows and columns for second matrix: "); scanf("%d %d", &r2, &c2); if(c1 != r2) { printf("Matrix multiplication not possible!\n"); return 0; } printf("Enter first matrix elements:\n"); for(int i = 0; i < r1; i++) for(int j = 0; j < c1; j++) scanf("%d", &a[i][j]); printf("Enter second matrix elements:\n"); for(int i = 0; i < r2; i++) for(int j = 0; j < c2; j++) scanf("%d", &b[i][j]); for(int i = 0; i < r1; i++) for(int j = 0; j < c2; j++) { prod[i][j] = 0; for(int k = 0; k < c1; k++) prod[i][j] += a[i][k] * b[k][j]; } printf("Product of matrices:\n"); for(int i = 0; i < r1; i++) { for(int j = 0; j < c2; j++) printf("%d ", prod[i][j]); printf("\n"); } return 0; }
Output:
Enter rows and columns for first matrix: 2 2
Enter rows and columns for second matrix: 2 2
Enter first matrix elements:
1 2
3 4
Enter second matrix elements:
5 6
7 8
Product of matrices:
19 22
43 50
10. String Length Using String Function
#include#include int main() { char str[50]; printf("Enter a string: "); scanf("%s", str); printf("Length of string: %d\n", strlen(str)); return 0; }
Output:
Enter a string: Hello
Length of string: 5
11. String Length Without String Function
#includeint main() { char str[50]; int len = 0; printf("Enter a string: "); scanf("%s", str); while(str[len] != '\0') len++; printf("Length of string: %d\n", len); return 0; }
Output:
Enter a string: World
Length of string: 5
12. Check if String is Palindrome
#include#include int main() { char str[50]; int len, isPal = 1; printf("Enter a string: "); scanf("%s", str); len = strlen(str); for(int i = 0; i < len/2; i++) { if(str[i] != str[len-1-i]) { isPal = 0; break; } } if(isPal) printf("String is a palindrome!\n"); else printf("String is not a palindrome\n"); return 0; }
Output:
Enter a string: radar
String is a palindrome!
13. Swap Two Variables Using Pointer and Function
#includevoid swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int x, y; printf("Enter two numbers: "); scanf("%d %d", &x, &y); printf("Before swap: x = %d, y = %d\n", x, y); swap(&x, &y); printf("After swap: x = %d, y = %d\n", x, y); return 0; }
Output:
Enter two numbers: 5 10
Before swap: x = 5, y = 10
After swap: x = 10, y = 5
14. Area of Circle Using Pointer and Function
#include#define PI 3.14159 void area(float *r, float *a) { *a = PI * (*r) * (*r); } int main() { float radius, area_result; printf("Enter radius: "); scanf("%f", &radius); area(&radius, &area_result); printf("Area of circle: %.2f\n", area_result); return 0; }
Output:
Enter radius: 5
Area of circle: 78.54
15. Find Maximum and Minimum Using Pointer to Function
#includevoid findMaxMin(int arr[], int n, int *max, int *min) { *max = *min = arr[0]; for(int i = 1; i < n; i++) { if(arr[i] > *max) *max = arr[i]; if(arr[i] < *min) *min = arr[i]; } } int main() { int n, arr[50], max, min; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); for(int i = 0; i < n; i++) scanf("%d", &arr[i]); findMaxMin(arr, n, &max, &min); printf("Maximum: %d\nMinimum: %d\n", max, min); return 0; }
Output:
Enter number of elements: 5
Enter 5 elements: 10 5 20 15 8
Maximum: 20
Minimum: 5
16. Sum and Average Using Dynamic Array
#include#include int main() { int n, *arr, sum = 0; float avg; printf("Enter number of elements: "); scanf("%d", &n); arr = (int *)malloc(n * sizeof(int)); printf("Enter %d elements: ", n); for(int i = 0; i < n; i++) { scanf("%d", &arr[i]); sum += arr[i]; } avg = (float)sum / n; printf("Sum: %d\nAverage: %.2f\n", sum, avg); free(arr); return 0; }
Output:
Enter number of elements: 4
Enter 4 elements: 10 20 30 40
Sum: 100
Average: 25.00
17. Sum of Prime Numbers Using Dynamic Array
#include#include int isPrime(int num) { if(num <= 1) return 0; for(int i = 2; i * i <= num; i++) if(num % i == 0) return 0; return 1; } int main() { int n, *arr, sum = 0; printf("Enter number of elements: "); scanf("%d", &n); arr = (int *)malloc(n * sizeof(int)); printf("Enter %d elements: ", n); for(int i = 0; i < n; i++) { scanf("%d", &arr[i]); if(isPrime(arr[i])) sum += arr[i]; } printf("Sum of prime numbers: %d\n", sum); free(arr); return 0; }
Output:
Enter number of elements: 5
Enter 5 elements: 2 3 4 5 6
Sum of prime numbers: 10
1. Add Two Complex Numbers by Passing Structure to a Function
#includestruct Complex { float real, imag; }; struct Complex addComplex(struct Complex c1, struct Complex c2) { struct Complex sum; sum.real = c1.real + c2.real; sum.imag = c1.imag + c2.imag; return sum; } int main() { struct Complex c1, c2, result; printf("Enter real and imaginary parts of first complex number: "); scanf("%f %f", &c1.real, &c1.imag); printf("Enter real and imaginary parts of second complex number: "); scanf("%f %f", &c2.real, &c2.imag); result = addComplex(c1, c2); printf("Sum = %.2f + %.2fi\n", result.real, result.imag); return 0; }
Output:
Enter real and imaginary parts of first complex number: 3 4
Enter real and imaginary parts of second complex number: 2 5
Sum = 5.00 + 9.00i
2. Demonstrate Difference Between Structure and Union
#includestruct StructExample { int a; char b; }; union UnionExample { int a; char b; }; int main() { struct StructExample s; union UnionExample u; printf("Size of structure: %d bytes\n", sizeof(s)); printf("Size of union: %d bytes\n", sizeof(u)); s.a = 65; s.b = 'B'; printf("Structure: a = %d, b = %c\n", s.a, s.b); u.a = 65; printf("Union after setting a: a = %d, b = %c\n", u.a, u.b); u.b = 'B'; printf("Union after setting b: a = %d, b = %c\n", u.a, u.b); return 0; }
Output:
Size of structure: 8 bytes
Size of union: 4 bytes
Structure: a = 65, b = B
Union after setting a: a = 65, b = A
Union after setting b: a = 66, b = B
3. Compare Two Dates Using Structure
#includestruct Date { int day, month, year; }; int main() { struct Date d1, d2; printf("Enter first date (dd mm yyyy): "); scanf("%d %d %d", &d1.day, &d1.month, &d1.year); printf("Enter second date (dd mm yyyy): "); scanf("%d %d %d", &d2.day, &d2.month, &d2.year); if(d1.day == d2.day && d1.month == d2.month && d1.year == d2.year) printf("Equal\n"); else printf("Not Equal\n"); return 0; }
Output:
Enter first date (dd mm yyyy): 10 06 2023
Enter second date (dd mm yyyy): 10 06 2023
Equal
4. Demonstrate Nested Structure
#includestruct Address { char city[20]; int pin; }; struct Person { char name[20]; struct Address addr; }; int main() { struct Person p; printf("Enter name: "); scanf("%s", p.name); printf("Enter city and pin code: "); scanf("%s %d", p.addr.city, &p.addr.pin); printf("Name: %s\nCity: %s\nPin: %d\n", p.name, p.addr.city, p.addr.pin); return 0; }
Output:
Enter name: John
Enter city and pin code: Delhi 110001
Name: John
City: Delhi
Pin: 110001
5. Copy Content of a Text File
#includeint main() { FILE *source, *dest; char ch; source = fopen("source.txt", "r"); if(source == NULL) { printf("Source file not found!\n"); return 1; } dest = fopen("copy.txt", "w"); if(dest == NULL) { printf("Cannot create destination file!\n"); fclose(source); return 1; } while((ch = fgetc(source)) != EOF) fputc(ch, dest); printf("File copied successfully!\n"); fclose(source); fclose(dest); return 0; }
Output (assuming source.txt contains "Hello World"):
File copied successfully!
(copy.txt will contain: Hello World)
6. Append Content to a File
#includeint main() { FILE *file; char str[100]; file = fopen("demo.txt", "a"); if(file == NULL) { printf("Cannot open file!\n"); return 1; } printf("Enter text to append: "); scanf(" %[^\n]", str); fprintf(file, "%s\n", str); printf("Content appended successfully!\n"); fclose(file); return 0; }
Output:
Enter text to append: New Line
Content appended successfully!
7. Write String to File and Reverse to Another File
#include#include int main() { FILE *demo, *reverse; char str[100]; printf("Enter a string: "); scanf(" %[^\n]", str); demo = fopen("demo.txt", "w"); if(demo == NULL) { printf("Cannot create demo.txt!\n"); return 1; } fprintf(demo, "%s", str); fclose(demo); demo = fopen("demo.txt", "r"); reverse = fopen("reverse.txt", "w"); if(demo == NULL || reverse == NULL) { printf("File error!\n"); return 1; } int len = strlen(str); for(int i = len-1; i >= 0; i--) fputc(str[i], reverse); printf("String reversed and saved!\n"); fclose(demo); fclose(reverse); return 0; }
Output:
Enter a string: Hello
String reversed and saved!
(demo.txt: Hello, reverse.txt: olleH)
8. Reverse Case of Text File Content
#include#include int main() { FILE *input, *output; char ch; input = fopen("input.txt", "r"); if(input == NULL) { printf("Input file not found!\n"); return 1; } output = fopen("output.txt", "w"); if(output == NULL) { printf("Cannot create output file!\n"); fclose(input); return 1; } while((ch = fgetc(input)) != EOF) { if(isupper(ch)) fputc(tolower(ch), output); else if(islower(ch)) fputc(toupper(ch), output); else fputc(ch, output); } printf("Content converted and saved!\n"); fclose(input); fclose(output); return 0; }
Output (assuming input.txt contains "Hello World"):
Content converted and saved!
(output.txt: hELLO wORLD)
9. Count Total Words in a Text File
#include#include int main() { FILE *file; char ch; int word_count = 0, in_word = 0; file = fopen("input.txt", "r"); if(file == NULL) { printf("File not found!\n"); return 1; } while((ch = fgetc(file)) != EOF) { if(isspace(ch) || ch == '\n' || ch == '\t') { in_word = 0; } else if(in_word == 0) { in_word = 1; word_count++; } } printf("Total words: %d\n", word_count); fclose(file); return 0; }
Output (assuming input.txt contains "Hello World This is a test"):
Total words: 6
0 Comments
If you have any doubts, Please let me know