You are on page 1of 141

MODULE IDX PROGRAM PAGE NO

NO
Problem solving using Raptor Tool & Basics of C Language
1.1 Find the distance travelled 2–3

1.2 Find the total people attended the show 4–5


1
1.3 Express the expenses in percentages 6–8

1.4 Find different category of tickets sold 9 – 11

1.5 Find average show rating 12 –1 3

1
AIM : write a C program to find the Distance Travelled.

FORMULA : D=S*T where S=Speed , T= Time , D= Distance

ALGORITHM :

STEP-1 : start
STEP-2 : read s,t
STEP-3 : calculate d=s*t
STEP-4 : display d
STEP-5 : stop

FLOWCHART :

2
PROGRAM :

#include <stdio.h>

int main()
{
int d,t,s;
printf("Enter Speed , time : ");
scanf("%d %d",&s,&t);
d=s*t;
printf("\n Distance = %d",d);

return 0;
}

OUTPUT :

3
AIM : write a C program to find, the total audience who had come for the show, if
1/3 were boys, 3/6 were girls and the rest of them were adults. If there
were 'x' more girls than adults, how many people were there in total? find the total
people who visited the show.

FORMULA :
Let total people = p
Boys = p/3 girls = 3p/6=p/2 , adults = p-(p/3+p/2)=p-(5p/6)=p/6
Given p/6+x=p/2 -> x=p/2-p/6=2p/6=p/3 -> p=3x

ALGORITHM :
STEP-1 : start
STEP-2 : read x
STEP-3 : calculate p=3*x
STEP-4 : display p
STEP-5 : stop

FLOWCHART:

4
PROGRAM :

#include <stdio.h>

int main()
{
int x,p;
printf("Enter x value : ");
scanf("%d",&x);
p=3*x;
printf("\n Total People attended show = %d",p);
return 0;
}

OUTPUT :

5
AIM : The program should get the branding expenses, travel expenses, food
expenses and logistics expenses as input from the user and calculate the total
expenses for an event and the percentage rate of each of these expenses.

FORMULA :
total=BE+TE+FE+LE;
BEP=(BE/total)*100;
TEP=(TE/total)*100;
FEP=(FE/total)*100;
LEP=(LE/total)*100;

Where BE = branding expenses , TE = travel expenses, FE = food expenses,


LE= logistics expenses and BEP, TEP,FEP,LEP are percentages of branding
expenses, travel expenses, food expenses and logistics expenses respectively.
total is the Total Expenses.

ALGORITHM :

STEP 1 : start
STEP 2 : read BE,TE,FE,LE
STEP 3 : Calculate
total=BE+TE+FE+LE;
BEP=(BE/total)*100;
TEP=(TE/total)*100;
FEP=(FE/total)*100;
LEP=(LE/total)*100;

STEP 4 : display total, BEP, TEP, FEP, LEP


STEP 6 : stop

6
FLOWCHART:

PROGRAM :

#include<stdio.h>
#include<math.h>
int main()
{
double BE,TE,FE,LE,total,BEP,TEP,FEP,LEP;
printf("\nEnter branding expenses : ");
scanf("%lf",&BE);
printf("\nEnter travel expenses : ");
scanf("%lf",&TE);
printf("\nEnter food expenses : ");
scanf("%lf",&FE);
printf("\nEnter logistics expenses : ");
scanf("%lf",&LE);
total=BE+TE+FE+LE;
BEP=(BE/total)*100;
TEP=(TE/total)*100;
FEP=(FE/total)*100;
LEP=(LE/total)*100;
7
printf("\nTotal expenses : Rs.%.2lf”,total);
printf(“\nBranding expenses percentage : %.2lf%%”,BEP);
printf(“\nTravel expenses percentage : %.2lf%%”,TEP);

printf(“\nFood expenses percentage : %.2lf%%”,FEP);


printf(“\nLogistics expenses percentage : %.2lf%%",LEP);
return 0;
}

OUTPUT :

8
AIM : Some NGO agent selling tickets to the public for the charity event. She
sold 'X' more adult tickets than children tickets and she sold twice as many senior
tickets as children tickets. Assume that an adult ticket costs Rs 5, children ticket
costs Rs 2 and senior ticket costs Rs 3. She made Total Rs 'Y' from ticket sales.
Find the number of adult tickets, children tickets, and senior tickets sold.

FORMULA:

Let the children tickets sold = c


Adult tickets = c + x
Senior tickets = 2c
5(x+c)+2c+3(2c)=y
13c=y-5x -> c=(y-5x)/13

ALGORITHM :

STEP 1 : Start
STEP 2 : Read x,y
STEP 3 : Calculate c = (y-5x)/13
a = x+c
s = 2c
STEP 4 : Display c,a,s
STEP 5 : Stop

9
FLOWCHART:

10
PROGRAM :

#include <stdio.h>
int main()
{
int x,y,c,a,s;
printf("Enter the value of x : ");
scanf("%d",&x);
printf("Enter the value of y : ");
scanf("%d",&y);
c=(y-5*x)/13;
a=x+c;
s=2*c;
printf("\n No of Children = %d",c);
printf("\n No of Adults = %d",a);
printf("\n No of Senior citizens = %d",s);
return 0;
}

OUTPUT :

11
AIM: LBRCE has organized the Magic Show. 3 magicians were invited to thrill
the crowd. At the end of each of the 3 magicians’ shows, the audience was
requested to give their feedback in a scale of 1 to 10. Number of people who
watched each show and the average feedback rating of each show are known. Write
a program to find the average feedback rating of the Magic show.

FORMULA:

Average feedback = (p1*r1+p2*r2+p3*r3)/(p1+p2+p3)

Where p1, p2, p3 are people watched show1, show2, show3 respectively. And r1,
r2, r3 are the average ratings for show1, show2, show3 respectively.

ALGORITHM :

STEP 1: Start
STEP 2: Read p1,r1,p2,r2,p3,r3
STEP 3: Calculate avg = (p1*r1+p2*r2+p3*r3)/(p1+p2+p3)
STEP 4: Display avg
STEP 5 : Stop

FLOWCHART:

12
PROGRAM :

#include <stdio.h>

int main()
{
int p1,p2,p3;
float r1,r2,r3,avg;
printf("Enter the number of people who watched show 1 : ");
scanf("%d",&p1);
printf("Enter the average rating for show 1 : ");
scanf("%f",&r1);
printf("Enter the number of people who watched show 2 : ");
scanf("%d",&p2);
printf("Enter the average rating for show 2 : ");
scanf("%f",&r2);
printf("Enter the number of people who watched show 3 : ");
scanf("%d",&p3);
printf("Enter the average rating for show 3 : ");
scanf("%f",&r3);
avg=(p1*r1+p2*r2+p3*r3)/(p1+p2+p3);
printf("\n The overall average rating for the show is = %.2f",avg);
return 0;
}
OUTPUT :

13
MODULE IDX PROGRAM PAGE NO
NO
Problem solving using Raptor Tool & Basics of C Language
2.1 Celsius to Fahrenheit conversion 15 – 16

2.2 Arithmetic operations 17 – 18


2 2.3 Area and perimeter of circle 19 – 20

2.4 Area of a triangle 21 – 22

2.5 Converting Decimal No to Different Forms 23 – 24

14
AIM : write a C program, algorithm and flow chart to convert the given
Fahrenheit temperature to Celsius.

FORMULA : C=5/9*(F-32) where C=Temperature in Celsius and


F= Temperature in Fahrenheit

ALGORITHM :

STEP-1 : start
STEP-2 : read f
STEP-3 : calculate c=5.0/9×(f-32)
STEP-4 : display c
STEP-5 : stop

15
FLOWCHART :

PROGRAM 2.1 :

#include<stdio.h>
int main ()
{
float C,f;
printf("\n enter farenheit temperature:");
scanf("\n %f",&f);
C=5.0/9*(f-32);
printf("\n Temperature in celcius =%f",C);
return 0;
}

OUTPUT :

16
AIM : write a C program to perform arithmetic operations.

FORMULA : a and b are any two given integers


Sum= a+b, Difference = a-b , Product = a*b , Quotient = a/b , remainder = a%b

ALGORITHM :
STEP-1 : start
STEP-2 : read a,b
STEP-3 : calculate s=a+b , d=a-b, p=a*b, q=a/b, r=a%b
STEP-4 : display s,d,p,q,r
STEP-5 : stop

FLOWCHART:

17
PROGRAM :

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("\n enter any two numbers \t");
scanf("%d %d",&a,&b);
printf("\n sum = %d",a+b);
printf("\n difference = %d",a-b);
printf("\n product = %d",a*b);
printf("\n quotient = %d",a/b);
printf("\n remainder = %d",a%b);
}

18
OUTPUT :

enter any two numbers 10 5

sum = 15
difference = 5
product = 50
quotient = 2
remainder = 0

AIM : Write a C Program to find the area and circumference of circle.

FORMULA:
2
Area = 𝐴 = π𝑟
Circumference = C= 2π𝑟
ALGORITHM :

STEP 1 : Start
STEP 2 : Read the radius as r
STEP 3 : Calculate A = π * 𝑟 * 𝑟
C=2 * π * 𝑟

19
STEP 4 : Display “Area = “, A and “Circumference = “, C
STEP 5 : Stop

FLOWCHART:

PROGRAM :

#include<stdio.h>
#include<conio.h>
#define pi 3.14
void main()
{
int r;
float a,c;
printf("\n enter radius of a circle \t");
scanf("%d",&r);
a=pi*r*r;
c=2*pi*r;
printf("\n area = %.2f",a);
printf("\n circumfrence = %.2f",c);
20
}

OUTPUT :

enter radius of a circle 3

area = 28.26

circumfrence = 18.84

AIM : Write a C Program to find the area of a triangle.

FORMULA:
Area of triangle = 𝐴 = √(𝑠 * (𝑠 − 𝑎) * (𝑠 − 𝑏) * (𝑠 − 𝑐))
where a, b, c are sides of a triangle. And s= (𝑎 + 𝑏 + 𝑐)/2

ALGORITHM :

STEP 1: Start
STEP 2: Read a, b, c
STEP 3: Calculate S = (a+b+c)/2
A = sqrt(s*(s-a)*(s-b)*(s-c))
21
STEP 4: Display A
STEP 5 : Stop

FLOWCHART:

PROGRAM :

#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c;
float area,s;
printf("\n enter sides of a triangle\t");
scanf("%d %d %d",&a,&b,&c);
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\n area = %.2f",area);
}

22
OUTPUT :

enter sides of a triangle 345

area = 6.00

AIM : Write a C Program to display given number in different number systems.

ALGORITHM :

STEP 1 : start
STEP 2 : read an integer (n)
STEP 3 : display decimal (n)
STEP 4 : display octal (n)
STEP 5 : display hexa-decimal(n)
STEP 6 : stop

FLOWCHART:

23
PROGRAM :

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\n enter the number \t");
scanf("%i",&n);
printf("\n decimal = %d",n);
printf("\n octal = %o",n);
printf("\n hexadecimal = %x",n);

24
getch();
}

OUTPUT :

enter the number 12

decimal = 12
octal = 14
hexadecimal = c

MODULE IDX PROGRAM PAGE NO


NO
Exercise Programs on selection statements
3.1 Biggest of three numbers 26-27

3.2 Grade of a student 28-29


3 3.3 Roots of quadratic equation 30-32

3.4 Leap year 33-34

3.5 Current Bill Calculation 35-37

25
AIM : write a C program to find the biggest of three numbers.

ALGORITHM :

STEP 1 : START
STEP 2 : Read a, b ,c values
STEP 3 : if (a>b) and (a>c) then display a is bigger go to step 5
STEP 4 : If b>c then display b is bigger otherwise display c is bigger
26
STEP 5 : Stop

FLOWCHART :

PROGRAM 3.1 :

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\n enter any three numbers\t");
scanf("%d %d %d",&a,&b,&c);
27
if ((a>b)&&(a>c))
printf("%d is big ",a);
else if (b>c)
printf("%d is big ",b);
else
printf("%d is big ",c);
getch();
}

OUTPUT :

enter any three numbers 324


4 is big
enter any three numbers 352
5 is big
enter any three numbers 743
7 is big

AIM : write a C program to find the grade of a student using switch statement.

ALGORITHM :

STEP 1 : Start
STEP 2 : Read the avg marks of the student
STEP 3 : Check the (avg>=80) then display outstanding grade go to step 8

28
STEP 4 : Check the (avg<80 )&(avg>=60) then display grade 1st class go to step
8
STEP 5 : Check (avg<60) & (avg>=50) then display grade 2nd class then go to
step 8
STEP 6 : Check (avg<50) and (avg>=40) then display grade 3rd class then go to
step 8
STEP 7 : Check (avg<40) then display grade fail
STEP 8 : Stop

FLOWCHART:

PROGRAM :
#include<stdio.h>
void main()
{
int marks;
printf("\n enter the average marks \t");
scanf("%d",&marks);
switch(marks/10)

29
{
case 0:
case 1:
case 2:
case 3: printf("\n fail \n");
break;
case 4: printf("\n third class \n");
break;

case 5: printf("\n second class \n");


break;
case 6: printf("\n first class \n");
break;
case 7:
case 8:
case 9:
case 10 : printf("\n distinction \n");
}}

OUTPUT :

enter the average marks 30


fail

enter the average marks 48


third class

enter the average marks 57


second class

enter the average marks 65


first class

enter the average marks 80


distinction

AIM : Write a C Program to find the Roots of a quadratic equation.

FORMULA:
30
The roots of the quadratic equation: x = (-b ± √D)/2a, where D = b2 – 4ac
 Nature of roots:

● D > 0, roots are real and distinct (unequal)


● D = 0, roots are real and equal (coincident)
● D < 0, roots are imaginary and unequal

ALGORITHM :

STEP-1 : Start
STEP-2 : Read a, b , c values
STEP-3 : Calculate d=b*b-4*a*c
STEP-4 : If (d<0) Indicate roots are imaginary go to step 7
STEP-5 : If (d>0) indicate roots are real and unequal and then calculate
r1=-b+sqrt(d)/(2*a)
r2=-b-sqrt(d)/(2*a)
display r1, r2 and goto step 7

STEP-6 : Indicate roots are real and equal , calculate r=-b/(2a) and display r
STEP-7: stop

FLOWCHART:

31
PROGRAM :
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c,d;
float r1,r2;
clrscr();
printf("\n enter values of a b c\t");
scanf("%d %d %d",&a,&b,&c);
d=b*b - 4*a*c;
if(d==0)
{
printf("\n Roots are real & equal");
r1=-b/(2.0*a);
printf("\n root = %.2f",r1);
}

32
else if(d>0)
{
printf("\n Roots are real & unequal");
r1=(-b+sqrt(d))/(2.0*a);
r2=(-b-sqrt(d))/(2.0*a);
printf("\n root1 = %.2f",r1);
printf("\n root2 = %.2f",r2);
}
else
printf("\n Roots are imaginary");
}
OUTPUT :

enter values of a b c 1 2 3
Roots are imaginary

enter values of a b c 1 -5 6
Roots are real & unequal
root1 = 3.00
root2 = 2.00

enter values of a b c 1 -2 1
Roots are real & equal
root = 1.00

33
AIM : Write a C Program to the given year is leap year or not.

FORMULA:
A year is a leap year if the following conditions are satisfied: 
1. The year is multiple of 400.
2. The year is multiple of 4 and not multiple of 100.

ALGORITHM :

STEP 1 : Start
STEP 2 : Read year
STEP 3 : If year%4=0 and (year%100 =0 or year %400=0) then
then go to step 4 otherwise go to step 5.
STEP 4 : print it is a leap year goto step 6
STEP 5 : print it is not a leap year
STEP 6 : stop

FLOWCHART:

34
PROGRAM :

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

OUTPUT :

enter the year 1900


1900 is not a leap year

enter the year 2000


2000 is a leap year

enter the year 1996


1996 is a leap year

enter the year 1999


1999 is not a leap year

35
AIM : Write a C Program to calculate current Bill.

FORMULA :

Given an integer the amount of KWh units of electricity consumed, the task is to


calculate the electricity bill with the help of the below charges:
● 1 to 50 units – Rs 0.50 / unit
● 51 to 150 units – Rs 0.75 / unit 
● 151 to 250 units – Rs 1.20 / unit 
● above 250 units – Rs 1.50 / unit 
ALGORITHM :

Step1 : Start
Step2 : Read units
Step3 : If units<=50 then amount =units*0.50 go to step 7
Step4 : If units>50 and <=150 then amount=25+(units-50)*0.75 go to step 7
Step5 : If units>150 and <=250 then amount=100+(units-150)*1.20 go to step 7
Step6 : if units >250 then amount=220+(units-250)*1.50
Step 7 : charges=amount *0.20
Step 8 : total= amount + charges
Step 9 : display total
Step 10 : stop

36
FLOWCHART:

37
PROGRAM :

#include<stdio.h>
int main()
{
int unit;
float amt, total_amt, charge;
printf("enter total units consumed:");
scanf("%d",&unit);
if(unit<=50)
amt=unit*0.50;
else if(unit<=150)
amt=25+((unit-50)*0.75);
else if(unit<=250)
amt=100+((unit-150)*1.20);
else
amt=220+((unit-250)*1.50);

charge=amt*0.20;
total_amt=amt+charge;
printf("electricity bill=Rs%2f",total_amt);
return 0;
}

OUTPUT :

38
MODULE IDX PROGRAM PAGE NO
NO
Exercise Programs on Loops
4.1 Count No of Digits 39-40

4.2 Multiplication Table 41-42


4 4.3 Prime or not 43-44

4.4 Reverse the given no 45-46

4.5 Factorial 47-48

39
AIM : write a C program to count no of digits of a given number.

ALGORITHM :

STEP 1 : START
STEP 2 : Read a num .
STEP 3 : count = 0.
STEP 4 : If num > 0 then increment count by 1 i.e. count++.
STEP 5 : Divide num by 10 i.e., num= num / 10.
STEP 6 : Repeat step 4 to 5 till num > 0 or num != 0.
STEP 7 : Display count
STEP 8 : STOP

FLOWCHART :

40
PROGRAM 4.1 :

#include <stdio.h>
int main()
{
int num, count=0;
printf("Enter any number: ");
scanf("%d", &num);
do
{
count++;
num /= 10;
} while(num != 0);
printf("Total digits: %d", count);
return 0;
}

OUTPUT :

41
Enter any number: 1234
Total digits: 4

AIM : write a C program write a program to print the multiplication table of the
given numbers up to 10 terms.

ALGORITHM :

Step 1 : start
Step 2 : read n
Step 3 : i=1
Step 4: display (n*i)
Step 5 : i=i+1
Step 6 : repeat step 5,6 until i<=10
Step 7 : stop

42
FLOWCHART:

PROGRAM :

#include<stdio.h>
void main()
{
int n,f,i;
printf("\n which table \t");
scanf("%d",&n);
printf(" \n multiplication table for %d is\n",n);
for(i=1; i<=10; i++)
printf("\n%d X %d = %d\n",i,n,i*n);
}

OUTPUT :

which table 5

multiplication table for 5 is

43
1X5=5
2 X 5 = 10
3 X 5 = 15
4 X 5 = 20
5 X 5 = 25
6 X 5 = 30
7 X 5 = 35
8 X 5 = 40
9 X 5 = 45
10 X 5 = 50

AIM : Write a C Program to find whether the given a number is prime or not.

ALGORITHM :

Step 1: start
Step 2: read n
Step 3: i=2
Step 4: repeat step 5 to 7 until i<=n/2
Step 5: put r=n%i
Step 6: if (r==0) then display not prime goto 8
Step7 : i=i+1
Step 7: if (i>n/2) display it is a prime
Step 8: stop

44
FLOWCHART:

PROGRAM :

#include<stdio.h>
void main()
{
int n,r,i=1;
clrscr();
printf("\n enter the number \t");
scanf("%d",&n);
for(i=2; i<=n/2; i++)
{
r= n%i;
if(r==0)
break;
}
if(i>n/2)
printf("\n it is a prime \n");
45
else
printf("\n it is not a prime \n");

getch();
}

OUTPUT :

enter the number 5


it is a prime

enter the number 21


it is not a prime

AIM : Write a C Program to find reverse of a given number.

ALGORITHM :

Step-1: Start
Step-2: Read n
Step-3: rev=0
Step-3: repeat step 4 to 6 until n!=0
Step-4: Calculate r=n%10
Step-5: rev=rev*10+r
Step-6: Calculate n=n/10
Step-7: display rev
Step-7: Stop
46
FLOWCHART:

PROGRAM :

#include<stdio.h>
main()
{
int n,r,rev=0;
printf("enter the number \t");
scanf("%d",&n);
printf("\n the reverse of the given no is \t");
while(n>0)
{
r = n%10;
rev=rev*10+r;
n = n/10;
}
printf("%d",rev);
47
}

OUTPUT :

enter the number 145

the reverse of the given no is 541

AIM : Write a C Program to find factorial of a given number.

ALGORITHM :

STEP-1: Start
STEP-2: read n
STEP-3: f=1, i=1
STEP-4: repeat step 5 until i>n
STEP-5: calculate f=f*i , i=i+1
STEP-6: Display f
STEP-7: Stop

FLOWCHART:

48
PROGRAM :

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1;
long f=1;
clrscr();
printf("\n enter the number \t");
scanf("%d",&n);
while(i<=n)
{
f=f*i;
i++;
49
}
printf("\n factorial = %ld",f);
getch();
}

OUTPUT :

enter the number 5


factorial = 120

MODULE IDX PROGRAM PAGE NO


NO
Exercise Programs on Loops & Nesting of Loops.
5.1 Fibonacci series 50-51

5.2 Palindrome 52-53

5.3 Factors 54-55

5 5.4 Armstrong or not 56-57

5.5 Print the following format


1
2 2 58-59
3 3 3
4 4 4 4 4
5 5 5 5 5

50
5.6 Print the following format
*
***
***** 60-62
*******
*********

AIM : write a C program to generate Fibonacci series unto n terms.

ALGORITHM :

step 1 : start
step 2 : let f0=0,f1=1
step 3 : read n
step 4 : repeat step 5 to step 8 'n' number of times
step 5 : display f0
51
step 6 : f2=f0+f1
step 7 : f0=f1
step 8 : f1=f2
step 9 : stop

FLOWCHART :

PROGRAM 5.1 :

#include<stdio.h>
#include<conio.h>
void main()
{
int n,f1=0,f2=1,f,i=1;
clrscr();
printf("\n enter the number of fibonacci numbers \t");
scanf("%d",&n);
printf("\n fibonacci series is \n\n");

52
while(i<=n)
{
f=f1+f2;
printf("%d\t",f1);
f1=f2;
f2=f;
i++;
}
getch();
}

OUTPUT :

enter the number of fibonacci numbers 8

fibonacci series is

0 1 1 2 3 5 8 13

AIM : write a C program find a number is palindrome (or) not.

ALGORITHM :
STEP 1 : start
STEP 2 : read a number
STEP 3 : [initialize] rev=0,originalvalue=n
STEP 4 : repeat step 4 through 7 until (n!=0)
STEP 5 : r=n%10
53
STEP 6 : rev=rev*10+r
STEP 7 : n=n/10
STEP 8 : if(originalvalue==rev)
Display "number is palindrome"
otherwise
Display"number not a palindrome"
STEP 9 : stop

FLOWCHART:

PROGRAM :

#include<stdio.h>
main()
{
int n,r,rev=0,m;
clrscr();
printf("enter the number \t");
scanf("%d",&n);
m = n;
while(n>0)
54
{
r = n%10;
rev = rev*10 + r;
n = n/10;
}
printf(" \nreverse of a number = %d\n",rev);
if( m == rev )
printf(" palendrome");
else
printf("not a palendrome");
getch();
}

OUTPUT :

enter the number 121


reverse of a number = 121
palendrome

enter the number 123


reverse of a number = 321
not a palendrome

AIM : Write a C Program to find the factors of a given a number.

ALGORITHM :

Step1 : start
Step2 : read n
Step3 : i=1

55
Step4 : repeat step 5 through 7 until while(i<=n/2)
Step5 : r=n%i
Step6 : if(r==0) then display i
Step7 : i=i+1
Step8 : stop

FLOWCHART:

PROGRAM :

#include<stdio.h>
void main()
{
int n,f,i;
printf("\n enter the number \t");
scanf("%d",&n);
printf(" \n factors of %d are",n);
for(i=1; i<=n/2; i++)
if(n%i==0)
56
printf("\t%d",i);
}

OUTPUT :

enter the number 153


factors of 153 are 1 3 9 17 51

AIM : Write a C Program to find the given number is Armstrong or not

ALGORITHM :
Step 1 : start
Step 2 : read the number n [initialize m=n]
Step3 : repeat step3 through 5 until(m!=0)
Step4 : calculate r=m%10
Step5 : sum=sum+r*r*r
57
m=m/10
step6 : if (sum ==n)
display it is Armstrong number
otherwise
display it is not a Armstrong number
step8 : stop

FLOWCHART:

PROGRAM :

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

OUTPUT :

enter the number 153


it is armstrong number

enter the number 678


it is not an armstrong number

AIM : Write a C Program to print the following format.

1
2 2
3 3 3
4 4 4 4 4
5 5 5 5 5

59
ALGORITHM :

Step 1 : start
Step 2 : read the number of lines n
Step3 : Taking i from 1 to n do step 4
Step4 : varying j from 1 to i display j
step6 : Stop

PROGRAM :

#include<stdio.h>
void main()
{
int n,i=1,j;
printf("\n enter the number of lines \t");
scanf("%d",&n);
while(i<=n)
{
j=1;
while(j<=i)
{
printf("%d\t",i);
j++;
}
printf("\n\n");
i++;
}
}

OUTPUT :

enter the number of lines 5


1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

FLOWCHART:
60
AIM : Write a C Program to print the following format.

enter no. of rows: 5


*
***
*****
*******
*********

61
ALGORITHM :

Step 1 : Decide the number of rows and columns

There is a typical structure to print any pattern, i.e., the number of rows and
columns. We need to use two loops to print any pattern, i.e., use nested loops.

The outer loop tells us the number of rows, and the inner loop tells us the
column needed to print the pattern.

Accept the number of rows from a user Iterate rows

Step 2 : Iterate columns

Next, write the inner loop or nested loop to handle the number of columns.
The internal loop iteration depends on the values of the outer loop.

Step 3 :Print star

Use the print() function in each iteration of nested for loop to display the


symbol (like a star (asterisk *)).

Step 4 : Add new line after each iteration of outer loop

Add a new line after each iteration of the outer loop so that the pattern display
appropriately

FlowChart :

62
PROGRAM :

63
#include <stdio.h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}

OUTPUT :

Enter the number of rows : 5

*
***
*****
*******
*********

64
MODULE IDX PROGRAM PAGE NO
NO
Exercise Programs on 1D Arrays
6.1 Sum and average of n numbers 64-65

6.2 Maximum and minimum in a list 66-67


6 6.3 Linear search 68-69

6.4 Binary search 70-71

6.5 Bubble Sort 72-73

65
AIM : write a C program to find the sum and average of given numbers.

ALGORITHM :

STEP1 : Start
STEP2 : Read n, A
STEP3 : Let i=0
STEP4 : Repeat step5 until i<n
STEP5 : calculate sum=sum+A[i]
i=i+1
STEP 6 : calculate avg=sum/n
STEP 7 : Display sum, avg
STEP 8 : Stop

PROGRAM 6.1 :
#include<stdio.h>
int main()
{
int A[100],n,i,sum=0;
float avg;
printf("enter no of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the element %d:",i+1);
scanf("%d",&A[i]);
sum=sum+A[i];
}
avg=(float)sum/n;
printf("\n the sum of the array is %d",sum);
printf("\n the avg of the array %.2f",avg);
return 0;
}

66
OUTPUT :

67
AIM : write a C program to find maximum and minimum element in a list.

ALGORITHM :

STEP 1 : START
STEP 2 : Read n , A
STEP 3 : Let i=0, min=max=A[0]
STEP 4 : Repeat step5 to step7 until i<n
STEP 5 : If min<A[i] then min=A[i]
STEP 6 : If max<A[i] then max=A[I]
STEP 7 : i=i+1
STEP 8 : Display min, max
STEP 9 : Stop

PROGRAM :

#include<stdio.h>
int main()
{

int A[20],i,n,min,max;
printf("enter the size of array:");
scanf("%d",&n);
printf("enter the numbers:");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
min=max=A[0];
for(i=0;i<n;i++)
{
if(min>A[i])
min=A[i];
if(max<A[i])
max=A[i];
}
printf("\nmin in array is %d",min);
printf("\nmax in array is %d",max);
return 0;
68
}

OUTPUT :

69
AIM : Write a C Program to perform linear search.
ALGORITHM :

STEP1 : Start
STEP2 : If i>n then go to step7
STEP3 : If A[i]=x then display element x found at index I and go to step 7
STEP4 : Set i to i+1
STEP5 : Go to step 2
STEP6 : Print element not found
STEP7 : Stop

PROGRAM :
#include<stdio.h>
void main()
{
int n,i,a[50],item,flag = 0;
clrscr();
printf(" enter no of elements \t ");
scanf("%d",&n);
printf("\n enter the elements \n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
printf("enter the element to search \t");
scanf("%d",&item);
for(i=0; i<n; i++)
{
if(a[i]== item)
{
flag = 1;
break;
}
}
if(flag == 1)
printf("item found at %d position ",i+1);
else
printf("\n item not found ");
getch();
70
}

OUTPUT :

enter no of elements 5
enter the elements
12 23 24 45 67
enter the element to search 45
item found at 4 position

71
AIM : Write a C Program to search for a given key using binary search.

ALGORITHM :

STEP 1 : Start
STEP 2 : Read n, A, key
STEP 3 : Set low=1,high=n
STEP 4 : If high<low then display key does not exits goto step 8
STEP 5 : Set mid=(low+high)/2
STEP 6 : If A[mid]<key Set low=mid+1 goto step 4
STEP 7 : If A[mid]>key Set high=mid-1 goto step 4
STEP 8: If A[mid]=key display key found at location mid
STEP 9 : Stop

PROGRAM :

#include<stdio.h>
void main()
{
int n,i,a[50],item,flag = 0,lb,ub,mid;
clrscr();
printf(" \n enter no of elements \t ");
scanf("%d",&n);
printf("\n enter the elements \n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
printf("enter the element to search \t");
scanf("%d",&item);
lb =0; ub = n;
while(lb<=ub)
{
mid = (lb+ub)/2;
if(a[mid] == item)
{
flag =1;
break;
}

72
else if(a[mid]>item)
ub = mid-1;
else
lb = mid+1;
}
if(flag == 1)
printf("item found at %d position ",mid+1);
else
printf("\n item not found ");
getch();
}

OUTPUT :

enter no of elements 5
enter the elements
10 20 30 40 50
enter the element to search 50
item found at 5 position

enter no of elements 5
enter the elements
10 20 30 40 50
enter the element to search 90
item not found

73
AIM : Write a C Program to sort the elements using bubble sort.

ALGORITHM :

STEP 1 : Start
STEP 2 : Read n, A
STEP 3 : Repeat step4 n-1 number of times
STEP 4 : For every pair of elements at j of A
If A[j]>A[j+1] then Swap(A[j],A[j+1])
STEP 5 : Display A
STEP6 : Stop

PROGRAM :

#include<stdio.h>
int main()
{
int n,A[20],i,j,t;
printf("\n enter the no of elements:");
scanf("%d",&n);
printf("\n enter elements in the list:");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(A[j]>A[j+1])
{
t=A[j];
A[j]=A[j+1];
A[j+1]=t;
}
}
}

74
printf("\n sorted list is \n");
for(i=0;i<n;i++)
printf("%d\t",A[i]);
return 0;
}

OUTPUT :

75
MODULE IDX PROGRAM PAGE NO
NO
Exercise Programs on 2D Arrays
7.1 Addition and subtraction of two matrices 75-77

7.2 Multiplication of matrices 78-80


7
7.3 Transpose of a matrix 81-82

7.4 Sorting city names in alphabetical order 83-84

76
AIM : write a C program to find the addition and subtraction of two matrices.

ALGORITHM :

Step-1:-start

Step-2:-declare matrix A and matrix B

Step-3:-read r1, c1, r2, c2, A and B

Step-4:-if (r1=r2) and (c1=c2)

Then add A , B and subtract A, B

Otherwise addition and subtraction are not possible

Step-5:-s and d are required matrix after addition

And subtraction of two matrix display s,d

Step-6:-stop.

PROGRAM 7.1 :

#include<stdio.h>
int main()
{
int a[5][5],b[5][5],s[5][5],d[5][5],r1,c1,r2,c2,i,j;
printf("\nenter the no.ofrows,columns of 1st matrix:\n");
scanf("%d %d",&r1,&c1);
printf("the elements in the 1st matrix are:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
77
}
printf("the elements of the 1st matrix are");
printf("\n");

for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
printf("\t%d",a[i][j]);
}
printf("\nenter the no.of rows, columns of 2nd matrix:\n");
scanf("%d %d",&r2,&c2);
printf("the elements in the 2nd matrix are:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&b[i][j]);
}
printf("the elements of the 2nd matrix are");
printf("\n");
for(i=0;i<r2;i++)
{
printf("\n");
for(j=0;j<c2;j++)
printf("\t%d",b[i][j]);
}

if(r1==r2&&c1==c2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
s[i][j]=a[i][j]+b[i][j];
d[i][j]=a[i][j]-b[i][j];
}
}
printf("\naddition of two matrices is\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
78
printf("%d\t",s[i][j]);
}
printf("\n");
}

printf("\nsubstraction of two matrices is\n");


for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
}
else
printf("addition and substraction of two matrices is not possible");
return 0;
}

OUTPUT :

79
AIM : write a C program to find matrices multiplication.

ALGORITHM :

Step-1 :-start
Step-2 :-enter the rows and columns of the 1st matrix
Step-3:- enter the rows and columns of the 2nd matrix
Step-4:- enter the elements of the 1st matrix in matrix form
Step-5:- enter the elements of the 2nd matrix in matrix form
Step-6:- print the elements of the 1stmatrix in the matrix form
Step-7:- print the elements of the 2nd matrix in the 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

80
Step-11:- multiply the 1st and 2nd matrix and store the elements in the 3rd matrix
Step-12:- print the final matrix
Step-13:- stop .

PROGRAM :

#include<stdio.h>
int main()
{
int a[5][5],b[5][5],p[5][5],r1,c1,r2,c2,i,j,k;
printf("enter the rows and columns in first matrix:\n");
scanf("%d%d",&r1,&c1);
printf("elements in the first matrix:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("the elements of the first matrix are");
printf("\n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)

printf("\t%d",a[i][j]);
}
printf("\nenter the rows and columns in second matrix:\n");
scanf("%d%d",&r2,&c2);
printf("\nenter the elements in the second matrix:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
}
printf("the elements in the second matrix are");
printf("\n");
for(i=0;i<r2;i++)
{
printf("\n");
for(j=0;j<c2;j++)
printf("\t%d",b[i][j]);
81
}
if(c1==r2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
p[i][j]=0;
for(k=0;k<c1;k++)
p[i][j]=p[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\nproduct of two matrices is\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",p[i][j]);
}
printf("\n");
}
return 0;
}

OUTPUT :

82
83
AIM : Write a C Program to find the transpose of a matrix.

ALGORITHM :

Step-1:- start
Step-2:- read matrix
Step-3:- display the entered matrix
Step-4:- swap (i,j) with (j,i)
Step-5:- display the transpose of the matrix
Step-6:- stop

PROGRAM :

#include<stdio.h>
int main()
{
int a[5][5],r1,c1,i,j,temp;
printf("enter the no.of rows and columns:\n");
scanf("%d %d",&r1,&c1);
printf("the elements in the 1st matrix are:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("the elements in the matrix are");
printf("\n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
printf("\t%d",a[i][j]);
}

for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
84
if(i<j)
{
temp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp;
}
}
}
printf("\ntranspose of matrices is:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
return 0;
}

OUTPUT :

85
AIM : Write a C Program to sort the given city names .

ALGORITHM :

Step-1:- start
Step-2:- read the no of cities as ‘n’, city names as ‘A’
Step-3:- repeat step for i varies from 0 to n-1
Step-4:- for j=0 to n-i-1 do
if (A[j]>A[j+1])
interchange (A[j],A[j+1])
Step-5:- display A
Step-6:- stop

PROGRAM :

#include<stdio.h>
#include<string.h>
int main()
{
char a[10][20],t[20];
int n, i, j;
printf("\nenterno.of cities:");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("\nenter the city name%d:\n",i+1);
scanf("%s",a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(strcmp(a[j],a[j+1])>0)
{
strcpy(t,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],t);
}

}
}
printf("\nnames after sorting are\n");
86
for(i=0;i<n;i++)
{
printf("%s\n",a[i]);
}
return 0;
}

OUTPUT :

MODULE IDX PROGRAM PAGE NO


NO
87
Exercise Programs on Strings with & without using built-in functions
8.1 find the string length 86

8.2 copy one string into another 87


8 8.3 concatenate two strings 88

8.4 check whether two strings are equal or not 89-90

8.5 convert the given string into upper and lower case 91

88
AIM : write a C program to find the length of a string.

ALGORITHM :
Step 1 : Start
Step 2 : Declare an array
Step 3 : Read the string as S
Step 4 : Use for loop l=0;
For each character of string s l=l+1
Step 5 : Print the length of the string as l
Step 6 : Stop.

PROGRAM 8.1 :

#include<stdio.h>
#include<string.h>
void main()
{
char str[50];
int l,i=0;
printf("enter any string \t");
gets(str);
l = strlen(str);
printf("\n string length using strlen() = %d",l);
for(i=0;str[i] !='\0'; i++);
printf(" \nstring length without using builtin function = %d\n",i);
}

OUTPUT :

enter any string Lbrce

string length using strlen() = 5


string length without using builtin function = 5

89
AIM : write a C program to copy one string into another string.

ALGORITHM :
Step1 : Start
Step2 : Intialize a 3 char arrays S1,S2,S3
Step3 : Get the array of the character or string S1 from the user by the function gets
Step4: Copy the string S1 to S2 using built in function string copy
Step5: Intialize the for loop copy the each character of a string S1 to S3
Step6: print the string S3
Step7: Stop

PROGRAM :

#include<stdio.h>
#include<string.h>
void main()
{
char str[50],str2[50],str3[50];
int i;
printf("enter string \t");
gets(str);
strcpy(str2,str);
strncpy(str3,str,2);
str3[2] = '\0';
printf("\n original string = %s \n",str);
printf("\n string using strcpy() function = %s\n",str2);
printf("\n string using strncpy() = %s \n",str3);
for(i=0; str[i] !='\0';i++)
str2[i] = str[i];
str2[i] = '\0';
printf(" \n copied string with out using strcpy() = %s ",str2);
}
OUTPUT :
enter string Lbrce
original string = Lbrce
string using strcpy() function = Lbrce
string using strncpy() = Lb
copied string with out using strcpy() = Lbrce

90
AIM : write a C program to concatenate two string.

ALGORITHM :

Step 1 : Start
Step 2 : Declare four arrays of strings S1, S2, S3, S4.
Step 3 : Intialize two variables i, j.
Step 4 : Get the strings S1,S2 from the user by the function gets
Step 5 : Copy the string S3 to S1 & S4 to S2 using the built in function strcpy
Step 6 : Concatenate the strings S1&S2 by using the string function strcat
Step7 : print the concatenated string of S3&S2 is S1.
Step8 : Use for loop to copy S2 at the end of S1 using S4.
Step9 : Print the concatenated string without using built-in function as S4.
Step 10 : Stop.
PROGRAM :
#include<stdio.h>
#include<string.h>
void main()
{
char str1[50],str2[50];
int i,j;
printf("\nenter first string \t");
gets(str1);
for(i=0; str1[i] !='\0'; i++);
printf("\nenter the second string\t");
gets(str2);
strcat(str1,str2);
printf("\n concatenated string using strcat() = %s \n",str1);
for(j=i,i=0; str2[i] != '\0';j++,i++)
str1[j] = str2[i];
str1[j] = '\0';
printf("\n concatenated string without using strcat() = %s",str1);
}
OUTPUT :
enter first string Lbrce
enter the second string College
concatenated string using strcat() = Lbrce College
concatenated string without using strcat() = Lbrce College

91
AIM : Write a C Program to check whether two strings are equal or not

ALGORITHM :
Step1 : Start
Step 2 : Declare two arrays of the strings.
Step 3 : Read the strings S1,S2.
Step 4 : Intialize a variable cmp of type int.
Step 5 : Compare two strings using the built in function strcmp().
Step 6 : If cmp>0 then print S1>S2. goto step 9
Step 7 : If cmp<0 then print S1<S2. goto step 9
Step 8 : If the above two conditions are not ture then print S1=S2.
Step 9 : Stop

PROGRAM :
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char str1[50],str2[50];
int j,flag = 0;
clrscr();
printf("enter first string \t");
gets(str1);
printf("enter second string ");
gets(str2);
for(j=0; str1[j] !='\0' || str2[j] !='\0'; j++)
if (str1[j] != str2[j])
{
flag = 1;
break;
}
if(flag == 1 )
printf(" strings are not equal ");
else
printf(" strings are equal");
getch();
}

92
OUTPUT :

enter first string Lbrce


enter second string LBRCE
strings are not equal

enter first string Lbrce


enter second string Lbrce
strings are equal

93
AIM : write a C program to convert the given string into lower and upper case
letters.

ALGORITHM :

Step 1 : Start.
Step 2 : Intailize a char array S.
Step 3 : Read the string S.
Step 4 : Using strupr() function convert S to upper case and display.
Step 5 : Using strlwr() function convert S to lower case and display.
Step 6 : Stop

PROGRAM 8.5 :

#include<stdio.h>
#include<string.h>

void main()
{
char str[50];
printf("enter any string \t");
gets(str);
strupr(str);
printf("\n string in uppercase = %s",str);
strlwr(str);
printf("\n string in lowercase = %s",str);
}

OUTPUT :

enter any string Lbrce


string in uppercase = LBRCE
string in lowercase = lbrce

94
MODUL IDX PROGRAM PAGE NO
E
NO
Exercise Programs on Functions & Recursive Functions.
9.1 Arithmetic operations using functions 93-94
9.2 LCM and GCD of two numbers 94-96
9.3 evaluate the expression 2.5logx+cos320+|x2-y2|+√2xy 97

9 9.4 Factorial of a number with and without recursion 98-99


9.5 Sum of the series 1+2+3+…+n using recursion 100

9.6 Fibonacci series using recursion 101-102


9.7 Towers of Hanoi using recursion 103-104
9.8 Binary Search using recursion 105-106

95
AIM : write a C program to Arithmetic operations using functions.

FORMULA : a and b are any two given integers


Sum= a+b, Difference = a-b , Product = a*b , Quotient = a/b , remainder = a%b

ALGORITHM :
STEP-1 : start
STEP-2 : read a,b
STEP-3 : sum() , difference(), product(), quotient(),remainder()
are functions that return a+, a-b, a*p, a/b, a%b respectively.
STEP-3 : calculate s=sum(a,b)
d=difference(a,b)
p=product(a,b)
q=quotient(a,b)
r=remainder(a,b)
STEP-4 : display s,d,p,q,r
STEP-5 : stop

PROGRAM 9.1 :

#include<stdio.h>
void main()
{
int sum(int,int);
int difference(int,int);
int product(int,int);
int quotient(int,int);
int remainder(int,int);
int x,y,r;
printf("\n enter any two numbers \t");
scanf("%d %d",&x,&y);
r=sum(x,y);
printf("\n sum = %d",r);
r=difference(x,y);
printf("\n difference = %d",r);
r=product(x,y);
printf("\n product = %d",r);
r=quotient(x,y);
96
printf("\n quotient = %d",r);

r=remainder(x,y);
printf("\n remainder = %d",r);
}

int sum( int a, int b)


{
return a+b;
}

int difference( int a, int b)


{
return a-b;
}

int product( int a, int b)


{
return a*b;
}

int quotient( int a, int b)


{
return a/b;
}

int remainder( int a, int b)


{
return a%b;
}

OUTPUT :

enter any two numbers 10 5


sum = 15
difference = 5
product = 50
quotient = 2
remainder = 0

97
AIM : write a C program to find LCM and GCD of two numbers using functions.

ALGORITHM :
STEP-1 : start
STEP-2 : read a,b
STEP-3 : GCD(a,b) function is defined as
repeat the following until a!=b
if(a>b) then a= a-b otherwise b=b-a
return a
STEP-4 : LCM(a,b) function is defined as
p=a*b
l=p/(GCD(a,b)
return l
STEP-5 : call l=LCM(a,b),and g= GCD(a,b) display l,g
STEP-6 : stop

PROGRAM :

#include<stdio.h>
void main()
{
int a,b,l,g;
int LCM(int,int);
int GCD(int,int);
printf("\n enter any two numbers \t");
scanf("%d %d",&a,&b);
g=GCD(a,b):
l=LCM(a,b);
printf("\n lcm=%d",l);
printf("\n gcd = %d",g);
}

int LCM(int a, int b)


{
return (a*b)/GCD(a,b);
}

98
int GCD(int a, int b)
{
while(a!=b)
{
if (a>b)
a = a-b;
else
b = b-a;
}
return a;
}

OUTPUT :
enter any two numbers 12 15
lcm=60
gcd = 3

99
AIM : write a C program to evaluate the expression 2.5logx+cos320+|x2-y2|+√2xy.

ALGORITHM :

STEP-1 : start
STEP-2 : read x,y
STEP-3 : using mathematical functions
Calculate e= 2.5*log(x)+abs(x*x-y*y)+sqrt(2)*x*y
STEP-4 : display e
STEP-5 : stop

PROGRAM :

#include<stdio.h>
#include<math.h>
void main()
{
int x,y;
float e;
float eval(int x,int y);
printf("\n enter values of x, y\t");
scanf("%d %d",&x,&y);
e=eval(x,y);;
printf("\n value of the expression = %.2f",e);
}

float eval(int x,int y)


{
float e;
e=2.5*log(x)+abs(x*x-y*y)+sqrt(2)*x*y;
return e;
}

OUTPUT :

enter values of x, y 5 4
value of the expression = 41.31
100
AIM : Write a C Program to find the Factorial of a number with and without
recursion

ALGORITHM :

STEP-1 : Start
STEP-2 : Read n
STEP-3 : fact() function is defined as
f=1;
for i varies from 1to n
f=f*i
return f
STEP-4 : rfact() is defined as
If( n==0 ) then
return 0
otherwise
return n* rfact(n-1)

STEP-5 : call fact() and rfact() function and display returned value
STEP-6 : Stop
PROGRAM :

#include<stdio.h>
#include<conio.h>
void main()
{
long rfact(int);
long fact(int);
int n;
long f;
clrscr();
printf("\n enter the number \t");
scanf("%d",&n);
f=fact(n);
printf("\n factorial without recursion = %ld",f);
f=rfact(n);
printf("\n factorial with recursion = %ld",f);

101
}

long fact(int n)
{
long f=1;
int i=1;
for(i=1;i<n;i++)
f=f*I;
return f;
}

long rfact(int n)
{
if(n==0)
return 1;
else
return(n*rfact(n-1));
}

OUTPUT :

enter the number 5


factorial without recursion = 120
factorial with recursion = 120

102
AIM : write a C program to Sum of the series 1+2+3+…+n using recursion.

ALGORITHM :

STEP-1 : Start
STEP-2 : Read n
STEP-3 : sum() function is defined as
s=0;
for i varies from 1to n
s=s+i
return s
STEP-5 : call sum function as s=sum(n) , display s
STEP-6 : Stop

PROGRAM 9.5 :

#include<stdio.h>
void main()
{
int n,s;
int sum(int);
printf("\n enter the number \t");
scanf("%d",&n);
s=sum(n);
printf("\n sum of the series = %d",s);
}

int sum(int n)
{
if (n==1)
return 1;
else
return(n+sum(n-1));
}

OUTPUT :

enter the number 5


103
sum of the series = 15

AIM : write a C program to Fibonacci series using recursion .

ALGORITHM :

step 1 : start
step 2 : read n
step 3 : fib() function is defined as
if (n==0) or( n==1) then
return n
otherwise
return fib(n-1)+fib(n-2)
Step 5 : call fib() function n times and display return value
step 9 : stop

PROGRAM 9.6 :

#include<stdio.h>
void main()
{
int n,f,i;
printf("\n enter the number of fibonacci numbers \t");
scanf("%d",&n);
printf("\n fibonacci series is \n\n");
for(i=0; i<n; i++)
{
f=fib(i);
printf("%d\t",f);
}
}

int fib(int n)
{
if(n==0)
return 0;
else if( n==1)
return 1;
else
return(fib(n-1) + fib(n-2));
104
}

OUTPUT :

enter the number of fibonacci numbers 8


fibonacci series is
0 1 1 2 3 5 8 13

105
AIM : write a C program to solve Towers of Hanoi problem using recursion.

ALGORITHM :

step 1 : start
step 2 : read no of discs as n
step 3 : tower(n,S,A,D) function is defined as
if(n=1) then
move disc from Source -> Destination
otherwise
call tower(n-1,S,D,A);
move disc from Source -> Destination
call tower(n-1,A,S,D);
step 3 : call tower() function as tower(n,S,A,D);
step 4 : stop

PROGRAM 9.7 :

#include<stdio.h>
void main()
{
void tower(int,char,char,char);
int n;
printf("\n enter the number of discs \t");
scanf("%d",&n);
printf(" \n\nthe moves are \n");
tower(n,'S','A','D');
}
void tower(int n, char s, char a, char d)
{
if(n==1)
printf("\n\n%c -> %c",s,d);
else
{
tower(n-1,s,d,a);
printf("\n\n%c -> %c",s,d);
tower(n-1,a,s,d);
}
106
}

OUTPUT :

enter the number of discs 3


the moves are
S -> D
S -> A
D -> A
S -> D
A -> S
A -> D
S -> D

107
AIM : write a C program to implement binary search using recursion.

ALGORITHM :

We basically ignore half of the elements just after one comparison.


Step 1 : Compare key with the middle element.
Step 2: If key matches with middle element, we return 1.
Step 3: Else If key is greater than the mid element, then key can only lie in right
half subarray after the mid element. So we recur for right half.
Step 4: Else (key is smaller) recur for the left half.

PROGRAM 9.8 :

#include<stdio.h>
void main()
{
int n,i,a[50],item,flag;
int bsearch(int,int,int[],int);
printf(" enter no of elements \t ");
scanf("%d",&n);
printf("\n enter the elements \n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
printf("enter the element to search \t");
scanf("%d",&item);
flag = bsearch(0,n,a,item);
if(flag == 1)
printf("item found ");
else
printf("\n item not found ");
}

108
int bsearch(int beg, int end, int a[], int item)
{
int mid;
if(beg>end)
return 0;
else
{
mid = (beg+end)/2;
if(a[mid] == item)
return 1;
else if(a[mid]>item)
return bsearch(beg,mid-1,a,item);
else
return bsearch(mid+1,end,a,item);
}
}

OUTPUT :

enter no of elements 5
enter the elements
10 20 30 40 50
enter the element to search 10
item found

enter no of elements 5
enter the elements
10 20 30 40 50
enter the element to search 70
item not found

109
MODULE IDX PROGRAM PAGE NO
NO
Exercise Programs on pointers
10.1 Swap two numbers using pointers 108

10.2 Arithmetic operations using dynamic allocation 109


10 10.3 Display array elements using pointers 110

10.4 Find small, large element using pointer 111

10.5 Display the city names using pointers 112

110
AIM : write a C program to Swap two numbers using pointers.

PROGRAM 10.1 :

#include<stdio.h>
void swap(int *x, int *y);
int main()
{
int a, b ;
printf(“\n Enter a,b values : \t”);
scanf(“%d %d”,&a,&b);
printf("\n before swapping => a = %d b=%d",a,b);
swap(&a,&b);
printf("\n after swapping => a = %d b=%d",a,b);
return 0;
}
void swap(int *x, int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}

OUTPUT :
Enter a,b values : 10 5

before swapping => a = 10 b=5

after swapping => a = 5 b=10

111
AIM : write a C program to Perform Arithmetic operations using dynamic
allocation

PROGRAM :

#include<stdio.h>
#include<alloc.h>
int main()
{
int *n1, *n2;
n1 = (int*) malloc(sizeof(int));
n2 = (int*) malloc(sizeof(int));
sum = (int*) malloc(2);
printf("\n enter two numbers\n");
scanf("%d %d",n1,n2);
printf("\n Sum = %d", *n1+*n2);
printf("\n Difference = %d", *n1 - *n2);
printf("\n Product = %d", *n1 * *n2);
printf("\n Quotient = %d", *n1/*n2);
printf("\n Remainder = %d", *n1%*n2);
free(n1);
free(n2);
return 0;
}

OUTPUT :

enter two numbers


10 5
Sum = 15
Difference = 5
Product = 50
Quotient = 2
Remainder = 0

112
AIM : write a C program to display array elements using pointers

PROGRAM :

#include<stdio.h>
int main()
{
int x[50], i = 0, *p1,*p2;
char resp='y';
while(resp=='y' || resp=='Y')
{
printf("\n enter an element : ");
scanf("%d",&x[i]);
printf("\n want to enter one more ? (y/n):");
fflush(stdin);
resp=getchar();
i++;
}
p1 = x;
p2 = x+i;
printf("\n no of elements in array = %d",p2-p1);
printf("\n display elements through pointers : \n");
for(;p1<p2;)
printf("%d\t",*p1++);
return 0;
}

OUTPUT :
enter an element : 10
want to enter one more ? (y/n):y
enter an element : 20
want to enter one more ? (y/n):y
enter an element : 30
want to enter one more ? (y/n):y
113
enter an element : 40
want to enter one more ? (y/n):n
no of elements in array = 4
display elements through pointers :
10 20 30 40

AIM : Write a C Program to find the largest and smallest of list of values using
pointers.

PROGRAM :

#include<stdio.h>
int main()
{
int i,n,small,large,*ptr, a[50];
printf("\n how many elements ?");
scanf("%d", &n);
printf("\n enter the elements: \n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
ptr=a;
small=large=*ptr;
for(i=1;i<n;i++)
{
if(*(ptr+i)<small)
small = *(ptr+i);
if(*(ptr+i)>large)
large = *(ptr+i);
}
printf("\nsmall element is %d",small);
printf("\nlarge element is %d",large);

return 0;
}

OUTPUT :

how many elements ?5

enter the elements:


114
10 56 34 23 89

small element is 10
large element is 89

AIM : write a C program to Display the city names using pointers.

PROGRAM 10.5 :

#include<stdio.h>
#include<stdlib.h>
int main()
{
char *name[50];
int i,n;
printf("\n how many names ?");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter name %d :",i+1);
name[i]=(char*)malloc(25);
gets(name[i]);
}
printf("\n Names list is :\n");
for(i=0;i<n;i++)
puts(name[i]);
return 0;
}

OUTPUT :

how many names ?5


enter name 1 :saritha
enter name 2 :manasa
enter name 3 :ajay
enter name 4 :vijaya
enter name 5 :reddy
Names list is :
saritha
115
manasa
ajay
vijaya
reddy

MODULE IDX PROGRAM PAGE NO


NO
Exercise Programs on user defined data types.
11.1 Print the electricity bill 114-116
11.2 Display the students marks memo 117-120
11
11.3 Display the employee information 121-122
11.4 Display the bank customers information 123-124
11.5 Display the total marks of each student & subject 125-126

116
AIM : write a C program to Print the electricity bill using structures.

PROGRAM 11.1 :

#include<stdio.h>
#define SPACE(n) for(i=0;i<n;i++) printf(" ")
#define LINE for(i=0;i<80;i++) printf("-")
struct ecustomer
{
int mno;
char cname[20];
int prev;
int present;
int units;
float charge;
float amt;

};
typedef struct ecustomer ebill;
ebill e[20];
int main()
{
int n,i;
printf("\n Enter No of customers : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter Meterno of customer %d : ",i+1);
scanf("%d",&e[i].mno);
printf("\n Enter Name of customer %d : ",i+1);
scanf("%s",e[i].cname);
printf("\n Enter Previous meter reading of customer %d : ",i+1);
scanf("%d",&e[i].prev);
printf("\n Enter Present meter reading of customer %d : ",i+1);
117
scanf("%d",&e[i].present);
e[i].units=e[i].present-e[i].prev;
if(e[i].units<200)
e[i].amt=e[i].units*1.0;

else if(e[i].units<=300)
e[i].amt=e[i].units*1.5;
else
e[i].amt=e[i].units*2.0;

e[i].charge=e[i].amt*0.01;
e[i].amt=e[i].amt+e[i].charge;
}
printf("\n");
SPACE(23);
printf("APSEB CURRENT BILL REPORT\n");
LINE;
printf("%5s","SNO");
printf("%8s","MeterNo");
printf("%25s","Customer Name");
printf("%8s","Units");
printf("%15s","Total Amount");
printf("\n");
LINE;
for(i=0;i<n;i++)
{
printf("%5d",i+1);
printf("%8d",e[i].mno);
printf("%25s",e[i].cname);
printf("%8d",e[i].units);
printf("%15.2f",e[i].amt);
printf("\n");
}
LINE;
return 0;
}

118
OUTPUT :

119
AIM : write a C program to Display the students marks memo.

PROGRAM :

#include<stdio.h>
#include<string.h>
#define SPACE(n) for(k=1;k<=n;k++) printf(" ");
#define LINE for(k=0;k<80; k++) printf("-");
void store(int);
void display(int);
struct student
{
int hno;
char name[20];
int marks[3];
int tot;
float percent;
char result[10];
};
struct student s[50];
int i,n,j;

int main()
{
printf("\n Enter No of students : ");
scanf("%d",&n);
for(i=0;i<n;i++)
store(i);
for(i=0;i<n;i++)
display(i);
return 0;
}
120
void store(int i)
{
printf("\n enter hall ticket no :");
scanf("%d",&s[i].hno);
fflush(stdin);
printf("\n enter student name : \t");
scanf("%s",s[i].name);
printf("enter marks in three subjects :");
s[i].tot=0;
fflush(stdin);
for(j=0;j<3;j++)
{
scanf("%d",&s[i].marks[j]);
s[i].tot=s[i].tot + s[i].marks[j];
}
s[i].percent = (float) s[i].tot/3;
if(s[i].percent < 35)
strcpy(s[i].result,"failed");
else if(s[i].percent < 50)
strcpy(s[i].result,"third");
else if(s[i].percent < 60)
strcpy(s[i].result,"second");
else
strcpy(s[i].result,"first");
for(j=0; j<3; j++)
if(s[i].marks[j] <35)
strcpy(s[i].result,"failed");
}

void display(int i)
{
printf("\n");
SPACE(30);
printf("BOARD OF SSC, HYDERADAD\n");
LINE;
printf("\n");
121
SPACE(20);
printf("hall ticket no : %d\n",s[i].hno);
SPACE(20);
printf("student name : %s\n",s[i].name);

for(j=0;j<3;j++)
{
SPACE(20);
printf("marks in subject %d : %d\n",j+1,s[i].marks[j]);
}
SPACE(20); printf("Total marks : %d\n",s[i].tot);
SPACE(20); printf("Percentage : %.2f\n",s[i].percent);
SPACE(20); printf("Result : %s\n",s[i].result);
LINE;
printf("\n");
SPACE(30);
printf("\n Please press a key to see next...\n");
getch();
}
OUTPUT :

122
123
AIM : Write a C Program to display the employee information

PROGRAM :

#include<stdio.h>
#include<stdlib.h>
#define SPACE(n) for(int i=0;i<n;i++) printf(" ")
#define LINE for(int i=0;i<80;i++) printf("-")

struct date
{
int dd,mm,yy;
};

struct employee
{
int eno;
char name[20];
struct date doj;
char dept[20];
int salary;
};

typedef struct employee emp;


emp *e;

int main()
{
e=(emp*)malloc(sizeof(emp));
printf("\n enter employee no :");
scanf("%d",&e->eno);
printf("\n enter employee name : \t");
scanf("%s",e->name);
printf("\n enter date of join in (dd-mm-yyyy) format :");
scanf("%d-%d-%d",&e->doj.dd,&e->doj.mm,&e->doj.yy);
printf("\n enter the department : \t");
scanf("%s",e->dept);

124
printf("\n enter the salary : \t");
scanf("%d",&e->salary);
printf("\n");
SPACE(33);
printf("EMPLOYEE INFORMATION \n");
LINE;
printf("\n");
SPACE(30);
printf("Employee no : %d\n",e->eno);
SPACE(30);
printf("Employee name : %s\n",e->name);
SPACE(30);
printf("Date Of Join : %d-%d-%d\n",e->doj.dd,e->doj.mm,e->doj.yy);
SPACE(30); printf("Department : %s\n",e->dept);
SPACE(30); printf("Salary : %d\n",e->salary);
LINE;
printf("\n");
return 0;
}

OUTPUT :

125
AIM : write a C program to display the bank customers information

PROGRAM :

#include<stdio.h>
#include<stdlib.h>
#define SPACE(n) for(i=0;i<n;i++) printf(" ")
#define LINE for(i=0;i<80;i++) printf("-")
struct customer
{
int accno;
char name[20];
float balance;
};

struct customer *c[20];

void main()
{
int n,i;
printf("\n Enter No of customers : ");
scanf("%d",&n);
for(i=0;i<n;i++)
c[i]=(struct customer*)malloc(sizeof(struct customer));
for(i=0;i<n;i++)
{
printf("\n Enter account number of customer %d : ",i+1);
scanf("%d",&c[i]->accno);
printf("\n Enter Name of customer %d : ",i+1);
scanf("%s",c[i]->name);
printf("\n Enter Balance of customer %d : ",i+1);
scanf("%f",&c[i]->balance);
}
printf("\n");
SPACE(28);
printf("S.B.I. CUSTOMERS REPORT\n");
LINE;
printf("\n%10s","SNO");
printf("%10s","ACCNO");
126
printf("%25s","CUSTOMER NAME");

printf("%12s\n","BALANCE");
LINE;
for(i=0;i<n;i++)
{
printf("\n %10d",i+1);
printf("%10d",c[i]->accno);
printf("%25s",c[i]->name);
printf("%12.2f\n",c[i]->balance);
}
printf("\n");
LINE;
}
OUTPUT :

127
AIM : write a C program to Display the total marks of each student & subject.

PROGRAM 11.5 :
#include<stdio.h>
struct marks
{
int sub[3];
int total;
};
int main()
{
int i,j;
struct marks student[3] = {{45,67,81,0},{75,53,69,0},{57,36,71,0}};
struct marks total;
for(i=0;i<3;i++)
total.sub[i] = 0;
total.total=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
student[i].total+=student[i].sub[j];
total.sub[j]+=student[i].sub[j];
}
total.total+=student[i].total;
}
printf("\n STUDENT\tTOTAL\n\n");
for(i=0; i<3; i++)
printf("student[%d]\t%d\n",i+1,student[i].total);
128
printf("\n SUBJECT\tTOTAL\n\n");
for(i=0; i<3; i++)
printf("Subject-%d\t%d\n",i+1,total.sub[i]);
printf("\n Grand total = %d \n",total.total);
return 0;
}

OUTPUT :
STUDENT TOTAL
student[1] 193
student[2] 197
student[3] 164

SUBJECT TOTAL
Subject-1 177
Subject-2 156
Subject-3 221

Grand total = 554

129
MODULE IDX PROGRAM PAGE NO
NO
Exercise Programs on Functions & Recursive Functions.
12.1 Copy contents of one file into another using command 128
line arguments and getc, putc
12.2 Create a product File using fscanf and fprintf 129-130
12 12.3 Create a bank binary file using fwrite 131-132
12.4 Reading bank file using fread 133-134
12.5 Modify the bank of a file (deposit/withdraw) 135-136
12.6 Count no of records in a bank file 137

130
AIM : write a C program to Copy contents of one file into another using
command line arguments and getc, putc.

PROGRAM 12.1 :

#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
char ch;
FILE *fp1,*fp2;
fp1 = fopen(argv[1],"r");
fp2 = fopen(argv[2],"w");
printf("\n Contents of file %s are \n",argv[1]);
while((ch = fgetc(fp1)) != EOF)
{
fputc(ch,fp2);
putchar(ch);
}
fclose(fp1);
fclose(fp2);
printf("\n File copied to %s \n",argv[2]);
fp1 = fopen(argv[2],"r");
printf("\n Contents of the file %s are \n",argv[2]);
while((ch = getc(fp1)) != EOF)
putchar(ch);
fclose(fp1);
131
return 0;
}
OUTPUT :

AIM : write a C program to Create a product File using fscanf and fprintf.

PROGRAM :

#include <stdio.h>
int main()
{
FILE *fp;
int code,qty;
float price,total;
char name[20];
fp=fopen("item.dat","wb");
for(;;)
{
printf("\n enter item code( Type 0 to stop) : ");
scanf("%d",&code);
if(code==0)
break;
printf("enter item name, price and qty ");
scanf("%s%f%d",name,&price,&qty);
fprintf(fp,"%s\t%d\t%f\t%d",name,code,price,qty);
}
fclose(fp);
fp=fopen("item.dat","rb");
printf("%12s","item name");
printf("%10s","item code");
printf("%10s","item price");
printf("%10s","quantity");
132
printf("%20s","amount to be paid\n");
while(!feof(fp))
{
fscanf(fp,"%s%d%f%d",name,&code,&price,&qty);
total = price * qty;
printf("%12s%10d%10.2f%10d%20.2f\n",name,code,price,qty,total);
}
fclose(fp);
return 0;
}

OUTPUT :

133
AIM : write a C program to Create a bank binary file using fwrite.

PROGRAM :

/* create a binary file using fwrite */


#include<stdio.h>
struct customer
{
int accno;
char name[20];
float balance;
};

int main()
{
struct customer cust;
FILE *fp;
fp = fopen("bank.dat","wb");
printf("\n type 0 at account no. to stop. \n");
for(;;)
{
printf("\n enter account no. \t");
scanf("%d",&cust.accno);
if(cust.accno == 0 )
break;
fflush(stdin);
printf("\n enter customer name \t");
134
gets(cust.name);
printf("\n enter balance amount :\t");
scanf("%f",&cust.balance);
fwrite(&cust,sizeof(cust),1,fp);
}
printf("\n bank.dat file is created ");
fclose(fp);
return 0;
}

OUTPUT :

type 0 at account no. to stop.


enter account no. 10101
enter customer name kamal
enter balance amount : 500

enter account no. 10102


enter customer name Malathi
enter balance amount : 1050

enter account no. 10103


enter customer name Subba Reddy
enter balance amount : 10000

enter account no. 0

bank.dat file is created

135
AIM : Write a C Program to Reading bank file using fread().

PROGRAM :

/* reading the binary file using fread */


#include<stdio.h>

#define SPACE(k) for(int j = 1; j<=k; j++) fprintf(stdout," ");


#define LINE for(int j = 1; j<=80; j++) fprintf(stdout,"-");
struct customer
{
int accno;
char name[20];
float balance;
};

int main()
{
struct customer cust;
char fname[30];
int j,k,i=1;
FILE *fp;
printf("\n enter the file name to open \t");
gets(fname);
fp = fopen(fname,"rb");
if(fp == NULL)
136
{
printf("\n file not found \n");
return;
}
printf(“\n”);
SPACE(28);
fprintf(stdout,"S.B.I. CUSTOMERS REPORT\n");
LINE;
fprintf(stdout,"\n%10s","SNO");
fprintf(stdout,"%10s","ACCNO");
fprintf(stdout,"%25s","CUSTOMER NAME");

fprintf(stdout,"%12s\n","BALANCE");
LINE;
fread(&cust,sizeof(cust),1,fp);
while(!feof(fp))
{
fprintf(stdout,"\n %10d",i);
fprintf(stdout,"%10d",cust.accno);
fprintf(stdout,"%25s",cust.name);
fprintf(stdout,"%12.2f\n",cust.balance);
i++;
fread(&cust,sizeof(cust),1,fp);
}
fprintf(stdout,"\n");
LINE;
fclose(fp);
return 0;
}

OUTPUT :

S.B.I. CUSTOMERS REPORT


--------------------------------------------------------------------------------

SNO ACCNO CUSTOMER NAME BALANCE


--------------------------------------------------------------------------------

1 10101 kamal 500.00

2 10102 Malathi 1050.00


137
3 10103 Subba Reddy 10000.00

--------------------------------------------------------------------------------

AIM : write a C program to Modify the bank of a file (deposit/withdraw).

PROGRAM 9.5 :

#include<stdio.h>
struct customer
{
int accno;
char name[20];
float balance;
};
int main()
{
struct customer cust;
float amount=0;
int tempno,recsize;
char ch;
FILE *fp;
fp = fopen("bank.dat","r+b");
printf("\n enter account no : ");
scanf("%d",&tempno);
fread(&cust, sizeof(cust),1,fp);
while(!feof(fp))
{
if(tempno == cust.accno)
{
printf("\n D for Deposit \n W for Withdraw \n ");
138
fflush(stdin);
scanf("%c",&ch);
if(toupper(ch) == 'D')
{
printf(" \n enter Deposit amount : ");
scanf("%f",&amount);
cust.balance +=amount;
}
else if(toupper(ch) == 'W')
{
printf("\n enter withdraw amount : ");

scanf("%f",&amount);
cust.balance -= amount;
}
else
printf("\n Wrong input ");

recsize = sizeof(cust);
fseek(fp,-recsize, SEEK_CUR);
fwrite(&cust,sizeof(cust),1,fp);
fclose(fp);
break;
}
fread(&cust,sizeof(cust),1,fp);
}
return 0;
}

OUTPUT :

enter account no : 10101


D for Deposit
W for Withdraw
D
enter Deposit amount : 1000

enter account no : 10102


D for Deposit
W for Withdraw
W
enter withdraw amount : 500

after executing above 2 times if we read the bank.dat using program 12.4
139
S.B.I. CUSTOMERS REPORT
--------------------------------------------------------------------------------

SNO ACCNO CUSTOMER NAME BALANCE


--------------------------------------------------------------------------------

1 10101 kamal 1500.00


2 10102 Malathi 550.00
3 10103 Subba Reddy 10000.00
--------------------------------------------------------------------------------

AIM : write a C program to Count no of records in a bank file.

PROGRAM 12.6 :

#include<stdio.h>
struct customer
{
int accno;
char name[20];
float balance;
};

int main()
{
struct customer cust;
char fname[30];
int recsize,no_records;
long int filesize;
FILE *fp;
fp = fopen("bank.dat","rb");
recsize = sizeof(cust);
printf("\n record size = %d bytes ",recsize);
fseek(fp,0,SEEK_END);
filesize = ftell(fp);
printf("\n file size = %ld bytes ",filesize);
no_records = filesize/recsize;
printf("\n there are %d records in the file", no_records);
140
fclose(fp);
return 0;
}

OUTPUT :

record size = 26 bytes


file size = 78 bytes
there are 3 records in the file

141

You might also like