0% found this document useful (0 votes)
6 views46 pages

C LAB Manual

The document is a laboratory manual for the GE3171 - Problem Solving and Python Programming Laboratory course at Jayaraj Annapackiam CSI College of Engineering. It outlines course objectives, a list of experiments, course outcomes, and provides sample programs in C for various programming concepts such as I/O statements, decision-making constructs, loops, arrays, strings, functions, recursion, pointers, structures, and file processing. The manual also includes algorithms and expected outputs for each experiment.

Uploaded by

angelezhilsheeba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views46 pages

C LAB Manual

The document is a laboratory manual for the GE3171 - Problem Solving and Python Programming Laboratory course at Jayaraj Annapackiam CSI College of Engineering. It outlines course objectives, a list of experiments, course outcomes, and provides sample programs in C for various programming concepts such as I/O statements, decision-making constructs, loops, arrays, strings, functions, recursion, pointers, structures, and file processing. The manual also includes algorithms and expected outputs for each experiment.

Uploaded by

angelezhilsheeba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

JAYARAJ ANNAPACKIAM CSI COLLEGE OF ENGINEERING

(Approved by AICTE, New Delhi and Affiliated to Anna


University)

MARGOSCHIS NAGAR, NAZARETH - 628 617

DEPARTMENT OF SCIENCE AND HUMANITIES

LABORATORY MANUAL

GE3171 - PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY

PREPARED BY VERIFIED BY APPROVED BY

I.Priya Magdalin HOD PRINCIPAL


CS3271 PROGRAMMING IN C LABORATORY L T P
C0042
COURSE OBJECTIVES:
 To familiarise with C programming constructs.
 To develop programs in C using basic constructs.
 To develop programs in C using arrays.
 To develop applications in C using strings, pointers, functions.
 To develop applications in C using structures.
 To develop applications in C using file processing.

LIST OF EXPERIMENTS:
Note: The lab instructor is expected to design problems based on the topics listed.
The Examination shall not be restricted to the sample experiments designed.
1. I/O statements, operators, expressions
2. decision-making constructs: if-else, goto, switch-case, break-continue
3. Loops: for, while, do-while
4. Arrays: 1D and 2D, Multi-dimensional arrays, traversal
5. Strings: operations
6. Functions: call, return, passing parameters by (value, reference), passing arrays
tofunction.
7. Recursion
8. Pointers: Pointers to functions, Arrays,Strings, Pointers to Pointers, Array of Pointers
9. Structures: Nested Structures, Pointers to Structures, Arrays of Structures and Unions.
10. Files: reading and writing, File pointers, file operations, random access,
processordirectives.
TOTAL: 60 PERIODS
COURSE OUTCOMES:
Upon completion of the course, the students will be
able to CO1: Demonstrate knowledge on C
programming constructs. CO2: Develop programs in C
using basic constructs.
CO3: Develop programs in C using arrays.
CO4: Develop applications in C using strings, pointers,
functions. CO5: Develop applications in C using structures.
CO6: Develop applications in C using file processing.

TEXT BOOKS:
1. ReemaThareja, “Programming in C”, Oxford University Press, Second Edition, 2016.
2. Kernighan, B.W and Ritchie,D.M, “The C Programming language”, Second
Edition,Pearson Education, 2015.

REFERENCES:
1. Paul Deitel and Harvey Deitel, “C How to Program with an Introduction to C++”,
Eighth edition, Pearson Education, 2018.
2. Yashwant Kanetkar, Let us C, 17th Edition, BPB Publications, 2020.
3. Byron S. Gottfried, "Schaum's Outline of Theory and Problems of Programming
with C", McGraw-Hill Education, 1996.
4. Pradip Dey, Manas Ghosh, “Computer Fundamentals and Programming in C”, Second
5. Edition, Oxford University Press, 2013.
6. Anita Goel and Ajay Mittal, “Computer Fundamentals and Programming in C”,
1st Edition, Pearson Education, 2013.
EX.No. : 1 PROGRAM USING I/O STATEMENTS AND EXPRESSIONS
DATE :

AIM
To write a C Program to perform I/O statements and expressions.

Algorithm:
1. Start
2. Define variables: name, rollno, sub1, sub2, sub3, sub4, sum, score
3. Take input from keyboard for all the input variables
4. Calculate the sum of marks of 4 subjects and also calculate the percentage score as:
sum = sub1 + sub2 + sub3 + sub4;
score = (sum/400) * 100
5. Display the name, roll number and percentage score.
6. Stop

PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
char name;
int rollno;
float sub1, sub2, sub3, sub4, sum, score;
printf("Enter name of student: ");
scanf(“%s”,&name[]);
printf ("\n Enter Roll Number: ");
scanf("%d", &rollno);
printf ("\n Enter Marks in 4 Subjects:\n");
scanf("%f%f%f%f", &sub1, &sub2, &sub3, &sub4);
sum=sub1+sub2+sub3+sub4;
score = (sum/400)*100;
printf("\n Name of student: %s", name);
printf("\n Roll Number: %d", rollno);
printf ("\nPercentage score secured: %2.2f%c", score,'%');
getch();
}

