JMS INSTITUTE OF TECHNOLOGY, GHAZIABAD
(Session–2024–26)
MASTERS OF COMPUTER APPLICATION (MCA)
(I YEAR – I SEM)
PRACTICAL FILE
OF
Problem solving using C
Dr. A. P. J. Abdul Kalam Technical University,
Lucknow
Submitted to: Submitted by:
Mohd Owais Ghazi
Assistant Professor,MCA Name:
JMS INSTITUTE OF TECHNOLOGY
Roll no:
SOFTWAREENGINEERINGPRACTICALFILE
TABLEOFCONTENTS
EXP NAMEOFTHEEXPERIMENT:
NO:
1. Program to implement conditional statements in C language.
1
2. Program to implement switch-case statement in C language
2
3 3. Program to implement looping constructs in C language.
4. Program to perform basic input-output operations in C language.
4
5 5. Program to implement user defined functions in C language.
6 6. Program to implement recursive functions in C language.
7 7. Program to implement one-dimensional arrays in C language.
8 8. Program to implement two-dimensional arrays in C language.
9 9. Program to perform various operations on two-dimensional arrays in C language.
10 10. Program to implement multi-dimensional arrays in C language.
11 11. Program to implement string manipulation functions in C language.
12 12. Program to implement structure in C language.
13 13. Program to implement union in C language.
14 14. Program to perform file handling operations in C language.
15 15. Program to perform graphical operations in C language.
16 Program to perform Pointers
17 Program to perform Dynamic Memory Allocation
18 Program to perform Command Line Arguments
19 Program to perform Bitwise Operations
20 Program to perform Preprocessor Directives
21 Program to perform Typedef
22 Program to perform Enum
23 Program to perform Structures with Functions
C Programming
1. Conditional Statements
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}
return 0;
}
I # Program 2: Switch-Case Statement
#include <stdio.h>
int main() {
int choice;
printf("Enter your choice (1-5): ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("You chose option 1.\n");
break;
case 2:
printf("You chose option 2.\n");
break;
case 3:
printf("You chose option 3.\n");
break;
case 4:
printf("You chose option 4.\n");
break;
case 5:
printf("You chose option 5.\n");
break;
default:
printf("Invalid choice.\n");
break;
}
return 0;
}
# Program 3: Looping Constructs
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
# Program 4: Basic Input-Output Operations
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}
4
C Programming
# Program 5: User-Defined Functions
#include <stdio.h>
void greet() {
printf("Hello, World!\n");
}
int main() {
greet();
return 0;
}
4
C Programming
# Program 6: Recursive Functions
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n-1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d = %d\n", num, factorial(num));
return 0;
}
4
C Programming
# Program 7: One-Dimensional Arrays
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
return 0;
}
# Program 8: Two-Dimensional Arrays
#include <stdio.h>
int main() {
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("arr[%d][%d] = %d\n", i, j, arr[i][j]);
}
}
return 0;
}
4
C Programming
# Program 9: Operations on Two-Dimensional Arrays
#include <stdio.h>
int main() {
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int i, j;
printf("Original array:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
// Transpose
printf("Transposed array:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", arr[j][i]);
}
printf("\n");
}
return 0;
}
4
C Programming
# Program 10: Multi-Dimensional Arrays
#include <stdio.h>
int main() {
int arr[2][3][4] = {{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}},
{{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}}};
int i, j, k;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
for (k = 0; k < 4; k++) {
printf("arr[%d][%d][%d] = %d\n", i, j, k, arr[i][j][k]);
}
}
}
return 0;
}
4
C Programming
# Program 11: String Manipulation
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
char str3[20];
// Copying string
strcpy(str3, str1);
printf("Copied string: %s\n", str3);
// Concatenating strings
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
// String length
printf("Length of string: %d\n", strlen(str1));
return 0;
}
4
C Programming
# Program 12: Structures
#include <stdio.h>
struct Student {
int rollNo;
char name[20];
float marks;
};
int main() {
struct Student s1 = {1, "John Doe", 85.5};
printf("Roll No: %d\n", s1.rollNo);
printf("Name: %s\n", s1.name);
printf("Marks: %.2f\n", s1.marks);
return 0;
}
4
C Programming
# Program 13: Unions
#include <stdio.h>
union Data {
int i;
float f;
char str[20];
};
int main() {
union Data data;
data.i = 10;
printf("data.i : %d\n", data.i);
data.f = 220.5;
printf("data.f : %f\n", data.f);
strcpy(data.str, "C Programming");
printf("data.str : %s\n", data.str);
return 0;
}
4
C Programming
# Program 14: File Handling
#include <stdio.h>
int main() {
FILE *fp;
char c;
// Open file in read mode
fp = fopen("test.txt", "r");
if (fp == NULL) {
printf("Could not open file\n");
return 1;
}
// Read and display file contents
while ((c = fgetc(fp)) != EOF) {
printf("%c", c);
}
// Close file
fclose(fp);
return 0;
}
4
C Programming
# Program 15: Graphics (using graphics.h library)
#include <graphics.h>
#include <conio.h>
int main() {
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
// Draw a circle
circle(100, 100, 50);
// Draw a rectangle
rectangle(150, 150, 250, 250);
getch();
closegraph();
return 0;
}
# Program 16: Pointers
#include <stdio.h>
int main() {
int var = 20;
int *ptr;
ptr = &var;
printf("Value of var variable: %d\n", var);
printf("Address stored in ptr variable: %p\n", ptr);
printf("Value of *ptr variable: %d\n", *ptr);
return 0;
}
4
C Programming
# Program 17: Dynamic Memory Allocation
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
ptr = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
ptr[i] = i + 1;
}
for (int i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}
free(ptr);
return 0;
}
4
C Programming
# Program 18: Command Line Arguments
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Number of arguments: %d\n", argc);
for (int i = 0; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
4
C Programming
# Program 19: Bitwise Operations
#include <stdio.h>
int main() {
unsigned int a = 5; // 101
unsigned int b = 3; // 011
printf("a = %d\n", a);
printf("b = %d\n", b);
printf("a & b = %d\n", a & b); // 001
printf("a | b = %d\n", a | b); // 111
printf("a ^ b = %d\n", a ^ b); // 110
printf("~a = %d\n", ~a); // 11111111111111111111111111111010
return 0;
}
4
C Programming
# Program 20: Preprocessor Directives
#include <stdio.h>
#define PI 3.14159
int main() {
float radius;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
printf("Area of the circle: %.2f\n", PI * radius * radius);
return 0;
}
4
C Programming
# Program 21: Typedef
#include <stdio.h>
typedef struct {
int rollNo;
char name[20];
float marks;
} Student;
int main() {
Student s1;
s1.rollNo = 1;
strcpy(s1.name, "John Doe");
s1.marks = 85.5;
printf("Roll No: %d\n", s1.rollNo);
printf("Name: %s\n", s1.name);
printf("Marks: %.2f\n", s1.marks);
return 0;
}
4
C Programming
# Program 22: Enum
#include <stdio.h>
enum Day {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
};
int main() {
enum Day today = WEDNESDAY;
printf("Today is %d\n", today);
return 0;
}
4
C Programming
# Program 23: Structures with Functions
#include <stdio.h>
struct Rectangle {
int length;
int width;
};
int area(struct Rectangle r) {
return r.length * r.width;
}
int main() {
struct Rectangle r1 = {10, 20};
printf("Area of rectangle: %d\n", area(r1));
return 0;
}