0% found this document useful (0 votes)
10 views

C file

This document is a practical file for the Problem Solving Using C course submitted by a student at Forte Institute of Technology. It includes various C programming exercises covering topics such as compiling, debugging, decision control structures, loops, arrays, and graphics. Each section provides example code and explanations for implementing different programming concepts.

Uploaded by

mohitkulhary30
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

C file

This document is a practical file for the Problem Solving Using C course submitted by a student at Forte Institute of Technology. It includes various C programming exercises covering topics such as compiling, debugging, decision control structures, loops, arrays, and graphics. Each section provides example code and explanations for implementing different programming concepts.

Uploaded by

mohitkulhary30
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

A

Practical File of Problem Solving Using C Submitted in partial


fulfilment of the requirement of MCA I semester of
Forte Institute of Technology, Meerut
Session [2024-2025]
MCA I semester

Submitted by :-
Mohit
Roll No. 2401290140027

Forte Institute of Technology, Meerut


BMC151 : PROBLEM SOLVING USING C LAB PRACTICAL FILE

Q1. Write, compile, debug and execute programs in a C


programming environment.
Ans: Create a C file using a text editor or an IDE.
Example Program (hello.c):
#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}

• Compiling the C Program


Using GCC (Windows)
1. Open a terminal (Command Prompt in Windows or Terminal in
Linux/Mac).
2. Navigate to the directory where your C file is saved.
3. Run the following command:

gcc hello.c
This compiles hello.c and generates an executable file (hello.exe on
Windows, hello on Linux/Mac).

• Debugging the C Program


Using GCC with Debugging Flags
Compile with debugging enabled:
sh
gcc -g hello.c -o hello
Set breakpoints and debug using:
sh
(gdb) break main
(gdb) run
(gdb) next
(gdb) print variable_name
(gdb) quit.
Executing the Program
After successful compilation:
Windows (Command Prompt):
sh
hello.exe
Expected output:
Hello, World!
Q2. Write programs that incorporate use of variables, operators and
expressions along with data types.
Ans: Here are some C programs that incorporate variables,
operators, expressions, and data types:

1. Using Different Data Types


#include <stdio.h>

int main() {
int integer = 10;
float decimal = 5.5;
char character = 'A';

printf("Integer: %d\n", integer);


printf("Float: %.2f\n", decimal);
printf("Character: %c\n", character);

return 0;
}

2. Using Arithmetic Operators


#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
printf("Modulus: %d\n", a % b);

return 0;
}

3. Using Relational and Logical Operators


#include <stdio.h>

int main() {
int x = 10, y = 5;
printf("x > y: %d\n", x > y);
printf("x < y: %d\n", x < y);
printf("x == y: %d\n", x == y);
printf("x != y: %d\n", x != y);
printf("(x > y) && (x > 0): %d\n", (x > y) && (x > 0));
printf("(x > y) || (x < 0): %d\n", (x > y) || (x < 0));

return 0;
}
4. Using Assignment and Compound Operators
#include <stdio.h>

int main() {
int num = 10;
num += 5; // num = num + 5
printf("After += : %d\n", num);

num *= 2; // num = num * 2


printf("After *= : %d\n", num);

num %= 3; // num = num % 3


printf("After %%= : %d\n", num);

return 0;
}

5. Using Type Casting


#include <stdio.h>

int main() {
int a = 10, b = 3;
float result;
result = (float)a / b; // Type casting to float
printf("Result: %.2f\n", result);

return 0;
}

Q3. Write programs for solving problems involving use of decision


control structures and loops.
Ans: C programs that use decision control structures (if-else, switch-
case) and loops (for, while, do-while) to solve problems.

1. Check Whether a Number is Even or Odd (if-else)


#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is Even\n", num);
} else {
printf("%d is Odd\n", num);
}
return 0;
}
2. Find the Largest of Three Numbers (if-else)
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c)
printf("%d is the largest\n", a);
else if (b >= a && b >= c)
printf("%d is the largest\n", b);
else
printf("%d is the largest\n", c);
return 0;
}

3. Simple Calculator (switch-case)


#include <stdio.h>
int main() {
char op;
float num1, num2;
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &op);
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
switch (op) {
case '+': printf("Result: %.2f\n", num1 + num2); break;
case '-': printf("Result: %.2f\n", num1 - num2); break;
case '*': printf("Result: %.2f\n", num1 * num2); break;
case '/':
if (num2 != 0)
printf("Result: %.2f\n", num1 / num2);
else
printf("Error! Division by zero.\n");
break;
default: printf("Invalid operator\n");
}
return 0;
}

4. Print Numbers from 1 to N (for loop)