Output:
Enter name of student: Ajit Singh
Roll Number: 25
Enter Marks in 4 Subjects:
50
75
85
62
Name of student: Ajit Singh
Roll Number: 25
Percentage score secured: 68.00%

RESULT:
Thus a C Program using i/o statements and expressions was executed and the
output was obtained.
EX.No. : 2(A) PROGRAM TO PRINT WHETHER A GIVEN NUMBER IS EVEN OR
ODD
DATE :

AIM
Write a program to print whether a given number is even or odd.
ALGORITHM
1. Start
2. Declare variables and initializations
3. Read the Input variable.
4. Check the condition if the condition is true print the if statement
5. If the condition is false print the else statement
6. Stop

PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf("Enter the number: ");
scanf(“%d”,&num);
if(num%2==0)
printf(“\n %d is even”, num);
else
printf(“\n %d is odd”, num);
getch();
}

Output:
Enter the number: 6
6 is even

RESULT:
Thus a C Program to find even or odd number was executed and the output was
obtained.
EX.No. : 2(B) PROGRAM TO FIND WHETHER A CHARACTER IS CONSONANT OR
VOWEL USING SWITCH STATEMENT.
DATE :

AIM
Write a program to find whether a character is consonant or vowel using switch
statement.

ALGORITHM
1. Start
2. Declare variables and initializations
3. Read the Input variable.
4. Using switch statement check the given alphabets is a vowel or consonants
5. If the given alphabets are present in the case given print the case statement
6. Or print the default statements
7. Stop

PROGRAMS

#include <stdio.h>
void main()
{
char ch;
printf(“Enter any alphabet:”); //input alphabet from user
scanf(“%c”, &ch);
switch(ch)
{
case ‘a’:
case ‘A’:
printf(“Vowel”);
break;
case ‘e’:
case ‘E’:
printf(“Vowel”);
break;
case ‘I’:
case ‘I’:
printf(“Vowel”);
break;
case ‘o’:
case ‘O’:
printf(“Vowel”);
break;
case ‘u’:
case ‘U’:
printf(“Vowel”);
break;
default:
printf(“Consonant”);
}
}

OUTPUT
Enter any alphabet:a
Vowel
Enter any alphabet:k
Consonant

RESULT:
Thus a C Program to find vowel or consonants was executed and the output was
obtained.
EX.No. : 2(C) PROGRAM TO FIND WHETHER THE GIVEN YEAR IS LEAP YEAR OR
NOT .
DATE :

AIM
To write a C Program to find whether the given year is leap year or Not .

ALGORITHM
1. Start
2. Declare variables
3. Read the Input .
4. Take a year as input and store it in the variable year.
5. Using if,else statements to,
a) Check whether a given year is divisible by 400.
b) Check whether a given year is divisible by 100.
c) Check whether a given year is divisible by 4.
6. If the condition at step 5.a becomes true, then print the ouput as “It is a leap year”.
7. If the condition at step 5.b becomes true, then print the ouput as “It is not a leap
year”.
8. If the condition at step 5.c becomes true, then print the ouput as “It is a leap year”.
9. If neither of the condition becomes true, then the year is not a leap year and print
the same.
10. Display the output of the calculations .
11. Stop

PROGRAM
#include<stdio.h>
void main()
{
int year;
printf("Enter a year \n");
scanf("%d", &year);
if ((year % 400) == 0)
printf("%d is a leap year \n", year);
else if ((year % 100) == 0)
printf("%d is a not leap year \n", year);
else if ((year % 4) == 0)
printf("%d is a leap year \n", year);
else
printf("%d is not a leap year \n", year);
}

OUTPUT
Enter a year
2012
2012 is a leap year
Enter a year
2009
2009 is not a leap year

RESULT
Thus a C Program to check Leap year or not was executed and the output was
obtained
EX.No. : 3(A) TO CHECK WHETHER THE GIVEN INTEGER IS PALINDROME OR
NOT.
DATE :

AIM:
To check whether the given integer is PALINDROME or NOT.

ALGORITHM:
1. [Initialize] Start
2. [Input the original number]read num
3. [Set number num to a variable n]n ← num
4. [Iterate until num is not equal to 0.
If num value becomes 0, control comes out of the loop. So num’s original value is lost.
So, num
value is stored in other variable n in step 3.
In step 4, reverse of the number is calculated.]
while ( num != 0) do
remainder ← num mod 10
num ← num/10
rev ← rev * 10 +remainder
5. [Print reverse number]print rev
6. [Check if original number & reverse number are same. If it is, number is palindrome.
Otherwise,not palindrome]
if (rev = n) then
print palindrome
else
print not a palindrome
endif
7. [Finished]
End

PROGRAM

#include<stdio.h>
#include<conio.h>
int main()
{
int temp,rev=0,num,remainder ;
clrscr();
printf("Enter the number\n");
scanf("%d",&num);
temp=num;
while(num!=0) //Reversing the number
{
remainder = num%10;
num = num/10;
rev = rev*10+ remainder;
}
printf("The reverse number is %d",rev);
if(rev == temp)
printf("\n%d is a palindrome",temp);
else
printf("\n%d is not a palindrome", temp);
getch();
}

