0% found this document useful (0 votes)
14 views55 pages

PC Lab Manual 2022 Final

The document outlines various C programming exercises, including algorithms and code for tasks such as calculating the sum of digits, evaluating expressions, checking odd/even numbers, generating Fibonacci series, determining leap years, performing arithmetic operations, checking Armstrong numbers, sorting based on weight, and calculating BMI. Each exercise includes a clear aim, algorithm, program code, output examples, and a result statement confirming successful execution. The exercises serve as practical applications of fundamental programming concepts.

Uploaded by

icedkrish18
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)
14 views55 pages

PC Lab Manual 2022 Final

The document outlines various C programming exercises, including algorithms and code for tasks such as calculating the sum of digits, evaluating expressions, checking odd/even numbers, generating Fibonacci series, determining leap years, performing arithmetic operations, checking Armstrong numbers, sorting based on weight, and calculating BMI. Each exercise includes a clear aim, algorithm, program code, output examples, and a result statement confirming successful execution. The exercises serve as practical applications of fundamental programming concepts.

Uploaded by

icedkrish18
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/ 55

EX.

NO: 1 SUM OF INDIVIDUAL DIGITS OF A POSITIVE INTEGER


DATE :
AIM:
To write a C program to find the sum of individual digits of a positive integer.
ALGORITHM:
1. Read the number n
2. Initialize sum  0
3. while n > 0
4. d = n%10
5. sum = sum+d
6. n =n/10
7. print sum.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n, sum=0,d;
clrscr();
printf(“Enter any integer:”);
scanf(“%d”, &n); while(n>0)
{
d=n%10;
sum=sum+d;
n=n/10;
}
Printf(“sum of individual digits is %d”,sum);
getch();
}
OUTPUT:
Enter any integer: 1234
Sum of individual digits is: 10

RESULT:
Thus the program to find sum of single digit of a number was successfully executed and verified.
1
EX.NO: 2 EXPRESSION EVALUATION
DATE :
AIM:
To write a C program to evaluate the given expression
ALGORITHM:
Step-1 Start the program.
Step-2 Input the values for declared variables
Step-3 Substitute the values in expression and calculate the results.
Step-4 Print the results
Step-5 Stop
PROGRAM:
/* Expression Evaluation*/
#include<stdio.h>
main()
{
int a,b,c,d;
float x,y,z;
printf ("Enter the values for a,b,c,d\n");
scanf("%d%d%d%d", &a,&b,&c,&d);
x = (a * b) – c;
y = (b/c) * a;
z = (a - b) / (c + d)
printf(" The value of x is %f “ ,x );
printf(“The value of y is %f “ ,y );
printf(”The value of z is %f “,z );
}

OUTPUT:
Enter the value for a,b,c,d
a= 5; b = 6; c= 12;d=2
x = 28.000; y =
2.5000 z =
0.0555

RESULT:
Thus the program to evaluate the given expression was performed and executed successfully.

2
EX.NO: 3 DECISION MAKING
DATE:

AIM:
To write a C program to check whether given Number is odd or even.
ALGORITHM:
1: Declare a variable to get a Number
2: Read the input
3: Get the remainder of given number using modulo operator
4: If remainder is 0 prints “Even Number”, else print “Odd Number”.
PROGRAM:
#include<stdio.h>
main()
{
int a,rem;
printf("Enter a Number\n");
scanf("%d",&a);
rem=a%2;
if(rem==0)
printf("The Given Number is Even"); else
printf("The Given Number is Odd");
}
OUTPUT:
Enter a Number 13
The Given Number is Odd

RESULT:
Thus the program to check whether given Number is odd or even was performed and
executed successfully.

3
EX.NO: 4 FIBONACCI SERIES
DATE :
AIM:
To write a C program to generate the first n terms of the sequence.

ALGORITHM
1. Read the number of terms n
2. Initialize a 0, b  1
3. print a and b values
4. for i  3 to n
a. increment the i value
b. c a+b
c. print c value
d. a  b
e. b  c
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{int a=0,b=1,c,n,i;clrscr();
printf(“Enter no. of
terms:”);scanf(“%d”, &n);
printf(“The Fibonacci
sequenceis:”);
printf(“%d%d”, a,b);
for(i=3;i<=n;i++)
{
c=a+b; printf(“%d”,c);
a=b;
b=c;
}
getch();
}
OUTPUT:
Enter no of items: 5
The Fibonacci sequence is0 1 1 2 3

RESULT
Thus the program for Fibonacci series was successfully executed and verified.

4
EX.NO:5 THE GIVEN YEAR IS LEAP YEAR OR NOT
DATE:
AIM:
To write a C program to check whether the given year is leap year or not.
ALGORITHM:
Step 1: Start the Program
Step 2: Enter the year
Step 3: check the given year is divisible by 4, it denotes leap year.
Step 4: Otherwise it is not leap year.
Step 5: print the result
Step 7: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("enter the year\n");
scanf("%d",&year);
if(year%4==0 &&year%100 !=0|| year%400==0)
printf("leap year");
else
printf("not leap year");
getch();
}

OUTPUT:
Enter the year = 2016
Leap year

RESULT:
Thus the C program for the given year is leap year or not was performed and
executed successfully.

5
EX.NO:6 DEMONSTRATE ARITHMETIC OPERATIONS (USING SWITCH…CASE)
DATE:
AIM:
To write a C program for demonstrating arithmetic operations using switch case statement.
ALGORITHM:
Step1: Start the program
Step 2: Display menu showing addition, subtraction, multiplication and division
operation.
Step-3: Get the values for two variables
Step 4: Obtain the choice from the user and accordingly switch over to
particular
block.
Step 5: Display the result.
Step 6: If the user wishes to continue repeat steps
2 and3 Step 7: Stop
PROGRAM:
/* Program to demonstrate arithmetic operations */
#include<stdio.h>
#include<conio.h
> void main()
{
int a, b, c,
n;
clrscr();
printf(“1. Addition\n”);
printf(“2. Subtraction\n”);
printf(“3. Multiplication\n”);
printf(“4. Division\n”);
printf(“5.Square of a
No\n:”);
printf(“0. Exit\n”);
printf(“Enter your choice :
“); scanf(“%d”,&n);
printf(“Enter the two numbers
:”); scanf(“%d%d”,&a,&b);
switch(n)
{
case 1:
c = a + b;
printf(“Addition :%d\n”,c);
break;
case 2:
c = a – b;
printf(“Subtraction
:%d\n”,c); break;
case 3:
c = a * b;
printf(“Multiplication :%d\n”,c);
6
break;
case 4:
c = a / b;
printf(“Division :%d\n”,c);
break;
case 5:
c=a*a;
printf(“Square : %d\n”,a);
case
0:
exit(0)
;
break;
}
getch();
}
OUTPUT:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Square
0. Exit
Enter Your Choice : 1
Enter the 2 nos a and
b: 28
Addition : 10.
Enter Your Choice : 2
Enter the 2 nos a and
b: 52
Subtraction : 3.
Enter Your Choice : 3
Enter the 2 nos a and
b: 28
Multiplication : 16.
Enter Your Choice : 4.
Enter the 2 nos a and
b: 84
Division : 2.
Enter your Choice: 5
Enter the number a :
2
Square : 4
Enter Your Choice : 0.
Exit.
RESULT:
7
Thus the C program for demonstrating arithmetic operations using switch case
statement was performed and executed successfully.

