Programmin C Lab Manual
Programmin C Lab Manual
ALGORITHM:
1. Start
2. Declare appropriate variables: l,w,r,h,b of integer data type and a1,a2,a3 to be
of float type.
3. Get input from the user for length and width of rectangle(l,w), radius of circle(r),
height and base of triangle(h,b).
4. With the inputs, calculate the areas of shapes with the appropriate variables.
a. a1= l*w
b. a2= 3.14*r*r
c. a3= 0.5*b*h
5. Print a1, a2, and a3 as area of rectangle, area of circle, and area of triangle.
6. Stop
Program:
#include <stdio.h>
void main()
{
int l,w,r,h,b;
float a1,a2,a3;
printf("Enter length of rectangle:");
scanf("%d", &l);
printf("Enter width of rectangle:");
scanf("%d",&w);
printf("Enter radius of circle:");
scanf("%d", &r);
printf("Enter height of triangle:");
scanf("%d", &h);
printf("Enter the base of triangle:");
scanf("%d",&b);
a1=(l*w);
printf("Area of the rectangle:%f \n",a1);
a2=(3.14*r*r);
printf("Area of the circle:%f \n",a2);
a3=(0.5*b*h);
printf("Area of the triangle:%f",a3);
}
OUTPUT:
Enter length of rectangle: 5
Enter width of rectangle: 5
Enter radius of circle: 10
Enter height of triangle: 4
Enter the base of triangle: 4
Area of the rectangle: 25.000
Area of the circle: 314.000
Area of the triangle: 8.000
RESULT:
Hence, the C-program to calculate the areas of different shapes was
successfully executedl
AIM:
To write a C-program to determine the greatest number from an input of three
numbers.
PROCEDURE:
1. Start
2. Declare appropriate variables, a, b, c for the respective three numbers as
integers.
3. Obtain inputs of the three numbers.
4. Using conditional if and logical AND, check if a>b && a>c then go to
Step 4.1, else go to step 5.
4.1. Print a is the largest number.
5. Using conditional if and logical AND, check if b>a && b>c then go to
Step 5.1, else go to step 6.
5.1. Print b is the largest number.
6. Using conditional if and logical AND, check if c>a && c>b then go to Step 6.1,
else go to step 7.
6.1. Print c is the largest number.
7. Using conditional if and logical AND, check if a==b && a==c, go to step 7.1,
else go to step 5.
7.1. Print “All numbers are equal"
8. Stop
PROGRAM:
#include <stdio.h>
void main() {
int a, b, c;
printf("Enter 1st number:");
scanf("%d",&a);
printf("Enter 2nd number:");
scanf("%d",&b);
printf("Enter 3rd number:");
scanf("%d",&c);
if(a>b && a>c)
{
printf("%d is the largest",a);
}
if(b>a && b > c)
{
printf("%d is the largest",b);
}
OUTPUT:
RESULT:
Hence the C-program to determine the largest number from an input of 3
Numbers was successfully executed.
AIM:
To design a calculator using C-language to perform basic arithmetic
operations such as addition, subtraction, multiplication, division, square of a
number
ALGORITHM:
1. Start
2. Read a, b, c
3. Print menu
4. Read choice
5. Switch(ch):
5.1. Add:
5.1.1. Result = a+b
5.1.2. Print Result
5.2. Subtract:
5.2.1. Result = a-b
5.2.2. Print Result
5.3. Multiplication
5.3.1. Result = a*b
5.3.2. Print Result
5.4. Division
5.4.1. Result = a/b
5.4.2. Print Result
5.5. Square
5.5.1. Result = a*a
5.5.2. Result1= b*b
5.5.3. Print Result
5.5.4. Print Result1
6. Stop
PROGRAM:
#include<stdio.h>
void main()
{
int a, b, result, sq1, sq2, ch;
float divide;
printf("Enter two integers:");
scanf("%d%d", &a, &b);
printf("1.Add, 2.Subtract, 3.Multiply 4.Divide 5.Square");
printf("\n Enter choice:");
scanf("%d", &ch);
switch(ch)
{
case 1:
{
result = a+b;
printf("The sum is %d \n", result);
break;
}
case 2:
{
result = a-b;
printf("The difference is %d \n", result);
break;
}
case 3:
{
result = a*b;
printf("Product is %d \n", result);
break;
}
case 4:
{
result = a/b;
printf("Quotient is: %d \n", result);
break;
}
case 5:
{
sq1 = a*a;
sq2 = b*b;
printf("Square of first number is:%d \n", sq1);
printf("Square of second number is %d \n", sq2);
break;
}
}
}
OUTPUT:
Enter two integers:200 50
1.Add, 2.Subtract, 3.Multiply 4.Divide 5.Square
Enter choice:1
The sum is 250
Enter two integers:134 20
1.Add, 2.Subtract, 3.Multiply 4.Divide 5.Square
Enter choice:2
The difference is 114
Enter two integers:2 28
1.Add, 2.Subtract, 3.Multiply 4.Divide 5.Square
Enter choice:3
Product is 56
8
Enter two integers:50 5
1.Add, 2.Subtract, 3.Multiply 4.Divide 5.Square
Enter choice:4
Quotient is: 10
Enter two integers:10 5
1.Add, 2.Subtract, 3.Multiply 4.Divide 5.Square
Enter choice:5
Square of first number is:100
Square of second number is 25
RESULT:
Thus the C program to design a calculator to perform basic arithmetic operations
such as addition, subtraction, multiplication, division, and square of a number was
executed and the output was verified
AIM:
To write a C program to prepare a student's mark sheet using ‘for’ loop.
PROCEDURE:
1. Start
2. Declare appropriate variables such as n, i, sum=0, initialize an array of
marks[100], avg, and total.
3. Read the number of subjects and store the input as an integer in the
variable ‘n’.
4. Similarly, using the number of subjects, obtain the marks for each subject
with the help of ‘for’ loop.
4.1. for (i=0; i<n; i++)
5. Inside the for loop, read each mark and store it as an integer.
5.1. Also, calculate the sum = sum + i with every iteration.
6. Once the condition can no longer be proven to be true, calculate
avg=sum/2.
7. Display total mark.
8. Display the average mark.
9. Stop
PROGRAM:
#include <stdio.h>
void main()
{
int i, n, sum=0, mark[100];
float avg;
printf("Enter number of subjects:");
scanf("%d", &n);
printf("\n Enter marks for each subject:");
for(i=0; i<n; i++)
{
scanf("%d", &mark[i]);
sum=sum+mark[i];
}
avg=(float)sum/2;
printf("\nThe total marks of the student is: %d", sum);
printf("\n The average mark of the student is:%.2f", avg);
}
OUTPUT:
Enter number of subjects: 5
Enter marks for each subject:95 95 95 93 87
The total marks of the student is: 465
The average mark of the student is: 232.50
RESULT:
Hence the C-program to prepare a student’s mark was successfully executed and
the Output was verified.
AIM:
To write a C-program to determine the greatest common divisor of two numbers using
the ‘while’ loop.
PROCEDURE:
1. Start
2. Declare appropriate variables such as n1, n2 as integers.
3. Obtain two numbers as input and store them in n1 and n2.
4. Using the ‘while’ loop, set the condition as n1!=n2. If the condition is
5. true, go to step 4.1 else go to step 6.
5.1 Check if n1>n2 is true then n1-=n2 if not true n2-=n1.
6. Display GCD of the two numbers.
7. Stop
PROGRAM:
#include <stdio.h>
int main(){
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d",&n1,&n2);
while(n1!=n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
printf("GCD of the two numbers is= %d",n1);
return 0;
}
OUTPUT:
Enter two positive integers: 24 42
GCD of the two numbers is = 6
RESULT:
Hence the C-program to determine the GCD of two numbers using the ‘while’
Loop was successfully executed and the output was verified
AIM:
To write a C-program to calculate the factorial of a number using the ‘do…while’
loop.
ALGORITHM:
1. Start
2. Declare appropriate variables such as num, i=1, fact=1
3. Obtain a number as input and store in the variable of num as an integer.
4. Using do…while
4.1. do fact=fact*i
4.2. i++
4.3. while (i<=num)
5. Display factorial of the number
6. Stop
PROGRAM:
#include <stdio.h>
void main()
{
int i=1, fact=1, num;
printf("Enter number to find its factorial:");
scanf("%d", &num);
do
{
fact=fact*i;
i++;
}
while (i<=num);
printf("\nThe factorial of %d is %d", num, fact);
}
OUTPUT:
Enter number to find its factorial:5
The factorial of 5 is 120
RESULT:
A C-program to calculate the factorial of a number using the ‘do…while’ loop was
successfully executed and the output was verified
AIM:
To write a C-program to implement linear search using a 1D array
ALGORITHM:
1. Start
2. Declare appropriate variables such as n, i, num and initialize an array a[100].
3. Obtain number of elements in array as input and store the value as an integer
in ‘n’.
4. Using ‘for’ loop, obtain ‘n’ number of elements as input and store each element
in a[i]
5. Obtain a target element for searching in the array and store it as an integer in
‘num’.
6. Using ‘for’ loop, iterate over the array to check if ‘num’=a[i], if so go to step 6.1
else go to step 7.
6.1. Display the target element and the position it was found in the array.
7. Display that the target element wasn’t found in the array of elements.
8. Stop
PROGRAM:
#include <stdio.h>
void main()
{
int i, n, num, a[100];
printf("Enter number of elements in array:");
scanf("%d", &n);
printf("\n Enter %d elements in array:", n);
for (i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter number to search in array:");
scanf("%d", &num);
for (i=0; i<=n; i++)
{
if (num==a[i])
{
printf("%d is found at index %d", num, i);
break;
}
if (i==n)
{
printf("%d is not found in array \n", num);
break;
}}}
OUTPUT:
Case 1:
Enter number of elements in array:4
Enter 4 elements in array:88 97 100 56
Enter number to search in array:100
100 is found at index 2
Case 2:
Enter number of elements in array:3
Enter 3 elements in array:1 2 3
Enter number to search in array:4
4 is not found in array
RESULT:
Hence the C-program to implement linear search using 1D array was successfully
executed and the output was verified.
AIM:
ALGORITHM:
1. Start
2. Declare appropriate variables.
3. Enter the rows and columns of the first matrix.
4. Enter the rows and columns of the second matrix.
5. Enter the elements of the first matrix
6. Enter the elements of the second matrix.
7. Print the elements of the first matrix in matrix form.
8. Print the elements of the second matrix in matrix form.
9. Set a loop up to row
10. Set up an inner loop to column.
11. Set up another inner loop up to the column.
12. Multiply the first and second matrix and store the elements in the third matrix.
13. Print the final matrix.
14. 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]);
}
}
printf("Product matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
RESULT:
Hence the C program to implement matrix multiplication by using 2D arrays was
successfully executed and the output was verified
EX NO: 9 MULTI-DIMENSIONAL ARRAYS
AIM:
To write a C program to implement multi-dimensional arrays.
ALGORITHM:
1. Start
2. Declare appropriate variables
3. Enter the rows and columns of the first matrix
4. Enter the rows and columns of the second matrix
5. Print the elements of the first matrix in matrix form
6. Print the elements of the second matrix in matrix form
7. Enter the block row and column number
8. Enter the position of the element which you want to update
9. Enter the new element to be replaced
10. Print array after updating
11. Stop
PROGRAM:
#include<stdio.h>
int i,j,k;
int num;
int main()
{
int arr[2][3][3];
printf("Enter the values in the array: \n\n");
for(i=1;i<=2;i++)
{
for(j=1;j<=3;j++)
{
for(k=1;k<=3;k++)
{
printf("The value at arr[%d][%d][%d]: ",i,j,k);
scanf("%d",&arr[i][j][k]);
}
}
}
printf("\n Printing the values in array: \n");
for(i=1;i<=2;i++)
{
for(j=1;j<=3;j++)
{
for(k=1;k<=3;k++)
{
printf("%d ",arr[i][j][k]);
if(k==3)
{
printf("\n");
}
}
}
printf("\n");
}
printf("\n Enter the block row and column number: \n");
scanf("%d %d %d ",&i,&j,&k);
printf("Enter the new number you want to update with: ");
scanf("%d",&num);
arr[i][j][k]=num;
printf("\n Array after updating: \n");
for(i=1;i<=2;i++)
{
for(j=1;j<=3;j++)
{
for(k=1;k<=3;k++)
{
printf("%d ",arr[i][j][k]);
if(k==3)
{
printf("\n");
}
}
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter the values in the array:
DEPARTMENT SUBJECT COLLEGE
IT PROGRAMMING IN C LABORATORY MSEC, C hennai-24
22
RESULT:
Hence the C program to implement multi-dimensional arrays was successfully
executed and the output was verified.
EX NO: 10 IMPLEMENTATION OF STRING OPERATIONS
AIM:
To write a series of C programs to implement various string operations.
ALGORITHM:
1. Start
2. To obtain length of string, initialize a string and declare variable length.
2.1. Using strlen(), length =strlen(string name).
2.2. Display length.
3. To concatenate two strings.
3.1. Initialize two strings as source and destination.
3.2. Using strcat() function, concatenate source into destination string.
3.3. Display destination.
4. To copy one string into another string.
4.1. Initialize a source string and an empty string.
4.2. Using strcpy(), copy the source string into the empty string.
4.3. Display destination string.
5. To compare two strings.
5.1. Initialize two strings along with variables of i, j, k.
5.2. Using strcmp(), compare the two strings.
6. Stop
PROGRAM:
Program:
i) String length using strlen()
#include<stdio.h>
#include<string.h>
int main()
{
int length;
char s[20] = “We are Here”;
length=strlen(s);
printf(“\Length of the string is = %d \n”, length);
return 0;
}
ii)Concatenation of strings using strcat()
#include<stdio.h>
#include<string.h>
int main()
{
char src[20]= “ before”;
char dest[20]= “after ”;
strcat(dest, src);
puts(dest);
return 0;
}
iii)Copying of one string to another using strcpy()
DEPARTMENT SUBJECT COLLEGE
IT PROGRAMMING IN C LABORATORY MSEC, C hennai-24
24
#include<stdio.h>
#include<string.h>
void main()
{
char src[20]= “ Destination”;
char dest[20]= “”;
printf(“\n source string is = %s”, src);
printf(“\n destination string is = %s”, dest);
strcpy(dest, src);
printf (“\n target string after strcpy() = %s”, dest);
}
iv)String compare using strcmp()
#include<stdio.h>
#include<string.h>
int main()
{
char str1[]=”copy”;
char str2[]=”Trophy”;
int i,j,k;
i=strcmp(str1, “copy”);
j=strcmp(str1, str2);
k-strcmp(str1, “f”);
printf(“\n %d %d %d”,i,j,k);
return 0;
}
OUTPUT:
i)
Length of string is = 11
DEPARTMENT SUBJECT COLLEGE
IT PROGRAMMING IN C LABORATORY MSEC, C hennai-24
25
ii)
After before
iii)
Source string is = Destination
Target string is =
Target string after strcpy() = Destination
iv)
0 -1 1
RESULT:
Hence the series of C programs to implement various string operations were
successfully executed and verified.
AIM:
To write a C program to convert temperature from Celsius to Fahrenheit and vice
versa using call by value in user defined functions.
PROCEDURE:
1. Start
2. Declare functions, main, covces, and covfah.
3. In the main function, declare appropriate variables of float data type, t1, t2, c, f.
4. Obtain temperature in Celsius from the user and store it in t1.
5. Similarly, obtain temperature in fahrenheit from the user and store it in t2.
6. Call the function covces by passing t1 into it. Store it in variable f.
6.1. In covces function, return ((t*9/5)+32) to the main function.
7. Call covfah function by passing t2 into it with the help of variable c.
7.1. In covfah function, return (((t-32)*5)/9) to the main function.
8. Display temperature converted to fahrenheit and celsius.
9. Stop.
PROGRAM:
#include<stdio.h>
float covces(float t)
{
return ((t*9/5)+32);
}
float covfah(float t)
{
return (((t-32)*5)/9);
}
int main()
{
float t1, t2,c,f;
printf("Enter Temperature in Celsius : ");
scanf("%f",&t1);
printf("\nEnter Temperature in Fahrenheit : ");
scanf("%f",&t2);
//Calling Function to convert Temperature From Celsius To Fahrenheit
f = covces(t1);
//Calling Function to convert Temperature From Fahrenheit To Celsius
c = covfah(t2);
//Display Result
printf("\nTemperature in Fahrenheit : %f",f);
printf("\nTemperature in Celsius : %f",c);
return 0;
}
Output:
Enter Temperature in Celsius : 37
Enter Temperature in Fahrenheit : 98
Temperature in Fahrenheit : 98.599998
DEPARTMENT SUBJECT COLLEGE
IT PROGRAMMING IN C LABORATORY MSEC, C hennai-24
27
RESULT:
Hence the c program to convert temperature from celsius to fahrenheit
and vice versa using call by value was executed successfully.
ALGORITHM:
1. Start
2. In the main function, declare appropriate variables of integer data
type; int
a = 100 and int b = 200.
3. Print the values of a and b as values before swap.
4. Call function swap, by passing variables a and b in it.
4.1. In the void swap function, declare an integer variable temp.
4.2. temp = *x
4.3. *x = *y
4.4. *y = temp
5. Print the values after swap.
6. Stop
PROGRAM:
#include <stdio.h>
int main () {
/* local variable definition */
int a = 100;
int b = 200;
printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b );
/* calling a function to swap the values */
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:
Before swap, value of a : 100
Before swap, value of b : 200
DEPARTMENT SUBJECT COLLEGE
IT PROGRAMMING IN C LABORATORY MSEC, C hennai-24
29
RESULT:
Hence the C program to swap two numbers using call by reference was
executed successfully.
AIM:
To write a C program to determine the average of five numbers using a
user defined function.
ALGORITHM:
1. Start
2. Declare a void function of 5 arguments of the integer data type.
3. In the main function, declare 5 variables a, b, c, d, e.
4. Obtain 5 numbers as input from the user and save them into the 5
variables
respectively.
5. Call the user defined function by passing the 5 variables.
6. In the user defined function, declare a variable as avg.
6.1. Calculate avg = (a+b+c+d+e)/5
6.2. Display avg.
7. Stop
PROGRAM:
#include <stdio.h>
void average(int, int, int, int, int);
void main()
{
int a, b, c, d, e;
printf("Enter five numbers:");
scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
average(a, b, c, d, e);
}
void average(int a, int b, int c, int d, int e)
{
float avg;
avg = (a+b+c+d+e)/5;
printf("The average of the five numbers is %.2f", avg);
}
OUTPUT:
Enter five numbers:1 2 3 4 5
DEPARTMENT SUBJECT COLLEGE
IT PROGRAMMING IN C LABORATORY MSEC, C hennai-24
31
RESULT:
Hence the C program to determine the average of 5 numbers using a
user defined
Function was successfully executed and verified.
AIM:
To write a C program to implement binary search using user defined
functions.
ALGORITHM:
1. Start
2. Declare a function for binary search with arguments int array[], int
start_index, int end_index, int element.
3. In the main function, initialize an array[]={1,4,7,9,16,56,70}, n=7
and
element = 16.
4. With the help of another variable, int found_index, call the binary
search
function by passing the array and element.
4.1. In the binary search function, using while function,
(start_index<=end_index)
4.1.1. int middle = start_index+(end_index - start_index)/2
4.2. If array[middle]==element, return middle to the main
function.
4.2.1. If array[middle]<element, start_index=middle+1.
4.2.2. Else end_index = middle-1
4.3. Return -1 to the main function.
5. If(found_index==-1), print element is not found in the array.
5.1. Else print the element is found at index.
6. Stop
PROGRAM:
#include <stdio.h>
int iterativeBinarySearch(int array[], int start_index, int end_index, int
element){
while (start_index <= end_index){
int middle = start_index + (end_index- start_index )/2;
clrscr();
if (array[middle] == element)
return middle;
if (array[middle] < element)
start_index = middle + 1;
else
end_index = middle - 1;
}
return -1;
}
int main(void){
OUTPUT:
RESULT:
Hence the C program to implement binary search using user defined
functions was executed successfully
AIM:
ALGORITHM:
1. Start
2. In the main function, initialize an array of size 100, i, n, tem.
3. Obtain n as the number of numericals to read, it should be less than
100.
4. Read the array using array as the pointer itself.
5. Obtain the numbers to read.
6. Using a for loop, print the array with each element.
7. Print the original array.
8. Using for loop print (*arr+i).
9. To reverse the array,
9.1. Using a for loop (i=0; i<n/2; i++), apply swapping of
variables
using the pointers.
10. Display reversed array.
11. Stop
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[100],i,n, temp;
clrscr();
printf("How many numbers to read? (< 100): ");
scanf("%d", &n);
/* Reading array using array itself as a pointer */
printf("Enter %d numbers:\n", n);
for(i=0;i< n;i++)
{
}
getch();
return 0;
}
OUTPUT:
How many numbers to read? (< 100): 5
Enter 5 numbers:
arr[0] = 1
arr[1] = 2
arr[2] = 5
arr[3] = 4
arr[4] = 3
Original array is:
12345
Reversed array is:
54321
RESULT:
Hence the C program to reverse an array using pointers was
successfully
Executed successfully.
AIM:
To write a C-program to check whether a string is a palindrome using
pointers.
ALGORITHM:
1. Start
2. Declare a void function with one argument, isPalindrome(char*string).
3. In the function, initialize pointers *ptr and *rev.
4. Set ptr=string.
5. Using a while loop (*ptr=0), increment ++ptr.
6. Using for loop, (rev==string; ptr>=rev;).
6.1 Using if conditional statement, (*ptr==*rev), decrement ptr and increment rev,
else break the loop.
PROGRAM:
#include <stdio.h>
#include<conio.h>
void isPalindrome(char* string)
{
char *ptr, *rev;
ptr = string;
clrscr();
while (*ptr != 0) {
++ptr;
}
DEPARTMENT SUBJECT COLLEGE
IT PROGRAMMING IN C LABORATORY MSEC, C hennai-24
39
--ptr;
for (rev = string; ptr >= rev;) {
if (*ptr == *rev) {
--ptr;
rev++;
}
else
break;
}
if (rev > ptr)
printf("String is Palindrome");
else
printf("String is not a Palindrome");
}
int main()
{ char str[1000] = "madam";
isPalindrome(str);
getch();
return 0;
}
OUTPUT:
String is a palindrome
RESULT:
Hence the C program to determine whether a string is a palindrome using
pointers Was successfully executed.
AIM:
To write a C program to generate a salary slip of employees using
structures and pointers.
ALGORITHM:
1. Start
2. Declares a structure, emp with appropriate variables, int empno, char
name[10], int bpay, allow, ded, npay.
3. In main function, declare struct emp e, *p along with int i, n.
4. Set p =&e
5. Obtain the number of employees and store it in n,
6. Using a for loop (i=0; i<n; i++), obtain employee number, name,
basic pay, allowances and deductions and store them in appropriate
variables.
7. Print the generation slip of employees.
8. Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct emp
{
int empno ;
char name[10] ;
int bpay, allow, ded, npay ;
};
void main()
{
struct emp e,*p;
int i, n ;
clrscr();
p=&e;
printf("Enter the number of employees : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the employee number : ") ;
scanf("%d", &(p+i)->empno) ;
printf("\nEnter the name : ") ;
scanf("%s", (p+i)->name) ;
printf("\nEnter the basic pay, allowances & deductions : ") ;
scanf("%d %d %d", &(p+i)->bpay, &(p+i)->allow, &(p+i)->ded) ;
(p+1)->npay = (p+i)->bpay + (p+i)->allow - (p+i)->ded ;
}
printf("\nEmp. No: Name: \t Basic pay: \t Allowances: \t Deductions: \t Nopay:
\n\n") ;
for(i = 0 ; i < n ; i++)
{
printf("%d \t %s \t %d \t %d \t %d \t %d \n", (p+i)->empno,
(p+i)->name, (p+i)->bpay, (p+i)->allow, (p+i)->ded, (p+i)->npay) ;
}
getch();
}
OUTPUT:
Enter the number of employees:1
Enter the employee number: 1544
Enter the name: XYZ
Enter the basic pay, allowances and deductions: 17000, 120 12
RESULT:
Hence the C program to generate the salary slip of an employee using
structures
and pointers was executed successfully.
ALGORITHM:
1. Start
2. Declare a structure of the number sub with int m1,m2, m3.
3. Declare a function, intervals, with three arguments (int m1, int m2,int
m3).
3.1. Declare int intervals, float result=0.
3.2. result = result+m1+m2+m3
3.3. internals = (result/150)*20
3.4. Return internals to the main function upon its function call.
4. In the main function, declare structure variable struct sub s, int i and
internal.
5. Using a for loop, print marks obtained in each subject out of 50 and
pass
the marks as arguments into the function call.
6. Print the calculated internal marks.
7. Stop
PROGRAM:
#include<stdio.h>
struct sub
{
int m1,m2,m3;
};
int internals(int m1,m2,m3)
{
int internals;
float result=0;
result=result+m1+m2+m3;
internals=(result/150)*20;
return internals;
}
void main()
{
struct sub s; //structure variable
int i,internal; //pointer to student structure
clrscr();
printf("\n Enter assessment marks obtained for 50 in each subject: ");
for (i=1;i<=5;i++)
{ printf ("\n Enter marks obtained in subject %d ",i);
printf("\n Enter assessment 1 mark");
scanf("%d",&s.m1);
printf("\n Enter assessment 2 mark");
scanf("%d",&s.m2);
printf("\n Enter assessment 3mark");
scanf("%d",&s.m3);
internal=internals(s.m1,s.m2,s.m3);
printf(“the internal is %d”,internal);
}
getch();
}
OUTPUT:
RESULT:
Hence the C program to compute the internal marks of a student using
structures
and functions was successfully executed.
ALGORITHM:
1. Start
2. Declare the structure, function and filename
3. To append data, declare the filename, structure, function.
4. Get the telephone number as an input from the user.
5. Close the file properly.
6. To show all the data, again declare the functions, filename and
structure.
7. Proceed the same step to find the data.
8. Display the specific record wanted by the user.
9. Print TELEPHONE DIRECTORY and the records.
10. Stop.
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <string.h>
struct person{
char name[20];
long telno;
};
void appendData(){
FILE *fp;
struct person obj;
clrscr();
fp=fopen("data.txt","a");
printf("*****Add Record****\n");
printf("Enter Name : ");
scanf("%s",obj.name);
printf("Enter Telephone No. : ");
scanf("%ld",&obj.telno);
fprintf(fp,"%20s %7ld",obj.name,obj.telno);
fclose(fp);
}
void showAllData(){
FILE *fp;
struct person obj;
clrscr();
fp=fopen("data.txt","r");
printf("*****Display All Records*****\n");
printf("\n\n\t\tName\t\t\tTelephone No.");
printf("\n\t\t=====\t\t\t===============\n\n");
while(!feof(fp))
{
fscanf(fp,"%20s %7ld",obj.name,&obj.telno);
printf("%20s %30ld\n",obj.name,obj.telno);
}
fclose(fp);
getch();
}
void findData(){
FILE *fp;
struct person obj;
char name[20];
int totrec=0;
clrscr();
fp=fopen("data.txt","r");
printf("*****Display Specific Records*****\n");
printf("\nEnter Name : ");
scanf("%s",&name);
while(!feof(fp))
{
fscanf(fp,"%20s %7ld",obj.name,&obj.telno);
if(strcmpi(obj.name,name)==0){
printf("\n\nName : %s",obj.name);
printf("\nTelephone No : %ld",obj.telno);
totrec++;
}
}
if(totrec==0)
printf("\n\n\nNo Data Found");
else
printf("\n\n===Total %d Record found===",totrec);
fclose(fp);
getch();
}
void main(){
char choice;
while(1){
clrscr();
printf("*****TELEPHONE DIRECTORY*****\n\n");
printf("1) Append Record\n");
printf("2) Find Record\n");
printf("3) Read all record\n");
printf("4) exit\n");
printf("Enter your choice : ");
fflush(stdin);
choice = getche();
switch(choice){
case'1' : //call append record
appendData();
break;
case'2' : //call find record
findData();
break;
case'3' : //Read all record
showAllData();
break;
case'4' :
case 27 : exit(1);
} }}
OUTPUT:
*****TELEPHONE DIRECTORY*****
1) Append Record
2) Find Record
3) Read all record
4) Exit
Enter your choice: 3
*****Display All Records*****
Name Telephone No.
===== =============
Rt 55
Welding 3217358
Welding 3217358
RESULT:
Hence the C program to create a telephone directory using files was
successfully
Executed.