OUTPUT:
1. Enter the number
1001
The reverse number is 1001
1001 is a palindrome
2. Enter the number
123
The reverse number is 123
123 is not a palindrome

RESULT
Thus a C Program to check palindrome or not was executed and the output was
obtained
EX.No. : 3(B) TO COMPUTE SIN(X) USING TAYLOR SERIES APPROXIMATION
DATE :

AIM:
To compute Sin(x) using Taylor series approximation given by

ALGORITHM:
Step 1: start
Step 2: enter the degree
Step 3: convert degree into radian
X <- degree*(PI/180)
Step 4: check the given number is odd/not
If(it is a odd number)
Then
{
term = nume/deno;
nume = -nume*x*x;
deno = deno*i*(i+1);
}
else
{
If(term<0.001)
{
Print thr sine value
}
else
{
Check the number
}
}
Step 5: stop

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define PI 3.142
int main()
{
int i, degree;
float x, sum=0,term,nume,deno;
clrscr();
printf("Enter the value of degree");
scanf("%d",&degree);
x = degree * (PI/180);
nume = x;
deno = 1;
i=2;
do
{
term = nume/deno;
nume = -nume*x*x;
deno = deno*i*(i+1);
sum=sum+term;
i=i+2;
} while (fabs(term) >= 0.00001);
printf("The sine of %d is %.3f\n", degree, sum);
printf("The sine function of %d is %.3f", degree, sin(x));
getch();
}

OUTPUT:
1. Enter the value of degree
0
The sine of 0 is 0.000
The sine function of 0 is 0.000
2. Enter the value of degree
45
The sine of 45 is 0.707
The sine function of 45 is 0.707
3. Enter the value of degree
90
The sine of 90 is 1.000
The sine function of 90 is 1.000

RESULT
Thus a C Program for sine series was executed and the output was obtained
EX.No. : 3(C) PROGRAM TO DISPLAY THE FOLLOWING PATTERN.
DATE :

AIM
Write a program to display the following pattern.

ALGORITHM
1. start
2. declare the variable i and j
3. Given the for loop with condition i=1 and i<=5 same for j=1;j<=5
4. loop should continue printing * until the loop terminates
5. Stop

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1; i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(“*”);
}
printf(“\n”);
}
getch();
}
Output
*
**
***
****
*****

RESULT
Thus a C Program to pyramid using for loop was executed and the output was obtained
EX.No. : 4(A) PROGRAM TO ARRANGE GIVEN N INTEGERS IN ASCENDING
ORDER USING BUBBLE SORT.
DATE :

AIM:
To arrange given N integers in ascending order using Bubble Sort.