EX.NO: 7 ARMSTRONG NUMBER


DATE:
AIM:
To write a C program to check whether the given number is Armstrong number or not.
ALGORITHM:
Step-1 Start the program
Step-2 Enter the number up yo which Armstrong numbers are to be
generated. Step-3 Set a loop upto the number
Step-4 Sum the cube of each individual digit of the number and store the
sum in s. Step-5 Check whether the entered digit and calculated sum are
equal.
Step-6 Find whether the reverse number is equal to the given number. If equal the
number is Armstrong else not an Armstrong number
Step-7 Increment the loop and perform steps 5 and 6 till the end of loop is
reached.
Step-8 Stop.
PROGRAM:
#include <stdio.h>
int main()
{
int number, originalNumber, remainder, result = 0;
printf("Enter a three digit integer: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}
if(result == number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);
OUTPUT:
Enter a three digit integer: 371
371 is an Armstrong number.

8
RESULT:
Thus the program to check whether the given number is present or not was performed
and executed successfully.

EX.NO: 8 SORT THE NUMBERS BASED ON WEIGHT


DATE:
AIM:
To write a C program to sort the numbers based on weight in the increasing order.
ALGORITHM:
Step -1 Read the weight of a person
Step-2 Check whether the given weight is perfect square of 5
Step-3 Check whether the weight is divisible by 3 and 4
Step-4 Print the weight is increasing order
Step-5 stop
PROGRAM:
#include<stdio.h>
#include<math.h>
int getWeight(int n)
{
int w=0;
float root=sqrt(n);
if(root==ceil(root))
w+=5;
if(n%4==0&&n%6==0)
w+=4;
if(n%2==0)
w+=3;
return w;
}
void main()
{
int nums[15];
int ws[15];
int i,j,t,n;
printf("Enter number of numbers");
scanf("%d",&n);
printf("\nEnter numbers");
for(i=0;i<n;i++)
scanf("%d",&nums[i]);
for(i=0;i<n;i++)
ws[i]=getWeight(nums[i]);
printf("\nBefore sorting:\n");
for(i=0;i<n;i++)
printf("%d:%d\t",nums[i],ws[i]);
for(i=0;i<n;i++)
9
for(j=0;j<n-i-1;j++)
if(ws[j]>ws[j+1])
{
t=ws[j+1];
ws[j+1]=ws[j];
ws[j]=t;
t=nums[j+1];
nums[j+1]=nums[j];
nums[j]=t;
}
printf("\nSorted:\n");
for(i=0;i<n;i++)
printf("%d:%d\t",nums[i],ws[i]);
}
OUTPUT:
Enter the Number of Numbers
3
Enter the Number
34
56
25
Sorted
25
34
56

10
RESULT:
Thus the program to sort the numbers based on weight in the increasing order was
performed and executed successfully.
EX.NO:9 HEIGHT AND WEIGHT OF THE PERSONS
DATE:
AIM:
To write a C program to check the numbers of persons height, weight and average height
ALGORITHM:
Step-1 Take the height of a person as input and store it in the variable height.
Step-2 If the variable height is lesser than 150 cm, then print the output as “Dwarf”.
Step-3 If the variable height is lesser than or equal to 165 cm and greater than or equal to
150 cm,
then print the output as “Average Height”.
Step-4 If the variable height is lesser than or equal to 195 cm and greater than 165 cm,
then print the
output as “Taller”.
Step-5 Stop.
PROGRAM :
#include < stdio.h>
void main()
{
float height;
printf(“Enter the height (in centimeters) \n”);
scanf(“%f”,&height );
if (height < 150.0)
printf(‘Dwarf\n”);
else if ((height>=150.0)&& (height <=165.0)
printf(“average height \n”);
else if ((height > 165.0) && (height <=195.0))
printf(“Taller \n”);
else
printf(“Abnormal height “);
}
OUTPUT:
Enter the Height (in centimetres)
165
Average Height
Enter the Height (in centimetres)
140
Dwarf
11
Enter the Height (in centimetres)
190
Taller

RESULT:
Thus the C program to check the numbers of persons height, weight and average
height was performed and executed successfully.

EX.NO:10 COMPUTE THE BODY MASS INDEX OF THE INDIVIDUALS


DATE:
AIM:
To write a C program with height and weight of persons and compute the body mass
index of the individuals.
ALGORITHM:
Step-1 : Read the values of weight and Height
Step-2 : Calculate BMI=Weight/Height/Height
Step-3 : Print the value of BMI
Step-4: Stop
PROGRAM:
/* Calculation of BMI (Body Mass Index) */
#include <stdio.h>
double bmi_cal(double h, double w) {
// BMI = weight(kg) / height(m) / height(m)
return w/h/h;
}
int main(void) {
double height, weight, bmi;
printf("Input height(m) and weight(kg) ");
scanf("%lf%lf", &height, &weight);
// Calculation of BMI
bmi = bmi_cal(height, weight);
printf("%5.2f m, %6.2f kg: BMI = %6.2f\n", height, weight, bmi);
return (0);
}
OUTPUT:
Input height(m) and weight(kg) 1.65 70
1.65 m, 70.00 kg: BMI = 25.71
12
RESULT:
Thus the program for body mass index was executed successfully.

EX.NO:15 SORT THE LIST OF NUMBERS USING PASS BY REFERENCE


DATE:
AIM:
To write a C program to sort the list of numbers using pass by reference.
ALGORITHM:
Step-1 : Read the list of numbers
Step-2 : Call the function sort
Step-3: Sort the values using temporary variable
Step-4: Print the values
Step-5 : Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void sort(int m, int x[ ]);
void main()
{
int i;
int marks[5] = {40, 90, 73, 81, 35};
printf("Marks before sorting\n");
for(i = 0; i < 5; i++)
printf("%d ", marks[i]);
printf("\n\n");
sort (5, marks);
printf("Marks after sorting\n");
for(i = 0; i < 5; i++)
printf("%4d", marks[i]);
printf("\n");
}
void sort(int m, int x[ ])
{
int i, j, t;
for(i = 1; i <= m-1; i++)
for(j = 1; j <= m-i; j++)
if(x[j-1] >= x[j])
{
t = x[j-1];
13
x[j-1] = x[j];
x[j] = t;
}
}
OUTPUT:
Marks before sorting
40 90 73 81 35
Marks after sorting
35 40 73 81 90
RESULT:
Thus the program for sorting using pass by reference was executed successfully.
EX.NO:16 EMPLOYEE PAYROLL USING STRUCTURE
DATE:
AIM:
To write a C program to generate employee payroll using structures.
ALGORITHM:
Step-1 Start the program
Step-2 Create a Structure named Employee containing records Id,name, Basic
Salary, Net Salary, HRA, DA and Tax.
Step-3 Get the employee Id and retrieve his details.
Step-4 Using the basic salary, DA, HRA, Tax calculate tax and net salary
Step-5 Display the details each employee record containing name, number,
basic salary, HRA, DA, Net Salary and Tax
Step-6 Stop.
PROGRAM:
#include<stdio.h>
#include<dos.h>
struct employee
{
int NO;
char NAME[10];
int DESIGN_CODE;
int DAYS_WORKED;
}EMP[12]={
{1,"GANESH",1,25},
{2,"MAHESH",1,30},
{3,"SURESH",2,28},
{4,"KALPESH",2,26},
{5,"RAHUL",2,24},
{6,"SUBBU",2,25},
{7,"RAKESH",2,23},
{8,"ATUL",2,22},
{9,"DHARMESH",3,26},
{10,"AJAY",3,26},
{11,"ABDUL",3,27},
{12,"RASHMI",4,29}
};
void main()
{
14
int EMPNO;
void gen_payslip(int);
clrscr();
printf("ENTER THE EMPLOYEE NO TO GENERATE PAYSLIP : ");
scanf("%d",&EMPNO);
if(EMPNO>0 && EMPNO<13)
gen_payslip(EMPNO);
else
printf("\nYOU HAVE ENTERED WRONG EMP NO. !!");
getch();
}
void gen_payslip(int EMPNO)
{
struct date D;
char DESG[10];
float NETPAY,BASIC,PF,PAYRATE,PTAX=200;
getdate(&D);
printf("\n\n\t\t\tSHREE KRISHNA CHEMISTS AND DRUGGIST");
printf("\n\t\t\t\tSALARY MONTH %d %d\n",D.da_mon,D.da_year);
printf("\n\n\tEMP.NO.: %d\t\tEMP.NAME: %s",EMPNO,EMP[EMPNO-1].NAME);
switch(EMP[EMPNO-1].DESIGN_CODE)
{
case 1: PAYRATE=400;
printf("\tDESIGNATION: CLERK");
break;
case 2: PAYRATE=300;
printf("\tDESIGNATION: SALESMEN");
break;
case 3: PAYRATE=250;
printf("\tDESIGNATION: HELPER");
break;
case 4: PAYRATE=350;
printf("\tDESIGNATION: COMP.OPTR");
break;
}
BASIC=PAYRATE*EMP[EMPNO-1].DAYS_WORKED;
PF=BASIC/10;
printf("\n\n\tDAYS WORKED: %d",EMP[EMPNO-1].DAYS_WORKED);
printf("\t\tPAY RATE: %.0f\t\tGEN.DATE:%d/%d/%d ",PAYRATE,D.da_day,D.da_mon,D.da_year);
printf("\n\t______________________________________________________________________");
printf("\n\n\tEARNINGS\tAMOUNT(RS.)\t\tDEDUCTIONS\tAMOUNT(RS.)");
printf("\n\t______________________________________________________________________");
printf("\n\n\tBASIC PAY\t%.0f\t\t\tP.F.\t\t%.0f",BASIC,PF);
printf("\n\n\t\t\t\t\t\tPROF.TAX\t%.0f",PTAX);
printf("\n\n\t______________________________________________________________________");
printf("\n\n\tGROSS EARN.\t%.0f\t\t\tTOTAL DEDUCT.\t%.0f",BASIC,PF+PTAX);
NETPAY=BASIC-(PF+PTAX);
printf("\n\t\t\t\t\t\tNET PAY\t\t%.0f",NETPAY);
printf("\n\t______________________________________________________________________");
}
OUTPUT:
Employee Details:
Enter the employee name : Robin
Enter the employee : 100
15
Id
Enter the basic :
salary 30000
Employee name : Robin
Employee Id : 100
Employee Basic salary : 30000.000000
HRA : 3000.000000
:
10,500.00000
DA 0
Tax : 4500.000000
Gross salary : 39000.000000
RESULT:
Thus the C program to generate employee payroll using structures was performed
and executed successfully.
EX.NO:19 COMPUTE INTERNAL MARKS OF STUDENTS
DATE:
AIM:
To write a C program to display the student internal marks using structures.
ALGORITHM:
Step-1: Read the values of internal marks
Step-2: Calculate internal marks of each student
Step-3: Print the marks of each student
Step-4: Stop
PROGRAM:
#include<stdio.h>
struct student
{
int sub1;
int sub2;
int sub3;
};
void main()
{
struct student s[10];
int i,total=0;
clrscr();
for(i=0;i<=2;i++)
{
printf("\nEnter Marks in Three Subjects = ");
scanf("%d%d%d",& s[i].sub1,&s[i].sub2,&s[i].sub3);
total=s[i].sub1+s[i].sub2+s[i].sub3;
printf("\nTotal marks of s[%d] Student= %d",i,total);
}
getch();
}
16
OUTPUT:

RESULT:
Thus the C program for student internal marks was executed successfully.
EX.NO:20 TELEPHONE DIRECTORY
DATE:
AIM:
To write a C program for telephone directory to perform insert, delete and append
operations.
ALGORITHM:
Step-1: Read the record number,name and age of a person
Step-2: Insert the biodata in to the file
Step-3: Display the file
Step-4:Stop
PROGRAM:
#include <stdio.h>
#include <conio.h>
struct biodata{
int recno,age;
char name[20],sex;
float salary;
};
void main()
{
void addData(void);
void delData(void);
void showAll(void);
void showRecord(void);
void alterData(void);
char choice;
clrscr();
while(1){
clrscr();
17
textcolor(BLACK);
cprintf(" B I O - D A T A\r\n");
printf("\n\n*****CHOOSE YOUR CHOICE*****\n");
printf("1) ADD DATA\n");
printf("2) DELETE DATA\n");
printf("3) SHOW ALL\n");
printf("4) SHOW RECORD\n");
printf("5) ALTER DATA\n");
printf("6) Exit \n");
printf("Enter your choice : ");
fflush(stdin);
choice = getche();
switch(choice){
case'1' : //call add data
addData();
break;
case'2' : //call delete databreak;
case'3' : //call show all data
showAll();
break;
case'4' : //call show record
showRecord();
break;
case'5' : //call alter databreak;
case'6' :
case 27 : clrscr();
gotoxy(25,10);
_setcursortype(_NOCURSOR);
textcolor(LIGHTMAGENTA);
cprintf("THANKS FOR USING THIS SOFTWARE");
getch();
exit(1);
}
}
}
//Adding Record to Filevoid addData(){
FILE *fp;
struct biodata obj;
fp = fopen("biodata.txt","a+t");
clrscr();
printf("\n*****ADDING DATA*****\n");
printf("\nEnter Record No : ");
scanf("%d",&obj.recno);
printf("Enter Name : ");
fflush(stdin);
scanf("%s",obj.name);
printf("Enter age : ");
scanf("%d",&obj.age);
fflush(stdin);
printf("Enter Sex : ");
scanf("%c",&obj.sex);
printf("Enter Salary : ");
scanf("%f",&obj.salary);
18
fscanf(stdin,"%d %s %d %c %f",&obj.recno,obj.name,&obj.age,&obj.sex,&obj.salary);
fprintf(fp,"%d %s %d %c %f",obj.recno,obj.name,obj.age,obj.sex,obj.salary);
fclose(fp);
}
void showRecord(){
FILE *fp;
struct biodata obj;
int rec;
long pos;
fp = fopen("biodata.txt","r");
clrscr();
printf("\n*****SHOWING SPECIFIC RECORD*****\n");
printf("\nEnter Record No : ");
scanf("%d",&rec);
pos = rec * sizeof(obj);
fseek(fp,pos,SEEK_SET);
if(feof(fp)==0)
printf("\n\nNO DATA FOUND\n");
else{
fscanf(fp,"%d %s %d %c %f",&obj.recno,obj.name,&obj.age,&obj.sex,&obj.salary);
printf("\n\n\tRecord No : %d\n",obj.recno);
printf("\tName : %s\n",obj.name);
printf("\tAge : %d\n",obj.age);
printf("\tSex : %c\n",obj.sex);
printf("\tSalary : %f\n",obj.salary);
}
getch();
fclose(fp);
}
void showAll(){
FILE *fp;
struct biodata obj;
int totrec,i;
fp = fopen("biodata.txt","r");
clrscr();
fseek(fp,0,SEEK_END);
totrec=ftell(fp)/sizeof(obj);
printf("\n*****SHOWING ALL RECORD*****\n");
printf("\n\nRecord_No\tName\t\tAge\tSex\tSalary\n\n");
printf("\n\n%d\n",totrec);
for(i=1;i<=totrec;i++){
fscanf(fp,"%d %s %d %c %f",&obj.recno,obj.name,&obj.age,&obj.sex,&obj.salary);
fprintf(stdout,"%-15d %-15s %-8d %-2c %10.2f\n",obj.recno,obj.name,obj.age,obj.sex,obj.salary);
}
getch();
fclose(fp);
}
OUTPUT:
BIO-DATA
CHOOSE YOUR CHOICE
("1) ADD DATA\n");
19
("2) DELETE DATA\n");
("3) SHOW ALL\n");
("4) SHOW RECORD\n");
("5) Exit
1
("\n*****ADDING DATA*****\n");
("\nEnter Record No : ");
234
("Enter Name : ");
Raja
("Enter age : ");
35
("Enter Sex : ");
F
("Enter Salary : ");
35000
RESULT:
Thus the program for telephone details was executed successfully.
EX.NO:21 BANK ACCOUNT DETAILS
DATE:
AIM:
To write a C program to count the number of account holders whose balance is less than
the minimum balance using sequential access file.
ALGORITHM:
Step-1: Read the account holder’s name
Step-2: Check the account holder’s balance
Step-4: Whether the balance is low means display the account holders name.
Step-5.Stop
PROGRAM:
\* Program in C to show the bank operation using structure with array and Function. *\
# include < stdio.h >
# include < conio.h >
void creation( ) ;
void deposit( ) ;
void withdraw( ) ;
void lowbal( ) ;
int a = 0 , i = 1001 ;
struct bank
{
int no ;
char name[20] ;
float bal ;
float dep ;
} s[100];
int main( )
{
20
int ch ;
do
{
printf(" \n**************************** ") ;
printf(" \nBANKING ") ;
printf(" \n**************************** ") ;
printf(" \n1. Create New Account ") ;
printf(" \n2. Cash Deposit ") ;
printf(" \n3. Cash Withdraw ") ;
printf(" \n4. Low Balance Enquiry ") ;
printf(" \n5. Exit ") ;
printf(" \nEnter your choice : ") ;
scanf("%d ",& ch) ;
switch ( ch)
{
case 1 : creation( ) ;
break ;
case 2 : deposit( ) ;
break ;
case 3 : withdraw( ) ;
break ;
case 4 : lowbal( ) ;
break ;
case 5 : ;
break ;
defalut : printf(" Choice a Valid option !! ") ;
break ;
getch( ) ;
}
} while( ch != 5 ) ;
}
void creation( )
{
printf(" \n**************************** ") ;
printf(" \nNEW ACCOUNT CREATION ") ;
printf(" \n**************************** ") ;
printf(" \nYour Account Number is :%d ",i) ;
s[a].no = i ;
printf(" \nEnter your Name : ") ;
scanf("%s ",& s[a].name) ;
printf(" \nYour Deposit is Minimum Rs.500") ;
s[a].dep=500 ;a++ ;
i++ ;
getch( ) ;
}
void deposit( )
{
int no, b = 0, m = 0 ;
int aa ;
printf(" \n**************************** ") ;
printf(" \nCASH DEPOSIT ") ;
printf(" \n**************************** ") ;
printf(" \nEnter your Account Number : ") ;
21
scanf("%d ",& no) ;
for ( b = 0 ; b < i ; b++)
{
if ( s[b].no == no)
m=b;
}
if ( s[m].no == no)
{
printf("\n Account Number : %d ",s[m].no) ;
printf("\n Name : %s ",s[m].name) ;
printf("\n Deposit : %f ",s[m].dep) ;
printf(" \nDeposited Amount : ") ;
scanf("%f ",& &aa) ;
s[m].dep+=aa ;
printf("\nThe Balance in Account is : %f ",s[m].dep) ;
getch( ) ;
}
else
{
printf("\nACCOUNT NUMBER IS INVALID ") ;
getch( ) ;
}
}
void withdraw( )
{
int no, b = 0, m = 0 ;
int aa ;
printf(" \n**************************** ") ;
printf(" \nCASH WITHDRAW ") ;
printf(" \n**************************** ") ;
printf(" \nEnter your Account Number : ") ;
scanf("%d ",& no) ;
for ( b = 0 ; b < i ; b++)
{
if ( s[b].no == no)
m=b;
}
if ( s[m].no == no)
{
printf("\n Account Number : %d ",s[m].no) ;
printf("\n Name : %s ",s[m].name) ;
printf("\n Deposit : %f ",s[m].dep) ;
printf(" \nWithdraw Amount : ") ;
scanf("%f ",& aa) ;
if ( s[m].dep < aa+500)
{
printf("\nCANNOT WITHDRAW YOUR ACCOUNT HAS MINIMUM BALANCE ") ;
getch( ) ;
}
else
{
s[m].dep-=aa ;
printf("\nThe Balance Amount in Account is : %f ",s[m].dep) ;
}
22
}
else
{
printf("\nACCOUNT NUMBER IS INVALID ") ;
getch( ) ;
}
getch( ) ;
}
void lowbal( )
{
int no, b = 0, m = 0 ;
int aa ;
printf(" \n**************************** ") ;
printf(" \nFOLLOWING ACCOUNT HOLDER'S BALANCE IS LESS THAN 1000 ") ;
printf(" \n**************************** ") ;
for ( b = 0 ; b < a ; b++)
{
if ( s[b].dep < 1000))
{
printf("\n\n Account Number : %d ",s[b].no) ;
printf("\n Name : %s ",s[b].name) ;
}
}
getch( ) ;
}

OUTPUT:

23
RESULT:
Thus the C program for account holder’s minimum balance was checked and executed
successfully.

EX.NO:22 RAILWAY RESERVATION SYSTEM (MINI PROJECT)


DATE:
AIM:
To implement a mini project “Railway reservation system” using C program.
ALOGRITHM:
Step-1: Read train number
Step-2: Read total number of seats in first class
Step:3: Read total number of seats in second class
24
Step-4: Read total number of sets in third class
Step -5: Check if it is any vacancy is there
Step-6: Reverse the ticket
Step-7 : Stop
PROGRAM:
#include <stdio.h>
int i=0;
int id_chk;
struct rail
{
int id,day,month,year,hour,min,seat_no;
char train[50],name[50],status;
}r[10];
add()
{
printf("\n\n============== +Add Records+ ===============\n\n");
for(i=0;i<=9;i++)
{
printf("\nEnter Ticket id : ");
scanf("%d",&r[i].id);
printf("\nEnter Customer's name : ");
fflush(stdin);
scanf("%s",&r[i].name);
printf("\nEnter Train name : ");
fflush(stdin);
scanf("%s",&r[i].train);
printf("\nEnter Seat Number : ");
scanf("%d",&r[i].seat_no);
printf("\nEnter date (dd/mm/yy) :");
scanf("%d %d %d",&r[i].day,&r[i].month,&r[i].year);
printf("\nEnter depature time (hour/minute) : ");
scanf("%d %d",&r[i].hour,&r[i].min);
printf("\nEnter Confirmation Status (y/n) : ");
fflush(stdin);
scanf("%c",&r[i].status);
}
printf("\n\n==========================================\n\n");
}
details()
{
printf("\nEnter ticket Id :\n");
scanf("%d",&id_chk);
printf("\n\n================== Customers Record ================== \n\n");
for(i=0;i<=9;i++)
{
if(id_chk==r[i].id)
{
printf("\nTicket id : %d",r[i].id);
printf("\nName : %s",r[i].train);
printf("\nTrain name : %s",r[i].name);
printf("\nSeat number : %d",r[i].seat_no);
25
printf("\nDate (dd/mm/yy) : %d / %d / %d",r[i].day,r[i].month,r[i].year);
printf("\nDepature time (hour/minute) : %d : %d",r[i].hour,r[i].min);
if(r[i].status=='Y' || r[i].status=='y')
printf("\nConfirmation Status : Confirmed");
else
printf("\nConfirmation Status : Waiting");
}
}
printf("\n\n");
}
status()
{
printf("\nEnter ticket Id :\n");
scanf("%d",&id_chk);
printf("\n\n================== Customers Record ================== \n\n");
for(i=0;i<=9;i++)
{
if(id_chk==r[i].id)
{
if(r[i].status=='Y' || r[i].status=='y')
{
printf("\nName : %s",r[i].train);
printf("\nSeat Number : %d",r[i].seat_no);
printf("\nConfirmation Status : Confirmed");
}
else
printf("\nConfirmation Status : Waiting");
}
}
printf("\n\n");
}
int main()
{
int i,ch;
printf("==================Welcome to railway sevices================= \n\n");
while(1)
{
printf("1.Add Details : \n");
printf("2.Ticket number id (Check details): \n");
printf("3.Ticket number (Check Status): \n");
printf("4.Exit : \n ");
printf("\nEnter ur choice : \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
add();
break;
case 2:
details();
break;
case 3:
status();
26
break;
case 4:
break;
default:
printf("Invalid option");
break;
}
}
return 0;
}
Output :
==================Welcome to railway sevices================
1.Add Details :
2.Ticket number id (Check details):
3.Ticket number (Check Status):
4.Exit :
Enter ur choice :
1
============== +Add Records+ ===============
Enter Ticket id : 115
Enter Customer's name : david beckham
Enter Train name : express
Enter Seat Number : 45
Enter date (dd/mm/yy) :12
5
2017
Enter depature time (hour/minute) : 5
00
Enter Confirmation Status (y/n) : Y
Enter Ticket id : 11
Enter Customer's name : oliver
Enter Train name : local
Enter Seat Number : 12
Enter date (dd/mm/yy) :12
6
2016
Enter depature time (hour/minute) : 2
50
Enter Confirmation Status (y/n) : n
Enter Ticket id : 54
Enter Customer's name : jack
Enter Train name : goa express
Enter Confirmation Status (y/n) : n
Enter Ticket id : 158
Enter Customer's name : sdf
Enter Train name : chennai
Enter Seat Number : 34
Enter date (dd/mm/yy) :12
4
2017
Enter depature time (hour/minute) : 12
12
27
Enter Confirmation Status (y/n) : y
Enter Ticket id : 34
Enter Customer's name : xyz
Enter Train name : madras
Enter Seat Number : 78
Enter date (dd/mm/yy) :12
54
2018
Enter depature time (hour/minute) : n
Enter Confirmation Status (y/n) : Y
Enter Ticket id : 45
Enter Customer's name : abc
Enter Train name : pune
Enter Seat Number : 97
Enter date (dd/mm/yy) :12
2
2015
Enter depature time (hour/minute) : 23
6
Enter Confirmation Status (y/n) : Y
Enter Ticket id : 56
Enter Customer's name : pqr
Enter Train name : abu
Enter Seat Number : 56
Enter date (dd/mm/yy) :12
9
2017
Enter depature time (hour/minute) : 23
67
Enter Confirmation Status (y/n) : n
Enter Ticket id : 34
Enter Customer's name : dfg
Enter Train name : mumbai
Enter Seat Number : 56
Enter date (dd/mm/yy) :13
7
2016
Enter depature time (hour/minute) : 5
6
Enter Confirmation Status (y/n) : n
Enter Ticket id : 34
Enter Customer's name : ashley
Enter Train name : surat
Enter Seat Number : 64
Enter date (dd/mm/yy) :12
4
28
2016
Enter depature time (hour/minute) : 12
12
Enter Confirmation Status (y/n) : y
Enter Ticket id : 34
Enter Customer's name : bdg
Enter Train name : jammu and kashmir
Enter Confirmation Status (y/n) : y
==========================================
1.Add Details :
2.Ticket number id (Check details):
3.Ticket number (Check Status):
4.Exit :
Enter ur choice :
3
Enter ticket Id :
12
================== Customers Record ==================

1.Add Details :
2.Ticket number id (Check details):
3.Ticket number (Check Status):
4.Exit :
Enter ur choice :
2
Enter ticket Id :
11
================== Customers Record ==================

Ticket id : 11
Name : local
Train name : oliver
Seat number : 12
29
Date (dd/mm/yy) : 12 / 6 / 2016
Depature time (hour/minute) : 2 : 50
Confirmation Status : Waiting

RESULT:
Thus the program for railway reservation system was executed successfully.

EX.NO:23 SINGLY LINKED LIST EXAMPLE PROGRAM ( INSERT, DELETE, DISPLAY AND COUNT)
DATE:
AIM:
Write a c program to implement singly Linked List using Functions.

30
ALGORITHM:
1. Singly Linked List Example - All Operations Example Program Using Functions in C
2. Data Structure Programs,
3. C Linked List Examples
4. Singly Linked List Example - Insert,Delete,Display and Count Operations
PROGRAM
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
void insert();
void display();
void delete();
int count();
typedef struct node DATA_NODE;
DATA_NODE *head_node, *first_node, *temp_node = 0, *prev_node, next_node;
int data;
int main() {
int option = 0;
printf("Singly Linked List Example - All Operations\n");
while (option < 5) {
printf("\nOptions\n");
printf("1 : Insert into Linked List \n");
printf("2 : Delete from Linked List \n");
printf("3 : Display Linked List\n");
printf("4 : Count Linked List\n");
printf("Others : Exit()\n");
printf("Enter your option:");
31
scanf("%d", &option);
switch (option) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
count();
break;
default:
break;
}
}
return 0;
}
void insert() {
printf("\nEnter Element for Insert Linked List : \n");
scanf("%d", &data);
temp_node = (DATA_NODE *) malloc(sizeof (DATA_NODE));
temp_node->value = data;
if (first_node == 0) {
first_node = temp_node;
} else {
head_node->next = temp_node;
}
temp_node->next = 0;
head_node = temp_node;
32
fflush(stdin);
}
void delete() {
int countvalue, pos, i = 0;
countvalue = count();
temp_node = first_node;
printf("\nDisplay Linked List : \n");
printf("\nEnter Position for Delete Element : \n");
scanf("%d", &pos);
if (pos > 0 && pos <= countvalue) {
if (pos == 1) {
temp_node = temp_node -> next;
first_node = temp_node;
printf("\nDeleted Successfully \n\n");
} else {
while (temp_node != 0) {
if (i == (pos - 1)) {
prev_node->next = temp_node->next;
if(i == (countvalue - 1))
{
head_node = prev_node;
}
printf("\nDeleted Successfully \n\n");
break;
} else {
i++;
prev_node = temp_node;
temp_node = temp_node -> next;
}
}
}
} else
33
printf("\nInvalid Position \n\n");
}
void display() {
int count = 0;
temp_node = first_node;
printf("\nDisplay Linked List : \n");
while (temp_node != 0) {
printf("# %d # ", temp_node->value);
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
}
int count() {
int count = 0;
temp_node = first_node;
while (temp_node != 0) {
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
return count;
}
Sample Output:
Singly Linked List Example - All Operations
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
34
Enter Element for Insert Linked List :
100
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Enter Element for Insert Linked List :
200
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Enter Element for Insert Linked List :
300
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Enter Element for Insert Linked List :
400
Options
1 : Insert into Linked List
2 : Delete from Linked List
35
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:1
Enter Element for Insert Linked List :
500
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:3
Display Linked List :
# 100 # # 200 # # 300 # # 400 # # 500 #
No Of Items In Linked List : 5

Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:4
No Of Items In Linked List : 5
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:2
36
No Of Items In Linked List : 5
Display Linked List :
Enter Position for Delete Element :
3
Deleted Successfully
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:3
Display Linked List :
# 100 # # 200 # # 400 # # 500 #
No Of Items In Linked List : 4
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
Others : Exit()
Enter your option:2
No Of Items In Linked List : 4
Display Linked List :
Enter Position for Delete Element :
6
Invalid Position
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List
37
Others : Exit()
Enter your option:
5
(program exited with code: 0)
Press any key to continue . . .

RESULT:
Thus the C program was successfully executed and verified.

EX.NO:24 FIND THE GCD OF TWO NUMBERS USING RECURSIVE AND NON RECURSIVE
FUNCTIONS
DATE:

AIM:
To write a C program to find the GCD(greatest common divisor) of two given integers by
using recursive and Non-recursive functions.
38
RECURSIVE ALGORITHM:
1. Define the recursive function
2. Read the a,b values
3. Call the recursive function
4. if n>m
5. then return the function with parameters m,n
6. if n==0
7. then return m
8. else return the function with parameters n,m%n.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
unsigned int GCDRecursive(unsigned m, unsigned n);
int main(void)
{
int a,b; clrscr();
printf("Enter the two numbers whose GCD is to be found: ");
scanf("%d%d",&a,&b);
printf("GCD of %d and %d Using Recursive Function is %d\n",a,b,GCDRecursive(a,b));
getch();
}
/* Recursive Function*/
unsigned int GCDRecursive(unsigned m, unsigned n)
{
if(n>m)
return GCDRecursive(n,m);
if(n==0)
return m;
else
return GCDRecursive(n,m%n);
}
OUTPUT:
Enter the two numbers whose GCD is to be found 18 6 GCD of 18 and 6 Using Recursive Function
is 6

NON-RECURSIVE ALGORITHM:
Step 1: start
Step 2: read a,b
Step 3: call sub program g=GCD(a,b)
Step 4: print the g value
Step 5: stop
39
Sub program: GCD(a,b)
Step 1: initialize the p=1, q, remainder
Step 2: remainder=p-(p/q*q)
Step 3: remainder=0 return q else goto step 4
Step 4: GCD(q,remainder) return to main program
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int gcdnonrecursive(int m,int n)
{
int remainder; remainder=m-(m/n*n);
if(remainder==0) return n;
else gcdnonrecursive(n,remainder);
}
void main()
{
int a,b,igcd; clrscr();
printf("enter the two numbers whose gcd is to be found:");
scanf("%d%d",&a,&b);
printf("GCD of %d",gcdnonrecursive(a,b));
getch();
}
OUTPUT:
Enter the two numbers whose gcd is to be found:5,25 GCD of a,b is : 5
Enter the two numbers whose gcd is to be found:36,54 GCD of a,b is : 18

RESULT:
Thus the C program was successfully executed and verified.

EX.NO:25 FIND THE FACTORIAL OF A GIVEN INTEGER USING RECURSIVE AND NON
RECURSIVE FUNCTION
DATE:
AIM:
To write a C program to find the factorial of a given integer by using recursive and non-
recursive functions.
40
1. Recursive Algorithm:
2. Define the recursive function
3. Read the number n
4. if n is equal to 0
5. then print “factorial of 0 is 1”
6. else call the recursive function
7. print the factorial value.
PROGRAM:
#include<stdio.h> #include<conio.h>
unsigned int factorial(int n); void main()
{
int n,i;
long int fact; clrscr();
printf("Enter the number: "); scanf("%d",&n);
if(n==0)
printf("Factorial of 0 is 1\n"); else
printf("Factorial of %d Using Recursive Function is %d\n",n,factorial(n));
getch();
}
/* Recursive Function*/ unsigned int factorial(int n)
{
return n>=1 ? n * factorial(n-1) : 1;
}
OUTPUT:
Enter number: 5
Factorial of 5 using recursive function is: 120
NON-RECURSIVE ALGORITHM: MAIN PROGRAM
Step 1: start
Step 2: read n
Step 3: call the sub program fact(n)
Step 4: print the f value
Step 5: stop

SUB PROGRAM: FACT


Step 1: initialize the f=1
Step 2: if n==0 or n=1 return 1 to main program. If not goto step 3
Step 3: perform the looping operation as follows
For i=1 i<=n; i++
Step 4: f=f*i
41
Step 5: return f value to the main program
FACTORIAL NONRECURSIVE
PROGRAM:
#include<stdio.h>
#include<conio.h>
int fact(int n) //starting of the sub program
{
int f=1,i;
if((n==0)||(n==1)) // check the condition for n value return(1);
else
for(i=1;i<=n;i++) // perform the looping operation for calculating the factorial f=f*i;
return(f);
}
void main()
{
int n; clrscr();
printf("enter the number :");
scanf("%d",&n);
printf("factoria of number%d",fact(n));
getch();
}
OUTPUT:
Enter a number: 7
Factorial of number: 5040

RESULT:
Thus the C program was successfully executed and verified.

EX.NO:26 ADDITION OF TWO MATRICES


DATE:
AIM:
To write a C program to find addition of two matrices.
ALGORITHM:
Step 1 -> Input matrix 1 and matrix 2.
42
Step 2 -> If the number of rows and number of columns of matrix 1 and matrix 2 are equal then
execute step 3 else addition not possible
Step 3 -> for i=1 to rows[matrix 1]
for j=1 to columns [matrix 1]
Input matrix 1 [i,j]
Input matrix 2 [i,j]
matrix 3 [i,j]= matrix 1 [i,j] + matrix 2 [i,j];
step 4-> Display matrix 3 [i,j];
STOP
PROGRAM:
//Program to Add Two Matrices In C.
#include <stdio.h>
int main()
{
int mat1[2][2], mat2[2][2], sum[2][2], i, j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&mat1[i][j]);
scanf("%d",&mat2[i][j]);
sum[i][j]=mat1[i][j]+mat2[i][j];
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{printf("%d\t",sum[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT
mat1 mat2
1 2
3 4
5 6
7 8
sum matrix :
3 7
11 15
RESULT:
Thus the program was successfully executed and verified.
EX.NO:27 MULTIPLICATION OF TWO MATRICES
DATE:
AIM:
To write a C program to perform multiplication of two matrices.
43
ALGORITHM:
Step 1: Start the Program.
Step 2: Enter the row and column of the first (a) matrix.
Step 3: Enter the row and column of the second (b) matrix.
Step 4: Enter the elements of the first (a) matrix.
Step 5: Enter the elements of the second (b) matrix.
Step 6: Print the elements of the first (a) matrix in matrix form.
Step 7: Print the elements of the second (b) matrix in matrix form.
Step 8: Set a loop up to row.
Step 9: Set an inner loop up to the column.
Step 10: Set another inner loop up to the column.
Step 11: Multiply the first (a) and second (b) matrix and store the element in the third matrix (c)
Step 12: Print the final matrix.
Step 13: Stop the Program.
PROGRAM:
#include <stdio.h>
void main()
{
int a[25][25],b[25][25],c[25][25],i,j,k,r,s;
int m,n;
printf("Enter the first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the second matrix\n");
scanf("%d%d",&r,&s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
{
printf("\n Enter the elements of first matrix ");
for(i= 0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&a[i][j]);
}
printf("\n Enetr the elements of second matrix ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&b[i][j]);
44
}
printf("\n The element of first matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
printf("\n The element of second matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",b[i][j]);
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\n Multiplication of two matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",c[i][j]);
}
}
Output:

45
46
RESULT:
Thus the C program was successfully executed and verified.

EX.NO:28 TRANSPOSE OF A MATRIX


DATE:
AIM:
To write a C program to perform transpose of a matrix.
ALGORITHM:
 Start
 Declare all the necessary variables
 Enter the order of matrix
 Enter the elements of matrix row-wise using loop
 Display the entered matrix in standard format (it is not a compulsory step)
 Assign number of rows with number of column
 Swap (i, j) element with (j, i)
th th
 Store the new elements as element of transposed matrix
 Print the elements of transpose matrix in format using loop
 Stop

PROGRAM:
#include <stdio.h>
int main()
{
int a, b, c, d, mat[10][10], trans[10][10]; //declaration of variable
printf(" Enter the number of rows and columns of matrix: ");
scanf("%d%d",&a,&b); // inputting order of matrix
printf(" Enter the elements of matrix \n");
for( c = 0 ; c < a ; c++ )// loop to input the matrix
{
for( d = 0 ; d < b ; d++ )
47
{
scanf("%d",&mat[c][d]);
}
}
for( c = 0 ; c < a ; c++ ) // loop to transpose the matrix
{
for( d = 0 ; d < b ; d++ )
{
trans[d][c] = mat[c][d];
}
}
printf(" The traspose of entered matrix is:-\n");
for( c = 0 ; c < b ; c++ ) // loop for printing transposed matrix
{
for( d = 0 ; d < a ; d++ )
{
printf("%d\t",trans[c][d]);
}
printf("\n");
}
return 0;
}
OUTPUT:

48
RESULT:
Thus the C program was successfully executed and the output is verified.

EX.NO:29 ROOTS OF QUADTRATIC EQUATION


DATE:
AIM:
To write a C program to find the roots of a quadratic equation.
ALGORITHM:
1. Read a,b,c values
2. Initialize d  b*b-4*a*c
3. if d==0
a. then print “roots are real and equal”
b. r1 -b/2*a,r2  r1
4. else if d>0
a. then print “roots are real and distinct”
b. r1 (-b+sqrt(d))/2*a, r2 (-b-sqrt(d))/2*a
5. else if d<0
a. then print “roots are complex”
b. rp -b/2a, imp sqrt(d)/2*a
c. print r1 and r2.

PROGRAM:
#include<stdio.h
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,r1,r2,imp,rp;
clrscr();
49
printf(“Enter a,b,c:”);
scanf(“%f%f%f”,&a,&b,&
c);d=b*b-4.0*a*c;
if(d= =0)
{
Printf(“roots are real and equal”);
r1=-b/2*a;
r2=r1; printf(“root1=%f”,r1);
printf(“root2=%f”,r2);
}
else if(d>0)
{
Printf(“roots are real and unequal”);
r1=(-b+sqrt(d))/2*a;
r2=(-b-sqrt(d))/2*a;
printf(“root1=%f”,r1);
printf(“root2=%f”,r2);
}
else if(d<0)
{
d=-d;
printf(“roots are complex”);
rp=-b/2*a;
imp=sqrt(d)/2*a;
printf(“root1=%f+i%f”,rp,imp);
printf(“root2=%f-i%f”,rp,imp);
}
getch();
}
OUTPUT:
Enter a,b & c: 1 5 3
Roots are real &
unequal

50
RESULT:
Thus the C program was successfully executed and verified.

EX.NO:30 SWAPPING OF TWO NUMBERS USING CALL BY REFERENCE


DATE:

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
51
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;
}
OUTPUT:
Before swapping a=10 b=20
After swapping a=20 b=10
RESULT:
Thus the C program to swap two numbers using call by reference is written and
executed successfully.

EX.NO:31 SWAPPING OF TWO NUMBERS USING CALL BY VALUE


DATE:
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 ) ;
52
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:
Before swapping a=10 b=20
After swapping a=20 b=10
RESULT:
Thus the C program to swap two numbers using call by value is written and executed
successfully.

EX.NO:32 COPY OF CONTENTS FROM ONE FILE TO ANOTHER FILE


DATE:
AIM:
To write a C program to copy the content from one file to another file.
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <process.h>
void main(int argc, char *argv[])
{
FILE *fs,*ft; char ch; clrscr(); if(argc!=3)
{
puts("Invalid number of arguments."); exit(0);
}
fs = fopen(argv[1],"r"); if(fs==NULL)
{
puts("Source file cannot be opened."); exit(0);
}
ft = fopen(argv[2],"w"); if (ft==NULL)
{
puts("Target file cannot be opened."); fclose(fs);
exit(0);
53
}
while(1)
{
ch=fgetc(fs); if (ch==EOF) break;
else fputc(ch,ft);
}
fclose(fs); fclose(ft); getch();
}

OUTPUT:
File created is passed as parameter: File is copied

RESULT:
Thus the C Program was successfully executed and output is verified.

EX.NO:33 REVERSE THE FIRST N CHARACTERS IN A FILE


DATE:
AIM:
To write a C program to reverse the first n characters in a file. (Note: The file name and n
are specified on the command line.)
ALGORITHM:
STEP1: Declare the variables and file pointer
STEP2: Check the number of arguments in command line if arg is not equal to 3 then print
“invalid arguments” exit
STEP3: Open an existed file in read mode
STEP4: if file not found then print “file can not be open”
STEP5: Assign 2nd argument in command line to a variable
STEP6: Read the contents from existed file and reverse first n characters in the string from file.
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>
void main(int argc, char *argv[])
{
char a[15]; char s[20]; char n;
int k; int j=0; int i; int len;
54
FILE *fp; if(argc!=3)
{
puts("Improper number of arguments."); exit(0);
}
fp = fopen(argv[1],"r"); if(fp == NULL)
{
puts("File cannot be opened."); exit(0);
}
k=atoi(argv[2]);
n = fread(a,1,k,fp);
a[n]='\0';
len=strlen(a); for(i=len-1;i>=0;i--)
{
s[j]=a[i]; printf("%c",s[j]); j=j+1;
}
s[j+1]='\0';
getch();
}
OUTPUT:
Abc.txt: He is a good boy
Output: yob doog a si eH
RESULT:
Thus the C Program was successfully executed and output is verified.

55

You might also like