[go: up one dir, main page]

0% found this document useful (0 votes)
12 views6 pages

Compact C Programs

The document contains multiple C programming examples demonstrating various concepts such as pointer usage, string manipulation, data structures, and mathematical computations. Key examples include calculating sums, finding maximum and minimum values, string length, and checking for palindromes. Additional examples cover employee data management, Fibonacci series, prime checking, and Armstrong numbers.

Uploaded by

r.p.singh01857
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Compact C Programs

The document contains multiple C programming examples demonstrating various concepts such as pointer usage, string manipulation, data structures, and mathematical computations. Key examples include calculating sums, finding maximum and minimum values, string length, and checking for palindromes. Additional examples cover employee data management, Fibonacci series, prime checking, and Armstrong numbers.

Uploaded by

r.p.singh01857
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

// 1.

Access two integers using pointers and add them


#include <stdio.h>
void main() {
int a = 5, b = 10, *p1 = &a, *p2 = &b;
printf("Sum = %d\n", *p1 + *p2);
}

// 2. Greatest and smallest among three using pointers


void main() {
int a = 5, b = 10, c = 3, *p1 = &a, *p2 = &b, *p3 = &c;
int max = *p1, min = *p1;
if (*p2 > max) max = *p2;
if (*p3 > max) max = *p3;
if (*p2 < min) min = *p2;
if (*p3 < min) min = *p3;
printf("Max = %d, Min = %d\n", max, min);
}

// 3. Length of string using pointer


void main() {
char *str = "hello";
int len = 0;
while (*str++) len++;
printf("Length = %d\n", len);
}

// 4. Sum of elements in array using pointer


void main() {
int arr[] = {1, 2, 3, 4, 5}, *p = arr, sum = 0;
for (int i = 0; i < 5; i++) sum += *(p + i);
printf("Sum = %d\n", sum);
}

// 5. Substring in main string


char *findSubstring(char *str, char *substr) {
while (*str) {
char *p = str, *q = substr;
while (*p && *q && *p == *q) { p++; q++; }
if (!*q) return str;
str++;
}
return NULL;
}

// 6. Employee structure
#include <string.h>
struct emp {
int no;
char name[20];
float basic, hra, net;
};
void main() {
struct emp e[10];
int n;
float sum = 0;
printf("Enter no. of employees: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Emp %d: ", i+1);
scanf("%d %s %f %f", &e[i].no, e[i].name, &e[i].basic, &e[i].hra);
e[i].net = e[i].basic + e[i].hra;
sum += e[i].net;
}
float avg = sum / n;
printf("Employees with net salary > average:\n");
for (int i = 0; i < n; i++)
if (e[i].net > avg)
printf("%d %s %.2f %.2f %.2f\n", e[i].no, e[i].name, e[i].basic, e[i].hra, e[i].net
}

// 7. Date structure and add 45 days


struct date {
int d, m, y;
};
void main() {
struct date dt = {10, 4, 2025};
dt.d += 45;
while (dt.d > 30) {
dt.d -= 30;
dt.m++;
if (dt.m > 12) {
dt.m = 1;
dt.y++;
}
}
printf("Final date: %d-%d-%d\n", dt.d, dt.m, dt.y);
}

// 8. Fibonacci using recursion


int fib(int n) {
if (n <= 1) return n;
return fib(n-1) + fib(n-2);
}
void main() {
for (int i = 0; i < 10; i++) printf("%d ", fib(i));
}

// 9. Convert lowercase to uppercase


void main() {
char str[] = "hello world";
for (int i = 0; str[i]; i++)
if (str[i] >= 'a' && str[i] <= 'z') str[i] -= 32;
printf("%s\n", str);
}

// 10. Last occurrence of a character


void main() {
char str[] = "hello world", ch = 'l';
int last = -1;
for (int i = 0; str[i]; i++)
if (str[i] == ch) last = i;
printf("Last occurrence at index: %d\n", last);
}

// 11. String functions


void main() {
char a[50] = "Hello ", b[] = "World";
strcat(a, b); printf("Concat: %s\n", a);
printf("Length: %lu\n", strlen(a));
strcpy(a, b); printf("Copy: %s\n", a);
}

// 12. Word count


void main() {
char str[] = "This is a sentence";
int words = 0;
for (int i = 0; str[i]; i++)
if ((i == 0 || str[i-1] == ' ') && str[i] != ' ')
words++;
printf("Words: %d\n", words);
}

// 13. Reverse a string


void main() {
char str[] = "hello", rev[10];
int len = strlen(str);
for (int i = 0; i < len; i++)
rev[i] = str[len - 1 - i];
rev[len] = '\0';
printf("Reversed: %s\n", rev);
}

// 14. String length without library


void main() {
char str[] = "hello";
int len = 0;
while (str[len]) len++;
printf("Length: %d\n", len);
}

// 15. Substring check


void main() {
char str[] = "hello world", sub[] = "world";
if (strstr(str, sub)) printf("Substring found\n");
else printf("Not found\n");
}

// 16. Palindrome check


void main() {
char str[] = "madam";
int i, len = strlen(str), flag = 1;
for (i = 0; i < len/2; i++)
if (str[i] != str[len-i-1]) flag = 0;
printf(flag ? "Palindrome\n" : "Not Palindrome\n");
}

// 17. Sort names


void sort(char name[][20], int n) {
char temp[20];
for (int i = 0; i < n-1; i++)
for (int j = i+1; j < n; j++)
if (strcmp(name[i], name[j]) > 0) {
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}

// 18. Student marks


void main() {
int marks[3][5], i, j;
for (i = 0; i < 3; i++)
for (j = 0; j < 5; j++)
scanf("%d", &marks[i][j]);
for (i = 0; i < 3; i++) {
int sum = 0;
for (j = 0; j < 5; j++) sum += marks[i][j];
printf("Total of student %d = %d\n", i+1, sum);
}
for (j = 0; j < 5; j++) {
int sum = 0;
for (i = 0; i < 3; i++) sum += marks[i][j];
printf("Avg of subject %d = %.2f\n", j+1, sum/3.0);
}
}

// 19. Odd numbers in descending


void main() {
int N;
scanf("%d", &N);
for (int i = N%2==0 ? N-1 : N; i > 0; i -= 2)
printf("%d ", i);
}

// 20. Fibonacci series


void main() {
int a = 0, b = 1, n = 10;
printf("%d %d ", a, b);
for (int i = 2; i < n; i++) {
int c = a + b;
printf("%d ", c);
a = b; b = c;
}
}

// 21. Prime check


void main() {
int n = 29, flag = 1;
for (int i = 2; i*i <= n; i++)
if (n % i == 0) flag = 0;
printf(flag ? "Prime\n" : "Not Prime\n");
}

// 22. Binary to decimal


void main() {
int bin = 1101, dec = 0, base = 1;
while (bin) {
int r = bin % 10;
dec += r * base;
base *= 2;
bin /= 10;
}
printf("Decimal = %d\n", dec);
}

// 23. Reverse number


void main() {
int n = 1234, rev = 0;
while (n) {
rev = rev*10 + n%10;
n /= 10;
}
printf("Reversed = %d\n", rev);
}

// 24. sin(x) series


#include <math.h>
void main() {
float x = 1, sum = x;
for (int i = 3; i < 10; i += 2) {
int sign = (i/2)%2 == 0 ? -1 : 1;
sum += sign * pow(x, i) / tgamma(i+1);
}
printf("Sin(x) = %f\n", sum);
}

// 25. Palindrome number


void main() {
int n = 121, rev = 0, orig = n;
while (n) {
rev = rev*10 + n%10;
n /= 10;
}
printf(orig == rev ? "Palindrome\n" : "Not Palindrome\n");
}

// 26. Armstrong
void main() {
int n = 371, temp = n, sum = 0;
while (n) {
int d = n%10;
sum += d*d*d;
n /= 10;
}
printf(sum == temp ? "Armstrong\n" : "Not Armstrong\n");
}

// 27. Patterns using loops - skip for brevity due to length


// 28. Multiplication tables up to k - skip for brevity
// 29. Fibonacci in range - skip for brevity
// 30. Nth prime - skip for brevity
// 31. Nth Armstrong - skip for brevity
// 32. Search and count in matrix - skip for brevity
// 33. Multiply matrices - skip for brevity
// 34. Magic square check - skip for brevity
// 35. Symmetric matrix - skip for brevity
// 36. Trace and norm - skip for brevity

You might also like