ALGORITHM:
1. [Initialize] Start
2. [Input number of elements]
read n
3. [Input unsorted elements in array]
read elements in array a[ ]
4. print elements of array a[ ]
5. [Iterate array a[ ] in two loops. Outer loop gives number of passes. Inner loop does
swap task.In each pass, compare each pair of adjacent items. If former element is
greater than latter one, swapthem.]
[Iterate array a[ ] with
for each value i in array a[i] to n do
for each value j in array a[j] to n-1 do
[Compare each pair of adjacent elements]
if (a[j] > a[j+1])then
[Swap these elements using temp variable]
temp ← a[j]
a[j] ← a[j+1]
a[j+1] ← temp
endif
endfor
endfor
6. Print array with sorted elements
7. [Finished] End

PROGRAM:

#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,j,a[10],temp;
clrscr();
printf("Enter the no. of elements : \n");
scanf("%d",&n);
printf("Enter the array elements \n");
for(i = 0 ; i < n ; i++)
scanf("%d",&a[i]);
printf("The original elements are \n");
for(i = 0 ; i < n ; i++)
printf("%d ",a[i]);
for(i= 0 ; i < n-1 ; i++) // Number of Passes
{
for(j= 0 ; j< n-i-1; j++) // Comparisons
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
printf("\n The Sorted elements are \n");
for(i = 0 ; i < n ; i++)
printf("%d ",a[i]);
getch();
}

Output:
1. Enter the no. of elements :
5
Enter the array elements
30 10 50 20 40
The original elements are
30 10 50 20 40
The Sorted elements are
10 20 30 40 50
2. Enter the no. of elements : 6
Enter the array elements
654321
The original elements are
654321
The Sorted elements are
123456

RESULT
Thus a C Program for bubble sort was executed and the output was obtained
EX.No. : 4(B) MATRIX MULTIPLICATION.
DATE :

AIM:
To read two matrices A(m x n )and B(p x q) and Compute the product A and B.

ALGORITHM
Step 1: GET THE MATRIX SIZE OF a
Input the size of matrix a and read the values of m and n.
Step 2: GET THE MATRIX VALUE OF a
For i=0 to n-1
For j=0 to n-1
Read a[i][j]
End For
Step 3: GET THE MATRIX SIZE OF b
Input the size of matrix b and read the values of p and q.
Step 4: GET THE MATRIX VALUE OF b
For i=0 to p-1
For j=0 to q-1
Read b[i][j]
End For
STEP :5 MULTIPLICATION OF MATRICES NOT POSSIBLE
If(n!=p)
Print(“multiplication is not possible”)
Stop
End if
STEP 6: MULTIPLICATION OF MATRICES IS POSSIBLE
For i=0 to m-1
For j=0 to q-1
Sum=0
For k=0 to n-1
Sum=Sum+a[i][k]*b[k][j]
End for
Step 7: DISPLAY RESULT
For i=0 to m-1
For j=0 to q-1
Print c[i][j]
End for
Step 8: STOP

PROGRAM :
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
clrscr();
printf("Enter the size of first matrix\n");
scanf("%d %d",&m,&n);
printf("Enter the size of second matrix\n");
scanf("%d %d",&p,&q);
if(n!=p)
printf(“Matrix multiplication is not possible”);
else
{
printf("Enter the elements of first matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the elements of the second matrix\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("\n A- matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("\n B- matrix is\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("%d\t",b[i][j]);
printf("\n");
}
printf("The product of two matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
getch();
}

OUTPUT:
1. Enter the size of first matrix
23
Enter the size of second matrix
32
Enter the elements of first matrix
123456
Enter the elements of the second matrix
123456
A- matrix is
123
456
B- matrix is
12
34
56
The product of two matrix is
22 28
49 6

RESULT
Thus a C Program for matrix multiplication was executed and the output was
obtained
EX.No. : 5(A) PROGRAM USING strlen() FUNCTION
DATE :

AIM:
To write a C program using strlen() function.

ALGORITHM:
Step 1: start
Step 2: include header file
Step 3:assign character name
Step 4: initialize length of string (I.e.,) len 1,len 2
Step 5:assign required character to len 1,len 2
Step 6.1: declare print statement of len 1
Step 6.2: declare print statement of len 2
Step 7: stop

PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
Chat name[ ]= "MUNI";
int len1, len2;
len1=strlen(name) ;
len2=strlen("LAK") ;
printf("\n string length of %s is%d" , name, len1) ;
printf("\n string length of %s is%d" , "LAK", len2) ;
}

***********
OUTPUT:
String length of MUNI is 4
String length of LAK is 3
***********

RESULT:
The program for finding length of the given string has been complied and executed
successfully
EX.No. : 5(B) PROGRAM USING strcpy() FUNCTION
DATE :

AIM:

To write a C program for coping a String

ALGORITHM:
step 1: start
Step 2: include header file
Step 3: initialize character source
Step 4: assign character target value i. e. (*required value)
Step 4.1: use strcpy() function to copy our source and target
Step 5: declare print statement to print source
Step 5.1: declare print statement to copy our source to target
Step 6: stop

PROGRAM:
#include <stdio.h>
main()
{
char source ="MUNI";
char target[10];
strcpy(target, source) ;
printf("/n Source string is%s", source) ;
printf("/n Target string is%s", target) ;
}

***********
OUTPUT:
Source string is MUNI
Target string is MUNI
***********

RESULT:
The program for coping the given string has been complied and executed
successfully.
EX.No. : 5(C) PROGRAM USING strcat() FUNCTION
DATE :

AIM:
To write a C program for combining two string together to form new string

ALGORITHM:
Step 1: start
Step 2: include header file
Step 3: assign character source
Step 3.1: initialize character target
Step 4: use strcat() function to combine both source and target
Step 5: declare print statement to print source
Step 5.1: declare print statement to print target
Step 6: stop

PROGRAM:
#include<stdio.h>
main()
{
char source[ ]= "Ramesh";
char target [10]="Babu";
strcat(source, target);
print("\n Source string is %d", source) ;
printf("\n Target string is%d", target) ;
}

***********
OUTPUT:
Source string is RameshBabu
Target string is Babu
***********

RESULT:
The program for combining two strings has been complied and executed
successfully.
EX.No. : 5(D) PROGRAM USING strcmp() FUNCTION
DATE :

AIM:
To write a C program to compare the given strings

ALGORITHM:
Step 1: start
Step 2: include header file
Step 3: declare required character name
Step 3.1: initialize variable i, j, k
Step 3.2: assign values to variable i, j, k
Step 4: declare print statement to print output
Step 5: stop

PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
char name [ ]="Kalai";
char name1[ ]="Malai ";
int I, j, k;
i=strcmp(name, "Kalai");
j=strcmp(name1, name) ;
k=strcmp(name, "Kalai mani");
printf("\n %d %d %d", i, j, k) ;
}

************
OUTPUT:
016
************

RESULT:
The program for comparing the given string has been complied and executed
successfully
EX.No. : 6 FACTORIAL OF NUMBER USING RECURSIVE FUNCTION
DATE :

AIM:
To find the factorial of number using recursive function

ALGORITHM:
1. Factorial of number [A recursive C function to find the factorial of number n!
defined by fact(n)=1,
2. if n=0, otherwise fact(n)=n*fact(n-1)]
3. FUNCTION FACTORIAL (N: INTEGER): INTEGER
4. (* RECURSIVE COMPUTATION OF N FACTORIAL *)
BEGIN
(* TEST FOR STOPPING STATE *)
IF N <= 0 THEN
FACTORIAL := 1
ELSE
FACTORIAL := N * FACTORIAL(N - 1)
5. END; (* FACTORIAL *)

PROGRAMS
#include<stdio.h>
#include<conio.h>
int fact(int n)
{
if(n==0)
{
return 1;
}
return (n*fact(n-1));
}
int main()
{
int n,r,res;
clrscr();
printf("Enter the value of n and r\n");
scanf("%d%d",&n,&r);
res=fact(n)/(fact(n-r)*fact(r));
printf("The NCR is = %d",res);
getch();
}

Output:
1. Enter the value of n and r
42
The NCR is =6
2. Enter the value of n and
73
The NCR is =6

RESULT
Thus a C Program to find factorial of number using recursive function was
executed and the output was obtained
EX.No. : 7 PROGRAM TO CALCULATE THE POWER OF A NUMBER USING
RECURSION.
DATE :

AIM:
To write the program to calculate the power of a number using recursion.

ALGORITHM
Step 1: Start
Step 2: Include header file
Step 3: Initialize power
Step 3.1: Declare base and exponential
Step 4: Declare print and scan to print and scan base
Step 4.1: Declare print and scan to pint and scan( power)exponential (in positive)
Step 5: If(exp! =1) return base*power
Step 6: Stop

PROGRAM
#include <stdio.h>
int power(int n1,int n2);
int main()
{
int base, exp;
printf("Enter base number: ");
scanf("%d",&base);
printf("Enter power number(positive integer): ");
scanf("%d",&exp);
printf("%d^%d = %d", base, exp, power(base, exp));
return 0;
}
int power(int base,int exp)
{
if ( exp!=1 )
return (base*power(base,exp-1));
}

OUTPUT

Enter base number: 4


Enter power number(positive integer): 6
4^6 = 4096
EX.No. : 8(A) POINTERS TO FUNCTIONS
DATE :

AIM:
Write a C program using pointers to compute the sum, mean and standard
deviation of all elements stored in an array of n real numbers.

ALOGORITHM

Step 1:start
Step2: Enter the value of n it may be integer or float
Step3: for(i=0; i<n;i++)
{
Sum=sum+*ptr;
Ptr++;
Mean=sum/n;
Ptr=a;
For(i=0;i<n;i++)
{
Sumstd=sumstd+pow((*ptr-mean),2);
Ptr++;
Step4: print the result for sum,mean,&stddev
Step5: stop

Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float a[10], *ptr, mean, std, sum=0, sumstd=0;
int n,i;
clrscr();
printf("Enter the no of elements\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+ *ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumstd=sumstd + pow((*ptr - mean),2);
ptr++;
}
std= sqrt(sumstd/n);
printf("Sum=%.3f\t",sum);
printf("Mean=%.3f\t",mean);
printf("Standard deviation=%.3f\t",std);
getch();
}

Output:
Enter the no of elements
5
Enter the array elements
53987
Sum=32.000
Mean=6.400
Standard deviation=2.154

RESULT
Thus a C Program to using pointers to compute the sum, mean and standard
deviation of all elements stored in an array of n real numbers was executed and the
output was obtained
EX.No. : 8(B) POINTERS TO POINTERS
DATE :

AIM:
Write a program to perform pointers to pointers function

ALGORITHM

Step 1:start
Step 2: include header file
Step 3:initialize number
Step 3.1:give int*p, int**p2
Step 4: assign p, p2
Step 5: declare print statement to print the value and address of p, p2
Step 5.1: give print statement to print the value and address of *p, **p2
Step 6: stop

PROGRAM

#include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
int **p2;//pointer to pointer
p=&number;//stores the address of number variable
p2=&p;
printf("Address of number variable is %x \n",&number);
printf("Address of p variable is %x \n",p);
printf("Value of *p variable is %d \n",*p);
printf("Address of p2 variable is %x \n",p2);
printf("Value of **p2 variable is %d \n",*p);
return 0;
}

OUTPUT
Address of number variable is fff4
Address of p variable is fff4
Value of *p variable is 50
Address of p2 variable is fff2
Value of **p variable is 50

RESULT
Thus a C Program to print pointers to pointers value was executed and the
output was obtained
EX.No.: 8(C) POINTERS TO ARRAY
DATE :

AIM:
Write a program to perform pointers to Array function

ALGORITHM:

Step 1: Start
Step 2: include header file
Step 3: declare pointer (p)
Step 3. 1: initialize value of required size
Step 3.2: assign p
Step 4: if condition is satisfied under for(int i=0;i<7;i++)
Step 4.1: print the value and address of p
Step 4.2: initialize increment of p
Step 5: stop

PROGRAM:

include <stdio.h>
int main( )
{
/Pointer variable/
int *p;
int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ;
p = &val[0];
for ( int i = 0 ; i<7 ; i++ )
{
printf("val[%d]: value is %d and address is %p\n", i, *p, p);
p++;
}
return 0;
}

OUTPUT:

Output:
val[0]: value is 11 and address is 0x7ffc8c976fc0
val[1]: value is 22 and address is 0x7ffc8c976fc4
val[2]: value is 33 and address is 0x7ffc8c976fc8
val[3]: value is 44 and address is 0x7ffc8c976fcc
val[4]: value is 55 and address is 0x7ffc8c976fd0
val[5]: value is 66 and address is 0x7ffc8c976fd4
val[6]: value is 77 and address is 0x7ffc8c976fd8
RESULT:
Thus a C Program to print pointers to Array value was executed and the output
was obtained

EX.No.: 9(A) STUDENT RECORD USING ARRAY OF STRUCTURES


DATE:

AIM:
To find student record using array of structures

ALGORITHM:
Step1: start
Step2: enter the num of students
Step3: if(strcmp(s[i].name, sname==0)
Then
{
Print the marks of students name & USN
}
Else
{
Print the given date is invaid
}
Step4: stop

PROGRAM:
/* program to maintain a record of student using structrue */
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno, marks;
char name[20], grade;
};
Void main()
{
int i,n,found=0;
struct student s[10];
char sname[20];
clrscr();
printf("Enter the number of student details n=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the %d student details \n",i+1);
printf("enter the roll number:");
scanf("%d",&s[i].rollno);
printf("enter the student name without white spaces:");
scanf("%s", s[i].name);
printf("enter the marks : ");
scanf("%d", &s[i].marks);
printf("enter the grade : ");
fflush(stdin);
scanf("%c",&s[i].grade);
}
printf("\nStudent details are \n");
printf("\nRollno\tName\t\t\tMarks\tGrade\n");
for(i=0;i<n;i++)
printf("%d\t%s\t\t%d\t%c\n", s[i].rollno, s[i].name, s[i].marks, s[i].grade);
printf("\nEnter the student name to print the marks:");
scanf("%s", sname);
for(i=0;i<n;i++)
{
if(strcmp(s[i].name,sname)==0)
{
Printf(“\Marks of the student is : %d”,s[i].marks);
found=1;
}
}
if(found ==0)
printf(“ Given student name not found\n”);
getch();
}

Output:
Enter the number of student details n=3
enter the 1 student details
enter the roll number: 100

enter the student name without white spaces:vishwa


enter the marks : 90
enter the grade : A
enter the 2 student details
enter the roll number: 101

enter the student name without white spaces:madhu


enter the marks : 50
enter the grade : B
enter the 3 student details
enter the roll number: 102

enter the student name without white spaces:kiran


enter the marks : 45
enter the grade : C

Roll Nae Mark Gra


s d
100 vishwa 90 A
101 madhu 50 B
102 kiran 45 C

Enter the student name to

print the marks: madhu Marks

of the student is : 50

RESULT
Thus a C Program to find student record using array of structures was executed
and the output was obtained
EX.No.: 9(B) SALARY SLIP OF EMPLOYEES
DATE:

AIM
To write a C Program to Generate salary slip of employees using structures and
pointers.

ALGORITHM
1. Start
2. Declare variables
3. Read the number of employees .
4. Read allowances, deductions and basic for each employee.
5. Calculate net pay= (basic+ allowances)-deductions
6. Display the output of the Pay slip calculations for each employee.
7. Stop

PROGRAM
#include<stdio.h>
#include<conio.h>
#include "stdlib.h"
struct emp
{
int empno ;
char name[10], answer ;
int bpay, allow, ded, npay ;
struct emp *next;
};
void main()
{
int I,n=0;
int more_data = 1;
struct emp e *current_ptr, *head_ptr;
clrscr() ;
head_ptr = (struct emp *) malloc (sizeof(struct emp));
current_ptr = head_ptr;
while (more_data)
{
{
printf("\nEnter the employee number : ") ;
scanf("%d", & current_ptr->empno) ;
printf("\nEnter the name : ") ;
www.universityquestions.in
38
scanf("%s",& current_ptr->name) ;
printf("\nEnter the basic pay, allowances & deductions : ") ;
scanf("%d %d %d", & current_ptr ->bpay, & current_ptr ->allow, & current_ptr -
>ded) ;
e[i].npay = e[i].bpay + e[i].allow - e[i].ded ;
n++;
printf("Would you like to add another employee? (y/n): ");
scanf("%s", answer);
if (answer!= 'Y')
{
current_ptr->next = (struct eme *) NULL;
more_data = 0;
}
else
{
current_ptr->next = (struct emp *) malloc (sizeof(struct emp));
current_ptr = current_ptr->next;
}}}
printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ;
current_ptr = head_ptr;
for(i = 0 ; i < n ; i++)
{
printf("%d \t %s \t %d \t %d \t %d \t %d \n", current_ptr->empno,
current_ptr->name, current_ptr->bpay, current_ptr->allow, current_ptr->ded,
current_ptr->npay) ;
current_ptr=current_ptr->next;
}
getch() ;
}

