0% found this document useful (0 votes)
10 views

PC LAB PROGRAMS (1)

The document contains a series of C programming exercises, each with a specific aim, algorithm, program code, output, and result. The exercises cover various topics such as calculating sums, converting temperatures, finding areas and circumferences, calculating simple interest, and checking for leap years. Each program is designed to demonstrate fundamental programming concepts and is verified for correctness.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

PC LAB PROGRAMS (1)

The document contains a series of C programming exercises, each with a specific aim, algorithm, program code, output, and result. The exercises cover various topics such as calculating sums, converting temperatures, finding areas and circumferences, calculating simple interest, and checking for leap years. Each program is designed to demonstrate fundamental programming concepts and is verified for correctness.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 110

EX.

NO:
1(a) SUM
DATE: AND

AIM:

To write C program to find sum and product of two given number

ALGORITHM:

STEP 1: Start the program

STEP 2: Read x, y

STEP 3: Compute sum = x+y

STEP 4: Compute product = x*y

STEP 5: Print sum, product

STEP 6: Stop
PROGRAM:

#include<stdio.h>
void main()
{
int x,y,sum,product;
printf("\nEnter two numbers: ");
scanf("%d %d",&x,&y);
sum=x+y;
product=x*y;
printf("\nSum = %d\nProduct = %d",sum,product);
}

OUTPUT:
Enter two numbers: 15 20
Sum = 35
Product = 300

RESULT:
Thus, C program using sum and product of two numbers was executed successfully
and the output was verified.
EX.NO:
1(b) CONVER
DATE: T

AIM:

To write C program to convert temperature in Fahrenheit to centigrade

ALGORITHM:

STEP 1: Start the program

STEP 2: Read f

STEP 3: Compute c=s/a(f-32)

STEP 4: Print c

STEP 5: Stop
PROGRAM:

#include<stdio.h>
void main()
{
float f,c;
printf("Enter the Fahrenheit: ");
scanf("%f",&f);
c=5.0/9.0*(f-32);
printf("\nCentigrade = %6.2f",c);
}

OUTPUT:

Enter the Fahrenheit: 105

Centigrade = 40.56

RESULT:

Thus, C program converting temperature in Fahrenheit to centigrade was executed


successfully and the output was verified.
EX.NO:
1(c)
AREA
DATE: AND

AIM:

To write C program to find the area and circumference of a circle

ALGORITHM:

STEP 1: Start the program

STEP 2: Read r

STEP 3: Compute a=(3.14) *r*r and c=2*3.14*r

STEP 4: Print a, c

STEP 5: Stop
PROGRAM:

#include<stdio.h>
void main()
{
float a,c,r;
printf("\nTo find the area and circumference of a circle");
printf("\nEnter the radius: ");
scanf("%f",&r);
a=(3.14)*r*r;
c=2*3.14*r;
printf("\nThe area of the circle is %f",a);
printf("\nThe circumference of the circle is %f",c);
}

OUTPUT:

To find the area and circumference of a circle

Enter the radius: 15

The area of the circle is 706.500000

The circumference of the circle is 94.199997 Product = 300

RESULT:

Thus, C program using area and circumference of a circle was executed successfully
and the output was verified.
EX.NO:
1(d) SIMPLE
DATE: INTERES

AIM:

To write C program to calculate the simple interest

ALGORITHM:

STEP 1: Start the program

STEP 2: Read p, n, r

STEP 3: Compute si = (p*n*r) /100

STEP 4: Print si

STEP 5: Stop
PROGRAM:

#include<stdio.h>

void main()

int p,n;

float r,si;

printf("\nEnter the value for principal amount (p): ");

scanf("%d",&p);

printf("\nEnter the number of years (n): ");

scanf("%d",&n);

printf("\nEnter the rate of interest (r): ");

scanf("%f",&r);

si=(p*n*r)/100;

printf("\nThe Simple Interest is %.2f",si);

OUTPUT:

Enter the value for principal amount (p): 5000

Enter the number of years (n): 1

Enter the rate of interest (r): 15

The Simple Interest is 750.00

RESULT:

Thus, C Program using simple interest was executed successfully and the output was
verified.
EX.NO:
1(e) AREA
DATE: AND

AIM:

To write C program to find area and perimeter of the rectangle

ALGORITHM:

STEP 1: Start the program

STEP 2: Read l, b

STEP 3: Compute area = l*b and p = 2*(l+b)

STEP 4: Print area, p

STEP 5: Stop
PROGRAM:

#include<stdio.h>
void main()
{
float area,p,l,b;
printf("\nEnter the length and width of a rectangle: ");
scanf("%f %f",&l,&b);
area=(l*b);
p=2*(l+b);
printf("\nArea of rectangle: %5.2f",area);
printf("\nPerimeter of the rectangle: %5.2f",p);
}

OUTPUT:

Enter the length and width of a rectangle: 15 20

Area of rectangle: 300.00

Perimeter of the rectangle: 70.00

RESULT:

Thus, C program using area and perimeter of the rectangle was executed successfully
and the output was verified.
EX.NO:
1(f) ILLUSTR
DATE: ATION

AIM:

To write C program using Arithmetic operators

ALGORITHM:

STEP 1: Start the program

STEP 2: Assign a=s and b=2

STEP 3: Print a+b, a-b, a*b, a/b, a%b

STEP 4: Stop
PROGRAM:

#include<stdio.h>
void main()
{
int a=5,b=2;
scanf(“%d%d”,&a,&b);
printf("\nAddition = %d",a+b);
printf("\nSubtraction = %d",a-b);
printf("\nMultiplication = %d",a*b);
printf("\nDivision = %d",a/b);
printf("\nModulo = %d",a%b);
}

OUTPUT:

Addition = 7

Subtraction = 3

Multiplication = 10

Division = 2

Modulo = 1

RESULT:

Thus, C program using arithmetic operators was executed successfully and the output
was verified.
EX.NO: 2(a)
VOTER’S AGE VALIDATION
DATE:

AIM:

To write C program to check candidate eligible for voting using if statement.

ALGORITHM:

STEP 1: Start the program

STEP 2: Read AGE

STEP 3: If age >17 then print "Eligible for voting"

STEP 4: Stop
PROGRAM:

#include<stdio.h>
void main()
{
int age;
printf("\nEnter your age: ");
scanf("%d",&age);
if(age>17)
{
printf("Eligible for Voting");
}
}

OUTPUT:

Enter your age: 25

Eligible for Voting

RESULT:

Thus, C Program using candidate eligible for voting using if statement was executed
and the output was verified.
EX.NO:
2(b) ODD OR EVEN
DATE:

AIM:

To write C program to find the given number is even or odd using if-else statement.

ALGORITHM:

STEP 1: Start the program

STEP 2: Read a

STEP 3: If(a%2==0) then print "even number" else print "odd number"

STEP 4: Stop
PROGRAM:

#include<stdio.h>
void main()
{
int a;
printf("\nEnter the number: ");
scanf("%d",&a);
if(a%2==0)
{
printf("\n%d is an even number",a);
}
else
{
printf("\n%d is an odd number",a);
}
}
OUTPUT:

Enter the number: 24

24 is an even number

Enter the number: 5

5 is an odd number

RESULT:

Thus, C program using the given number is odd or even using if-else statement was
executed successfully and the output was verified.
EX.NO:
2(c) BIGGEST OF THREE NUMBERS
DATE:

AIM:

To write C program to find the biggest of three number using nested if statement

ALGORITHM:

STEP 1: Start the program

STEP 2: Read x, y, Z

STEP 3: if ((x>y) &&(x>z)) then print "biggest number=x" else step 3a.

3 a) If (y>z) then print " Biggest number=y" else print "biggest number =z”

STEP 4: Stop
PROGRAM:
#include<stdio.h>
void main()
{
int x,y,z;
printf("\nEnter the three numbers: ");
scanf("%d %d %d",&x,&y,&z);
if((x>y)&&(x>z))
{
printf("The Biggest number = %d",x);
}
else
{
if(y>z)
{
printf("The Biggest number = %d",y);
}
else
{
printf("The Biggest number = %d",z);
}
}
}
OUTPUT:

Enter the three numbers: 12 56 28

The Biggest number = 56

RESULT:

Thus, C program using biggest of three number using nested id statement was
executed successfully and the output was verified.
EX.NO: 2
(d) ELECTRICITY BILL CALCULATION
DATE:

AIM:

To write C program to prepare the electricity bill using if else ladder statement. An
electric charges and its domestic consumer as follows:

Consumption units Rate of charge

0-100 Rs.0.50/unit

101-200 Rs.50+R.s 0.75/unit

201-300 Rs.125+R.s 1.00/unit

above 300 R.s 225+R.s 1.50/unit

ALGORITHM:

STEP 1: Start the program

STEP 2: Declare the variable unit, number as integer and amount as float and name
as character

SREP 3: Get the consumer number, name and units.

STEP 4: If unit <=100 then amount = unit * 0.50

STEP 5: If unit <=200 then amount = unit * 50+0.75*(unit-100)

STEP 6: If unit <=300 then amount = unit * 125+1.00*(unit-200)

STEP 7: If unit is 300 then amount = unit * 225+1.50*(unit-300)

STEP 8: Print the consumer number, name and amount


STEP 9: Stop
PROGRAM:

#include<stdio.h>
void main()
{
int units,num;
float amount;
char name[100];
printf("\n\tELECTRICITY BILL PREPARATION");
printf("\n\n\tEnter Consumer Number: ");
scanf("%d",&num);
printf("\n\tEnter the Consumer Name: ");
scanf("%s",name);
printf("n\tEnter the Consumed Units: ");
scanf("%d",&units);
if(units<=100)
{
amount=units*0.50;
}
else if(units<=200)
{
amount=50+0.75*(units-100);
}
else if(units<=300)
{
amount=125+1.00*(units-200);
}
else
{
amount=225+1.50*(units-300);
}
printf("\n\tELECTRICITY BILL");
printf("\n\t________________");
printf("\n\n\tNumber: %d\t\tName: %s",num,name);
printf("\n\n\tAmount to be paid: Rs.%6.2f",amount);
}

OUTPUT:

ELECTRICITY BILL PREPARATION

Enter Consumer Number: 101

Enter the Consumer Name: Raju

Enter the Consumed Units: 256

ELECTRICITY BILL

________________

Number: 101 Name: Raju

Amount to be paid: Rs.181.00

RESULT:

Thus, C program using electricity bill preparation using if else ladder statement was
executed successfully and the output was verified.
EX.NO: 3
TO
DATE: CHECK

AIM:

To write a c program to find whether the given year is leap year or not

ALGORITHM:

STEP 1: Take a year as input and store it in the variable year.

STEP 2: Using if else statement 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.

STEP 3: If the condition at step 2 ‘a becomes true’, then print the output as “It is a
leap year”.

STEP 4: If the condition at step 2 ‘b becomes true’, then print the output as “It is a
not leap year”.

STEP 5: If the condition at step 2 ‘c becomes true’, then print the output as “It is a
leap year”.

STEP 6: If neither of condition becomes true, then the year is not a leap year and
print the same.
PROGRAM:

#include<stdio.h>
void main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if((year%400)==0)
{
printf("%d is a leap year\n",year);
}
else if((year%100)==0)
{
printf("%d is not a 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: 2021

2021 is not a leap year

Enter a year: 2020

2020 is a leap year

RESULT:

Thus, the program for checking the given year is leap year or not was executed
successfully and the output was verified.
EX.NO: 4
SIMPLE
DATE: CALCULA

AIM:

To write the c program to design a calculator to perform the operations namely,


addition, subtraction, multiplication, division, and square of a number.

ALGORITHM:

STEP 1: Start the program

STEP 2: Declare the variables n, a ,b ,c, d.

STEP 3: Read the values of a and b.

STEP 4: Enter the choice.

STEP 5: If the choice is 1, calculate c=a+b and print the c, then go to step11

STEP 6: If the choice is 2, calculate c=a-b and print the c, then goto step11

STEP 7: If the choice is 3, calculate c=a*b and print the c, then goto step11

STEP 8: If the choice is 4, calculate d=a/b and print the d, then goto step11

STEP 9: If the choice is 5, calculate c=a*a and print c then goto step11

STEP 10: If the choice is 0, exit from the program.

STEP 11: Stop the program.


PROGRAM:

#include<stdio.h>

#include<stdlib.h>

void main()

float d;

int a,b,c,n;

printf("\nMENU");

printf("\n1.Addition");

printf("\n2.Sutbraction");

printf("\n3.Multiplication");

printf("\n4.Division");

printf("\n5.Square of a number");

printf("\n6.Exit");

printf("\nEnter your choice:");

scanf("%d",&n);

switch(n)

case 1:

printf("\nEnter two numbers:");

scanf("%d %d",&a,&b);

c=a+b;

printf("\nAddition: %d",c);
break;

case 2:

printf("\nEnter two numbers:");

scanf("%d %d",&a,&b);

c=a-b;

printf("\nSubtraction: %d",c);

break;

case 3:

printf("\nEnter two numbers:");

scanf("%d %d",&a,&b);

c=a*b;

printf("\nMultiplication: %d",c);

break;

case 4:

printf("\nEnter two numbers:");

scanf("%d %d",&a,&b);

d=a/b;

printf("\nDivision: %f",d);

break;

case 5:

printf("\nEnter a number:");

scanf("%d",&a);

c=a*a;
printf("\nSquare of a number: %d",c);

break;

case 6:

printf("\nProgram Terminated");

exit(0);

break;

default:

printf("Invalid choice");

break;

}
OUTPUT:

MENU

1.Addition

2.Sutbraction

3.Multiplication

4.Division

5.Square of a number

6.Exit

Enter your choice:1

Enter two numbers:10 20

Addition: 30

RESULT:

Thus, c program using simple calculator was executed successfully and the output
was verified.
EX.NO: 5
TO
DATE: CHECK

AIM:

To write a c program to check whether a given number is Armstrong number or not.

ALGORITHM:

STEP 1: Read the values of n.

STEP 2: Assign t=n and s=0.

STEP 3: Repeat the step 4 until n>0

STEP 4: Compute

a) r=n%10
b) s=s+(r*r*r)
c) n=n/10

STEP 5: If (t==s) then print” Armstrong number” else print “not an Armstrong
number”.
PROGRAM:

#include<stdio.h>
void main()
{
int n,t,s,r;
printf("\nEnter the number: ");
scanf("%d",&n);
t=n;s=0;
do
{
r=n%10;
s=s+(r*r*r);
n=n/10;
}
while(n>0);
if(t==s)
{
printf("%d is an Armstrong number",t);
}
else
{
printf("%d is not an armstrong number",t);
}
}
OUTPUT:

Enter the number: 153

153 is an Armstrong number

Enter the number: 204

204 is not an Armstrong number

RESULT:

Thus, c program for checking the given number is Armstrong number or not was
executed successfully and the output was verified.
EX.NO: 6 FIND
SUM OF
DATE: WEIGHT
BASED

AIM:

To write a c program to given set of numbers like <10,36,54,89,12,27>, find sum of


weights based on the following conditions.

5 if it is perfect cube.

4 if is a multiple of 4 and divisible by 6

3 if it is a prime number

ALGORITHM:

STEP 1: Read the value of n.

STEP 2: Assign i=0.

STEP 3: Repeat the step 4 and 5 until i<n.

STEP 4: Read the array num[ i ]

STEP 5: Compute i=i+1

STEP 6: Assign i=0

STEP 7: Repeat the step 8 and 9 until i<n

STEP 8: ws[ i ]= call get weight (n) function

STEP 9: Compute i=i+1

STEP 10: Assign i=0

STEP 11: Repeat the step12 and 13 until i<n

STEP 12: Print the array content s nums[ i ] and ws[ i ]


STEP 13: Compute i=i+1

STEP 14: Assign i=0

STEP 15: Repeat the steps 16 to 20 until i<n

STEP 16: Assign j=0

STEP 17: Repeat the step19 until j<n-1

STEP 18: If(ws[ j ] >ws[j+1]) then

t = ws[j=1];
ws[j+1] = ws[ j ];
t= nums[j+1];
nums[j+1] = nums[ j ];
nums[ j ] = k;

STEP 19: Compute j=j+1

STEP 20: Compute i=i+1

STEP 21: Assign i=0

STEP 22: Repeat the steps 23 and 24 until i<n

STEP 23: Print the array contents nums[ i ] and ws[ i ]

STEP 24: Compute i=i+1

STEP 25: Stop

cube(int num) function:

STEP 1: Assign flag=0


STEP 2: Compute a=ceil(pow(num,1.0/3.0));
STEP 3: If ((a*a*a*)==num)then flag=1
STEP 4: Return flag to get weight ()function

Print (int num)function:

STEP 1: Assign flag=0 and i=2


STEP 2: Repeat the steps 3 and 4 until num/2

STEP 3: If (num%i=0)then flag=1 and exit from for loop

STEP 4: Compute i=i+1

STEP 5: Return flag to get weight () function

Get weight (int n) function:

STEP 1: Assign w=0

STEP 2: If (call cube(n) function==1) then w+=5

STEP 3: If (n%4==0 && n%6==0) then w+=4

STEP 4: If (call prime (n) function==0) then w+=3

STEP 5: Return w to main program.


PROGRAM:

#include<stdio.h>
#include<math.h>
int cube(int num)
{
int a,flag=0;
a=ceil(pow(num,1.0/3.0));
if((a*a*a)==num)
{
flag=1;
}
return flag;
}
int prime(int num)
{
int i,flag=0;
for(i=2;i<=num/2;++i)
{
//condition for nonprime number
if(num%i==0)
{
flag=1;break;
}
}
return flag;
}
int getWeight(int n)
{
int w=0;
if(cube(n)==1)
{
w+=5;
}
if(n%4==0&&n%6==0)
{
w+=4;
}
if(prime(n)==0)
{
w+=3;
}
return w;
}
void main()
{
int nums[15];
int ws[15];
int i,j,t,n;
printf("Enter the limit:");
scanf("%d",&n);
printf("\nEnter the numbers:");
for(i=0;i<n;i++)
{
scanf("%d",&nums[i]);
}
for(i=0;i<n;i++)
{
ws[i]=getWeight(nums[i]);
printf("%d:%d\t",nums[i],ws[i]);
}
for(i=0;i<n;i++)
{
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 limit: 6

Enter the numbers: 45 56 89 72 65 58

45:0 56:0 89:3 72:4 65:0 58:0

Sorted:

45:0 56:0 65:0 58:0 89:3 72:4

RESULT:

Thus, c program for finding sum of weights based on given conditions and sort
number based on weight was executed successfully and the output was verified.
EX.NO: 7 POPULAT
E AN
DATE: ARRAY

AIM:

To write a c program to populate an array with height of persons and find how many
persons are above the average height.

ALGORITHM:

STEP 1: Assign sum=0, count=0 and height[100].

STEP 2: Read the value of n

STEP 3: Assign i=0

STEP 4: Repeat the steps 5 to 7 until i<n

STEP 5: Read the array height[ i ]

STEP 6: Compute sum=sum+height[ i ]

STEP 7: Compute i=i+1

STEP 8: Compute avg=(float)sum/n;

STEP 9: Assign i=0

STEP 10: Repeat the steps 11 and 12 until i<n

STEP 11: If (height [ i ]>avg)then count ++

STEP 12: Compute i=i+1

STEP 13: Print the value of avg and count

STEP 14: Stop


PROGRAM:

#include<stdio.h>
void main()
{
int i,n,sum=0,count=0,height[100];
float avg;
printf("Enter the Number of persons: ");
scanf("%d",&n);
printf("\nEnter the Height of each person in centimeter:");
for(i=0;i<n;i++)
{
scanf("%d",&height[i]);
sum=sum+height[i];
}
avg=(float)sum/n;
for(i=0;i<n;i++)
if(height[i]>avg)
{
count++;
}
printf("\nAverage Height of %d person is: %.2f",n,avg);
printf("\nThe number of persons above average: %d",count);
}
OUTPUT:

Enter the Number of persons: 6

Enter the Height of each person in centimetre: 165 158 169 160 145 151

Average Height of 6 person is: 158.00

The number of persons above average: 3

RESULT:

Thus, c program using array with height of persons and finding how many persons
are above the average height was executed and the output was verified.
EX.NO: 8 COMPUT
E BMI OF
DATE: THE

AIM:

To write a c program to populate a two-dimensional array with height and weight of


persons and compute the body mass index of the individuals.

ALGORITHM:

STEP 1: Assign meter=100

STEP 2: Read the value of n

STEP 3: Assign i=0

STEP 4: Repeat the step 5 until i<n

STEP 5: Read the array hweight[ i ][ 0 ] and hweight[ i ][ 1 ]

STEP 6: Compute temp=hweight[ i ][ 0 ]/meter

STEP 7: Compute bmi=bweight[ i ][ 1 ]/(temp+temp)

STEP 8: Print the value of bmi

STEP 9: Compute i=i+1

STEP 10: Stop


PROGRAM:

#include<stdio.h>
void main()
{
int i,n,meter=100;
float hweight[10][2],temp,bmi;
printf("Enter the Number of persons:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the person [%d] height(in cm) and Weight(in kg) of each person:\
n",i+1);
scanf("%f%f",&hweight[i][0],&hweight[i][1]);
temp=hweight[i][0]/meter;
bmi=hweight[i][1]/(temp*temp);
printf("\nBody Mass Index of person %d = %f\n",i+1,bmi);
}
}
OUTPUT:

Enter the Number of persons:2

Enter the person [1] height(in cm) and Weight(in kg) of each person:

165 70

Body Mass Index of person 1 = 25.711664

Enter the person [2] height(in cm) and Weight(in kg) of each person:

169 98

Body Mass Index of person 2 = 34.312523

RESULT:

Thus, c program for computing BMI of the individual using 2D array with height and
weight was executed and output was verified.
EX.NO: 9 REVERSE
THE
DATE: GIVEN

AIM:

To write a c program for the given a string a&bcd./fg.find its reverse without
changing the position of special characters. (Example input :a@gh%j and the output :j
%hg@a)

ALGORITHM:

Main() program:

STEP 1: Read the string str.

STEP 2: Call reverse(str) function.

STEP 3: Print the reversed string str.

Swap(char *a, char *b) function:

STEP 1: Assign t=*a.

STEP 2: Assign *a=*b

STEP 3: Assign *b=t

STEP 4: Return to reverse()function

IsAlpha(char X) function:

STEP 1: Return((x>=’A’&&x<==’Z’)//(x>’a’&&x<=’z’)) to reverse() function.

Reverse(char str[100]) function:

STEP 1: assign r=strlen(str)-1 and l=0

STEP 2: Repeat the steps 3 to 5 until (l<r)

STEP 3: if (isAlpha (str[r])) then 1++ else step 4


STEP 4: if(isAlpha(str[r])) then r—else step5

STEP 5: call swap(&str[1],&str[r])function

a) 1++;
b) r--;

