CP_lab_manual.doc
CP_lab_manual.doc
[Autonomous]
CHENNAI
Lab manual
1
0 0 3 1 2
OBJECTIVES:
● To develop programs in C using basic constructs.
● To develop applications in C using strings, pointers, functions, structures.
● To develop applications in C using file processing.
LIST OF EXPERIMENTS:
1. Programs using only I/O functions.
2. Programs to study operators and data types.
3. Programs based on control structures (IF, SWITCH CASE).
4. Programs using FOR and WHILE loops.
5. Programs using single dimensional arrays.
6. Programs using multi dimensional arrays.
7. Programs on Sorting and searching using arrays.
8. Programs based on String manipulations.
9. Programs based on User Defined Functions.
10. Programs using Functions with Parameters.
11. Programs using Storage Classes.
12. Programs to introduce Pointers.
13. Programs using Structures and Union.
14. Programs using Array of Structures.
15. Programs based on Files.
MINI PROJECT:
1. Create a ―Railway reservation system / Airline reservation system with the following
modules
- Booking
- Availability checking
- Cancellation
- Prepare chart
OUTCOMES:
Upon completion of the course, the students will be able to:
● Develop C programs for simple applications making use of basic constructs, arrays
and strings.
● Develop C programs involving functions, recursion, pointers, and structures.
● Design applications using sequential and random access file processing.
Total : 60 Periods
2
List of Experiments
3
AIM:
To Write a C program to use Input and Output Statements
PROCEDURE:
1. Start the program
2. Use Unformulated input & output statements to get char or string
● getchar(), getche() , getch() & gets()
3. Print the char or string using unformulated output statements
● putch(), putchar() & puts()
4. Use formatted Input statements to read different types of data
● scanf()
5. Print different types of data using formatted output statements
● printf()
6. Stop the program
SYNTAX:
Unformatted I/O
◦ charvariable=getchar();
◦ charvariable=getche();
◦ charvariable=getch();
◦ gets(chararray_variable);
◦ putch(charvariable);
◦ puts(chararray_variable);
Formatted I/O
◦ scanf(“format specifiers”,address of variables);
◦ printf(“Any text”);
◦ printf(“Any text,format spcifiers”,variables);
◦ printf(“format spcifiers”,variables);
PROGRAMS:
P1: Get & Display a single character using getch()
#include<stdio.h>
#include<conio.h>
main()
{
char c1;
c1=getch();
putch(c1);
}
4
P2: Get & Display a single character using getche()
#include<stdio.h>
#include<conio.h>
main()
{
char c1;
c1=getche();
putch(c1);
}
P3: Get & Display a single character using getchar()
#include<stdio.h>
#include<conio.h>
main()
{
char c1;
c1=getchar();
putch(c1);
}
P4: Get & Display a More characters using gets()
#include<stdio.h>
#include<conio.h>
main()
{
char name[20];
gets(name);
puts(name);
}
P5: Get & Display a student’s regno,Name and GPA using scanf & printf
#include<stdio.h>
#include<conio.h>
void main()
{
int regno;
char name[25];
5
float GPA;
printf(“\nEnter a Student Regno,Name & GPA\n”);
scanf(“%d%s%f”,®no,name,&GPA);
printf(“\n------------------------------------------------------\n”);
printf(“\n\t\t REG.NO \t\t NAME \t\t GPA \t\t\n”);
printf(“\n------------------------------------------------------\n”);
printf(“\t\t%d\t\t%s\t\t%f\t\t”,regno,name,GPA);
}
OUTPUT:
P1:
A
P2:
AA
P3:
Apple
A
P4:
Apple is good
Apple is good
P5:
RESULT:
Thus the program for I/O statements has been written & executed successfully.
EX.NO. :2 PROGRAMS USING EXPRESSIONS
6
AIM:
To Write C programs using Expressions
PROCEDURE:
1. Start the program
2. Use Arithmetic operators (+,-,*,/,%)
3. Use Increment & Decrement Operators (++,--)
4. Use comparison operators (>,<,>=,<=,==,!=)
5. Use Bit wise operators (~,^,|,&)
6. Use logical operators (||,&&,!)
7. Use Ternary Operator (?:)
8. Stop the program
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
PROGRAM:
P1: Arithmetic Operators
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr(); //to clear the output screen
a=20;
b=5;
printf(“Addition of %d + %d is = %d\n”,a,b,a+b);
printf(“Subtraction of %d - %d is = %d\n”,a,b,a-b);
printf(“Multiplication of %d * %d is = %d\n”,a,b,a*b);
printf(“Division of %d / %d is = %d\n”,a,b,a/b);
printf(“Modulo Division of %d % %d is = %d\n”,a,b,a%b);
getch();
}
7
P2: Bit wise Operators
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr(); //to clear the output screen
a=20;
b=5;
printf(“Bit wise OR of %d | %d is = %d\n”,a,b,a|b);
printf(“Bit wise AND of %d & %d is = %d\n”,a,b,a&b);
printf(“One’s complement of %d is = %d\n”,a,~a );
getch();
}
P3: Relational Operators
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr(); //to clear the output screen
a=20;
b=5;
printf(“a>b : %d\n”,a>b);
getch();
}
P4: Logical, Ternary Operators, Increment & decrement Operators
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr(); //to clear the output screen
a=20;
8
b=5;
c=(a>b)?(a+b):(a-b);
printf(“The value of c is =%d\n ”, c );
d=(a>b)&&(a>c);
printf(“The value of d is =%d\n ”, d);
d=(b<c)&&(a<c);
printf(“The value of d is =%d\n ”, d);
printf(“a++ & ++b is = %d,%d\n”,++a,b++);
getch();
}
OUTPUT:
Addition of 20 + 5 is = 25
Subtraction of 20 - 5 is = 15
Multiplication of 20 *5 is = 100
Division of 20 / 5 is = 4
Modulo Division of 20 % %d is = %d
Bit wise OR of 20 | 5 is = 21
Bit wise AND of 20 & 5 is = 4
One's complement of 20 is = -21
a>:1
RESULT:
Thus the C program for expressions has been written & executed successfully.
PROCEDURE:
1. Start the program
2. Read an input Value (Year)
3. If the Year is divisible by both 4 and 100 then,
4. Print the given year is LEAP YEAR
5. Otherwise, Print the given year is NOT A LEAP YEAR
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
printf(“Enter a year”);
scanf(“%d”,&year);
if( (year%100 !=0 && year%4 ==0) || year%400==0)
printf(“%d is a LEAP YEAR”, year );
else
printf(“%d is NOT a LEAP YEAR”, year);
}
OUTPUT:
Enter a year1900
1900 is NOT a LEAP YEAR
Enter a year2000
2000 is a LEAP YEAR
RESULT:
Thus the program is written & executed Successfully.
EX.NO.:3b CREATING A CALCULATOR
AIM:
10
To Write a C program to design a calculator to perform the operations, namely,
addition, subtraction, multiplication, division and square of a number.
ALGORITHM:
1. Start the program
2. Read an option to select the operation: 1.Add 2.Sub 3.Mul 4.Div 5.Square
3. Enter the input values according to the operation.
4. Use switch case to perform the operation.
5. Print the Result.
6. Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{ int option,a,b,c=0; clrscr();
printf(“Enter the option”);
printf(“\n 1.ADD \t 2.SUB \t 3.MUL \t 4.DIV \t 5.SQUARE\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”);
11
scanf(“%d%d”,&a,&b);
c=a/b;
break;
case 5:
printf(“Enter two values”);
scanf(“%d”,&a);
c=a*a;
break;
default:
printf(“Choose the correct option\n”);
}
printf(“Result is =%d”,c);
getch();
}
OUTPUT:
Enter the option
1.ADD 2.SUB 3.MUL 4.DIV 5.SQUARE
1
Enter two values 10 20
Result is=30
Enter the option
1.ADD 2.SUB 3.MUL 4.DIV 5.SQUARE
5
Enter a value2
Result is=4
Enter the option
1.ADD 2.SUB 3.MUL 4.DIV 5.SQUARE
9
Choose the correct option
Result is=0
RESULT:
Thus the program is written & executed Successfully.
PROGRAMS USING FOR AND WHILE LOOPS
12
AIM:
To Write a C program to print the table of given numbers using for loop.
ALGORITHM:
1. Start the program
2. Get input values for table and max value
3. Initialize outer for loop for assigning multiplication table
4. Initialize inner for loop for assigning max values
5. Print the multiplication values using inner loop
6. Display the output
7. Stop the program
PROGRAM:
#include <stdio.h>
int main() {
int i, j;
int table = 2;
int max = 5;
for (i = 1; i <= table; i++) { // outer loop
for (j = 0; j <= max; j++) { // inner loop
printf("%d x %d = %d\n", i, j, i*j);
}
printf("\n"); /* blank line between tables */
}}
Output:
1x0=0
13
1x1=1
1x2=2
1x3=3
1x4=4
1x5=5
2x0=0
2x1=2
2x2=4
2x3=6
2x4=8
2 x 5 = 10
RESULT:
Thus the program is written & executed Successfully.
14
AIM:
To Write a C program to find whether a given number is Armstrong number or not.
ALGORITHM:
1. Start the program.
2. Read an input value.
3. Split the digits using modulo division.
4. Cube the individual digits
5. Sum the cubes.
6. If the given number == sum of cubes,then
Print “ ARMSTRONG NUMBER”
7. Otherwise,
Print “ NOT AN ARMSTRONG NUMBER”
8. Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int N,A,digit,cube,sum=0;
printf(“Enter a Number”);
scanf(“%d”,&N);
A=N;
while(N>0 )
{
digit=N%10;
cube=(digit*digit*digit);
sum=sum+cube;
N=N/10;
}
if ( sum==A)
printf(“%d is Armstrong Number”, );
else
printf(“%d is not an Armstrong Number”, );
getch();
}
OUTPUT:
Enter a Number
153
15
153 is Armstrong Number
Enter a Number
123
123 is not an Armstrong Number
RESULT:
Thus the program is written & executed successfully.
16
EX.NO.: 5.a FINDING BODY MASS INDEX USING ARRAY
AIM:
To Write a C program to perform the following,
▪ Populate a two dimensional array with height and weight of persons
▪ Compute the Body Mass Index of the individuals.
ALGORITHM:
1. Start the program.
2. Read the Number of Persons.
3. Enter the height,weight values.
4. Read height in centi meters & weight in Kilograms of a person.
5. Height in Meter= height in cm/100;
6. Compute the body mass index value using the following formula
BMI=(weight in Kg)/(Height in Meters)2
7. Store the BMI values in a resultant array.
8. Print the results.
9. Stop the program
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
float height[20],weight[20],BMI[20],HIM[20];
int i,j,N;
clrscr();
printf("\nEnter the No.of the elements\n");
scanf("%d",&N);
printf("\n Enter the Height & Weight values\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]);
}
17
getch();
}
OUTPUT:
Enter No. of the elements
3
RESULT:
Thus the program is written & executed successfully.
18
EX.NO.: 6 MATRIX MULTIPLICATION USING ARRAY
AIM:
To write a program for performing matrix multiplication using multi-dimensional array.
ALGORITHM:
1. Start
2. Declare variables and initialize necessary variables
3. Enter the element of matrices by row wise using loops
4. Check the number of rows and column of first and second matrices
5. If number of rows of first matrix is equal to the number of columns of second matrix,
go to step 6. Otherwise, print matrix multiplication is not possible and go to step 3.
6. Multiply the matrices using nested loops.
7. Print the product in matrix form as console output.
8. Stop
PROGRAM:
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
19
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}
OUTPUT:
RESULT:
Thus the program is written & executed successfully.
20
Ex:7.a FINDING SUM OF WEIGHTS & SORTING THE ELEMENTS BASED ON
WEIGHTS
AIM:
Write a C program to find 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:
● Declare & initialize an integer array : a[]
● Declare an array called w[] to store the weight values of the respective elements.
● Assign the weight values to each element of that array by checking the conditions
◦ If a[i] is a perfect cube , assign weight 5 to w[i]
◦ If a[i] is multiples of 4 & divisible by 6 then assign 4 to w[i]
◦ If a[i] is a prime number then assign 3 to w[i]
● Add all weight values
● Print the sum of weights
● Sort the array elements in increasing order based on their weight values.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
double cubroot,cube;
int a[10]={ },w[10],i,j,N=6,count=0,A,sum=0;
clrscr();
for(i=0;i<N;i++)
{
A=a[i];
w[i]=0;
cubroot=ceil(pow(A,(1/3)));
cube=cubroot*cubroot*cubroot;
if(A==cube)
w[i]=w[i]+5;
if(A%4==0 && A%6==0)
w[i]=w[i]+4;
for(j=2;j<=A;j++)
{
if(A%j == 0)
count=count+j;
}
if(A==1 || A==count)
w[i]=w[i]+3;
}
21
printf("\n\nELEMENT\t\t\t\tWEIGHT \n\n");
for(i=0;i<N;i++)
{
printf("%d \t\t\t\t%d\n\n",a[i],w[i]);
sum=sum+w[i];
}
printf("The sum of Weights is = sum);
printf("Sorting of the Elements based on weight values\n");
for(i=0;i<N;i++)
for(j=0;j<N;j++)
if(w[j]>w[j+1])
{
t=w[j];
w[j]=w[j+1];
w[j+1]=t;
k=a[j];
a[j]=a[j+1];
a[j+1]=k;
}
for(i=0;i<N;i++)
{
printf("<%d,%d> \t",a[i],w[i]);
}
getch();
}
OUTPUT:
RESULT:
Thus the program is written & executed Successfully.
Ex.No:7.b FINDING THE PERSONS HAVING ABOVE AVERAGE HEIGHT
AIM:
22
Write a C program to populate an array with height of persons and find how many persons are
above the average height.
ALGORITHM:
1. Declare an array to store the height values of persons
2. Initialize the count value to 0.
3. Read the number of persons
4. Read the individual person’s height
5. Find the sum of heights
6. Find the average of heights
7. Check whether a person’s height is greater than the average height or not.
1. If Yes, then increase the count value
8. Print the count value.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
float height[20],avg;
int N,i,sum=0,count=0;
clrscr();
printf("Enter the no. of persons");
scanf("%d",&N);
printf("Enter the persons height on eby one\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 %d Persons are having above average height",count);
getch();
}
OUTPUT:
23
RESULT:
Thus the program is written & executed Successfully.
AIM:
24
To Write a C program to perform the following,
▪ Populate a two dimensional array with height and weight of persons
▪ Compute the Body Mass Index of the individuals.
ALGORITHM:
10. Start the program.
11. Read the Number of Persons.
12. Enter the height,weight values.
13. Read height in centi meters & weight in Kilograms of a person.
14. Height in Meter= height in cm/100;
15. Compute the body mass index value using the following formula
BMI=(weight in Kg)/(Height in Meters)2
16. Store the BMI values in a resultant array.
17. Print the results.
18. Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
float height[20],weight[20],BMI[20],HIM[20];
int i,j,N;
clrscr();
printf("\nEnter the No.of the elements\n");
scanf("%d",&N);
printf("\n Enter the Height & Weight values\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
25
Enter the Height & Weight values
152 58
122 28
135 40
RESULT:
Thus the program is written & executed successfully.
26
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:
1. Start the program.
2. Read a String.
3. Call a function to reverse a string.
4. Reverse function
● Find strlen
● Set forward & reverse pointers ( f=0,r=strlen-1)
● Check the following
o If both str[f] and str[l] are alphanumeric then swap these two chars in that
string and do f++,r--
o If str[f] is a special char then f++
o Id str[r] is a special char then r--;
o If both are special chars then f++ &r--
5. Print the reversed string.
6. Stop the program.
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];
27
str[r]=str[f];
str[f]=t;
f++;
r--;
}
else if(isalnum(str[f])!=0 && isalnum(str[r])==0)
{ r--; }
}
}
OUTPUT:
Enter a string:
a@gh%;j
Input string: a@gh%;j
Output string:j@hg%;a
RESULT:
Thus the program is written & executed Successfully.
Ex.No.9 Finding Sum of all Array Elements using User Define Functions.
28
AIM:
To find Sum of all Array Elements by passing array as an argument using User Define
Functions.
ALGORITHMS:
1. Start the program
2. Decide the user defined function and declare it.
3. Declare and initialize required array variables.
4. Get input values for array of elements.
5. Make function call to the user defined functions
6. Display the result
7. Stop the program
PROGRAM:
#include<stdio.h>
#define MAX_ELEMENTS 100
/*function declaration*/
int sumOfElements(int [],int);
int main()
{
int N,i,sum;
int arr[MAX_ELEMENTS];
printf("Enter total number of elements(1 to %d): ",MAX_ELEMENTS);
scanf("%d",&N);
if(N>MAX_ELEMENTS)
{
printf("You can't input larger than MAXIMUM value\n");
return 0;
}
else if(N<0)
{
printf("You can't input NEGATIVE or ZERO value.\n");
return 0;
}
/*Input array elements*/
printf("Enter array elements:\n");
for(i=0; i<N; i++)
{
printf("Enter element %4d: ",(i+1));
scanf("%d",&arr[i]);
}
/*function calling*/
sum=sumOfElements(arr,N);
printf("\nSUM of all Elements: %d\n",sum);
return 0;
}
29
/* function definition...
* Function : sumOfElements
* Argument(s) : int [], int - An integer array, Total No. of Elements
* Return Type : int - to return integer value of sum
*/
int sumOfElements(int x[],int n)
{
int sum,i;
sum=0;
for(i=0; i<n; i++)
{
sum += x[i];
}
return sum;
}
OUTPUT:
First Run:
Enter total number of elements(1 to 100): 5
Enter array elements:
Enter element 1: 11
Enter element 2: 22
Enter element 3: 33
Enter element 4: 44
Enter element 5: 55
Second Run:
Enter total number of elements(1 to 100): 120
You can't input larger than MAXIMUM value
Third Run:
Enter total number of elements(1 to 100): -10
You can't input NEGATIVE or ZERO value.
RESULT:
Thus the program is written & executed Successfully.
Ex.No:10.a FUNCTIONS WITH ARGUMENTS AND WITH RETURN VALUES
30
AIM:
To write a program to illustrate a function with arguments and with return values.
ALGORITHM:
Step 1: Start the program.
Step 2: Enter the two numbers.
Step 3: Call the function with two arguments passed to it.
Step 4: Find GCD of two numbers in the calling function.
Step 5:Return answer to the called function from the calling function.
Step 6: Print the GCD value in the main function.
Step 7: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int ans,a,b;
clrscr();
printf("n Enter value of a and b :");
scanf("%d%d",&a,&b);
ans=gcd(a,b);
printf("n GCD of %d, %d is %d",a,b,ans);
getch();
}
int gcd(int x, int y)
{
int t;
while(x!=y)
{
if(x>y)
x=x-y;
else if(x<y)
y=y-x;
}
return x;
}
OUTPUT:
31
RESULT:
Thus the C program to illustrate a function with arguments and with return values is written
and executed successfully.
32
Ex.No.10.b Swapping of two numbers using call by value
AIM:
To write a C program to swap two numbers using call by value.
ALGORITHM:
Step 1: Start the program.
Step 2: Set a ← 10 and b ← 20
Step 3: Call the function swap(a,b)
Step 3a: Start function.
Step 3b: Assign t ← x
Step 3c: Assign x ← y
Step 3d: Assign y ← t
Step 3e: Print x and y.
Step 3f: End function.
Step 4: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void swap(int , int); // Declaration of function
void main( )
{
int a = 10, b = 20 ;
clrscr();
printf("n Before swapping");
printf ( "n a = %d b = %d", a, b ) ;
swap(a,b);// call by value : a and b are actual parameters
getch();
}
void swap( int x, int y ) // x and y are formal parameters
{
int t ;
t=x;
x=y;
y=t;
printf("n After swapping");
printf ( "n a = %d b = %d", x, y ) ;
}
OUTPUT:
33
RESULT:
34
Thus the C program to swap two numbers using call by value is written and executed
successfully.
Ex.No:10.c Swap two numbers using call by reference
AIM:
To write a C program to swap two numbers using call by reference.
ALGORITHM:
Step 1: Start the program.
Step 2: Set a ← 10 and b ← 20
Step 3: Call the function swap(&a,&b)
Step 3a: Start fuction
Step 3b: Assign t ← *x
Step 3c: Assign *x ← *y
Step 3d: Assign *y ← t
Step 3e: End function
Step 4: Print x and y.
Step 5: Stop the program.
PROGRAM :
#include<stdio.h>
#include<conio.h>
void swap(int *,int *); // Declaration of function
void main( )
{
int a = 10, b = 20 ;
clrscr();
printf("n Before swapping");
printf( "n a = %d b = %d", a, b );
swap(&a,&b); // call by reference
printf("n After swapping");
printf( "n a = %d b = %d", a, b );
getch();
}
void swap( int *x, int *y )
{
int t;
t = *x;
*x = *y;
*y = t;
}
35
OUTPUT:
RESULT:
36
Thus the C program to swap two numbers using call by reference is written and executed
successfully.
AIM
To create a program to demonstrate all available storage class specifier in C language.
ALGORITHM:
PROGRAM:
#include <stdio.h>
int x;
void autoStorageClass()
{
printf("\nDemonstrating auto class\n\n");
auto int a = 32;
printf("Value of the variable 'a'" " declared as auto: %d\n", a);
printf("--------------------------------");
}
void registerStorageClass()
{
printf("\nDemonstrating register class\n\n");
register char b = 'G';
printf("Value of the variable 'b'" " declared as register: %d\n", b);
printf("--------------------------------");
}
void externStorageClass()
{
printf("\nDemonstrating extern class\n\n");
extern int x;
printf("Value of the variable 'x'" " declared as extern: %d\n", x);
x = 2;
printf("Modified value of the variable 'x'" " declared as extern: %d\n", x);
printf("--------------------------------");
}
void staticStorageClass()
{
int i = 0;
printf("\nDemonstrating static class\n\n");
37
printf(""Declaring 'y' as static inside the loop.\n" "But this declaration will occur only"
" once as 'y' is static.\n" "If not, then every time the value of 'y' " "will be the declared
value 5" as in the case of variable 'p'\n"");
printf("\nLoop started:\n");
printf("\nLoop ended:\n");
printf("--------------------------------");
}
int main()
{
38
Output:
A program to demonstrate Storage Classes in C
Loop started:
Loop ended:
——————————–
39
RESULT:
Thus the C program for demonstrating various storage class specifiers was successfully
implemented and output was verified.
Ex.No. 12. Programs to count vowels and consonants in a string using pointer
AIM:
To create a C program to count vowels and consonants in a string using pointer.
ALGORITHM:
1. Start the program
2. Declare pointer and other required variables
3. Get input string to count vowels and constants
4. Check individual char matches with constants or vowels to make count
5. Print the number of vowels and constants
6. Stop the program.
PROGRAM:
#include <stdio.h>
int main()
{
char str[100];
char *ptr;
int cntV,cntC;
printf("Enter a string: ");
gets(str);
//assign address of str to ptr
ptr=str;
cntV=cntC=0;
while(*ptr!='\0')
{
if(*ptr=='A' ||*ptr=='E' ||*ptr=='I' ||*ptr=='O' ||*ptr=='U' ||*ptr=='a' ||*ptr=='e' ||*ptr=='i'
||*ptr=='o' ||*ptr=='u')
cntV++;
else
cntC++;
//increase the pointer, to point next character
ptr++;
}
printf("Total number of VOWELS: %d, CONSONANT: %d\n",cntV,cntC);
return 0;
}
OUTPUT:
40
RESULT:
Thus the C program using pointer was implemented to check number of vowels and constants
in a given string and output verified successfully.
AIM:
To Write a C program to generate employee salary slip using structure & pointer.
ALGORITHM:
1. Start the program.
2. Create a structure called Employee with empid,empname,dept,designation & salary
fields.
3. Create a pointer to a structure.
4. Call a function to generate the Salary slip of particular employee
5. Stop the program.
PROGRAM:
#include<stdio.h>
struct employee
{
char ename[25];
int eid;
char edes[20];
char edept[20];
int esal;
};
41
}
}
}
void main()
{
struct employee emp[20],*emp1;
int m,i;
printf("Enter the no. of employee details");
scanf("%d",&m);
printf("\nEnter employee id, name, department, designation & salary\n");
for(i=0;i<m;i++)
{
scanf("%d%s%s%s%d",&emp[i].eid,&emp[i].ename,&emp[i].edes,&emp[i].e
dept,&emp[i].esal);
}
emp1=emp;
salaryslip(emp1,m);
getch();
}
OUTPUT
Enter the no. of employee details2
42
RESULT:
AIM:
To Write a C program to compute internal marks of students for five different subjects
using structures and functions.
ALGORITHM:
1. Start the program.
2. Create a structure called student with the following fields,
● Name
● Test marks for subjects
● Internal mark
3. Create a function called internal
4. Call a function to compute and print the marks
5. Stop the program.
PROGRAM:
#include<stdio.h>
struct student
{
char name[20];
int t[15][15];
int mark[5];
};
void internal( struct student);
void main()
{
struct student s;
int j,k;
clrscr();
for(j=0;j<3;j++)
{
printf("Enter the internal test %d marks for five subjects:\t",j+1);
for(k=0;k<5;k++)
scanf("%d",&s.t[j][k]);
}
internal(s);
getch();
43
}
void internal(struct student s1)
{
int i,j,k,sum[20],c=0;
for(j=0;j<5;j++)
{
c=0;
for(k=0;k<3;k++)
{
c=c+s1.t[k][j];
}
s1.mark[j]=((c/3)/5);
}
for(i=0;i<5;i++)
printf("\nSubject %d Internal Mark (max. marks 20)= %d",i+1,s1.mark[i]);
}
OUTPUT:
Enter the internal test 1 marks for five subjects: 10 10 10 10 10
Enter the internal test 2 marks for five subjects: 70 80 90 10 80
Enter the internal test 3 marks for five subjects: 90 90 90 90 90
44
RESULT:
AIM:
Write a C program to count the number of account holders whose balance is less than
the minimum balance using sequential access file.
ALGORITHM:
1. Start the program
2. Read choice to insert records & count minimum balance account
3. If choice is 1, then
● Open a dat file in write mode
● Read the No. of records
● Write the records into the file using fprintf() function
● Close the file
4. If Choice is 2, then
● Open the file in Read mode
● Read the records one by one using fscanf(0 function until reach the end of file.
● Check the account balance with min bal.
● If account balance is less than min balance, then 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:");
45
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;
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");
46
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", count);
}
}
OUTPUT:
1 insert records
2 Count min balance holders
3 Exit
Enter choice:1
Enter the No. 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
1 insert records
2 Count min balance holders
3 Exit
Enter choice: 3
47
RESULT:
AIM:
To Write a C program to update telephone details of an individual or a company into a
telephone directory using random access file.
ALGORITHM:
1. Start the program
2. Store the telephone details into a file
3. Read the data & Display it
4. Enter the telephone number to be modified & the new number
5. Use fseek() function to randomly access the record
6. Copy the contents from source file to destination file
7. Store the updated record into the new file
8. Stop the program
PROGRAM:
#include<stdio.h>
struct teledir
{
int no;
char name[3];
};
void main()
{
struct teledir t1,t2,t3;
int i,n,p,newp;
FILE *fp,*fp1;
clrscr();
fp=fopen("td.txt","w");
printf("Enter the no of records\n");
scanf("%d",&n);
printf("Enter the record\n");
for (i=0;i<n;i++)
{
scanf("%d%s",&t1.no,t1.name);
fwrite(&t1,sizeof(struct teledir),1,fp);
}
fclose(fp);
48
fp=fopen("td.txt","r");
while(fread(&t2,sizeof(struct teledir),1,fp)!=NULL)
{
printf("\t%d%s\n",t2.no,t2.name);
}
fclose(fp);
getch();
}
49
OUTPUT:
Enter the no of records
3
Enter the record
111 abc
222 xyz
333 nmo
111abc
222xyz
333nmo
Enter number to be modified & a new number
222 9898
111 abc
9898 xyz
333 nmo
50
RESULT:
Thus the program is written & executed successfully.
51