OUTPUT
Enter the number of employees : 2
Enter the employee number : 101
Enter the name : Arun
Enter the basic pay, allowances & deductions : 5000 1000 250
www.universityquestions.in
39
Enter the employee number : 102
Enter the name : Babu
Enter the basic pay, allowances & deductions : 7000 1500 750
Emp.No. Name Bpay Allow Ded Npay
101 Arun 5000 1000 250 5750
102 Babu 7000 1500 750 7750

RESULT
Thus a C Program To find student record using array of structures was executed
and the output was obtained
EX.No.: 9(C) PROGRAM TO DECLARE, INITIALIZE AN UNION.
DATE:

AIM:
Write a program to declare, initialize an UNION.

ALGORITHM:

step 1: start
Step 2: include header file
Step 3:declare union
Step 3.1:initialize character, integer, double
Step 3.2:declare main function
Step 3.2: pack p and declare print statement to print the size
Step 4: assign (p.a, p. b, p. c) declare print statement to print values
Step 5: stop

PROGRAM:
#include <stdio.h>
union pack{
char a;
int b;
double c;
};
int main()
{
pack p;
printf("\nOccupied size by union pack: %d",sizeof(pack));
p.a='A';
printf("\nValue of a:%c",p.a);
p.b=10;
printf("\nValue of b:%d",p.b);
p.c=12345.6790;
printf("\nValue of c:%f",p.c);
p.a='A';
p.b=10;
p.c=12345.6790;
printf("\nValue of a:%c, b:%d, c:%f",p.a,p.b,p.c);
return 0;
}

