Computational (1) Aritro
Computational (1) Aritro
Mullana, Ambala
(Established under Section 3 of the UGC Act 1956)
(NAAC Accredited Grade ‘A++’ University)
Practical File
2. W:
DFS: Signature (with
V: date):
4. W:
DFS:
V: Signature (with
date):
DOA: E: Marks (Out of
WAP to Show the Use S: 30):
of Switch Statement in
C Language
5. W:
DFS: Signature (with
V: date):
Conclusion
Understanding these fundamentals provides a solid foundation for exploring
more advanced topics in computer science and technology. Whether you're
interested in hardware, software development, or system design, these concepts
are essential.
PRACTICAL - 2
AIM:. WAP to calculate roots of quadratic equation.Tool Installation and
CODE:
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and different: %.2lf and %.2lf\n", root1, root2);
}
else if (discriminant == 0) {
root1 = -b / (2 * a);
printf("Roots are real and same: %.2lf\n", root1);
}
else {
realPart = -b / (2 * a);
imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex: %.2lf + %.2lfi and %.2lf - %.2lfi\n", realPart,
imaginaryPart, realPart, imaginaryPart);
}
return 0;
}
OUTPUT:
PRACTICAL -3
AIM: - WAP to Show the Use of Different Operators Precedence Order and
Associativity in C
CODE:
#include <stdio.h>int main() {
int a = 5, b = 10, c = 15;
int result;
result = a + b * c;
printf("Result of a + b * c: %d\n", result);
result = (a + b) * c;
printf("Result of (a + b) * c: %d\n", result);
result = a * b / c;
printf("Result of a * b / c: %d\n", result);
result = a + b - c;
printf("Result of a + b - c: %d\n", result);
return 0;
}
OUTPUT:
PRACTICAL -4
AIM: - WAP to demonstrate the use of conditional statement in C Language
CODE:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("\n %d", &num);
if (num > 0) {
printf("The number is positive.\n");
}
else if (num < 0) {
printf("The number is negative.\n");
}
else {
printf("The number is zero.\n");
}
return 0;
}
OUTPUT:
PRACTICAL -5
AIM: - WAP to Show the Use of Switch Statement in C Language
CODE:
#include <stdio.h>
int main() {
int choice;
printf("Enter a choice (1-3): ");
scanf("\n %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;
default:
printf("Invalid choice!\n");
break; }
return 0;
}
OUTPUT:
PRACTICAL -6
AIM: - Write a Program to Apply Different Types of Loops in C Language
CODE:
#include <stdio.h>
int main() {
int i;
printf("For loop:\n");
for (i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
printf("While loop:\n");
i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
printf("\n");
printf("Do-While loop:\n");
i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 5);
printf("\n");
return 0;
}
OUTPUT:
PRACTICAL -7
AIM: - WAP to Develop Different Patterns Using C Language
CODE:
#include <stdio.h>
int main() {
int rows, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
// Print spaces
for (j = 1; j <= rows - i; j++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
OUTPUT:
PRACTICAL -8
AIM: - WAP to Show the Use of Functions and Concept of Parameter Passing
in C Language
CODE:
#include <stdio.h>
int add(int a, int b);
int main() {
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
sum = add(num1, num2);
printf("Sum = %d\n", sum);
return 0;
}
int add(int a, int b) {
return a + b;
}
OUTPUT:
PRACTICAL -9
AIM: - WAP to Demonstrate the Use of Recursion in C Language
CODE:
#include <stdio.h>
int factorial(int n);
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Factorial of %d = %d\n", num, factorial(num));
return 0;
}
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
OUTPUT:
PRACTICAL -10
AIM: - Introduction About Arrays and Develop a Program to Multiply Two
Matrices
CODE:
#include <stdio.h>
int main() {
int a[10][10], b[10][10], result[10][10];
int r1, c1, r2, c2, i, j, k;
printf("Enter rows and columns for the first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns for the second matrix: ");
scanf("%d %d", &r2, &c2);
if (c1 != r2) {
printf("Matrix multiplication not possible.\n");
return 0;
}
printf("Enter elements of matrix 1:\n");
for (i = 0; i < r1; ++i)
for (j = 0; j < c1; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of matrix 2:\n");
for (i = 0; i < r2; ++i)
for (j = 0; j < c2; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
for (i = 0; i < r1; ++i)
for (j = 0; j < c2; ++j) {
result[i][j] = 0;
for (k = 0; k < c1; ++k) {
result[i][j] += a[i][k] * b[k][j];
}
}
printf("Resultant matrix:\n");
for (i = 0; i < r1; ++i)
for (j = 0; j < c2; ++j) {
printf("%d ", result[i][j]);
if (j == c2 - 1)
printf("\n");
}
return 0;
}
OUTPUT:
PRACTICAL -11
AIM: - WAP to Demonstrate the Use of Pointers in C Language
CODE:
#include <stdio.h>
int main() {
int var = 20;
int *ptr = &var;
printf("Value of var: %d\n", var);
printf("Address of var: %p\n", (void*)&var);
printf("Value pointed to by ptr: %d\n", *ptr);
printf("Address stored in ptr: %p\n", (void*)ptr);
return 0;
}
OUTPUT:
PRACTICAL -12
AIM: - WAP to Develop the Concept of File Handling
CODE:
#include <stdio.h>
int main() {
FILE *fptr;
char filename[100], c;
printf("Enter the filename to open for reading: ");
scanf("%s", filename);
fptr = fopen(filename, "r");
if (fptr == NULL) {
printf("Cannot open file \n");
return 0;
}
printf("Content of the file:\n");
c = fgetc(fptr);
while (c != EOF) {
printf("%c", c);
c = fgetc(fptr);
}
fclose(fptr);
return 0;
}
OUTPUT:
PRACTICAL -13
AIM: - WAP to Show the Use of Structure and Union in C Language
CODE:
#include <stdio.h>
#include <string.h>
struct Student {
int id;
char name[50];
float percentage;
};
union Data {
int intVal;
float floatVal;
char str[20];
};
int main() {
struct Student student1 = {1, "Alice", 87.5};
union Data data;
data.intVal = 10;
printf("Student ID: %d\n", student1.id);
printf("Student Name: %s\n", student1.name);
printf("Student Percentage: %.2f\n", student1.percentage);
printf("Union integer value: %d\n", data.intVal);
data.floatVal = 220.5;
printf("Union float value: %.2f\n", data.floatVal);
strcpy(data.str, "C Programming");
printf("Union string value: %s\n", data.str);
return 0;
}
OUTPUT:
PRACTICAL -14
AIM: - Develop a Calculator by Using Concepts of GUI in C Language
CODE:
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf("\n %c", &operator);
printf("Enter two operands: ");
scanf("\n %lf %lf", &num1, &num2);
switch (operator) {
case '+':
result = num1 + num2;
printf("Result: %.2lf + %.2lf = %.2lf\n", num1, num2, result);
break;
case '-':
result = num1 - num2;
printf("Result: %.2lf - %.2lf = %.2lf\n", num1, num2, result);
break;
case '*':
result = num1 * num2;
printf("Result: %.2lf * %.2lf = %.2lf\n", num1, num2, result);
break;
case '/':
if (num2 != 0)
result = num1 / num2;
else {
printf("Division by zero is not allowed.\n");
return 1;
}
printf("Result: %.2lf / %.2lf = %.2lf\n", num1, num2, result);
break;
default:
printf("Invalid operator.\n");
}
return 0;
}
OUTPUT: