C Lab Manual Complete
C Lab Manual Complete
C Lab Manual Complete
AIM:
ALGORITHM:
RESULT:
Thus the C Program to perform Arithmetic operators has been successfully executed and
verified.
1B. C PROGRAM TO IMPLEMENT FORMATTED I/O STATEMENTS.
AIM:
To write C program to demonstrate the uses of various formatted and unformatted input and
output functions
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
Char alphabh="A";
int number1= 55;
float number2=22.34;
printf(“char= %c\n”,alphabh);
printf(“int= %d\n”,number1);
printf(“float= %f\n”,number2);
getch();
clrscr();
retrun 0;
}
OUTPUT:
char =A
int= 55
float=22.340000
RESULT:
Thus the C program to demonstrate the uses of various formatted and unformatted input and
output functions has been successfully executed.
1C. PROGRAM TO ACCEPT CHARACTERS AND DISPLAY USING UNFORMATTED
I/O STATEMENTS
AIM:
To implement a C program to accept characters and display using unformatted I/O function.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
char x,y,z;
clrscr();
printf("Enter 1st character: ");
x = getchar();
printf("Enter 2nd character: ");
y = getche();
printf("\nEnter 3rd character: ");
z = getch();
printf("\nFirst character is ");
putchar(x);
printf("\nSecond character is ");
putch(y);
printf("\nThird character is ");
putchar(z);
}
OUTPUT:
RESULT:
Thus the C program to accept characters and display using unformatted I/O function has
been successfully executed and verified.
1D. PROGRAM TO EVALUATE THE EXPRESSIONS
AIM:
To implement a C program to evaluate the expression using datatypes.
ALGORITHM:
PROGRAM:
#include<stdio.h>
main()
{
int a=9,b=13,c=3;
float x,y,z;
x = a-b/3.0+c*2-1;
y = a-(float)b/(3+c)*(2-1);
z = a-((float)b/(3+c)*2)-1;
printf("x = %f\t\ty = %f\t\tz = %f",x,y,z);
getch();
}
OUTPUT:
x = 9.666667
y = 6.833333
z = 3.666667
RESULT:
Thus the C program to evaluate the expression using datatypes has been successfully
executed and verified.
EX.NO:2 DECISION-MAKING CONSTRUCTS: IF-ELSE, GOTO, SWITCH-
CASE, BREAK-CONTINUE
2A. PROGRAM TO FIND THE GREATER NUMBER BETWEEN TWO NUMBERS USING IF-
ELSE STATEMENT
AIM:
To implement the C Program to find the greater number between two numbers.
ALGORITHM:
Step 1: Start.
Step 2: Take two inputs (a and b) from the user.
Step 3: If a is greater than b then go to step 4 otherwise go to step 5
Step 4: Print a greater than b
Step 5: Print b greater than a
Step 6: Stop.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter two numbers :");
scanf("%d %d",&a,&b);
if (a>b)
printf("%d is greater",a);
else
printf("%d is greater",b);
getch();
}
OUTPUT:
Enter two numbers: 4 5
5 is greater
RESULT:
Thus the C Program to find the greater number between two numbers has been
successfully executed and verified.
2B. PROGRAM USING SWITCH CASE STATEMENT
AIM:
To implement the c program to calculate the area of a rectangle or circle or triangle by taking the
user’s choice.
ALGORITHM:
Step 1: Start
Step 3: Take input for choice and then for area variables from the user
Step 6: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int ac,ar,at,r,a,b,choice;
printf("Enter your choice\n”);
prinft(“A for area of circle\n”);
printf(“B for area of rectangle\n”);
printf(“C for area of triangle\n");
scanf("%c",&choice);
switch(choice)
{
case A:
printf("Enter radius: ");
scanf("%d",&r);
ac=3.14*3.14*r;
printf("Area of circle is: %d",ac);
break;
case B:
printf("Enter length and breadth:");
scanf("%d %d",&a,&b);
ar=a*b;
printf("Area of rectangle is: %d",ar);
break;
case C:
printf("Enter base and height: ");
scanf("%d %d",&a,&b);
at=0.5*a*b;
printf("Area of triangle is: %d",at);
break;
}
getch();
}
OUTPUT:
RESULT:
Thus the C program to calculate the area of a rectangle or circle or triangle by taking the
user’s choice has been successfully executed and verified.
2C. PROGRAM TO FIND SQUARES OF THE POSITIVE NUMBERS USING CONTINUE
STATEMENT
AIM:
To implement the C Program to find squares of the positive numbers.
ALGORITHM:
Step 1: Start.
Step 5: Stop
PROGRAM:
#include <stdio.h>
main()
{
int i, n, a, sq;
clrscr();
printf("\nHow many numbers you want to enter: ");
scanf("%d", &n);
for (i=1;i<=n; i++)
{
printf("\nEnter number: ");
scanf("%d", &a);
if(a<0)
continue;
sq = a * a;
printf("\nSquare = %d\n", sq);
}
getch();
}
OUTPUT:
RESULT:
Thus the program finds square of positive numbers only has been successfully executed and
verified.
2E. PROGRAM TO PRINT MULTIPLICATION TABLE USING GOTO STATEMENT
AIM:
ALGORITHM:
Step 1: Start.
Step 5: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int a,i=1;
clrscr();
printf("Enter the value of a: ");
scanf("%d",&a);
printf("\nMultiplication Table for %d\n",a);
printf(" \n");
start:
printf("%d x %d = %d\n",a,i,a*i);
i=i+1;
if(i<=10)
goto start;
getch();
}
OUTPUT:
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
RESULT:
Thus the C Program to print Multiplication Table using GOTO statements has been
successfully executed and verified.
2F. PROGRAM TO IMPLEMENT BREAK STATEMENT
AIM:
ALGORITHM:
Step 1: Start.
Step 2: local variable definition.
Step 3: while loop execution
Step 4: terminate the loop using break statement
Step 5: Stop
PROGRAM:
#include <stdio.h>
int main ()
{
int a = 10;
while( a < 20 )
{
printf("value of a: %d\n", a);
a++;
return 0;
}
OUTPUT:
Value of a: 10
Value of a: 11
Value of a: 12
Value of a: 13
Value of a: 14
Value of a: 15
RESULT:
Thus the C program using break statement has been successfully executed and verified.
EX.NO:3
LOOPS: FOR,WHILE, DO-WHILE
AIM:
ALGORITHM:
Step 1: Start.
Step 5: f = f * i.
Step 6: Go to step 3.
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main()
{
int i, a ,f = 1;
printf("Enter a number:\n");
scanf("%d", &a);
for (i = 1; i <= a; i++)
f=f*i;
printf("Factorial of %d is %d\n", a, f);
getch();
}
OUTPUT:
RESULT:
Thus the C program to find the factorial of a number has been successfully executed and
verified.
3B.PROGRAM TO FIND THE SUM OF 1 TO 10 USING DO-WHILE LOOP
AIM:
To implement the C program to find sum of 1 to 10 using the do-while loop statements.
ALGORITHM:
Step 1: Start.
Step 2: initializing the variable
Step 3: do-while loop execution
Step 4: incrementing operation of given variable
Step 5: Print the sum of the values
Step 6: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i = 1,a = 0;
do
{
a = a + i;
i++;
}
while(i <= 10);
printf("Sum of 1 to 10 is %d",a);
getch();
}
OUTPUT:
Sum of 1 to 10 is 55
RESULT:
Thus the C program to find sum of 1 to 10 using the do-while loop statements has been
successfully executed and verified.
3C. PROGRAM TO PRINT 1 TO 10 NUMBERS USING WHILE STATEMENT
AIM:
ALGORITHM:
Step 1: Start.
Step 2: initializing the variable
Step 3: while loop execution
Step 4: incrementing operation of given variable
Step 5: Print the sum of the values
Step 6: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n = 1;
getch();
}
OUTPUT:
1
2
3
4
5
6
7
8
9
10
RESULT:
Thus the C program to print 1 to 10 numbers using while statement has been successfully
executed and verified.
EX.NO:4 ARRAYS: 1D AND 2D, MULTI-DIMENSIONAL ARRAYS,
TRAVERSAL
AIM :
To implement a C program to populate an array with height of persons and finding how
many persons are above the average height.
ALGORITHM :
PROGRAM :
RESULT:
Thus the implementation of a C program to populate an array with height of persons and
finding how many persons are above the average height has been successfully executed and
verified.
4B. BODY MASS INDEX OF THE INDIVIDUALS
AIM :
ALGORITHM :
STEP 1.Start
STEP 2. Declare the necessary variables and arrays.
STEP 3. Get the values for the two arrays
STEP 4. Multiply the matrices
STEP5. Print the result
STEP 6:Stop.
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
RESULT:
Thus the implementation of a C program to multiply two matrices is executed successfully.
4C. WRITE A C PROGRAM TO PERFORM TRAVERSE OPERATION ON AN ARRAY.
AIM:
To implement the C program to traverse operation on an array
ALGORITHM:
Step 01: Start
Step 02: [Initialize counter variable. ] Set i = LB.
Step 03: Repeat for i = LB to UB.
Step 04: Apply process to arr[i].
Step 05: [End of loop. ]
Step 06: Stop
PROGRAM:
#include<stdio.h>
void main()
{
int i, size;
int arr[]={1, -9, 17, 4, -3};
RESULT:
Thus the C program to perform traverse operation on an array has been successfully
executed and verified.
EX.NO:5
STRINGS: OPERATIONS
AIM :
To implement a C program to reverse the given string without changing the position of
special characters.
ALGORITHM :
PROGRAM :
#include<stdio.h>
#include<string.h>
#include<conio.h>
int isAlphabet(char);
void reverse(char str[]);
void main()
{
char str[20] ;
clrscr();
printf("Enter the Input string:\n");
scanf("%s", str);
reverse(str);
printf("Reversed string: %s",str);
}
int isAlphabet(char x)
{
return ( (x >= 'A' && x <= 'Z') || (x >= 'a' && x <= 'z') );
}
void reverse(char str[])
{
int r = strlen(str) - 1,
l = 0;
char temp;
while (l < r)
{
if (!isAlphabet(str[l]))
l++;
else if(!isAlphabet(str[r]))
r--;
else
{
temp=str[l];
s tr[l]=str[r];
str[r]=temp;l++;
r--;
}
}
}
OUTPUT:
wel%come@
RESULT:
Thus the implementation of a C program to reverse the given string has been successfully
executed and verified.
5B. C PROGRAM TO FIND THE TOTAL NUMBER OF WORDS IN A STRING.
AIM :
To implement a C program to find the total number of words in a string.
ALGORITHM :
1. Declare the character variable ‘ch’ and an array s with a size 200.
2. Declare the integer variables i, n and count. Initialize count to 0.
3. Read a string as input and store it in the array s[].
4. Using for loop search for a space ‘ ‘ in the string and consecutively increment a variable
count.
5. Repeat the step-2 until the end of the string.
6. Increment the variable count by 1 and then print the variable count as output.
PROGRAM :
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char s[200], ch;
int count = 0, i, n;
clrscr();
printf("\nEnter the string\n");
for(i=0;ch!='\n';i++)
{
ch=getchar();
s[i]=ch;
}
s[i]=’\0’;
n=strlen(s);
for (i = 0;s[i] >= n ;i++)
{
if (s[i] == ' ')
count++;
}
printf("\nNumber of words in given string is: %d\n", count + 1);
getch();
}
OUTPUT :
RESULT:
Thus the implementation of a C programs to find the total number of words in a string has
been successfully executed and verified.
5C. C PROGRAM TO CAPITALIZE THE FIRST WORD OF EACH SENTENCE.
AIM :
To implement a C program to capitalize the first word of each sentence.
ALGORITHM :
PROGRAM :
#include <stdio.h>
#include <string.h>
#include<conio.h>
#define SIZE 100
void main()
{
char sentence[SIZE];
int i;
clrscr();
Output String:
RESULT:
Thus the C program to capitalize the first word of each sentence has been
successfully executed and verified.
5D. C PROGRAM TO REPLACE A GIVEN WORD WITH ANOTHER WORD IN THE
STRING.
AIM :
To implement a C program to replace a given word with another word in the string.
ALGORITHM :
1. Define a function replaceWord() with three arguments the input string, oldWord and
newWord and performs as follows:
a) Compute the length of both the words.
b) Count the number of times old word occur in the input string by using for loop and
strstr() function. Count is stored in ‘cnt’.
c) Allocate a memory for the output string as result[ ].
d) Compare the substring with the result and copy the newWord in the place of oldWord
in the result[ ].
e) Print the result[ ].
2. Read the sentence in str[ ].
3. Read the word to be replaced in c[ ].
4. Read the word with which it is to be replaced in d[ ].
5. Call the replaceWord() function from the main() by passing the arguments str[ ], c[ ] and
d[ ].
PROGRAM :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
while (*s)
{
// compare the substring with the result
if (strstr(s, oldW) == s)
{
strcpy(&result[i], newW);
i += newWlen;
s += oldWlen;
}
else
result[i++] = *s++;
}
result[i] = '\0';
return result;
}
void main()
{
char str[100];
char c[25];
char d[50];
char *result = NULL;
printf("Enter the sentence :\n");
gets(str);
printf("\nEnter the word to be replaced:\n");
gets(c);
printf("\nEnter the word with which it is to be replaced:\n");
gets(d);
printf("\nGiven string: %s\n", str);
result = replaceWord(str, c, d);
printf("\nNew String: %s\n", result);
free(result);
}
OUTPUT :
RESULT:
Thus the implementation of C programs to replace a given word with another word
has been successfully executed and verified.
FUNCTIONS: CALL, RETURN, PASSING PARAMETERS BY
EX.NO:6
(VALUE, REFERENCE), PASSING ARRAYS TO FUNCTION.
AIM:
To implement the C program to swap the values using function call.
ALORITHM:
STEP1.Start
STEP2. Declare the function and local variable.
STEP3. Read the function call to swap function.
STEP4. Compute the value and print the value.
STEP 5:Stop.
PROGRAM:
#include <stdio.h>
void swap(int x, int y);
int main ()
{
int a = 100;
int b = 200;
printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b );
swap(a, b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
return 0;
}
void swap(int x, int y)
{
int temp;
temp = x; /* save the value of x */
x = y; /* put y into x */
y = temp; /* put temp into y */
return;
}
OUTPUT:
RESULT:
Thus the C program to swap the values using function call has been successfully executed
and verified.
6B. C PROGRAM TO SORT LIST OF NUMBERS USING PASS BY REFERENCE
AIM :
To implement a C program to sort list of numbers using pass by reference.
ALGORITHM :
1. Define a function sort(int m, int x[]) , where m is the number of elements and x is the m
number of array elements to be sorted.
2. The function sort() will compare the nearby elements and sort the elements by swapping
them using a temporary variable ‘t’.
3. Read the number of elements in ‘n’ and also read ‘n’ number of array elements in arr[ ]
from main() function.
4. Print the actual array elements before sorting.
5. Call the sort(n, arr) function by passing the number of elements(n) and array elements
(arr[ ]). Here the reference of the array arr[ ] is automatically passed to the function sort().
6. Print the sorted array elements after sorting.
PROGRAM :
#include<stdio.h>
#include<conio.h>
void sort(int m, int x[]);
void main()
{
int arr[100],i,n,j,k;
clrscr();
sort(n,arr);
arr[1] = 2
arr[2] = 45
arr[3] = 13
arr[4] = 32
RESULT:
Thus the implementation of a C program to sort list of numbers using pass by reference has
been successfully executed and verified.
EX.NO:7
RECURSION
AIM :
To implement a C program to solve the Tower of Hanoi problem using Recursion.
ALGORITHM :
PROGRAM :
#include <stdio.h>
#include<conio.h>
void TOH(int, char, char, char);
void main()
{
int n;// defined to store the number disc
clrscr();
printf("Enter the number of disks : ");
scanf("%d", &n);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
TOH(n, 'A', 'C', 'B'); // A, B, C are tower
getch();
}
void TOH(int n, char fr, char tr, char ar)
{
if (n == 1)
{
printf("\n Move disk 1 from rod %c to rod %c", fr, tr);
return;
}
towerfun(n - 1, fr, ar, tr);
printf("\n Move disk %d from rod %c to rod %c", n, fr, tr);
towerfun(n - 1, ar, tr, fr);
}
OUTPUT :
RESULT:
Thus the implementation of a C program to solve the Tower of Hanoi problem using
recursion has beensuccessfully executed and verified.
POINTERS: POINTERS TO FUNCTIONS, ARRAYS, STRINGS,
EX.NO:8
POINTERS TO POINTERS, ARRAY OF POINTERS
AIM:
To implement a C program using pointer to functions.
ALGORITHM:
STEP1:Start
STEP2: Declare the function and local variable.
STEP3: Call the swap () function by passing the address of the two
Variables.
STEP4: Save the content of the first variable pointed by ‘a’ in the
Temporary variable.
STEP 5: Update the second variable (pointed by b) by the value
STEP 6: Stop.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void swap (int *a, int *b);
int main()
{
int m = 25;
int n = 100;
printf("m is %d, n is %d\n", m, n);
swap(&m, &n);
printf("m is %d, n is %d\n", m, n);
return 0;
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
OUTPUT:
m is 25, n is 100
m is 100, n is 25
RESULT:
AIM:
To implement a C program to an array of pointers to string.
ALGORITHM:
STEP1.Start
STEP2. Declaring the string pointer array as well as a char
array
STEP3. Taking inputs in char array as well.
STEP4. As copying them to the string pointer array.
STEP 5: used malloc to allocate dynamic memory. l+1 to
Store "\0".
STEP 6: Stop.
PROGRAM:
#include <String.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *names[5], a[5];
int l, i;
char *x;
printf(“Enter 5 strings:\n”);
for(i=0;i<5;i++)
{
for (i = 0; i < 5; i++)
scanf("%s", a);
l = strlen(a);
x = (char *)malloc(l + 1);
strcpy(x, a);
names[i] = x;
}
printf("The strings are:\n");
for (i = 0; i < 5; i++)
printf("%s\n", names[i]);
return 0:
}
OUTPUT:
Enter 5 strings:
Tree
Bowl
Hat
Mice
Toon
RESULT:
Thus the implementation of C program to an array of pointers to string has been successfully
executed and verified.
EX.NO:9 STRUCTURES: NESTED STRUCTURES, POINTERS TO
STRUCTURES, ARRAYS OF STRUCTURES AND UNIONS.
AIM :
ALGORITHM :
1. Define a structure ‘employee’ with empId, name[ ], basic, hra, da, ma, pf, insurance, gross
and net members.
2. Read the number of employees in ‘num’.
3. Allocate the memory for the ‘num’ number of structure variable
4. Read the structure members values for empId, name[ ], basic, hra, da, ma, pf, insurance.
5. Compute the gross and net salary and deduction amount.
6. Call the printSalary() function to print the salary slip of employees.
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct employee
{
int empId; char
name[32];
int basic, hra, da, ma;
int pf, insurance; float
gross, net;
};
/* prints payslip for the requested employee */
void printSalary(struct employee e1)
{
printf("Salary Slip of %s:\n", e1.name);
printf("Employee ID: %d\n", e1.empId);
printf("Basic Salary: %d\n", e1.basic);
printf("House Rent Allowance: %d\n", e1.hra);
printf("Dearness Allowance: %d\n", e1.da);
printf("Medical Allowance: %d\n", e1.ma);
printf("Gross Salary: %.2f Rupees\n", e1.gross);
printf("\nDeductions:\n");
printf("Provident fund: %d\n", e1.pf);
printf("Insurance: %d\n", e1.insurance);
printf("\nNet Salary: %.2f Rupees\n\n", e1.net);
}
void main()
{
int i, ch, num, flag, empID;
struct employee *e1;
printf("Enter the number of employees:");
scanf("%d", &num);
e1 = (struct employee *)malloc(sizeof(struct employee) * num);
printf("Enter your input for every employee:\n");
for (i = 0; i < num; i++)
{
printf("Employee ID:");
scanf("%d", &(e1[i].empId));
getchar();
printf("Employee Name:");
fgets(e1[i].name, 32, stdin);
e1[i].name[strlen(e1[i].name) - 1] = '\0';
printf("Basic Salary, HRA:");
scanf("%d%d", &(e1[i].basic), &(e1[i].hra));
printf("DA, Medical Allowance:");
scanf("%d%d", &(e1[i].da), &(e1[i].ma));
printf("PF and Insurance:");
scanf("%d%d", &(e1[i].pf), &(e1[i].insurance));
printf("\n");
}
Employee ID:
1002
Employee Name: Hari
Basic Salary, HRA: 32000 3200
DA, Medical Allowance: 550 760
PF and Insurance: 3000 2400
Deductions:
Provident fund: 3000
Insurance: 2400
RESULT:
AIM:
To write a C program to calculate the gross salary using structure within structure.
ALGORITHM:
1. Start
2. Struct employee e_code[4]=char e_name[20]=char e_bp=float
struct e_da=float e_hra=floatallo e_pf=float end struct.
3. Read the employee code, name, basic pay, da, hra, pf.
4. Print the employee code, name, basic pay, da, hra, pf.
5. Calculate gross salary=emp1.e_bp+emp1.allo.e_da+emp1.allo.e_hra+emp1.e_pf. Print the
gross salary.
6. Stop.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
struct employee
{
char e_code[4];
char e_name[20];
float e_bp;
struct
{
float e_da;
float e_hra;
}
float e_pf;
};
struct employee emp1;
float gross;
printf(“\nEnter the code:”);
gets(emp1.e_code);
printf(“\nEnter thename:”);
gets(emp1.e_name);
printf(“\nEnter the basic pay:”);
scanf(“%f”,&emp1.e_bp);
printf(“\nEnter the dearness allowance:”);
scanf(“%f”,&emp1.allo.e_da);
printf(“\nEnter the housrentallowance:”);
scanf(%f”,&emp1.allo.e_hra);
printf(“ \ nEnter the provident fund:”);
scanf (“%f”,&emp1.e_pf);
printf(“ \ nCode :”);
puts(emp1.e_code);
printf(“ \ nName :”);
puts(emp1.e_name);
printf(“ \ nBasicpay :%8.2f”,emp1.e_bp);
printf(“ \ nDearness allowance :%8.2f”,emp1.allo.e_da);
printf(“ \ nHouserent allowance:%8.2f”,emp1.allo.e_hra);
printf(“ \ nP rovidentfund :%8.2f”,emp1.e_pf);
gross=emp1.e_bp+emp1.allo.e_da+emp1.allo.e_hra+emp1.e_pf;
printf(“ \ nNetpay :%8.2f”,gross);
}
OUTPUT:
RESULT:
Thus the C program to calculate the gross salary using structure within structure was executed
successfully executed and output is verified.
9C. POINTERS TO STRUCTURES: PRINTING STUDENT DETAILS
AIM:
ALGORITHM:
1. Start
2. Declare student structure
3. Read student roll number, student name, branch, marks.
4. Print student roll number, student name, branch, marks.
5. Stop.
PROGRAM:
#include<stdio.h>
void main()
{
struct
{
int rollno;
char name[30];
char branch[4];
int marks;
}
*stud;
printf(“ \ n Enter Rollno :”);
scanf (“%d”, &stud - >rollno);
printf(“ \ n Enter Name :”);
scanf(“%s”,stud - >name);
printf(“ \ n Enter Branch:”);
scanf(“%s”,stud - >branch);
printf(“ \ n Enter Marks:”);
scanf(“%d”,&stud - >marks);
printf(“ \ n Roll Number:%d”,stud - >rollno);
printf(“ \ n Name:%s”,stud - >name);
printf(“ \ n Branch:%s”,stud - >branch);
printf(“ \ n Marks:%d”,stud - >marks);
getch();
OUTPUT:
Roll Number:1000
Name:Ebisha
Branch:CSE
Marks:90
RESULT:
Thus the C program to print student details using pointers and structures was executed successfully
executed and output is verified.
9D. ARRAY OF STRUCTURES:CALCULATING STUDENT MARK DETAILS
AIM:
ALGORITHM:
1. Start
2. Declare the structure with members.
3. Initialize the marks of the students
4. Calculate the subject total by adding student
[i].sub1+student[i].sub2+student[i].sub3.
5. Print the total marks of the students
6. Stop.
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct marks
{
int sub1;
int sub2;
int sub3;
int total;
};
void main()
{
int i;
struct marks student[3] = { {45, 67, 81, 0},{75, 53, 69, 0}, {57, 36, 71, 0}};
struct marks total;
for(i=0; i<=2; i++)
{
student[i]. total = student[i].sub1+ student[i].sub2+ student[i].sub3;
}
printf( “TOTAL MARKS \ n \ n”);
for(i=0; i<=2; i++)
printf(“student[%d]: %d \ n”, i+1, student[i].total);
}
OUTPUT:
TOTAL MARKS
student[1]: 193
student[2]: 197
student[3]: 164
RESULT:
Thus the C program to calculate the total marks using array of structures was executed successfully
executed and output is verified.
FILES: READING AND WRITING, FILE POINTERS, FILE
EX.NO:10
OPERATIONS, RANDOM ACCESS, AND PROCESSOR
DIRECTIVES.
AIM:
To write a C program to count number of characters and number of lines in a file
using file pointer.
ALGORITHM:
1. Start
2. Create file pointers
3. Enter the file name
4. Open the file with read mode
5. Till the end of file reached read one character at a time
(a) If it is newline character „\n‟, then increment no_of_lines countvalue by one.
(b) if it is a character then increment no_of_characters count value byone.
6. Print no_of_lines and no_of_characters values
7. Stop.
PROGRAM:
#include<stdio.h>
#include <string.h>
main ()
{
FILE *fp;
int ch, no_of_characters = 0, no_of_lines = 1;
char filename[20];
printf(“ \ n Enter the filename: “);
fp = fopen(filename, “r”);
if(fp==NULL)
{
printf(“ \ n Error opening the File”);
exit (1);
}
ch= fgetc(fp);
while(ch!=EOF)
{
if(ch ==‟ \ n‟);
no_of_lines++;
no_of_characters++;
ch = fgetc(fp);
}
if (no_of_characters >0)
printf(“ \ n In the file %s, there are %d lines and %d characters”, filename, no_ of_lines,
no_of_characters);
else
printf(“ \ n File is empty”);
fclose(fp);
}
OUTPUT:
RESULT:
Thus the C program to count number of characters and number of lines in a file using file pointer
was executed successfully executed and output is verified.
10B. PROGRAM TO COPY ONE FILE TO ANOTHER
AIM:
ALGORITHM:
1. Start
6. Read first file character by character and write the characters in the second file till end of file
reached.
8. Stop.
PROGRAM:
#include <stdio.h>
#include <conio.h>
main ()
{
RESULT:
Thus the C program to copy one file to another was executed successfully executed and output is
verified.
10C. PROGRAM TO RANDOMLY READ THE NTH RECORD OF A FILE
AIM:
ALGORITHM:
1. Start
5. From the file pointed by fp read a record of the specified record starting fromthe beginning of
the file
7. Stop.
PROGRAM:
#include<stdio.h>
#include <conio.h>
main ()
{
typedef struct employee
{
int emp_code;
char name[20];
int hra;
int da;
int ta;
};
FILE *fp;
struct employee e;
int result, rec_no;
fp = fopen (“employee.txt”, “rb”);
if(fp==NULL)
{
printf (“ \ n Error opening file”);
exit(1);
}
printf(“ \ n \ n Enter the rec_no you want to read: ”);
scanf(“%d”, &rec_no);
if(rec_no >= 0)
{
fseek(fp, (rec_no - 1)*sizeof (e), SEEK_SET);
result = fread(&e, sizeof(e), 1, fp);
if (result == 1)
{
printf(“ \ n EMPLOYEE CODE: %d”, e. Emp_code);
printf(“ \ n Name: %S”, e.name);
printf(“ \ n HRA, TA and DA: %d %d %d”, e.hra, e.ta, e.da);
}
else
printf(“ \ n Record Not Found”);
}
fclose(fp);
getch();
return 0;
}
OUTPUT:
RESULT:
Thus the C program to read nth record in file using random access method was executed
successfully executed and output is verified.
10D. PROGRAM TO COMPUTE AREA OF A CIRCLE USING PREPROCESSOR DIRECTIVES
AIM:
directives.
ALGORITHM:
1. Start
2. Define pi as 3.1415
4. Read radius
6. Print area
7. Stop.
PROGRAM:
#include <stdio.h>
#define PI 3. 1415
#definecircleArea(r) (PI*r*r)
int main()
{
float radius, area;
printf(“Enter the radius:”);
scanf(“%f”, &radius);
area = circleArea(radius);
printf(“Area = %.2f”, area);
return();
}
OUTPUT:
RESULT:
Thus the C program to compute area of a circle using pre-processor directives was executed
successfully executed and output is verified.