1.
Swap the two numbers using three methods
// Method 1: Using a temporary variable
int temp = a;
a = b;
b = temp;
// Method 2: Using arithmetic operators
a = a + b;
b = a - b;
a = a - b;
// Method 3: Using bitwise XOR
a = a ^ b;
b = a ^ b;
a = a ^ b;
2. Simple interest and compound interest
// Simple Interest: SI = (P * R * T) / 100
float SI = (P * R * T) / 100.0;
// Compound Interest: CI = P * (pow((1 + R / 100), T)) - P
float CI = P * pow((1 + R / 100.0), T) - P;
3. Addition of Matrix
int C[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
C[i][j] = A[i][j] + B[i][j];
4. Print array elements using pointers
for (int i = 0; i < size; i++)
printf("%d ", *(arr + i));
5. Biggest and smallest number in an array
int max = arr[0], min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
6. Count number of vowels and alphabet
int vowels = 0, letters = 0;
for (int i = 0; str[i]; i++) {
if (isalpha(str[i])) {
letters++;
if (strchr("aeiouAEIOU", str[i])) vowels++;
}
}
7. Create a structure for storing account details
struct Account {
int acc_no;
char name[50];
float balance;
};
8. Accessing account details using pointers
struct Account acc, *ptr = &acc;
ptr->acc_no = 123;
strcpy(ptr->name, "John");
ptr->balance = 1000.50;
9. Fahrenheit to Celsius and vice versa
celsius = (fahrenheit - 32) * 5 / 9;
fahrenheit = (celsius * 9 / 5) + 32;
10. Function to print Fibonacci numbers
void fibonacci(int n) {
int a = 0, b = 1, c;
printf("%d %d ", a, b);
for (int i = 2; i < n; i++) {
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
}
11. Alphabetical order
char str[10][50], temp[50];
for (int i = 0; i < n-1; i++)
for (int j = i+1; j < n; j++)
if (strcmp(str[i], str[j]) > 0) {
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
12. Armstrong numbers up to 1000
for (int i = 1; i <= 1000; i++) {
int sum = 0, temp = i, rem;
while (temp) {
rem = temp % 10;
sum += rem * rem * rem;
temp /= 10;
}
if (sum == i) printf("%d ", i);
}
13. Simple calculator using switch and goto
char op;
float a, b, result;
start:
printf("Enter operator: "); scanf(" %c", &op);
printf("Enter two numbers: "); scanf("%f%f", &a, &b);
switch(op) {
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break;
case '/': result = a / b; break;
default: printf("Invalid operator"); goto start;
}
printf("Result = %.2f\n", result);
14. Structure for student details
struct Student {
int roll;
char name[50];
float marks;
};
15. Find grade using if-else
if (marks >= 90) grade = 'A';
else if (marks >= 75) grade = 'B';
else if (marks >= 60) grade = 'C';
else if (marks >= 45) grade = 'D';
else grade = 'F';
16. Factorial and reverse a number
// Factorial
int fact = 1;
for (int i = 1; i <= n; i++) fact *= i;
// Reverse
int rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
17. Switch case (with any 5 conditions)
int choice;
printf("Enter choice (1-5): ");
scanf("%d", &choice);
switch(choice) {
case 1: printf("Option 1"); break;
case 2: printf("Option 2"); break;
case 3: printf("Option 3"); break;
case 4: printf("Option 4"); break;
case 5: printf("Option 5"); break;
default: printf("Invalid");
}