Ge23211 C Programming Lab
Ge23211 C Programming Lab
Ge23211 C Programming Lab
COURSE OBJECTIVES:
LIST OF EXPERIMENTS:
TOTAL:60 PERIODS
COURSE OUTCOMES:
CO1:DemonstrateknowledgeonCprogrammingconstructs. CO2:
CO3:DevelopprogramsinCusingarrays.
CO4:DevelopapplicationsinCusingstrings,pointers,functions.
CO5: Develop applications in C using structures.
CO6:DevelopapplicationsinCusingfileprocessing.
TEXTBOOKS:
1. ReemaThareja,“ProgramminginC”,OxfordUniversityPress,SecondEdition,2016.
2. Kernighan,B.and Ritchie,D.M,“The C Programming language”,
SecondEdition,Pearson Education, 2015.
REFERENCES:
1. PaulDeitelandHarveyDeitel,“CHowtoProgramwithanIntroductionto
C++”,Eighth Edition, Pearson Education, 2018.
2. YashwantKanetkar,LetusC,17thEdition,BPBPublications,2020.
3. Byron S. Gottfried, "Schaum's Outline of Theory and Problems of
Programming with C",McGraw-Hill Education, 1996.
4. PradipDey,ManasGhosh,“ComputerFundamentalsandProgramminginC”,Second
5. Edition,OxfordUniversityPress,2013.
6. AnitaGoelandAjayMittal,“ComputerFundamentalsandProgrammingin
C”,1stEdition, Pearson Education, 2013.
List of Experiments
Page Staff
Sl.No Ex.No. Date Title Of The Experiments
No. sign
1. 1a Programs using i/o statements
2. 1b Programs using operators and expressions
3. 2a Find odd or even using goto statement
4. 2b Finding leap year or not
5. 2c Creating a menu driven calculator
6. 2d Checking vowel or not using switch case
Print 1 to n numbers except multiples of 5
7. 2e
using continue statement
8. 3a Finding armstrong number
Sum of first n natural numbers using
9. 3b
looping
10. 3c Sum the cubes of series of n numbers
Finding sum of weights & sorting the
11. 4a
elements based on weights
Finding the persons having above average
12. 4b
height
13. 4c Finding body mass index using array
14. 4d Matrix addition and multiplication
Reverse string without changing the
15. 5
position of special character
16. 6a Swap the values using call by value
Sorting the values using pass by reference
17. 6b
Sum of array elements by passing to a
18. 6c
function
19. 7a Towers of Hanoi
20. 7b Finding factorial of a number
21. 8a Pointer to pointer
22. 8b Array of Pointer
23. 9a Employee Salary Slip
24. 9b Students Internal MarkSheet
25. 9c Accessing Union Members
26. 10a Sequential Access File
27. 10b Random Access File
28. 10c Processor Directives
EX.NO:1a PROGRAMS USING I/O STATEMENTS
AIM:
To Write a C program to use Input and Output Statements
ALGORITHM:
SYNTAX:
Unformatted I/O
◦ charvariable=getchar();
◦ charvariable=getche();
◦ charvariable=getch();
◦ gets(chararray_variable);
◦ putch(charvariable);
◦ puts(chararray_variable);
Formatted I/O
◦ scanf(“formatspecifiers”,addressofvariables);
◦ printf(“Anytext”);
◦ printf(“Anytext,formatspcifiers”,variables);
◦ printf(“formatspcifiers”,variables);
PROGRAM:
#include <stdio.h>
int main() {
//Start the program
printf("Starting the program...\n");
int num;
float fnum;
char ch;
char str[100];
// Use unformatted input statements
RESULT :
Thus the C program for I/O statements has been written & executed successfully.
EX.NO:1b PROGRAMS USING OPERATORS AND EXPRESSIONS
AIM:
To Write C programs using Operators and Expressions
ALGORITHM:
SYNTAX:
Expression = Operand operator Operand
Operator : Symbol
Operand : variable or Constant
var1=Var2+Var3(or)Var1=Value1+Value2;
(or)
Var1=Var2+value
Logical Operators:
returns True (1) or false(0) values-Condition1 && Condition2
Ternary Operator:
If condition is true True part will be executed.
Otherwise, False part will be executed - Var1= ( condition)? True part: False Part
PROGRAMS:
#include <stdio.h>
int main() {
// Step 1: Start the program
printf("Starting the program...\n");
// Step 2: Use arithmetic operators
int a = 10, b = 5;
printf("Arithmetic Operations:\n");
printf("Addition: %d + %d = %d\n", a, b, a + b);
printf("Subtraction: %d - %d = %d\n", a, b, a - b);
printf("Multiplication: %d * %d = %d\n", a, b, a * b);
printf("Division: %d / %d = %d\n", a, b, a / b);
printf("Modulus: %d %% %d = %d\n", a, b, a % b);
// Step 3: Use increment and decrement operators
printf("\nIncrement and Decrement Operations:\n");
printf("Original a: %d\n", a);
printf("a after increment: %d\n", ++a); // Pre-increment
printf("a after decrement: %d\n", --a); // Pre-decrement
// Step 4: Use comparison operators
printf("\nComparison Operations:\n");
printf("Is a > b? %d\n", a > b);
printf("Is a < b? %d\n", a < b);
printf("Is a >= b? %d\n", a >= b);
printf("Is a <= b? %d\n", a <= b);
printf("Is a == b? %d\n", a == b);
printf("Is a != b? %d\n", a != b);
// Step 5: Use bitwise operators
printf("\nBitwise Operations:\n");
printf("Bitwise AND: %d & %d = %d\n", a, b, a & b);
printf("Bitwise OR: %d | %d = %d\n", a, b, a | b);
printf("Bitwise XOR: %d ^ %d = %d\n", a, b, a ^ b);
printf("Bitwise NOT: ~%d = %d\n", a, ~a);
// Step 6: Use logical operators
printf("\nLogical Operations:\n");
printf("Logical AND (true && false): %d\n", 1 && 0);
printf("Logical OR (true || false): %d\n", 1 || 0);
printf("Logical NOT (!true): %d\n", !1);
// Step 7: Use ternary operator
printf("\nTernary Operator:\n");
int max = (a > b) ? a : b; // Find maximum
printf("Maximum of %d and %d is %d\n", a, b, max);
// Step 8: Stop the program
printf("Stopping the program...\n");
return 0;
}
OUTPUT:
Multiplication: 10 * 5 = 50
Division: 10 / 5 = 2
Modulus: 10 % 5 = 0
Comparison Operations:
Is a > b? 1
Is a < b? 0
Is a >= b? 1
Is a <= b? 1
Is a == b? 0
Is a != b? 1
Bitwise Operations:
Bitwise AND: 10 & 5 = 0
Bitwise OR: 10 | 5 = 15
Bitwise XOR: 10 ^ 5 = 15
Bitwise NOT: ~10 = -11
Logical Operations:
Logical AND (true && false): 0
Logical OR (true || false): 1
Logical NOT (!True): 0
Ternary Operator:
Maximum of 10 and 5 is 10
Stopping the program...
RESULT:
Thus the C program for expressions has been written & executed successfully.
EX.NO:2a FINDING ODD OR EVEN
AIM:
To Write a C program to find whether a given number is odd or even using goto
statement.
ALGORITHM:
SYNTAX:
if(condition)
statement;
else
statement;
PROGRAM:
#include <stdio.h>
#include<stdlib.h>
void main()
{
Int num;
printf("Enter a number\n");
scanf("%d", &num);
if(num%2==0)
goto even;
else
goto odd;
even:
printf("%d is even\n",num);
exit(0);
odd:
printf("%d is odd\n", num);
}
OUTPUT 1:
Enter a number
74
74 is even
OUTPUT 2:
Enter a number
35
35 is odd
RESULT:
Thus the C program for finding even or odd is written & executed Successfully.
EX. NO: 2b FINDING LEAP YEAR OR NOT
AIM:
To write a C program to find whether the given year is a leap year or not.
ALGORITHM:
PROGRAM:
#include <stdio.h>
int main() {
int y;
printf("Enter the year to check: ");
scanf("%d", &y);
return 0;
}
OUTPUT:
RESULT:
Thus, the C program for finding whether the year is a leap year or not is written and executed
successfully.
EX. NO: 2c CREATING A MENU DRIVEN CALCULATOR
AIM:
To write a C program for creating a menu-driven calculator using switch case.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <conio.h>
int main() {
int option, a, b, c;
clrscr(); // Clear the screen
printf("Enter the option:");
printf("\n1. ADD\t 2. SUB\t 3. MUL\t 4. DIV\n");
scanf("%d", &option);
switch(option) {
case 1:
printf("Enter two values: ");
scanf("%d%d", &a, &b);
c = a + b;
break;
case 2:
printf("Enter two values: ");
scanf("%d%d", &a, &b);
c = a - b;
break;
case 3:
printf("Enter two values: ");
scanf("%d%d", &a, &b);
c = a * b;
break;
case 4:
printf("Enter two values: ");
scanf("%d%d", &a, &b);
c = a / b;
break;
default:
printf("Choose the correct option\n");
return 0; // Exit if option is incorrect
}
return 0;
}
OUTPUT:
RESULT:
Thus, the C program for creating a menu-driven calculator using switch case is written and executed
successfully.
EX. NO: 2d CHECKING VOWEL OR NOT USING SWITCH CASE
AIM:
To write a C program for checking if a character is a vowel or not using switch case.
ALGORITHM:
PROGRAM:
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch); // Read a character input
switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is not a vowel.\n", ch);
}
return 0;
}
OUTPUT:
Enter a character: a
a is a vowel.
RESULT:
Thus, the C program for checking whether a character is a vowel or not using switch case is written
and executed successfully.
EX.NO:2e PRINT 1 TO N NUMBERS EXCEPT MULTIPLES OF 5
AIM:
To write a C program to print numbers from 1 to N, except multiples of 5, using the continue
statement.
ALGORITHM:
PROGRAM:
#include <stdio.h>
int main() {
int N;
printf("Enter a positive integer N: ");
scanf("%d", &N); // Read the value of N
// Loop from 1 to N
for (int i = 1; i <= N; i++) {
// Check if the number is a multiple of 5
if (i % 5 == 0) {
continue; // Skip the current iteration
}
// Print the number
printf("%d ", i);
}
OUTPUT:
RESULT:
Thus, the C program to print numbers from 1 to N, excluding multiples of 5 using the continue
statement, is written and executed successfully.
EX.NO:3a FINDING ARMSTRONG NUMBER
AIM:
To Write a C program to find whether a given number is Armstrong number or not.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <conio.h>
int main() {
int N, A, digit, cube, sum = 0;
printf("Enter a number: ");
scanf("%d", &N);
A = N; // Store the original number
// Loop to calculate the sum of cubes of digits
while (N > 0) {
digit = N % 10; // Get the last digit
cube = digit * digit * digit; // Calculate the cube of the digit
sum = sum + cube; // Add the cube to sum
N = N / 10; // Remove the last digit
}
// Check if the sum of cubes is equal to the original number
if (sum == A)
printf("%d is an Armstrong number.\n", A);
else
printf("%d is not an Armstrong number.\n", A);
getch();
return 0;
}
OUTPUT:
Enter a Number 153
RESULT:
Thus the C program to find whether a given number is Armstrong number or not is written &
executed successfully.
EX. NO: 3b SUM OF FIRST N NATURAL NUMBERS USING LOOPING
AIM:
To write a C program for calculating the sum of the first N natural numbers using different looping
constructs: while loop, do...while loop, and for loop.
ALGORITHM:
Using While Loop
1. Start the program.
2. Declare an integer variable `N` to hold the number of natural numbers.
3. Read the value of `N` from the user.
4. Initialize a variable `sum` to 0 and a counter variable `counter` to 1.
5. While the `counter` is less than or equal to `N`:
- Add `counter` to `sum`.
- Increment `counter`.
6. Print the result of `sum`.
7. Stop the program.
ALGORITHM:
Using Do...While Loop
1. Start the program.
2. Declare an integer variable `N` to hold the number of natural numbers.
3. Read the value of `N` from the user.
4. Initialize a variable `sum` to 0 and a counter variable `counter` to 1.
5. Use a do...while loop:
- Add `counter` to `sum`.
- Increment `counter`.
- Repeat until `counter` is greater than `N`.
6. Print the result of `sum`.
7. Stop the program.
ALGORITHM:
Using For Loop
1. Start the program.
2. Declare an integer variable `N` to hold the number of natural numbers.
3. Read the value of `N` from the user.
4. Initialize a variable `sum` to 0.
5. Use a for loop to iterate from 1 to `N`:
- Add the current loop variable to `sum`.
6. Print the result of `sum`.
7. Stop the program.
PROGRAM 1:
#include <stdio.h>
int main() {
int N, sum = 0, counter = 1;
PROGRAM 2:
#include <stdio.h>
int main() {
int N, sum = 0, counter = 1;
#include <stdio.h>
int main() {
int N, sum = 0;
OUTPUT:
RESULT:
Thus, the C program for calculating the sum of the first N natural numbers using while, do...while,
and for loops is written and executed successfully.
EX. NO: 3c SUM THE CUBES OF SERIES OF N NUMBERS
AIM:
To write a C program to sum the cubes of a series of N numbers.
ALGORITHM:
PROGRAM:
#include <stdio.h>
int main() {
int n;
int sum = 0;
OUTPUT:
RESULT:
Thus, the C program for summing the cubes of a series of N numbers has been executed successfully
and the output was verified.
EX. NO: 4a
AIM:
Write a C program to find the sum of weights for a given set of numbers like <10, 36, 54, 89, 12,
27>`. Find the sum of weights based on the following conditions:
1) 5 if it is a perfect cube.
2) 4 if it is a multiple of 4 and divisible by 6.
3) 3 if it is a prime number.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <math.h>
int main() {
int a[10] = {10, 36, 54, 89, 12, 27};
int w[10] = {0};
int N = 6, sum = 0;
// Calculate weights
for (int i = 0; i < N; i++) {
int A = a[i];
double cubroot = round(pow(A, 1.0/3));
int cube = (int)(cubroot * cubroot * cubroot);
if (A == cube) {
w[i] += 5; // Perfect cube
}
if (A % 4 == 0 && A % 6 == 0) {
w[i] += 4; // Multiple of 4 and divisible by 6
}
if (is_prime(A)) {
w[i] += 3; // Prime number
}
}
OUTPUT:
ELEMENT WEIGHT
10 0
36 4
54 0
89 3
12 4
27 5
RESULT:
Thus, the C program for finding the sum of weights and sorting the elements based on weights has
been executed successfully, and the output was verified.
Ex.No:4b FINDING THE PERSONS HAVING ABOVE AVERAGE HEIGHT
AIM:
Write a C program to populate an array with height of persons and find how many persons are
above the average height.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
float
height[20],avg;intN,i,s
um=0,count=0;
clrscr();
printf("Entertheno.ofpersons");
scanf("%d",&N);
printf("Enterthepersonsheightonebyone\n");
for(i=0;i<N;i++)
{
scanf("%f",&height[i]);
sum=sum+height[i];
}
avg=sum/N;
for(i=0;i<N;i++)
{
if(height[i]>avg)
{ count=count+1;}
}
printf("Totally%dPersonsarehavingaboveaverageheight",count);
getch();
}
OUTPUT:
RESULT:
Thus the program is written & executed Successfully.
EX.NO:4c FINDING BODY MASS INDEX USING ARRAY
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
floatheight[20],weight[20],BMI[20],HIM[20];
int i,j,N;
clrscr();
printf("\nEntertheNo.oftheelements\n");
scanf("%d",&N);
printf("\nEntertheHeight&Weightvalues\n");
for(i=0;i<N;i++)
{
scanf("%f%f",&height[i],&weight[i]);
HIM[i]=height[i]/100;
}
printf("\nPerson\tHeight\tWeight\tBMS\n");
for(i=0;i<N;i++)
{
BMI[i]=weight[i]/(HIM[i]*HIM[i]);
printf("\n%d\t%.2f\t%.2f\t%.2f\n",(i+1),HIM[i],weight[i],BMI[i]);
}
getch();
}
OUTPUT:
Enter No. of the elements 3
RESULT:
Thus the program is written & executed Successfully.
EX.NO:4d MATRIX ADDITION AND MULTIPLICATION
AIM:
ALGORITHM 1:
Matrix Addition
ALGORITHM 2:
Matrix Multiplication
PROGRAM:1
#include <stdio.h>
int main() {
int r, c, a[10][10], b[10][10], sum[10][10], i, j;
return 0;
}
PROGRAM:2
#include <stdio.h>
int main() {
int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2;
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);
// Taking input until 1st matrix columns is not equal to 2nd matrix rows
while (c1 != r2) {
printf("Error! Enter rows and columns again.\n");
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);
}
return 0;
}
OUTPUT :1
Enter elements:
Enter a11: 2
Enter a12: -3
Enter a13: 4
Enter a21: 5
Enter a22: 3
Enter a23: 5
Enter elements:
Enter a11: 3
Enter a12: 3
Enter a21: 5
Enter a22: 0
Enter a31: -3
Enter a32: 4
Output Matrix:
-2 12
19 7
RESULT:
Thus, the C program to calculate matrix addition and multiplication has been executed successfully
and the output was verified.
EX.NO: 5 REVERSE STRING WITHOUT CHANGING THE POSITION OF
SPECIAL CHARACTER
AIM:
To write a C program for reversing a string without changing the position of special characters.
(Example input: a@gh%;j and output: j@hg%;a)
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <conio.h>
void reverse(char*);
void main() {
char str[50];
clrscr();
printf("Enter a string\n");
scanf("%s", str);
printf("Input string: %s\n", str);
reverse(str);
printf("Output string: %s\n", str);
getch();
}
void reverse(char* str) {
int r = strlen(str) - 1, f = 0;
char t;
while (f < r) {
if (isalnum(str[f]) != 0 && isalnum(str[r]) != 0) {
t = str[r];
str[r] = str[f];
str[f] = t;
f++;
r--;
} else if (isalnum(str[f]) != 0 && isalnum(str[r]) == 0) {
r--;
} else if (isalnum(str[f]) == 0 && isalnum(str[r]) != 0) {
f++;
} else {
f++;
r--;
}
}
}
OUTPUT:
Enter a string: a@gh%;j
Input string: a@gh%;j
Output string: j@hg%;a
RESULT:
Thus, the C program to reverse a string without changing the position of special characters has been
executed successfully and the output was verified.
EX.NO: 6a SWAP THE VALUES USING CALL BY VALUE
AIM:
To write a C program for swapping two numbers using call by value.
ALGORITHM:
PROGRAM:
#include <stdio.h>
void swap(int, int);
int main() {
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(x, y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
RESULT:
Thus, the C program for swapping two numbers using call by value has been executed successfully
and the output was verified.
EX.NO: 6b SORT THE LIST OF NUMBERS USING PASS BY REFERENCE
AIM:
Write a C program to sort numbers using pass by reference.
ALGORITHM:
PROGRAM:
#include <stdio.h>
void sort(int*, int);
void main() {
int a[20], N, i;
printf("Enter the no. of elements\n");
scanf("%d", &N);
printf("Enter the Elements one by one\n");
for (i = 0; i < N; i++)
scanf("%d", &a[i]);
sort(a, N);
printf("\nSorted Order\n");
for (i = 0; i < N; i++)
printf("%d\t", a[i]);
getch();
}
void sort(int* x, int n) {
int t, i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1; j++) {
if (*(x + j) > *(x + (j + 1))) {
t = *(x + j);
*(x + j) = *(x + (j + 1));
*(x + (j + 1)) = t;
}
}
}
}
OUTPUT:
Sorted Order:
3 45 100
RESULT:
Thus, the C program for sorting the values using pass by reference has been executed successfully
and the output was verified.
EX.NO: 7a TOWERS OF HANOI
AIM:
To write a C program to solve the Towers of Hanoi using recursion.
ALGORITHM:
1. Start the program.
2. Create a function called `towers()`.
3. Read the number of disks.
4. Call the function `towers()` to move the disks from source to destination.
5. Stop the program.
PROGRAM:
#include <stdio.h>
int main() {
int num;
printf("Enter the number of disks: ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are:");
towers(num, 'A', 'C', 'B');
return 0;
}
OUTPUT:
RESULT:
The program has been executed successfully, and the sequence of moves has been verified.
EX.NO: 7b FINDING FACTORIAL OF A NUMBER
AIM:
To write a C program for finding the factorial of a number using recursion.
ALGORITHM:
1. Start the program.
2. Ask the user to enter an integer to find the factorial.
3. Read the integer and assign it to a variable.
4. Multiply each digit recursively from the integer down to 1.
5. The final value is the factorial.
6. Stop the program.
PROGRAM:
#include <stdio.h>
long factorial(int n) {
if (n == 0) return 1;
else return n * factorial(n - 1);
}
int main() {
int number;
long fact;
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
return 0;
}
OUTPUT:
Enter a number: 6
Factorial of 6 is 720
RESULT:
The C program for finding the factorial of a number using recursion has been executed successfully,
and the output was verified.
EX.NO: 8a POINTER TO POINTER
AIM:
To write a C program to print the value and address of a variable using pointer to pointer.
ALGORITHM:
1. Start the program.
2. Declare an integer variable and a pointer to it.
3. Declare a pointer to the pointer.
4. Print the addresses and values.
PROGRAM:
#include <stdio.h>
int main() {
int a = 10;
int *p;
int pp;
OUTPUT:
Address of a: 0x7ffeedc8a3c8
Address of p: 0x7ffeedc8a3c4
Value stored at p: 10
Value stored at pp: 10
RESULT:
The C program to print the value and address of a variable using pointer to pointer has been executed
successfully, and the output was verified.
EX.NO: 8b ARRAY OF POINTER
AIM:
To write a C program to read and display 5 students' names using an array of pointers.
ALGORITHM:
1. Start the program.
2. Declare an array of pointers to store names.
3. Read the names.
4. Display the names.
PROGRAM:
#include <stdio.h>
int main() {
const int MAX = 5;
char *names[MAX] = { "Zara Ali", "Hina Ali", "Nuha Ali", "Sara Ali", "Sameer Ali" };
int i;
return 0;
}
OUTPUT:
RESULT:
The C program to read and display 5 student names using an array of pointers has been executed
successfully, and the output was verified.
EX.NO: 9a EMPLOYEE SALARY SLIP
AIM:
To write a C program to generate an employee salary slip using structure and pointer.
ALGORITHM:
1. Start the program.
2. Create a structure called `Employee` with fields for employee ID, name, department, designation,
and salary.
3. Create a pointer to a structure.
4. Call a function to generate the salary slip for a particular employee.
5. Stop the program.
PROGRAM:
#include <stdio.h>
struct Employee {
char name[25];
int eid;
char dept[20];
char des[20];
int salary;
};
int main() {
struct Employee emp[20], *emp1;
int m, i;
return 0;
}
OUTPUT:
RESULT:
The C program to generate employee salary slips using structure and pointer has been executed
successfully, and the output was verified.
EX.NO:9b STUDENTS INTERNAL MARK SHEET
AIM:
To write a C program to compute internal marks of students for five different subjects using nested
structure.
ALGORITHM:
1. Start the program.
2. Create a structure called `Student` with fields for name, test marks for subjects, and internal marks.
3. Create a function called `calculateInternal()`.
4. Call the function to compute and print the marks.
5. Stop the program.
PROGRAM:
#include <stdio.h>
struct Student {
char name[20];
int marks[3][5]; // 3 tests for 5 subjects
int internalMarks[5];
};
s.marks[k][j];
}
s.internalMarks[j] = (total / 3) * 0.2; // Assuming max marks 20
}
int main() {
struct Student s;
int j, k;
return 0;
}
OUTPUT:
RESULT:
The C program to compute internal marks of students for five different subjects using nested
structure has been executed successfully, and the output was verified.
EX.NO:9c ACCESSING STUDENT DETAILS USING A UNION.
AIM:
To write a C program for accessing student details using a union.
ALGORITHM:
1. Start the program.
2. Define a union for student details, including fields for the student's ID, name, and grade.
3. Read the student details from the user.
4. Print the student details.
5. Stop the program.
PROGRAM:
#include <stdio.h>
#include <string.h>
int main() {
union Student student;
// Accessing student ID
printf("Enter Student ID: ");
scanf("%d", &student.id);
printf("Student ID: %d\n", student.id);
// Note: Only the last accessed member (grade) will have a valid value
printf("Accessing ID after Grade: %d (may be garbage)\n", student.id);
printf("Accessing Name after Grade: %s (may be garbage)\n", student.name);
return 0;
}
OUTPUT:
RESULT:
This C program successfully demonstrates how to use a union to access student details.
EX.NO: 10a SEQUENTIAL ACCESS FILE
AIM:
To write a C program to count the number of account holders whose balance is less than the
minimum balance using a sequential access file.
ALGORITHM:
1. Start the program.
2. Read choice to insert records or count minimum balance accounts.
3. If choice is 1, then:
- Open a data file in write mode.
- Read the number of records.
- Write the records into the file using `fprintf()`.
- Close the file.
4. If choice is 2, then:
- Open the file in read mode.
- Read the records one by one using `fscanf()` until reaching the end of the file.
- Check the account balance against the minimum balance.
- If the account balance is less than the minimum balance, display the account details.
- Close the file.
5. Stop the program.
PROGRAM:
#include <stdio.h>
void insert();
void count();
int main(void) {
int choice = 0;
while (choice != 3) {
printf("\n1. Insert records\n");
printf("2. Count min balance holders\n");
printf("3. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: insert(); break;
case 2: count(); break;
}
}
}
void insert() {
unsigned int account, i;
char name[30];
double balance;
FILE* cfPtr;
if ((cfPtr = fopen("clients.dat", "w")) == NULL) {
puts("File could not be opened");
} else {
int records, i = 0;
printf("Enter the number of records: ");
scanf("%d", &records);
while (i < records) {
printf("Enter the account, name, and balance: ");
scanf("%d %29s %lf", &account, name, &balance);
fprintf(cfPtr, "%d %s %.2f\n", account, name, balance);
i++;
}
fclose(cfPtr);
}
}
void count() {
unsigned int account;
char name[30];
double balance;
float minBal = 5000.00;
int count = 0;
FILE* cfPtr;
if ((cfPtr = fopen("clients.dat", "r")) == NULL) {
printf("File could not be opened");
} else {
printf("%-10s%-13s%s\n", "Account", "Name", "Balance");
fscanf(cfPtr, "%d %29s %lf", &account, name, &balance);
while (!feof(cfPtr)) {
if (balance < minBal) {
printf("%-10d%-13s%7.2f\n", account, name, balance);
count++;
}
fscanf(cfPtr, "%d %29s %lf", &account, name, &balance);
}
fclose(cfPtr);
printf("The number of account holders whose balance is less than the minimum balance:
%d\n", count);
}
}
OUTPUT:
1. Insert records
2. Count min balance holders
3. Exit
Enter choice: 1
Enter the number of records: 2
Enter the account, name, and balance: 1001 A 10000
Enter the account, name, and balance: 1002 B 300
1. Insert records
2. Count min balance holders
3. Exit
Enter choice: 2
Account Name Balance
1002 B 300.00
The number of account holders whose balance is less than the minimum balance: 1
RESULT:
The C program to count the number of account holders whose balance is less than the minimum
balance using a sequential access file has been executed successfully, and the output was verified.
EX.NO: 10b RANDOM ACCESS FILE
AIM:
To write a C program to update telephone details of an individual or a company in a telephone
directory using a random access file.
ALGORITHM:
1. Start the program.
2. Store the telephone details into a file.
3. Read the data and display it.
4. Enter the telephone number to be modified and the new number.
5. Use `fseek()` function to randomly access the record.
6. Copy the contents from the source file to the destination file.
7. Store the updated record into the new file.
8. Stop the program.
PROGRAM:
#include <stdio.h>
#include <string.h>
struct teledir {
int no;
char name[30];
};
int main() {
struct teledir t1, t2, t3;
int i, n, p, newp;
FILE *fp, *fp1;
fp = fopen("td.txt", "w");
fclose(fp);
fp = fopen("td.txt", "r");
if (t2.no == p) {
fseek(fp, -sizeof(struct teledir), SEEK_CUR);
t3.no = newp;
strcpy(t3.name, t2.name);
fwrite(&t3, sizeof(struct teledir), 1, fp1);
}
else {
fwrite(&t2, sizeof(struct teledir), 1, fp1);
}
}
fclose(fp);
fclose(fp1);
fp = fopen("td1.txt", "r");
while (fread(&t3, sizeof(struct teledir), 1, fp) != 0) {
printf("\t%d\t%s\n", t3.no, t3.name);
}
fclose(fp);
return 0;
}
OUTPUT:
RESULT:
The C program to update telephone details of an individual or a company into a telephone directory
using a random access file has been executed successfully, and the output was verified.
EX.NO: 10c PREPROCESSOR DIRECTIVES.
AIM:
To write a C program to find the greatest of three numbers using the ternary operator with
preprocessor directives.
ALGORITHM:
1. Start the program.
2. Define preprocessor directives for input.
3. Read three numbers from the user.
4. Use the ternary operator to determine the greatest number.
5. Print the greatest number.
6. Stop the program.
PROGRAM:
#include <stdio.h>
int main() {
int num1, num2, num3, greatest;
return 0;
}
OUTPUT:
RESULT:
This C program successfully finds the greatest of three numbers using the ternary operator defined
through preprocessor directives.