#include <stdio.h>
int main() {
int n, i;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("%d ", i);
} return 0; }
5. Print Multiplication Table (while loop)
#include <stdio.h>
int main() {
int num, i = 1;
printf("Enter a number: ");
scanf("%d", &num);
while (i <= 10) {
printf("%d x %d = %d\n", num, i, num * i);
i++;
}
return 0;
}
6. Find the Sum of Digits of a Number (do-while loop)
#include <stdio.h>
int main() {
int num, sum = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);

do {
digit = num % 10;
sum += digit;
num /= 10;
} while (num > 0);
printf("Sum of digits: %d\n", sum);
return 0;
}

Q 4. Write programs that involve the use of arrays, structures and


user defined functions.
Ans: C programs demonstrating the use of arrays, structures, and
user-defined functions.
Involve Arrays, Structures, and Functions are :
#include <stdio.h>

// Structure to store student data


struct Student {
char name[50];
int age;
float marks;
};

// Function to input student data


void inputStudent(struct Student s[], int n) {
for (int i = 0; i < n; i++) {
printf("\nEnter details for student %d\n", i + 1);
printf("Enter name: ");
scanf("%s", s[i].name);
printf("Enter age: ");
scanf("%d", &s[i].age);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
}
// Function to display student data
void displayStudent(struct Student s[], int n) {
printf("\nStudent Information:\n");
for (int i = 0; i < n; i++) {
printf("\nStudent %d:\n", i + 1);
printf("Name: %s\nAge: %d\nMarks: %.2f\n", s[i].name, s[i].age,
s[i].marks);
}
}
int main() {
int n;
printf("Enter the number of students: ");
scanf("%d", &n);
struct Student students[n]; // Array of structures
inputStudent(students, n);
displayStudent(students, n);
return 0;
Q5. Write a Program to implement conditional statements in C
language using graphics and file handling operations.
Ans: #include <stdio.h>
#include <graphics.h>
#include <conio.h>

void drawCircle() {
setcolor(RED);
circle(200, 200, 50);
outtextxy(180, 270, "Circle Drawn");
}
void drawRectangle() {
setcolor(BLUE);
rectangle(150, 150, 300, 250);
outtextxy(180, 270, "Rectangle Drawn");
}
void drawTriangle() {
setcolor(GREEN);
line(200, 100, 150, 200);
line(200, 100, 250, 200);
line(150, 200, 250, 200);
outtextxy(180, 270, "Triangle Drawn");
}
void saveChoice(int choice) {
FILE *file = fopen("shapes.txt", "a");
if (file == NULL) {
printf("Error opening file!");
return;
}
fprintf(file, "User chose option: %d\n", choice);
fclose(file);
}
int main() {
int gd = DETECT, gm;
int choice;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Initialize graphics
printf("Choose a shape to draw:\n");

printf("1. Circle\n");
printf("2. Rectangle\n");
printf("3. Triangle\n");

printf("Enter your choice: ");


scanf("%d", &choice);

saveChoice(choice); // Save user choice to file


cleardevice();
// Conditional statements to draw selected shape
switch (choice) {
case 1:
drawCircle();
break;
case 2:
drawRectangle();
break;
case 3:
drawTriangle();
break;
default:
outtextxy(150, 200, "Invalid Choice!");
break;
}

getch(); // Wait for user input before closing graphics


closegraph();
return 0;
}

OUTPUT: User chose: Circle


User chose: Triangle
User chose: Rectangle
Q6. Program to implement switch-case statement in C language
using graphics and file handling operations.
Ans: #include <stdio.h>
#include <graphics.h>
#include <conio.h>

void saveChoice(int choice) {


FILE *file = fopen("shapes.txt", "a"); // Open file in append mode
if (file == NULL) {
printf("Error opening file!");
return;
}
fprintf(file, "User chose option: %d\n", choice);
fclose(file);
}

void drawCircle() {
setcolor(RED);
circle(200, 200, 50);
outtextxy(180, 270, "Circle Drawn");
}

void drawRectangle() {
setcolor(BLUE);
rectangle(150, 150, 300, 250);
outtextxy(180, 270, "Rectangle Drawn");
}

void drawTriangle() {
setcolor(GREEN);
line(200, 100, 150, 200);
line(200, 100, 250, 200);
line(150, 200, 250, 200);
outtextxy(180, 270, "Triangle Drawn");
}

int main() {
int gd = DETECT, gm;
int choice;

initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Initialize graphics


mode
printf("Choose a shape to draw:\n");
printf("1. Circle\n");
printf("2. Rectangle\n");
printf("3. Triangle\n");
printf("Enter your choice: ");
scanf("%d", &choice);
saveChoice(choice); // Save the user's choice to a file
cleardevice(); // Clear the screen before drawing
switch (choice) {
case 1:
drawCircle();
break;
case 2:
drawRectangle();
break;
case 3:
drawTriangle();
break;
default:
outtextxy(150, 200, "Invalid Choice!");
printf("Invalid choice! Please run the program again.\n");
break;
}
getch(); // Wait for user input before closing graphics
closegraph();
return 0;
}
OUTPUT : User chose option: 1
User chose option: 3
User chose option: 2
Q7. Write programs to implement looping constructs in C language
using graphics and file handling operations.
Ans: #include <stdio.h>
#include <graphics.h>
#include <conio.h>
// Function to save user choice in a file
void saveChoice(const char *shape, int count) {
FILE *file = fopen("shapes.txt", "a"); // Open file in append mode
if (file == NULL) {
printf("Error opening file!");
return;
}
fprintf(file, "User chose to draw %d %s(s)\n", count, shape);
fclose(file);
}
// Function to draw circles using a loop
void drawCircles(int count) {
int i;
for (i = 0; i < count; i++) {
setcolor(RED);
circle(200, 100 + (i * 50), 30);
delay(300);
}
}
// Function to draw rectangles using a loop
void drawRectangles(int count) {
int i = 0;
while (i < count) {
setcolor(BLUE);
rectangle(150, 50 + (i * 60), 300, 100 + (i * 60));
delay(300);
i++;
}
}

// Function to draw triangles using a loop


void drawTriangles(int count) {
int i = 0;
do {
setcolor(GREEN);
line(200, 50 + (i * 70), 150, 120 + (i * 70));
line(200, 50 + (i * 70), 250, 120 + (i * 70));
line(150, 120 + (i * 70), 250, 120 + (i * 70));
delay(300);
i++;
} while (i < count);
}
int main() {
int gd = DETECT, gm;
int choice, count;

initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Initialize graphics


mode

printf("Choose a shape to draw multiple times:\n");


printf("1. Circles (For Loop)\n");
printf("2. Rectangles (While Loop)\n");
printf("3. Triangles (Do-While Loop)\n");
printf("Enter your choice: ");
scanf("%d", &choice);

printf("Enter how many times to draw the shape: ");


scanf("%d", &count);

cleardevice(); // Clear the screen before drawing

switch (choice) {
case 1:
drawCircles(count);
saveChoice("Circle", count);
break;
case 2:
drawRectangles(count);
saveChoice("Rectangle", count);
break;
case 3:
drawTriangles(count);
saveChoice("Triangle", count);
break;
default:
outtextxy(150, 200, "Invalid Choice!");
printf("Invalid choice! Please run the program again.\n");
break;
}

getch(); // Wait for user input before closing graphics


closegraph();
return 0;
}
OUTPUT:
User chose to draw 5 Circle(s)
User chose to draw 3 Rectangle(s)
User chose to draw 4 Triangle(s)
Q 8. Program to implement one-dimensional arrays in C language
using graphics and file handling operations.
Ans : #include <stdio.h>
#include <graphics.h>
#include <conio.h>

// Function to save array values to a file


void saveArrayToFile(int arr[], int size) {
FILE *file = fopen("array_data.txt", "a"); // Open file in append
mode
if (file == NULL) {
printf("Error opening file!\n");
return;
}
fprintf(file, "Array Elements: ");
for (int i = 0; i < size; i++) {
fprintf(file, "%d ", arr[i]);
}
fprintf(file, "\n");
fclose(file);
}

// Function to draw a bar chart based on array values


void drawBarChart(int arr[], int size) {
int i, x = 50, y = 400; // Initial position for bars

for (i = 0; i < size; i++) {


setfillstyle(SOLID_FILL, i + 1); // Different color for each bar
bar(x, y - arr[i], x + 40, y); // Draw bars based on array values
x += 60; // Move x position for next bar
}
}
int main() {
int gd = DETECT, gm;
int arr[5], i, size = 5;

initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Initialize graphics

printf("Enter 5 values for the array: \n");


for (i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
saveArrayToFile(arr, size); // Save array to file

cleardevice(); // Clear screen before drawing


drawBarChart(arr, size); // Draw the bar chart
outtextxy(50, 420, "Array Visualized as Bar Chart");
getch(); // Wait for user input before closing graphics
closegraph();
return 0;
}

OUTPUT: Array Elements: 10 30 20 40 25


Array Elements: 15 25 35 45 55

Q9. Program to implement structure in C language using graphics


and file handling operations.
Ans: #include <stdio.h>
#include <graphics.h>
#include <conio.h>

// Define a structure to store shape properties


struct Shape {
char name[20];
int color;
int x, y, size;
};
// Function to save shape details to a file
void saveShapeToFile(struct Shape s) {
FILE *file = fopen("shapes.txt", "a"); // Open file in append mode
if (file == NULL) {
printf("Error opening file!\n");
return;
}
fprintf(file, "Shape: %s, Color: %d, Position: (%d, %d), Size: %d\n",
s.name, s.color, s.x, s.y, s.size);
fclose(file);
}

// Function to draw a shape using graphics


void drawShape(struct Shape s) {
setcolor(s.color);
if (strcmp(s.name, "Circle") == 0) {
circle(s.x, s.y, s.size);
} else if (strcmp(s.name, "Rectangle") == 0) {
rectangle(s.x, s.y, s.x + s.size, s.y + s.size / 2);
} else if (strcmp(s.name, "Triangle") == 0) {
line(s.x, s.y, s.x - s.size, s.y + s.size);

line(s.x, s.y, s.x + s.size, s.y + s.size);


line(s.x - s.size, s.y + s.size, s.x + s.size, s.y + s.size);
}
}
int main() {
int gd = DETECT, gm;
struct Shape shape;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Initialize graphics
mode
printf("Enter shape name (Circle/Rectangle/Triangle): ");
scanf("%s", shape.name);
printf("Enter color (1-15): ");
scanf("%d", &shape.color);
printf("Enter x, y position: ");
scanf("%d %d", &shape.x, &shape.y);
printf("Enter size: ");
scanf("%d", &shape.size);
saveShapeToFile(shape); // Save shape details to file
cleardevice(); // Clear the screen before drawing
drawShape(shape); // Draw the shape
outtextxy(50, 420, "Shape Drawn Successfully!");
getch(); // Wait for user input before closing graphics
closegraph();
return 0;
}

OUTPUT : Shape: Circle, Color: 4, Position: (200, 200), Size: 50


Shape: Rectangle, Color: 2, Position: (100, 100), Size: 80
Q 10. Program to implement union in C language s using graphics
and file handling operations.
Ans: #include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <string.h>

// Define a union to store shape properties


union Shape {
struct {
int radius;
} circle;

struct {
int length, width;
} rectangle;

struct {
int base, height;
} triangle;
};

// Function to save shape details to a file


void saveShapeToFile(const char *shapeType, union Shape s) {
FILE *file = fopen("shapes.txt", "a"); // Open file in append mode
if (file == NULL) {
printf("Error opening file!\n");
return;
}
if (strcmp(shapeType, "Circle") == 0) {
fprintf(file, "Shape: Circle, Radius: %d\n", s.circle.radius);
} else if (strcmp(shapeType, "Rectangle") == 0) {
fprintf(file, "Shape: Rectangle, Length: %d, Width: %d\n",
s.rectangle.length, s.rectangle.width);
} else if (strcmp(shapeType, "Triangle") == 0) {
fprintf(file, "Shape: Triangle, Base: %d, Height: %d\n",
s.triangle.base, s.triangle.height);
}
fclose(file);
}

// Function to draw a shape using graphics

void drawShape(const char *shapeType, union Shape s) {


setcolor(WHITE);

if (strcmp(shapeType, "Circle") == 0) {
circle(200, 200, s.circle.radius);
outtextxy(180, 270, "Circle Drawn");
} else if (strcmp(shapeType, "Rectangle") == 0) {
rectangle(150, 150, 150 + s.rectangle.length, 150 +
s.rectangle.width);

outtextxy(180, 270, "Rectangle Drawn");


} else if (strcmp(shapeType, "Triangle") == 0) {
line(200, 100, 150, 200);
line(200, 100, 250, 200);
line(150, 200, 250, 200);
outtextxy(180, 270, "Triangle Drawn");
}
}

int main() {
int gd = DETECT, gm;
char shapeType[10];
union Shape shape;

initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Initialize graphics


mode

printf("Enter shape name (Circle/Rectangle/Triangle): ");


scanf("%s", shapeType);
} else if (strcmp(shapeType, "Triangle") == 0) {
printf("Enter base and height: ");
scanf("%d %d", &shape.triangle.base, &shape.triangle.height);
} else {
printf("Invalid shape!\n");
return 0;
}

saveShapeToFile(shapeType, shape); // Save shape details to file

cleardevice(); // Clear the screen before drawing


drawShape(shapeType, shape); // Draw the shape

getch(); // Wait for user input before closing graphics


closegraph();
return 0;
}

OUTPUT : Shape: Circle, Radius: 50


Shape: Rectangle, Length: 80, Width: 40
Shape: Triangle, Base: 100, Height: 50

You might also like