Course Practical List-Programming for Problem Solving
1.Write a program that performs basic arithmetic operations (addition, subtraction, multiplication, and division) and demonstrates the use of different data types.
#include <stdio.h>
int a = 10, b =
3;
float div;
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %.2f\n", div);
}
2.Create a program that uses if, else, and switch
statements to implement a simple menu-driven application. Use loops (for,
while, and do-while) to repeat tasks.
#include <stdio.h>
int choice, a,
b;
printf("\n1.Add\n2.Subtract\n3.Multiply\n4.Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
}
case 1:
printf("Sum = %d\n", a + b); break;
case 2:
printf("Difference = %d\n", a - b); break;
case 3:
printf("Product = %d\n", a * b); break;
case 4:
printf("Exiting...\n"); break;
default: printf("Invalid choice\n");
}
} while (choice
!= 4);
}
Develop a program that calculates the factorial of a
number using both iterative and recursive functions.
#include <stdio.h>
if (n == 0)
return 1;
return n *
factRec(n - 1);
}
int n, fact =
1;
scanf("%d", &n);
fact *= i;
printf("Recursive Factorial: %d\n", factRec(n));
}
#include <stdio.h>
#include <string.h>
int a[5] = {5,
3, 1, 4, 2}, temp;
char s1[20] =
"Hello";
char s2[20] =
"World";
for (int i = 0;
i < 5; i++)
for (int j
= i + 1; j < 5; j++)
if
(a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
printf("Sorted Array: ");
for (int i = 0;
i < 5; i++)
printf("%d ", a[i]);
strcat(s1, s2);
printf("\nConcatenation: %s", s1);
printf("\nComparison: %d\n", strcmp("abc",
"abc"));
}
Implement
a program that uses pointers to create and manipulate dynamic arrays,
demonstrating the useof malloc, calloc, realloc, and free.
#include <stdio.h>
#include <stdlib.h>
int *a, n = 3;
for (int i = 0;
i < n; i++)
a[i] = i +
1;
a[3] = 4;
a[4] = 5;
printf("%d ", a[i]);
return 0;
}
Design a student record system using structures that store and display information such as name, roll number, and grades.
#include <stdio.h>
char name[20];
int roll;
float marks;
};
struct student
s;
scanf("%s
%d %f", s.name, &s.roll, &s.marks);
s.name,
s.roll, s.marks);
}
Write a program to read from and write to files, such as creating a simple text editor that performs basic file operations.
#include <stdio.h>
FILE *fp;
char text[50];
fprintf(fp,
"Hello File");
fclose(fp);
fscanf(fp,
"%s", text);
printf("File Content: %s", text);
fclose(fp);
}
Implement a singly linked list with operations like
insertion, deletion, and traversal.
#include <stdio.h>
#include <stdlib.h>
int data;
struct node
*next;
};
struct node
*head = NULL, *newnode, *temp;
int n = 3;
newnode =
(struct node *)malloc(sizeof(struct node));
newnode->data = i + 1;
newnode->next = NULL;
head =
newnode;
else {
temp =
head;
while
(temp->next)
temp = temp->next;
temp->next = newnode;
}
}
temp = head;
while (temp) {
printf("%d ", temp->data);
temp =
temp->next;
}
return 0;
Develop programs to
simulate stack operations (push, pop, peek) and queue operations (enqueue, dequeue)
using arrays and linked lists.
#include <stdio.h>
int stack[5], top = -1;
stack[++top] = 20;
printf("Popped: %d\n",
stack[top--]);
int queue[5], front = 0, rear = 0;
queue[rear++] = 5;
queue[rear++] = 15;
printf("Dequeued: %d\n",
queue[front++]);
}
Provide students with a
program containing intentional errors and inefficiencies. Have them use debugging
tools (like gdb) to find and fix the errors and optimize the code for better
performance.
int i;
for (i = 0; i
<= 5; i++) // error: should be i
< 5
printf("%d ", i);
return 0;
}