Lab Manual - 21.06.2021
Lab Manual - 21.06.2021
Ex no: 1 a)
AIM:-
To write the C program to compute area of circle
ALGORITHM:-
Step1: Start the program
Step 2: Read the value of r
Step 3. Calculate area=pi*r*r
Step 4. Print area
Step 5. Stop the program
PROGRAM:-
#include <stdio.h>
#include <math.h>
int main()
scanf("%f", &radius);
area = 3.14159*radius*radius;
return 0;
OUTPUT:-
Radius=5
Area=78.500000
RESULT:-
Thus the C program to implement input /output statements and expressions are executed
and verified successfully.
Ex no: 1 b)
/*Program to illustrate the concept of puts() with gets() functions*/
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
puts("Enter the Name");
gets(name);
puts("Name is :");
puts(name);
}
Ex no: 1 c)
int main() {
int a;
float b;
double c;
char d;
return 0;
Ex no: 1 d)
#include <stdio.h>
int main() {
char c;
scanf("%c", &c);
return 0;
Ex no: 1 e)
C Program to swap two numbers without third
variable
#include<stdio.h>
int main()
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
return 0;
#include <stdio.h>
#include <conio.h>
void main()
int i=5;
Output:
Post Increment i=5
Pre Increment i=5
Post Decrement i=5
Pre Decrement i=3
Ex no 2 a)
Aim: To write a C program to generate Pascal's triangle.
#include <stdio.h>
void main()
{
int no_row,c=1,blk,i,j;
printf("Input number of rows: ");
scanf("%d",&no_row);
for(i=0;i<no_row;i++)
{
for(blk=1;blk<=no_row-i;blk++)
printf(" ");
for(j=0;j<=i;j++)
{
if (j==0||i==0)
c=1;
else
c=c*(i-j+1)/j;
printf("% 4d",c);
}
printf("\n");
}
}
Input number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Ex no 2 b)
Aim: To write a C program, which takes two integer operands and one operator from the user,
performs the operation and then prints the result. (Consider the operators +,-,*,/,% and use
Switch Statement)
AIM:-
To write a C program to design a calculator to perform the operations namely, addition,
subtraction, multiplication, division and square of a number.
ALGORITHM:-
Step 6: Stop.
PROGRAM:-
#include<stdio.h>
int main()
float divide;
subtract = a-b;
multiply = a*b;
divide = a/b;
return 0;
#include<stdio.h>
#include<conio.h>
void main()
int a,b;
int op;
clrscr();
scanf("%d %d",&a,&b);
scanf("%d",&op);
switch(op)
{
case 1 :
break;
case 2 :
break;
case 3 :
break;
case 4 :
break;
default :
break;
getch();
1.Addition
2.Subtraction
3.Multiplication
4.Division
Sum of 4 and 2 is : 6
RESULT:-
Ex no 2c)
Aim:To write a C program to find the sum of individual digits of a positive integer.
AIM:-
To write a program to find sum of digits
ALGORITHM:-
Step 1: Start the program.
Step 2: Declare the variables of respective data type.
n,r,sum->integer.
Step 3: Set sum=0
Step 4: If n>0 then
Step 5: Calculate r=n%10
Sum=sum+r
n=n/10
Step 6: Print sum
Step 7: Stop
PROGRAM:-
/*Sum of Digits*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
intn,r,sum=0;
clrscr();
printf("Enter the number:\n");
scanf("%d",&n);
while(n >0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("\nSum of digits =%d",sum);
getch();
}
OUTPUT :-
Sum of digits=11
RESULT:-
Thus the sum of digits using C program has been executed and verified successfully
Ex no 2 d)
Aim: A Fibonacci sequence is defined as follows: the first and second terms in the sequence are
0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write
a C program to generate the first n terms of the sequence.
1. #include<stdio.h>
2.
3. main()
4. {
5. int n, first = 0, second = 1, next, c;
6.
7. printf("Enter the number of terms\n");
8. scanf("%d",&n);
9.
10. printf("First %d terms of Fibonacci series are :-\n",n);
11.
12. for ( c = 0 ; c < n ; c++ )
13. {
14. if ( c <= 1 )
15. next = c;
16. else
17. {
18. next = first + second;
19. first = second;
20. second = next;
21. }
22. printf("%d\n",next);
23. }
24.
25. return 0;
26. }
Ex no 2 e)
Write a C program to generate all the prime numbers between 1 and n, where n is a value
supplied by the user.
#include<stdio.h>
int main(){
int num,i,count,n;
printf("Enter max range: ");
scanf("%d",&n);
for(num = 1;num<=n;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
return 0;
}
Sample output:
Enter max range: 30
2 3 5 7 11 13 17 19 23 29
Ex no 2 f)
3.Arrays
Ex no 3 a) Write a C program to search an array element using linear search.
#include <stdio.h>
#include<conio.h>
int main()
{
int array[100], search, c, n;
clrscr();
printf("Enter number of elements in array\n");
scanf("%d", &n);
Ex no 3 b) Write a C program to find both the largest and smallest number in a list of
integers.
#include<stdio.h>
#include<conio.h>
int main()
{
int a[50],i,n,large,small;
clrscr();
printf("How many elements:");
scanf("%d",&n);
printf("Enter the Array:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("The largest element is %d",large);
printf("\nThe smallest element is %d",small);
getch();
return 0;
}
return 0;
}
if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");
multiply[c][d] = sum;
sum = 0;
}
}
printf("\n");
}
}
getch();
return 0;
}
AIM: - Write a C - program to insert a sub-string in to a given main string from a given
position
Algorithm:
Step 1: Start
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
clrscr();
gets(str1);
l1 = strlen(str1);
gets(str2);
l2 = strlen(str2);
scanf("%d", &n);
str1[n + i] = str2[i];
str2[l2 + 1] = '\0';
getch();
}
Input & Output:
sachin
tendulkar
RESULT:-
Thus the a sub-string in to a given main string from a given position has been inserted using C
program and verified successfully
ii) To delete n Characters from a given position in a given string.
AIM: - Write a C - program to delete n Characters from a given position in a given string
Algorithm:
Step 1: Start
Step 5: string copy part of string from position to end, and (position + number of characters to
end)
Step 6: Stop
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char str[20];
int i, n, l, pos;
clrscr();
gets(str);
scanf("%d", &pos);
l = strlen(str);
str[i - n] = str[i];
str[i - n] = '\0';
getch();
sachin
RESULT:-
Thus the n Characters from a given position in a given string has been deleted using C program
and verified successfully
Ex no 4 b) Write a C program to determine if the given string is a palindrome or
not
Algorithm:
Step 1: start
Step 8: stop
Program:
#include <stdio.h>
#include <string.h>
#include<conio.h>
int main()
{
char a[100], b[100];
clrscr();
printf("Enter a string to check if it's a palindrome\n");
gets(a);
Input:
Enter a string
madam
Output:
madam is a palindrome
RESULT:-
Thus the given string whether palindrome or not has been executed using C program and
verified successfully
5.Functions &Pointers:
Write C programs that use recursive functions
ALGORITHM:
Main Program
Step 1: start
Step 2: read n
Step 5: stop
Sub Program:
Program
#include <stdio.h>
int factorial(int n);
int main()
{
int n,result;
printf("Enter a positive integer: ");
scanf("%d", &n);
result=factorial(n);
printf("Factorial of %d = %d", n,result);
return 0;
}
int factorial( int n ) /*Function Definition*/
{
int temp ;
if( n == 0)
return 1 ;
else
temp = n * factorial( n-1 ) ; // Recursive Function Call
return temp ;
}
output:
Enter a positive integer: 5
Factorial of 5 = 120
RESULT:-
Thus the given number’s factorial value using C program has been executed and verified
successfully
Ex no 5b) To solve Towers of HanoiProblem.
AIM:-
PROGRAM:-
#include <stdio.h>
#include<conio.h>
void hanoi(int , char , char , char );
void main()
{
int n = 4; // Number of disks
hanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
getch();
}
RESULT:-
Thus the C program to solve the towers of Hanoi problem is developed and
executed successfully using Turbo C IDE.
Ex no 5 c) To swap the variables using call by value and call by reference.
Aim:-
Algorithm:
Main Function
Step 1: Start
Step 6: call sub program as swap(num1, num2) by passing arguments using call
by value method
Step 8: Stop
Sub Function
Program:
#include <stdio.h>
#include<conio.h>
void swap(int,int) ;
void main()
{
int num1, num2 ;
num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
swap(num1,num2) ;
printf("\nAfter swap: num1 = %d\nnum2 = %d", num1, num2);
getch() ;
}
void swap(int a, int b) // called function
{
int temp ;
temp = a ;
a=b;
b = temp ;
}
Output:
Result:
Thus the program to swap the variables using call by value in C programming is
developed and executed successfully.
Algorithm:
Main Function
Step 1: Start
Step 6: call sub program as swap(num1, num2) by passing arguments using call
by reference method
Step 8: Stop
Sub Function
Program:
#include <stdio.h>
#include<conio.h>
void swap(int *,int *) ;
void main()
{
int num1, num2 ;
num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
swap(&num1,&num2) ;
printf("\nAfter swap: num1 = %d\nnum2 = %d", num1, num2);
getch() ;
}
void swap(int *a, int *b)
{
int temp ;
temp = *a ;
*a = *b ;
*b = temp ;
}
output:
Before swap: num1 = 10, num2 = 20
After swap: num1 = 20
num2 = 10
Result:
Thus the program to swap the variables using call by reference in C programming
is developed and executed successfully.
6.
Ex no 6a) Generate mark sheet of students using structures.
Ex no 6b). Compute salary slip for five employees using structures and functions.
AIM:-
Write a C program to Generate Salary Slip of Employees Using Structures and Pointers.
ALGORITHM:-
1. Start the program
2. Declare a structure with structure elements to hold values of emp ID, name, basic, HRA,
DA, PF, GROSS, NET
3. Get the user input for the elements in step 2
4. Calculate the Gross salary by adding the percentage of basic salary with respect to HRA
and DA
5. Calculate the net salary by deducting the percentage of PF from the basic salary
6. Print the employee salary slip details
7. Stop the program
PROGRAM CODING:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct employee {
int empId;
char name[32];
int basic, hra, da;
int pf;
float gross, net;
};
void printSalary(struct employee e1)
{
printf("Salary Slip of %s:\n", e1.name);
printf("Employee ID: %d\n", e1.empId);
printf("Basic Salary: %d\n", e1.basic);
printf("House Rent Allowance: %d\n", e1.hra);
printf("daily allowance:%d",e1.da);
printf("\nProvident Fund : %d\n", e1.pf);
printf("Gross Salary: %.2f Rupees\n", e1.gross);
printf("\nNet Salary: %.2f Rupees\n\n", e1.net);
return;
}
void main()
{
int i, ch, num, flag, empID;
struct employee *e1;
clrscr();
printf("Enter the number of employees:");
scanf("%d", &num);
e1 = (struct employee *)malloc(sizeof(struct employee) * num);
for (i = 0; i < num; i++)
{
printf("Employee ID:");
scanf("%d", &(e1[i].empId));
printf("Employee Name:");
scanf("%s",e1[i]);
e1[i].name[strlen(e1[i].name) - 1] = '\0';
printf("Basic Salary");
scanf("%d", &(e1[i].basic));
printf("House Rent Allowance in %");
scanf("%d", &(e1[i].hra));
printf("Daily Allowance in %");
scanf("%d", &(e1[i].da));
printf("Provident Fund in %:");
scanf("%d",&e1[i].pf);
printf("\n");
}
for (i = 0; i < num; i++)
{
e1[i].gross = e1[i].basic + ((e1[i].hra * e1[i].basic) / 100) + ((e1[i].da *
e1[i].basic) / 100 );
e1[i].net = e1[i].gross-(e1[i].basic*e1[i].pf/100) ;
}
while (1)
{
printf("Enter employee ID to get payslip:");
scanf("%d", &empID);
flag = 0;
for (i = 0; i < num; i++)
{
if (empID == e1[i].empId)
{
printSalary(e1[i]);
flag = 1;
}
}
if (!flag)
{
printf("No Record Found!!\n");
}
printf("Do You Want To Continue(1/0):");
scanf("%d", &ch);
if (!ch)
{
break;
}
}
getch();
}
OUTPUT:-
Enter the number of employees : 1
Employee ID : 123
Employee name : bala
Basic salary : 10000
House rent allowance in % : 80
Daily allowance in % : 20
Provident fund in % : 14
Thus the program to generate the salary slip of the employees using structure and pointers
using C programming is developed and executed successfully.
7.
Ex no 7) Insert, Update, delete and append telephone details of an individual or a company into
a telephone directory using random access file
Ex no 8)CBS