STEP 6: Return to main program


PROGRAM:

#include<stdio.h>
#include<string.h>
int isAlpha(char X);
void swap(char*a,char*b)
{
char t;
t=*a;
*a=*b;
*b=t;
}
void reverse(char str[100])
{
//Initialize left and right pointers
int r=strlen(str)-1,l=0;
//Traverse string from both ends until 'l'and'r'
while(l<r)
{
//Ignore special characters
if(!isAlpha(str[l]))
{
l++;
}
else if(!isAlpha(str[r]))
{
r--;
}
else
{
swap(&str[l],&str[r]);
l++;
r--;
}
}
}
//To check X is alphabet or not if it an alphabet then return 0 else 1
int isAlpha(char X)
{
return((X>='A'&&X<='Z')||(X>='a'&&X<='z'));
}
void main()
{
char str[100];
printf("Enter the Given string: ");
scanf("%s",str);
reverse(str);
printf("Reverse String: %s",str);
}
OUTPUT:

Enter the Given string: SachinTendulkar

Reverse String: rakludneTnihcaS

RESULT:

Thus, c program for reverse the given string without changing the position of special
characters was executed and the output was verified.
CONVERT
EX.NO:
THE
10
GIVEN
DATE:
DECIMAL
NUMBER

AIM:

To write a c program to convert the given decimal number into binary, octal and
hexadecimal numbers using user defined functions.

ALGORITHM:

Main( ) program:

STEP 1: Read the decimal number num

STEP 2: Call binary (num)function

STEP 3: Call octal(num)function

STEP 4: Call hexa(num)function

STEP 5: Stop

Binary(num) function:

STEP1: Take a decimal number as input and store it in the variable num.

STEP 2: Assign binary=0 and i=1

STEP 3: Repeat the step4 until num!=0

STEP 4: Compute

i) remainder=num%2;
ii) num=num/2
iii) binary=binary+(remainder*i);
iv) i=i*10;

STEP 5: Print the value of binary


STEP 6: Return to main function

octal(num) function:

STEP 1: Take a decimal number as input and store it in the variable num.

STEP 2: Copy the variable num to the variable quotient.

STEP 3: Divide the variable quotient and obtain its remainder and quotient .store the
remainder in the array octal number and override the variable quotient with the
quotient

STEP 4: Repeat the step 3 until the quotient becomes zero

STEP 5: When it becomes zero. Print the array octal number in the reverse order to
get the output.

STEP 6: Return to main program.

Hexa(num) function:

STEP 1: Take a decimal number as input and store it in the variable num.

STEP 2: Initialize the variable j=0 and copy num to variable quotient.

STEP 3: Obtain the quotient and remainder of the variable quotient. Store the
obtained remainder in the variable remainder and override the variable quotient
with obtained quotient.

STEP 4: Check if the remainder less then 10

STEP 5: Do step3-4 until variable quotient becomes zero

STEP 6: When it becomes zero, print the array hexadecimal num in the reversed
fashion as output.

STEP 7: Return to main program.


PROGRAM:

#include<stdio.h>
void octal(int);
void binary(int);
void hexa(int);
void main()
{
int num;
printf("\nEnter the Decimal number: ");
scanf("%d",&num);
binary(num);
octal(num);
hexa(num);
}
void binary(int num)
{
int remainder;
long binary=0,i=1;
while(num!=0)
{
remainder=num%2;
num=num/2;
binary=binary+(remainder*i);
i=i*10;
}
printf("\nEquivalent Binary value: %ld",binary);
}
void octal(int num)
{
long remainder,quotient;
int octalNumber[100],i=1,j;
quotient=num;
while(quotient!=0)
{
octalNumber[i++]=quotient%8;
quotient=quotient/8;
}
printf("\nEquivalent Octal value: ");
for(j=i-1;j>0;j--)
{
printf("%d",octalNumber[j]);
}
}
void hexa(int num)
{
long quotient,remainder;
int i,j=0;
char hexadecimalnum[100];
quotient=num;
while(quotient!=0)
{
remainder=quotient%16;
if(remainder<10)
{
hexadecimalnum[j++]=48+remainder;
}
else
{
hexadecimalnum[j++]=55+remainder;
}
quotient=quotient/16;
}
//display integer into character
printf("\nEquivalent Hexadecimal value:");
for(i=j-1;i>=0;i--)
{
printf("%c",hexadecimalnum[i]);
}
}
OUTPUT:

Enter the Decimal number: 10

Equivalent Binary value: 1010

Equivalent Octal value: 12

Equivalent Hexadecimal value: A

RESULT:

Thus, c program for converting the given decimal number into binary, octal and the
hexadecimal numbers using User defined functions was executed and the output was
verified.
EX.NO:
11(a) FIND
DATE: THE

AIM:

To write a C program to find the total number of words in a given paragraph.

ALGORITHM:

STEP 1: Take a string as input and store it in the array s[ ].

STEP 2: Using a for loop search for a space ‘ ‘ in the string and consecutively
increment a variable count.

STEP 3: Do step 2 until the end of the string.

STEP 4: Increment the variable count by 1 and print the variable count as input.
PROGRAM:

#include<stdio.h>
void main()
{
char s[200];
int count=0,i;
printf("Enter the string:\n");
scanf("%[^\n]s",s);
for(i=0;s[i]!='\0';i++)
{
if(s[i]==' ')
{
count++;
}
}
printf("\nNumber of words in given string are: %d\n",count+1);
}
OUTPUT:

Enter the String:

This is a C program

Number of words in given string are: 5

RESULT:

Thus, the C program to find the total number of words in a given paragraph was
executed successfully and the output was verified.
EX.NO: CAPITAL
11(b) IZE THE
DATE: FIRST

AIM:

To write a C program to capitalize the first character of each word in a sentence.

ALGORITHM:

STEP 1: Take a string as input and store it in the array str[ ].

STEP 2: Assign i=0.

STEP 3: Repeat the steps 4 to 7 until str[ i ]!=’\0’.

STEP 4: If(i==0) then check next character is lowercase alphabet. Subtract 32 to make
it capital and continue to the loop.

