12. Program to Print the Reverse Star Pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main() { int rows = 3; for(int i = rows; i >= 1; i--) { for(int j = 1; j <= i; j++) { printf("* "); } printf("\n"); } return 0; } |
13. Operator Precedence and Associativity
Operator Precedence determines which operator is evaluated first in an expression with multiple operators.
Associativity determines the order when operators have the same precedence (left-to-right or right-to-left).
1 2 3 4 5 6 |
int result = 5 + 3 * 2; // * has higher precedence than +, so evaluates to 5 + 6 = 11 int x = 10, y = 5, z = 2; int res = x = y = z; // = has right-to-left associativity, so z (2) assigned to y then x |
14. Factorial Using Recursion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> long factorial(int n) { if(n == 0 || n == 1) return 1; else return n * factorial(n - 1); } int main() { int num; printf("Enter a positive integer: "); scanf("%d", &num); if(num < 0) printf("Factorial doesn't exist for negative numbers"); else printf("Factorial of %d = %ld", num, factorial(num)); return 0; } |
15. Sum of Array Elements Using Pointer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> int main() { int arr[5] = {1, 2, 3, 4, 5}; int *ptr = arr; // Points to first element int sum = 0; for(int i = 0; i < 5; i++) { sum += *ptr; // Add value at pointer ptr++; // Move to next element } printf("Sum of array elements: %d", sum); return 0; } |
16. String to Uppercase Without String Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> void toUpperCase(char *str) { while(*str) { if(*str >= 'a' && *str <= 'z') *str = *str - 32; // Convert to uppercase str++; } } int main() { char str[100]; printf("Enter a string: "); fgets(str, sizeof(str), stdin); toUpperCase(str); printf("Uppercase string: %s", str); return 0; } |
17. Program to Filter Students from Pokhara
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <stdio.h> #include <string.h> struct Student { char name[50]; int age; char address[100]; char cell[15]; }; int main() { FILE *file = fopen("student.txt", "r"); if(file == NULL) { printf("Error opening file!\n"); return 1; } printf("Students from Pokhara:\n"); printf("----------------------\n"); struct Student s; while(fscanf(file, "%49s %d %99s %14s", s.name, &s.age, s.address, s.cell) == 4) { if(strcmp(s.address, "Pokhara") == 0) { printf("Name: %s\n", s.name); printf("Age: %d\n", s.age); printf("Address: %s\n", s.address); printf("Cell: %s\n\n", s.cell); } } fclose(file); return 0; } |
19. Prefix vs Postfix Increment/Decrement Operators
Difference:
Operator Type | Evaluation | Example (int x=5, y;) | Result |
---|---|---|---|
Prefix (++x) | Increment then use | y = ++x; | x=6, y=6 |
Postfix (x++) | Use then increment | y = x++; | x=6, y=5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> int main() { int a = 5, b; // Prefix example b = ++a; printf("Prefix: a=%d, b=%d\n", a, b); // a=6, b=6 // Reset values a = 5; // Postfix example b = a++; printf("Postfix: a=%d, b=%d\n", a, b); // a=6, b=5 return 0; } |
20.Array Sorting Program (Ascending Order)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#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]) { // Swap elements int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } int main() { int arr[10]; printf("Enter 10 integers:\n"); for(int i = 0; i < 10; i++) { scanf("%d", &arr[i]); } bubbleSort(arr, 10); printf("Sorted array (ascending order):\n"); for(int i = 0; i < 10; i++) { printf("%d ", arr[i]); } return 0; } |
22. Passing Arguments by Value vs by Address
Comparison Table
Feature | Pass by Value | Pass by Address (Pointer) |
---|---|---|
Mechanism | Copies the actual value | Passes memory address of the value |
Memory Usage | Creates duplicate | No duplication |
Original Value | Cannot be modified | Can be modified |
Performance | Slower for large data | Faster for large data |
Syntax | Normal parameter | Pointer parameter |
Safety | More secure (no side effects) | Less secure (can modify original) |
Example Programs
Pass by Value:
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <stdio.h> void increment(int x) { x++; // Only changes local copy } int main() { int num = 5; increment(num); printf("Original value: %d", num); // Output: 5 (unchanged) return 0; } |
Pass by Address:
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <stdio.h> void increment(int *x) { (*x)++; // Modifies original value } int main() { int num = 5; increment(&num); printf("Modified value: %d", num); // Output: 6 (changed) return 0; } |