OUTPUT:

Occupied size by union pack: 8


Value of a:A
Value of b:10
Value of c:12345.679000
Value of a: , b:-377957122, c:12345.679000

RESULT
Thus a C program to declare, initialize an UNION was executed and the output
was obtained
EX.No.: 10(A) PROGRAM TO ILLUSTRATE HOW FILE STORED IN DISK IS READ
DATE:

AIM:
Write a Program to illustrate how file stored in disk is read

ALGORITHM
Step 1: Start
Step 2: Define file
Step 2. 1: Initialize character
Step 3: Give print statement to open your file
Step 3.1: Give scan statement to scan your filename
Step 4: Declare file on(r) in reading mode
Step 4.1: if (fptr==NULL), print file cannot be open
Step 5: while(ch=EOF), print ch
Step 5.1: Declare close file
Step 6: Stop

PROGRAM

#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fptr;
char filename[15];
char ch;
printf("Enter the filename to be opened \n");
scanf("%s", filename);
/* open the file for reading */
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);}
ch = fgetc(fptr);
while (ch != EOF)
{
printf ("%c", ch);
ch = fgetc(fptr); }
fclose(fptr);
}
RESULT
Thus a C program to illustrate how file stored in disk is read was executed and
the output was obtained
EX.No.: 10(B) PROGRAM TO ILLUSTRATE HOW TO WRITING TO A FILE
DATE:

AIM:
Write a Program to illustrate how to writing to the files

ALGORITHM

Step 1: Start
Step 2: include header file
Step 3: initialize character, i, n
Step 3.1:define file open if (fp==NULL) print file cannot be open
Step 4: give print and scan statement to enter name and details of student
Step 4.1: declare print and scan statement to print and scan roll number
Step 4.2: give print and scan statement to print and scan marks
Step 5: print name, roll no, marks
Step 6: close file
Step 7: stop

PROGRAM

include<stdio.h>
int main() {
FILE *fp;
char name[50];
int roll_no, i, n;
float marks;
fp = fopen("marks.txt", "w");
if(fp == NULL) {
printf("file can't be opened\n");
exit(1);
}
printf("Enter the number of student details you want to enter: ");
scanf("%d", &n);
for(i = 0; i < n; i++) {
fflush(stdin);
printf("\nEnter the details of student %d \n\n", i +1);
printf("Enter name of the student: ");
gets(name);
printf("Enter roll no: ");
scanf("%d", &roll_no);
printf("Enter marks: ");
scanf("%f", &marks);
printf(fp, "Name: %s\t Roll no: %d \tMarks: %f \n", name, roll_no, marks);
printf("\n Details successfully written to the file\n\n");
}
fclose(fp);
return 0;
}

OUTPUT

Enter the number of student details you want to enter: 1


Enter the details of student 1
Enter name of the student: aaa
Enter roll no: 1
Enter marks: 100
Details successfully written to the file

RESULT
Thus a C program to illustrate how to write to a file was executed and the output
was obtained
EX.No. : 10(C) Telephone directory
DATE :

AIM
To write a C Program to add, delete ,display ,Search and exit options for telephone
details of an individual into a telephone directory using random access file.

ALGORITHM
1. Start.
2. Declare variables, File pointer and phonebook structures.
3. Create menu options.
4. Read the option .
5. Develop procedures for each option.
6. Call the procedure (Add, delete ,display ,Search and exit)for user chosen option.
7. Display the message for operations performed.
8. Stop

PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Phonebook_Contacts
{
char FirstName[20];
char LastName[20];
char PhoneNumber[20];
} phone;
void AddEntry(phone * );
void DeleteEntry(phone * );
void PrintEntry(phone * );
void SearchForNumber(phone * );
www.universityquestions.in
44
int counter = 0;
char FileName[256];
FILE *pRead;
FILE *pWrite;
int main (void)
{
phone *phonebook;
phonebook = (phone*) malloc(sizeof(phone)*100);
int iSelection = 0;
if (phonebook == NULL)
{
printf("Out of Memory. The program will now exit");
return 1;
}
else {}
do
{
printf("\n\t\t\tPhonebook Menu");
printf("\n\n\t(1)\tAdd Friend");
printf("\n\t(2)\tDelete Friend");
printf("\n\t(3)\tDisplay Phonebook Entries");
printf("\n\t(4)\tSearch for Phone Number");
printf("\n\t(5)\tExit Phonebook");
printf("\n\nWhat would you like to do? ");
scanf("%d", &iSelection);
if (iSelection == 1)
{
AddEntry(phonebook);
}
if (iSelection == 2)
{
DeleteEntry(phonebook);
}
if (iSelection == 3)
{
PrintEntry(phonebook);
}
if (iSelection == 4)
{
SearchForNumber(phonebook);
}
if (iSelection == 5)
{
printf("\nYou have chosen to exit the Phonebook.\n");
return 0;
}
} while (iSelection <= 4);
}
void AddEntry (phone * phonebook)
{
pWrite = fopen("phonebook_contacts.dat", "a");
if ( pWrite == NULL )
{
perror("The following error occurred ");
exit(EXIT_FAILURE);
}
else
{
counter++;
realloc(phonebook, sizeof(phone));
printf("\nFirst Name: ");
scanf("%s", phonebook[counter-1].FirstName);
printf("Last Name: ");
scanf("%s", phonebook[counter-1].LastName);
printf("Phone Number (XXX-XXX-XXXX): ");
scanf("%s", phonebook[counter-1].PhoneNumber);
printf("\n\tFriend successfully added to Phonebook\n");
fprintf(pWrite, "%s\t%s\t%s\n", phonebook[counter-1].FirstName,
phonebook[counter-1].LastName, phonebook[counter-1].PhoneNumber);
fclose(pWrite);
}
}
void DeleteEntry (phone * phonebook)
{
int x = 0;
int i = 0;
char deleteFirstName[20]; //
char deleteLastName[20];
printf("\nFirst name: ");
scanf("%s", deleteFirstName);
printf("Last name: ");
scanf("%s", deleteLastName);
for (x = 0; x < counter; x++)
{
if (strcmp(deleteFirstName, phonebook[x].FirstName) == 0)
{
if (strcmp(deleteLastName, phonebook[x].LastName) == 0)
{
for ( i = x; i < counter - 1; i++ )
{
strcpy(phonebook[i].FirstName, phonebook[i+1].FirstName);
strcpy(phonebook[i].LastName, phonebook[i+1].LastName);
strcpy(phonebook[i].PhoneNumber, phonebook[i+1].PhoneNumber);
}
printf("Record deleted from the phonebook.\n\n");
--counter;
return;
}
}
}
printf("That contact was not found, please try again.");
}
void PrintEntry (phone * phonebook)
{
int x = 0;
printf("\nPhonebook Entries:\n\n ");
pRead = fopen("phonebook_contacts.dat", "r");
if ( pRead == NULL)
{
perror("The following error occurred: ");
exit(EXIT_FAILURE);
}
else
{
for( x = 0; x < counter; x++)
{
printf("\n(%d)\n", x+1);
printf("Name: %s %s\n", phonebook[x].FirstName, phonebook[x].LastName);
printf("Number: %s\n", phonebook[x].PhoneNumber);
}

}
fclose(pRead);
}
void SearchForNumber (phone * phonebook)
{
int x = 0;
char TempFirstName[20];
char TempLastName[20];
printf("\nPlease type the name of the friend you wish to find a number for.");
printf("\n\nFirst Name: ");
scanf("%s", TempFirstName);
printf("Last Name: ");
scanf("%s", TempLastName);
for (x = 0; x < counter; x++)
{
if (strcmp(TempFirstName, phonebook[x].FirstName) == 0)
{
if (strcmp(TempLastName, phonebook[x].LastName) == 0)
{
printf("\n%s %s's phone number is %s\n", phonebook[x].FirstName,
phonebook[x].LastName, phonebook[x].PhoneNumber);
}
}
}
}
OUTPUT
Phonebook Menu
(1) Add Friend
(2) Delete Friend"
(3) Display Phonebook Entries
(4) Search for Phone Number
(5) Exit Phonebook

What would you like to do? 1


First Name: Ram
Last Name: Mohan
Phone Number (XXX-XXX-XXXX): 717-675-0909
Friend successfully added to Phonebook
www.universityquestions.in
48
Phonebook Menu
(1) Add Friend
(2) Delete Friend"
(3) Display Phonebook Entries
(4) Search for Phone Number
(5) Exit Phonebook

What would you like to do? 5


You have chosen to exit the Phonebook.

RESULT
Thus a C Program was executed and the output was obtained.

You might also like