STEP 5: If (str[ i ]==’ ‘) then check next character is lowercase alphabet. Subtract 32
to make it capital and continue to the loop.

STEP 6: If neither (i.e. STEP 4 or 5 ) of the condition becomes true then all other
uppercase characters should be in lowercase. Subtract 32 to make it small /
lowercase.

STEP 7: Compute i=i+1.

STEP 8: Print the capitalized string str.

STEP 9: Stop.
PROGRAM:

#include<stdio.h>
#include<string.h>
void main()
{
char str[100];
int i;
printf("Enter the string: ");
scanf("%[^\n]s",str); //read string with spaces
for(i=0;str[i]!='\0';i++)
{
if(i==0)
{
if((str[i]>='a'&&str[i]<='z'))
{
str[i]=str[i]-32;
continue;
}
}
if(str[i]==' ')
{
++i;
if(str[i]>='a'&&str[i]<='z')
{
str[i]=str[i]-32;
continue;
}
}
else
{
if(str[i]>='A'&&str[i]<='Z')
str[i]=str[i]+32;
}
}
printf("Capitalize string is: %s\n",str);
}

OUTPUT:

Enter the String: this is a c program

Capitalize string is: This Is A C Program

RESULT:

Thus, the C program for capitalizing the first character of each word in a given
paragraph was executed and the output was verified.
EX.NO: REPLAC
11(c)
EA
DATE:
GIVEN

AIM:

To write a C program to replace a given word with another word.

ALGORITHM:

STEP 1: Assign i=0,j=0 and k=0.

STEP 2: Take a string as input and store it in the array s[ ].

STEP 3: Read the word to be replaced in the variable word.

STEP 4: Read the new word to be replaced in the variable rpwrd.

STEP 5: Assign k =0.

STEP 6: Repeat the steps 7 to 8 until k<p.

STEP 7: If (s[ k ]!=’ ‘) then step 7a else step 7b.

7a. str[ i ][ j ] = s[ k ] and j++

7b. str[ i ][ j ] = ‘\0’,j=0 and i++

STEP 8: Compute k=k+1.

STEP 9: Assign str[ i ][ j ] = ‘\0’,w=I and i=0.

STEP 10: Repeat the steps 11 to 12 until i<=w

STEP 11: If (strcmp(str[ i ],word)==0) then

a) strcpy(str[ i ],rpwrd);
b) print = the word str[ i ];

STEP 12: Compute i=i+1.

STEP 13: Stop.


PROGRAM:

#include<stdio.h>
#include<string.h>
void main()
{
char s[100];
char word[10],rpwrd[10],str[10][10];
int i=0,j=0,k=0,w,p;
printf("Enter the string:\n");
scanf("%[^\n]s",s);
printf("\nENTER WHICH WORD IS TO BE REPLACED\n");
scanf("%s",word);
printf("\nENTER BY WHICH WORD THE %s IS TO BE REPLACED\n",word);
scanf("%s",rpwrd);
printf("\n");
p=strlen(s);
for(k=0;k<p;k++)
{
if(s[k]!=' ')
{
str[i][j]=s[k];
j++;
}
else
{
str[i][j]='\0';
j=0;i++;
}
}
str[i][j]='\0';
w=i;
for(i=0;i<=w;i++)
{
if(strcmp(str[i],word)==0)
{
strcpy(str[i],rpwrd);
}
printf("%s ",str[i]);
}
}
OUTPUT:

Enter the String:

C is a programming language.

ENTER THE WORD IS TO BE REPLACED

ENTER BY WHICH WORD THE C IS TO BE REPLACED

Java

Java is a programming language

RESULT:

Thus, the C program for replacing a given word with another word in a given
paragraph was executed successfully and the output was verified.
EX.NO:
12 TOWERS
DATE: OF

AIM:

To write a C program for Towers of Hanoi using recursion.

ALGORITHM:

Main Program:

STEP 1: Read the no.of disks as num.

STEP 2: Call towers(num,’A’,’C’,’B’) function.

STEP 3: Stop.

Hanoi (int, char,char,char) recursive function:

STEP 1: If (num ==1) then print ”Move disk 1 freom source to dest “ return ;

STEP 2: Call Hanoi(num-1,source,aux,dest) recursive function.

STEP 3: Print “Move disk num from source to dest”.

STEP 4: Call Hanoi (num-1,aux,desk,source) recursive function.


PROGRAM:

#include<stdio.h>
void Hanoi(int,char,char,char);
void main()
{
int num;
printf("Enter the number of disks: ");
scanf("%d",&num);
printf("The sequence of moves involved in Tower of Hanoi are:\n");
Hanoi(num,'A','C','B');
}
void Hanoi(int num,char source,char dest,char aux)
{
if(num==1)
{
printf("\nMove disk 1 from %c to %c",source,dest);
return;
}
Hanoi(num-1,source,aux,dest);
printf("\nMode disk %d from %c to %c",num,source,dest);
Hanoi(num-1,aux,dest,source);
}
OUTPUT:

Enter the number of disks: 3

The sequence of moves involved in Tower of Hanoi are:

Move disk 1 from A to C

Mode disk 2 from A to B

Move disk 1 from C to B

Mode disk 3 from A to C

Move disk 1 from B to A

Mode disk 2 from B to C

Move disk 1 from A to C

RESULT:

Thus, the C program using Towers of Hanoi using recursion was executed successfully
and the output was verified.
EX.NO:
13 SORT
DATE: THE LIST

AIM:

To write a C program to sort the list of numbers using pass by reference.

ALGORITHM:

Main Program:

STEP 1: Read the value of n.

STEP 2: Assign i=0.

STEP 3: Repeat the steps 4 and 5 until i<n.

STEP 4: Read the array a[ i ].

STEP 5: Compute i=i+1.

STEP 6: Call sort (&a,n) function.

STEP 7: Assign i=0.

STEP 8: Repeat the steps 9 & 10 until i<n.

STEP 9: Print the array a[ i ].

STEP 10: Compute i=i+1.

STEP 11: Stop.

Sort (int *b,int num) function:

STEP 1: Assign i=0.


STEP 2: Repeat the steps 3 to 7 until i<num.

STEP 3: Assign j=0.

STEP 4: Repeat the steps 5 to 6 until i<num-i-1

STEP 5: If (b[ j ]>b[ j+1 ]) then

Assign t=b[ j ], b[ j ]=b[ j+1 ] and b[ j=1 ]=t.

STEP 6: Compute j=j+1.

STEP 7: Compute i=i+1.

STEP 8: Return to main program


PROGRAM:

#include<stdio.h>
void sort(int *b,int num);
void main()
{
int n,i,a[50];
printf("\nEnter the limit: ");
scanf("%d",&n);
printf("\nEnter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nAfter Sorting...\n");
sort(a,n);
}
void sort(int *b,int num)
{
int i,j,t;
for(i=0;i<num;i++)
{
for(j=i+1;j<num;j++)
{
if(*(b+j) < *(b+i))
{
t = *(b + i);
*(b + i) = *(b + j);
*(b + j) = t;
}
}
}
for(i=0;i<num;i++)
{
printf("%d ",*(b+i));
}
}

OUTPUT:

Enter the limit: 6

Enter the elements:

12 34 56 7 8 10

After Sorting...

7 8 10 12 34 56

RESULT:

Thus, the C program for sorting the list of numbers using pass by reference was
executed successfully and the output was verified.
EX.NO: GENERA
14 TE
DATE:
SALARY

AIM:

To write a C program to generate salary slip of employees using structures and


pointers

ALGORITHM:

Main Program:

STEP 1: Declare a structure as employee with following fields no, name, design_code
and days_worked.

STEP 2: Declare structure variable as emp[ 12 ] and assign 12 employee records using
arrays.

STEP 3: Read the value of EMPNO.

STEP 4: If (EMPNO && EMPNO<13) then call gen_payslip (EMPNO) function else
print”You have entered wrong employee no”.

STEP 5: Stop.

gen_payslip(int EMPNO) function:

STEP 1: ASSIGN PATV=200.

STEP 2: Read the value of data D.

STEP 3: Do the following by using switch case statement.

a) Assign(EMP[EMPNO-1].DESIGN_CODE)=”CLERK” then PAYRATE = 400.


b) Assign(EMP[EMPNO-1].DESIGN_CODE)=”SALESMEN” then PAYRATE = 300.
c) Assign(EMP[EMPNO-1].DESIGN_CODE)=”HELPER” then PAYRATE = 250.
d) Assign(EMP[EMPNO-1].DESIGN_CODE)=”COMP.OPTR” then PAYRATE =
350.
BASIC = PAYRATE*EMP[EMPNO-1]DAYS_WORKED.

STEP 4: Compute PF=BASIC /10.

STEP 5: Print = EMP[EMPNO-1], DAYS_WORKED, PAYRATE, D.da_day,D.da_mon and


D.da_year.

STEP 6: Print BASIC, PF and PTAV.

STEP 7: Print “GROSS EARN \t\t TOTAL DEDUCT”, BASIC, PF+PTAV.

STEP 8: Compute NETPAY = BASIC – (PF+PTAV)

STEP 9: Print NETPAY

STEP 10: Return to main program.


PROGRAM:

#include<stdio.h>
#include<dos.h>
#include<conio.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()
{
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.!!");
}
}
void gen_payslip(int EMPNO)
{
struct date D;
char DESG[10];
float NETPAY,BASIC,PF,PAYRATE,PTAX=200;
getdate(&D);
printf("\n\tSHREE KRISHNA CHEMISTS AND DRUGGIST");
printf("\n\tSALARY MONTH %d %d",D.da_mon,D.da_year);
printf("\n\tEMP.NO.: %d \tEMP.NAME: %s",EMPNO,EMP[EMPNO-1].NAME);
switch(EMP[EMPNO-1].DESIGN_CODE)
{
case 1:
PAYRATE=400;
printf("\tDESIGNATOION: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\tDAYS WORKED: %d",EMP[EMPNO-1].DAYS_WORKED);
printf("\tPAY RATE: %.0f \t\tGEN.DATE:
%d/%d/%d",PAYRATE,D.da_day,D.da_mon,D.da_year);
printf("\n\
t______________________________________________________________");
printf("\n\tEARNINGS \tAMOUNT(RS.) \tDEDUCTIONS \tAMOUNT(RS.)");
printf("\n\
t______________________________________________________________");
printf("\n\tBASIC PAY \t%.0f \t\tP.F. \t\t%.0f",BASIC,PF);
printf("\n\tPROF.TAX \t%.0f",PTAX);
printf("\n\
t______________________________________________________________");
printf("\n\tGROSS EARN. \t%.0f \tTOTAL DEDUCT. \t%.0f",BASIC,PF+PTAX);
NETPAY=BASIC-(PF+PTAX);
printf("\n\tNET PAY \t%.0f",NETPAY);
printf("\n\
t______________________________________________________________");
}
OUTPUT:

ENTER THE EMPLOYEE NO TO GENERATE PAYSLIP: 1

SHREE KRISHNA CHEMISTS AND DRUGGIST

SALARY MONTH 7 2021

EMP.NO.: 1 EMP.NAME: GANESH DESIGNATOION:CLERK

DAYS WORKED: 25 PAY RATE: 400 GEN.DATE: 12/7/2021

______________________

EARNINGS AMOUNT(RS.) DEDUCTIONS AMOUNT(RS.)

______________________

BASIC PAY 10000 P.F. 1000

PROF.TAX 200

______________________

GROSS EARN. 10000 TOTAL DEDUCT. 1200

NET PAY 8800

______________________

RESULT:

Thus, the C program for generating salary slip of employees using structures and
pointers was executed successfully and the output was verified.
EX.NO: COMPU
15 TE
DATE: INTERN
AL
AIM:

To write a C program to compute internal marks of students for five different


subjects using structure and functions.

ALGORITHM:

STEP 1: Declare a structure as stud with following field rollno, name, s1, s2, s3, s4, s5 ,
tot and avg.

STEP 2: Declare structure variable as s[ 10 ].

STEP 3: Assign i=0.

STEP 4: Repeat the steps 5 to 8 until i<n.

STEP 5: Read s[ i ].rollno, S[ i ].name, s[ i ].s1, s[ i ].s2, s[ i ].s3, s[ i ].s4 and s[ i ].s5.

STEP 6: Compute s[ i ].tot=s[ i ].s1 + s[ i ].s2 + s[ i ].s3 + s[ i ].s4 + s[ i ].s5 and

STEP 7: Compute s[ i ].avg = s[ i ].tot/5.0.

STEP 8: Compute i=i+1.

STEP 9: Assign i=0.

STEP 10: Repeat the steps 11 to 12 until i<n.

STEP 11: Print = s[ i ].rollno, s[ i ].name, s[ i ].s1, s[ i ].s2, s[ i ].s3, s[ i ].s4,


s[ i ].s5,s[ i ].tot and

s[ i ].avg.

STEP 12: Compute i=i+1.

STEP 13: Stop


PROGRAM:

#include<stdio.h>
#include<conio.h>
struct stud
{
int rollno,s1,s2,s3,s4,s5,tot;
char name[100];
float avg;
}s[10];
void main()
{
int i,n;
printf("Enter the number of students: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the roll number: ");
scanf("%d",&s[i].rollno);
printf("Enter the name: ");
scanf("%s",s[i].name);
printf("Enter the marks in 5 subjects:\n");
scanf("%d %d %d %d %d",&s[i].s1,&s[i].s2,&s[i].s3,&s[i].s4,&s[i].s5);
s[i].tot=s[i].s1+s[i].s2+s[i].s3+s[i].s4+s[i].s5;
s[i].avg=s[i].tot/5.0;
}
printf("\n************************Student's Mark Details*************");
printf("\n_______________");
printf("\nRoll.No Name\t\tSub1\tSub2\tSub3\tSub4\tSub5\tTotal\tAverage");
printf("\n_________________\n");
for(i=0;i<n;i++)
{
printf("%d\t%s\t\t%d\t%d\t%d\t%d\t%d\t%d\t%.2f\
n",s[i].rollno,s[i].name,s[i].s1,s[i].s2,s[i].s3,s[i].s4,s[i].s5,s[i].tot,s[i].avg);
}
}
OUTPUT:

Enter the number of students: 2

Enter the roll number: 101

Enter the name: Raju

Enter the marks in 5 subjects:

45 56 89 73 95

Enter the roll number: 102

Enter the name: Jagan

Enter the marks in 5 subjects:

89 98 75 63 72

************************Student's Mark Details*************

_______________

Roll.No Name Sub1 Sub2 Sub3 Sub4 Sub5 Total Average

_________________

101 Raju 45 56 89 73 95 358 71.60

102 Jagan 89 98 75 63 72 397 79.40

RESULT:

Thus, the C program to compute internal marks of students for five different subjects
using structures and functions was executed successfully and the output was verified.
EX.NO: GENERA
16 TE
DATE: TELEPH
ONE
DETAILS
AIM: OF AN
INDIVID
To write a C program to insert, update, delete and append telephone details of an
individual or a company into a telephone directory using random access file.

ALGORITHM:

STEP 1: Declare a structure as with following fields name and telno.

STEP 2: Declare Structure variable as

STEP 3: Create a menu with following options.

1. Append Record
2. Find Record
3. Read all record
4. Exit

Using while loop

STEP 4: Read the value of variable choice from the user

STEP 5: Do the following by using switch case statement.

a) If choice = 1 then call appendData( ) function


b) If choice = 2 then call findData ( ) function
c) If choice = 3 then call showallDate( ) function
d) If choice = 4 then call exit( ) function

STEP 6: Stop

appenddate( ):

STEP 1: Declare file pointers fp.

STEP 2: Declare Structure variable obj for person structure.


STEP 3: Open the file “data.txt” under append mode by assigning file pointer fp

STEP 4: Read the obj.name and obj.telno

STEP 5: Write the obj.name and obj.telno to data.txt by using fprintf( ) function.

STEP 6: Close the file pointers fp.

STEP 7: Return to main program.

showalldate( ):

STEP 1: Declare file pointer fp.

STEP 2: Declare structure variable obj for person structure.

STEP 3: Open the file “data.txt” under read mode by assigning file pointer fp.

STEP 4: Repeat the steps 5 to 6 until (!feof(fp))

STEP 5: Read Obj.name and Obj.telnodatas from data.txt by using fscanf( ) function.

STEP 6: Print the Obj.name and Obj.telno.

STEP 7: Close the file pointers fp.

finddata( ):

STEP 1: Declare file pointers fp and assign totrec==0.

STEP 2: Declare structure variable Obj for person structure.

STEP 3: Open the file “data.txt” under read mode by assigning file pointer fp.

STEP 4: Read the value of the variable name from the user.

STEP 5: Repeat the steps 5 to 7 until (!feof(fP))

STEP 6: Read Obj.name and Obj.telnodatas from data.txt by using fscanf( ) function.

STEP 7: If (strcmp(obj.name,name)==0) then print the Obj.name and Obj.telno and


totrec++.

STEP 8: If (totrec==0) then print”No data found” else print totrec.

STEP 9: Close the file pointer fp.


PROGRAM:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct person

char name[20];

long telno;

};

void appendData()

FILE*fp;

struct person obj;

fp=fopen("data.txt","a");

printf("\n****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;

fp=fopen("data.txt","r");

printf("\n***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);

void findData()

FILE*fp;

struct person obj;

char name[20];

int totrec=0;

fp=fopen("data.txt","r");

printf("\n***Display Specific Records***\n");

printf("\nEnter Name: ");


scanf("%s",name);

while(!feof(fp))

fscanf(fp,"%20s %7ld",obj.name,&obj.telno);

if(strcmp(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);

void main()

int choice;

while(1)

printf("\n\n****TELEPHONRE 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);

scanf("%d",&choice);

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:

exit(1);

}
OUTPUT:

****TELEPHONRE DIRECTORY****

1)Append Record

2)Find Record

3)Read all record

4)Exit

Enter your choice: 1

****Add Record****

Enter Name: Raju

Enter Telephone No.: 9638527419

****TELEPHONRE DIRECTORY****

1)Append Record

2)Find Record

3)Read all record

4)Exit

Enter your choice: 3

***Display All Records***

Name Telephone No.

==== =======

Raju 9638527419

RESULT:

Thus, the C program to generate telephone details of an individual or a company into


a telephone directory using Random Access file was executed successfully and the
output was verified.
COUNT
EX.NO:
THE
17
NUMBE
DATE:
R OF
ACCOU

AIM:

To write a C program to count the number of account holders whose balance is less
than minimum balance using sequential access file.

ALGORITHM:

STEP 1: Define a symbolic constant MBAL = 1000.

STEP 2: Declare a structure as account with following fields acno, acname and bal.

STEP 3: Declare structure variable as a1 and f=0.

STEP 4: Declare file pointer fp1.

STEP 5: Create a menu with following options

1. Add Account
2. Display
3. Deposit or withdraw
4. No.of Account holders whose bal is less than the min_bal
5. Delete all records
6. Exit using do while loop.

STEP 6: Read the value of variable ch from the user.

STEP 7: Do the following by using switch case statement.

STEP 8: If ch= 1 then

a) Open the file “a1.dat” under append mode by assigning file pointer fp1.
b) Read the a1.acno, a1.acname and a1.bal
c) Move the file pointer fp1 to last position in the file.
d) Write the a1.acno, a1.acname and a1.bal to data.txt by using fwrite( )
function.
e) Close the file pointer fp1.

STEP 9: If ch = 2 then

a) Open the file “a1.dat” under read mode by assigning file pointer fp1.
b) If (fp1==NULL) then print “No record exists in the file” else steps 9c to 9e.
c) Repeat the step 9d until (fread(&a1.sizeof(a1),1,fp1)==1)
d) Print a1.acno, a1.acname and a1.bal.
e) Close the file pointer fp1.

STEP 10: If ch = 3 then

a) Assign f=0.
b) Open the file “a1.dat” under read mode by assigning file pointer fp1.
c) Read the value of the variable a1.acno
d) Assign pos1 = ftell(fp1).
e) Repeat the steps 10f until (fread(&a1.sizeof(a1),1,fp1)==1)
f) If (strcmp(a1.acno,aacno)==0) then
i) Read the variable type
ii) Print a1.bal
iii) Read the variable amt.
iv) If (type==1) then bal = atof(a1.bal) + atof(amt)
else
bal = atof(a1.bal)-atof(amt)
if (bal<0) then print amt and assign f=2 and exit the condition.
v) Assign f = f+1 and exit from if condition.
vi) Assign pos1=ftell(fp1)
vii) If (f=1) then
pos2=ftell(fp1)
pos=pos2-pos1
fseek(fp1,-pos,1)
sprint(amt,”%.2f”,bal)
strcpy(a1.bal,amt)
fwrite(&a1,sizeof(a1),1,fp1)
else if (f==0)
print “A/C Number not exists……Check it again”
g) close the file pointer fp1.

STEP 11: If ch=4 then

a) OPen the file “a1.dat” under read mode by assigning file pointer fp1
b) Assign f=0
c) Repeat the steps 11d to 11e until (fread(&a1,sizeof(a1),1,fp1)==1)
d) Assign bal=atof(a1.bal)
e) If (bal<MBAL) then f++
f) Printf
g) Close the file pointer fp1

STEP 12: If ch = 5 then remove the file a1.dat

STEP 13: If cha = 6 then call exit( ) function

STEP 14: Stop


PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MBAL 1000
struct account
{
char acno[10];
char acname[20];
char bal[15];
};
struct account a1;
void main()
{
long int pos1,pos2,pos;
FILE*fp1;
char *aacno,*amt;
int ch;
int type,f=0;
float bal;
do
{
fflush(stdin);
printf("\n");
printf("1.Add Account\n");
printf("2.Display\n");
printf("3.Deposit or Withdraw\n");
printf("4.No. of Account Holder Whose Bal is less than the Min_Bal\n");
printf("5.Delete all records\n");
printf("6.Exit\n");
printf("Enter your ch:");
scanf("%d",&ch);
switch(ch) {
case 1:
fflush(stdin);
fp1=fopen("a1.dat","a");
printf("\nEnter the Account Number: ");
scanf("%s",a1.acno);
printf("\nEnter the Account Holder Name:");
scanf("%s",a1.acname);
printf("\nEnter the Initial Amount to deposit: ");
scanf("%s",a1.bal);
fseek(fp1,0,2);
fwrite(&a1,sizeof(a1),1,fp1);
fclose(fp1);
break;
case 2:
fp1=fopen("a1.dat","r");
if(fp1==NULL)
printf("\nNo record exists in the File");
else
{
printf("\nA/c Number\tA/c Holder Name\tBalance\n");
while(fread(&a1,sizeof(a1),1,fp1)==1)
printf("%-10s\t%20s\t%s\n",a1.acno,a1.acname,a1.bal);
fclose(fp1);
}
break;
case 3:
fflush(stdin);
f=0;
fp1=fopen("a1.dat","r");
printf("\nEnter the Account Number:");
scanf("%s",a1.acno);
for(pos1=ftell(fp1);fread(&a1,sizeof(a1),1,fp1)==1;pos1=ftell(fp1))
{
if(strcmp(a1.acno,aacno)==0)
{
printf("\nEnter the Type 1 for deposit & Type 2 for withdraw:");
scanf("%d",&type);
printf("\n Your Current Balance is :%s",a1.bal);
printf("\nEnter the Amount to transact:");
fflush(stdin);
scanf("%s",amt);
if(type==1)
{
bal=atof(a1.bal)+atof(amt);
}
else
{
bal=atof(a1.bal)-atof(amt);
if(bal<0)
{
printf("\nRs.%sNot available in your A/c\n",amt);
f=2;
break;
}
}
f++;
break;
if(f==1)
{
pos=ftell(fp1);
pos=pos2-pos1;
fseek(fp1,-pos,1);
sprintf(amt,"%.2f",bal);
strcpy(a1.bal,amt);
fwrite(&a1,sizeof(a1),1,fp1);
}
else if(f==0)
{
printf("\nA/c Number Not exits...Check it again");
fclose(fp1);
break;
}}
}
break;
case 4:
fp1=fopen("a1.dat","r");
f=0;
while(fread(&a1,sizeof(a1),1,fp1)==1)
{
bal=atof(a1.bal);
if(bal<MBAL)
f++;
}
printf("\nThe Number of Account Holder Whose Balance less than the minimum
Balance:%d",f);
fclose(fp1);
break;
case 5:
remove("a1.dat");
break;
case 6:
fclose(fp1);
exit(1);
}
printf("\nPress any key to continue...");
}while(ch!='6');
}
OUTPUT:

1.Add Account

2.Display

3.Deposit or Withdraw

4.No. of Account Holder Whose Bal is less than the Min-Bal

5.Delete all records

6.Exit

Enter your ch:1

Enter the Account Number: 4105689

Enter the Account Holder Name:Rajesh

Enter the Initial Amount to deposit: 5000

Press any key to continue...

1.Add Account

2.Display

3.Deposit or Withdraw

4.No. of Account Holder Whose Bal is less than the Min_Bal

5.Delete all records

6.Exit

Enter your ch:2

A/c Number A/c Holder Name Balance

4105689 Rajesh 5000


RESULT:

Thus, the C program to count the number of account holders whose balance is less
than minimum balance using sequential access file was executed successfully and the
output was verified.
EX.NO: 18 MINI
PROJEC
DATE:
T

PROGRAM:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define TRUE 1
void Booking(); void Display(); void Cancel();void Availability();
struct node
{
int seatno;
char name[20];
int age;
char sex;
struct node *next;
};
struct node *head=NULL, *end;
void main()
{
int op;
while(TRUE)
{
printf("\n Welcome to Railway Reservation System ");
printf("\n 1.Booking\n 2.Display Chart\n 3.Cancellation\n 4.Availability Checking\n
5.Exit ");
printf("\nEnter the choice: ");
scanf("%d",&op);
switch(op)
{
case 1:
Booking();
break;
case 2:
Display();
break;
case 3:
Cancel();
break;
case 4:
Availability();
break;
case 5:
exit(0);
}
}
}
void Booking()
{
struct node *newnode;
newnode=malloc(sizeof(struct node));
newnode->next=NULL;
if(head==NULL)
{
printf("Enter the Passenger name : ");
scanf("%s",newnode->name);
printf("Enter the Passenger age : ");
scanf("%d",&newnode->age);
printf("Enter the Passenger sex : ");
scanf("%s",&newnode->sex);
newnode->seatno=1;
head=newnode;
}
else
{
end = head;
while(end->next!=NULL)
{
end=end->next;
}
if(end->seatno==100)
{
printf("\nTrain Seat Capacity is Full");
return;
}
printf("Enter the Passenger name :");
scanf("%s",newnode->name);
printf("Enter the Passenger age : ");
scanf("%d",&newnode->age);
printf("Enter the Passenger sex : ");
scanf("%s",&newnode->sex);
newnode->seatno=end->seatno+1;
end->next = newnode;
}
}
void Display()
{
struct node *temp;
temp=head;
printf("\n\t\tDisplay Chart \n");
printf("\n SeatNo. Passenger Name\tAge\tSex \n");
while(temp!=NULL)
{
printf("\n %d\t%s\t%d\t%c", temp->seatno,temp->name,temp->age,temp->sex);
temp = temp->next;
}
}
void Cancel()
{
struct node *del;
struct node *temp;
int a;
del=head;
printf("Enter the Seat Number to be deleted: ");
scanf("%d",&a);
while(del!=NULL)
{
if(del->seatno== a)
{
if(del== head)
{
head=del->next;
}
else
{
temp->next=del->next;
free(del);
printf("\n Seat Number %d deleted \n",a);
return;
}
}
temp=del;
del=del->next;
}
if(del==NULL)
{
printf("No record exist");
return;
}
}
void Availability()
{
int a,b;
struct node *temp; temp=head;
while(temp!=NULL)
{
b = temp->seatno;
temp = temp->next;
}
printf("Last Booked Seat no = %d",b);
a=100-b;
printf("\n The Available Seats : %d", a);
return;
}

OUTPUT:

Welcome to Railway Reservation System

1.Booking

2.Display Chart

3.Cancellation

4.Availability Checking

5.Exit

Enter the choice: 1


Enter the Passenger name : Rajesh

Enter the Passenger age : 29

Enter the Passenger sex : M

Welcome to Railway Reservation System

1.Booking

2.Display Chart

3.Cancellation

4.Availability Checking

5.Exit

Enter the choice: 1

Enter the Passenger name :Malar

Enter the Passenger age : 26

Enter the Passenger sex : F

Welcome to Railway Reservation System

1.Booking

2.Display Chart

3.Cancellation

4.Availability Checking

5.Exit

Enter the choice: 2


Display Chart

SeatNo. Passenger Name Age Sex

1 Rajesh 29 M

2 Malar 26 F

Welcome to Railway Reservation System

1.Booking

2.Display Chart

3.Cancellation

4.Availability Checking

5.Exit

Enter the choice: 5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy