0% found this document useful (0 votes)
95 views94 pages

List of Programs With Solution

The document contains 50 programs written in C language with their solutions. The programs cover a wide range of concepts in C like basic input/output, arithmetic operations, conditional statements, loops, functions, arrays, pointers, structures, file handling and more. Standard programs include calculating area of shapes, temperature conversion, reversing numbers, sorting arrays etc. The complete solutions with input/output are provided for each program.

Uploaded by

esmani84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views94 pages

List of Programs With Solution

The document contains 50 programs written in C language with their solutions. The programs cover a wide range of concepts in C like basic input/output, arithmetic operations, conditional statements, loops, functions, arrays, pointers, structures, file handling and more. Standard programs include calculating area of shapes, temperature conversion, reversing numbers, sorting arrays etc. The complete solutions with input/output are provided for each program.

Uploaded by

esmani84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 94

List of Programs With Solution

1. Write a program in C to print “ideal is the best institute”.


2. Write a program in C to find sum of two numbers.
3. Write a program in C to calculate the area of the circle.
4. Write a program in C to calculate the area of triangle.
5. Write a program in C to calculate average of three numbers.
6. Write a program in C to convert the temperature in Celsius to
Fahrenheit.
7. Write a program in C to division of two numbers.
8. Write a program in C to convert the temperature in Fahrenheit to
Celsius.
9. Write a program in C to reverse a three digit numbers.
10. Write a program in C to find multiplication of two numbers.
11.Write a program in C to check the range of the integer.
12.Write a program in C to calculate the value of s for s=ut+1/2*a *t2
13.Write a program in C to find the subtraction of two numbers.
14.Write a program in C to swap the value of two variables using third
variable.
15.Write a program in C to swap the value of two variables without using
third variable.
16. Write a program in C to calculate the value for v=u+a*t.
17.Write a program in C to calculate the volume of sphere.
18.Write a program in C to calculate the commission for a sales executive.
19.Write a program in C to find whether the given number is even odd.
20.Write a program in C to find the Division of a student.
21.Write a program in C to find the greater of two numbers.
22.Write a program in C to find the greatest of three numbers.
23.Write a program in C to find the gross salary of an employee.
24.Write a program in C to find the roots of a quadratic equation.
25.Write a program in C to check whether the given year is leap year or
not.
26.Write a program in C to print characters with corresponding ASCII value.
27.Write a program in C to find the factorial of the given number.
28.Write a program in C to print the series (1+1/2+1/3+………..+1/n)
29.Write a program in C to find the given number is Armstrong or not
30.Write a program in C to find the Armstrong number in 1 to 1000

1
31.Write a program in C to print the given number in the words.
32.Write a program in C to print Fibonacci series up to nth term.
33.Write a program in C to print n natural number.
34. Write a program in C to reverse a number using loop.
35.Write a program in C to find the sum of the digits.
36.Write a program in C to print the series (1+ (1+2) +……. + (1+2+3+……n)).
37.Write a program in C to add two numbers using function.
38.Write a program in C to calculate the value of binomial expression using
functions.
39.Write a program in C to find a factorial of a given number using
function.
40.Write a program in C to find Fibonacci series using function.
41.Write a program in C to find the value of (N!)^N using function.
42.Write a program in C to reverse a number using function.
43.Write a program in C to calculate the value of binomial expression using
recursion.
44.Write a program in C to find a factorial of a given number using
recursion.
45.Write a program in C to find sum of N natural number using recursion.
46.Write a program in C to access value of variable using pointers.
47.Write a program in C to using call by reference.
48.Write a program in C to using call by value
49.Write a program in C to swap values using call by values.
50.Write a program in C to using array find maximum and minimum
number.
51.Write a program in C to read and print the element of the 2D array.
52.Write a program in C to sort the array using bubble sort algorithm.
53.Write a program in C to sort the array using Insertion sort algorithm.
54.Write a program in C to sort the array using selection sort algorithm.
55.Write a program in C to sort the array using insertion sort algorithm
using dynamic memory allocation.
56.Write a program in C to find greatest number using dynamic memory
allocation.
57.Write a program in C to reverse the string using realloc()
58.Write a program in C to sort the array using selection sort algorithm
using dynamic memory allocation.

2
59.Write a program in C to count the number of vowels.
60.Write a program in C to find the length of the string.
61.Write a program in C to count a given letter in a string.
62.Write a program in C to add two string with user defined function.
63.Write a program in C to reverse the string.
64.Write a program in C to arrange the students in increasing order of their
marks using structure.
65.Write a program in C to copy the context of one file to another.
66.Write a program in C to count chars, tabs, spaces and new lines in a file.
67.Write a program in C to read record from a file using structure.
68.Write a program in C read records from binary file and display them on
VDU.
69.Write a program in C receives records from keyboard and writes them
to file in binary mode.
70. Write a program in C to insert and delete the elements.
71.Write a program in C to implement the STACK.
72.Write a program in C to implement the linked list.

And there are more than list program are here with solution.

/* Simple program in C language */

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Ideal is the best institute.");
getch();
}

3
OUTPUT

Ideal is the best institute

4
/* Addition of two numbers */

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the value of a. ");
scanf("%d",&a);
printf("Enter the value of b. ");
scanf("%d",&b);
c=a+b;
printf("The sum of two numbers is %d",c);
getch();
}

OUTPUT

Enter the value of a. 12


Enter the value of b. 34
The sum of two numbers is 46

5
/* Calculate the area of circle. */

#include<stdio.h>
#include<conio.h>
void main()
{
float r,a;
clrscr();
printf("Enter the value of radius ");
scanf("%f",&r);
a=3.14*r*r;
printf("The area of circle is %f",a);
getch();
}

OUTPUT

Enter the value of radius 55


The area of circle is 9498.500000

6
/* Area of triangle */

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
float s,area;
clrscr();
printf("Enter the value for a,b and c.");
scanf("%d %d %d",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("The area of triangle is %f",area);
getch();
}

OUTPUT

Enter the value for a, b and c. 6 7 8


The area of triangle is 15.491934

7
/* To calculate the average of three numbers. */

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
float average;
clrscr();
printf("Enter the value of a,b,c");
scanf("%d %d %d",&a,&b,&c);
average=(a+b+c)/3;
printf("The average of three numbers is %f",average);
getch();
}

OUTPUT

Enter the value of a, b, c 90 1 2 3


The average of three numbers is 38.000000

8
/* To change the temperature in Celsius to degree Fahrenheit. */

#include<stdio.h>
#include<conio.h>
void main()
{
float c,f;
clrscr();
printf("Enter the temperature in celcius ");
scanf("%f",&c);
f=1.8*c+32;
printf("The temperature in fahrenheit is %f",f);
getch();
}

OUTPUT

Enter the temperature in celcius 45


The temperature in fahrenheit is 113.000000

9
/* Division of two numbers */

#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
clrscr();
printf("Enter the value of a. ");
scanf("%f",&a);
printf("Enter the value of b. ");
scanf("%f",&b);
c=a/b;
printf("The sum of two numbers is %f",c);
getch();
}

OUTPUT

Enter the value of a. 67


Enter the value of b. 8
The sum of two numbers is 8.375000

10
/* To change the temperature in Fahrenheit to degree celcius. */

#include<stdio.h>
#include<conio.h>
void main()
{
float c,f;
clrscr();
printf("Enter the temperature in fahrenheit ");
scanf("%f",&f);
c=.56*(f-32);
printf("The temperature in celcius is %f",c);
getch();
}

OUTPUT

Enter the temperature in farenheit 90


The temperature in celcius is 32.480000

11
/* To invert a three digit number */

#include<stdio.h>
#include<conio.h>
void main()
{
int num,a,b,c,result;
clrscr();
printf("\n Enter a three digit number : ");
scanf("%d",&num);
a=num%10;
num=num/10;
b=num%10;
num=num/10;
c=num%10;
num=num/10;
result=a*100+b*10+c;
printf("After inverting, the number formed is %d", result);
getch();
}

OUTPUT

Enter a three digit number : 123


After inverting, the number formed is 321

12
/* Multiplication of two numbers */

#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
clrscr();
printf("Enter the value of a. ");
scanf("%f",&a);
printf("Enter the value of b. ");
scanf("%f",&b);
c=a*b;
printf("The multiplication of two numbers is %f",c);
getch();
}

OUTPUT

Enter the value of a. 45


Enter the value of b. 67
The sum of two numbers is 3015.000000

13
/* To check the range of the integer.*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
i=32767;
printf("\n The current value of i is %d",i);
i=i+1;
printf("\n After adding \"1\" i becomes %d",i);
printf("\n Therefore the range of integer is %d to 32767",i);
getch();
}

OUTPUT

The current value of i is 32767


After adding "1" i becomes -32768
Therefore the range of integer is -32768 to 32767

14
/* Calculate the value of s for s=ut+1/2*at*t. */

#include<stdio.h>
#include<conio.h>
void main()
{
float s,u,t,a;
clrscr();
printf("Enter the value of u, t, a ");
scanf("%f %f %f",&u,&t,&a);
s=u*t+.5*a*t*t;
printf("The value of s is %f",s);
getch();
}

OUTPUT

Enter the value of u,t,a 8.9 0.1 2.3


The value of s is 0.901500

15
/* Subtraction of two numbers */

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the value of a. ");
scanf("%d",&a);
printf("Enter the value of b. ");
scanf("%d",&b);
c=a-b;
printf("The sum of two numbers is %d",c);
getch();
}

OUTPUT

Enter the value of a. 4567


Enter the value of b. 890
The sum of two numbers is 3677

16
/* Swap the value of two variables using third variable. */

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the value for a and b.");
scanf("%d %d",&a,&b);
c=a;
a=b;
b=c;
printf("\n After swapping values are:");
printf("\n a = %d , b = %d",a,b);
getch();
}

OUTPUT

Enter the value for a and b. 12 34


After swapping values are:
a = 34 , b = 12

17
/* Swap the value of two variables without using third variable. */

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the value for a and b.");
scanf("%d %d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("\n After swapping values are:");
printf("\n a = %d , b = %d",a,b);
getch();
}

OUTPUT

Enter the value for a and b. 56 78


After swapping values are:
a = 78 , b = 56

18
/* Calculate the value of v for v=u+at */

#include<stdio.h>
#include<conio.h>
void main()
{
float v,u,t,a;
clrscr();
printf("Enter the value of u,a,t ");
scanf("%f %f %f",&u,&a,&t);
v=u+a*t;
printf("The value of v is %f",v);
getch();
}

OUTPUT

Enter the value of u, a, t 9 0 1


The value of v is 9.000000

19
20
/*To calculate the volume of sphere */

#include<stdio.h>
#include<conio.h>
#define pi 3.14
void main()
{
float r,v;
clrscr();
printf("Enter the value of radius ");
scanf("%f",&r);
v=1.33*pi*r*r*r;
printf("The volume of sphere is %f",v);
getch();
}

OUTPUT

Enter the value of radius 2


The volume of sphere is 33.409599

21
//To calculate the commission for a sales executive.

#include<stdio.h>
#include<conio.h>
void main()
{
float sales, comm;
clrscr();
printf("\n Enter the sales of an executive.");
scanf("%f",&sales);
if((sales>=0)&&(sales<500))
comm=.05*sales;
else
if((sales>=500)&&(sales<2000))
comm=35+.10*(sales-500);
else
if((sales>=2000)&&(sales<5000))
comm=185+.12*(sales-2000);
else
if(sales>=5000)
comm=.125*sales;
else
printf("\n Sales cannot be negative.");
printf("\n The commission of this executive is %f",comm);
getch();
}

OUTPUT

Enter the sales of an executive. 3456


The commission of this executive is 359.720001

22
/* To check whether the given number is even or odd */

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("\n Enter a number : ");
scanf("%d",&num);
if(num%2==0)
printf("\n The number is even.");
else
printf("\n The number is odd.");
getch();
}

OUTPUT

Enter a number: 78
The number is even.

23
/* Print the grade according to the marks. */

#include<stdio.h>
#include<conio.h>
void main()
{
float m,bc,pc,fit,p,per;
clrscr();
printf("\n Enter the marks for all the five subjects.");
scanf("%f %f %f %f %f",&m,&bc,&pc,&fit,&p);
per=(m+bc+pc+fit+p)/5;
if((per>=80)&&(per<100))
printf("\n HONOURS ");
else
if((per>=60)&&(per<80))
printf("\n I DIVISION");
else
if((per>=40)&&(per<60))
printf("\n II DIVISION");
else
if((per>=0)&&(per<40))
printf("\n You are FAIL");
else
printf("\n Please check the entry of the marks.");
getch();
}

OUTPUT

Enter the marks for all the five subjects.90 12 34 56 78


II DIVISION

24
/* Greater of two numbers */

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the values for a and b");
scanf("%d %d",&a,&b);
if(a>b)
printf("\n A is greater");
else
if(b>a)
printf("\n B is greater");
else
printf("\n Both are equal");
getch();
}

OUTPUT

Enter the values for a and b 901 23


A is greater

25
/* Greatest of three numbers */

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the values for a,b and c");
scanf("%d %d %d",&a,&b,&c);
if((a>b)&&(a>c))
printf("\n A is greatest");
else
if((b>a)&&(b>c))
printf("\n B is greatest");
else
if((c>a)&&(c>b))
printf("\n C is greatest");
else
printf("\n All are equal");
getch();
}

OUTPUT

Enter the values for a, b and c 67 45 89


C is greatest

26
/* Gross salary of an employee */

#include<stdio.h>
#include<conio.h>
void main()
{
float bs,da,hra,cca,gs;
clrscr();
printf("Enter the base salary");
scanf("%f",&bs);
if(bs>=3000)
{
da=.75*bs;
hra=.15*bs;
cca=320;
gs=bs+da+hra+cca;
printf("\n The gross salary of an employee is %f",gs);
}
else
{
da=.30*bs;
hra=.12*bs;
cca=200;
gs=bs+da+hra+cca;
printf("\n The gross salary of an employee is %f",gs);
}
getch();
}

OUTPUT

Enter the base salary 1234


The gross salary of an employee is 1952.280029

27
//To find the roots.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,x,y,d;
clrscr();
printf("\n Enter the value for a,b and c");
scanf("%f %f %f",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
printf("\n Roots are imaginary");
else
if(d==0)
printf("\n Roots are equal");
else
{
x=(-b+sqrt(d))/(2*a);
y=(-b-sqrt(d))/(2*a);
printf("\n The roots of the equation are %f %f",x,y);
}
getch();
}

OUTPUT

Enter the value for a, b and c 5 6 7


Roots are imaginary

28
/*Evaluating the value of function y */

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int y,x,n;
clrscr();
printf("\n Enter the value for x and n : ");
scanf("%d %d",&x,&n);
if(n==1)
y=1+x;
else if(n==2)
y=1+x/n;
else if(n==3)
y=1+pow(x,n);
else if(n>3)
y=1+n*x;
else
printf("\n n should be greater than 0");
printf("\n The value of y is %d",y);
getch();
}

OUTPUT

Enter the value for x and n : 8 9


The value of y is 73

29
/* To evaluate the value of y. */

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,a;
float y;
clrscr();
printf("\n Enter the value for x.");
scanf("%d",&x);
a=1-x*x;
if(a<=0)
printf("\n Cannot evaluate the value for y.");
else
{
if(x<0)
{
y=1+x/sqrt(a);
printf("\n The value of y is %f",y);
}
else
if(x==0)
{
y=0;
printf("\n The value of y is %f",y);
}
else
{
y=1-x/1-x*x;
printf("\n The value of y is %f",y);
}
}
} OUTPUT

Enter the value for x.1


Cannot evaluate the value for y.

30
/* To check whether the given year is leap or not */

#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("\n Enter a year : ");
scanf("%d",&year);
if(year%4==0)
printf("\n This is a leap year.");
else
printf("\n This is not a leap year.");
getch();
}

OUTPUT

Enter a year : 1923


This is not a leap year.

31
//To find the value of y using switch case.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float y,x;
int n;
clrscr();
printf("\n Enter the value for x,n");
scanf("%f %d",&x,&n);
switch(n)
{
case 1: y=1+x;
break;
case 2: y=1+x/n;
break;
case 3: y=1+pow(x,n);
break;
default:y=1+n*x;
break;
}
printf("\n The value of y is %f",y);
getch();
}

OUTPUT

Enter the value for x , n 4 5


The value of y is 21.000000

32
/* Print characters with corresponding ASCII value */

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=0;i<=255;i++)
printf("\t %c \t %d",i,i);
getch();
}

OUTPUT

0 ☺ 1 ☻ 2 ♥ 3 ♦ 4 ♣ 5 ♠ 6 7 8 9
13 ♫ 14 ☼ 15► 16◄ 17 ↕ 18 ‼ 19 ¶ 20 § 21 ▬ 22 ↨
23 ↑ 24 ↓ 25← 27∟ 28↔ 29▲ 30 ▼ 31 32 ! 33 "

33
/* To find the factorial. */

#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,fact=1;
clrscr();
printf("\n Enter a number.\t");
scanf("%d",&num);
for(i=num;i>0;i--)
fact=fact*i;
printf("\n The factorial of %d is %d",num,fact);
getch();
}

OUTPUT

Enter a number. 6
The factorial of 6 is 720

34
/* To print the series (1+1/2+1/3+...+1/n) */

#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=1,i;
clrscr();
printf("\n Enter the value for n.");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum+=1/i;
}
printf("\n The sum of the series :");
for(i=1;i<=n;i++)
{
printf(" + %d/%d ",1,i);
}
printf("is %d",sum);
getch();
}

OUTPUT

Enter the value for n.7


The sum of the series: + 1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 is 2

35
/* Find whether the given number is armstrong or not. */

#include<stdio.h>
#include<conio.h>
void main()
{
int num,a,x,s=0,b;
clrscr();
printf("\n Enter a number.");
scanf("%d",&num);
x=num;
while(num>0)
{
a=num%10;
b=a*a*a;
s=s+b;
num=num/10;
}
if(s==x)
printf("\n The given number is an armstrong number");
else
printf("\n The given number is not an armstrong number.");
getch();
}

OUTPUT

Enter a number.890
The given number is not an armstrong number.

36
/* Armstrong numbers between 1 to 1000 */

#include<stdio.h>
#include<conio.h>
void main()
{
int num,a,x,s,b,i;
clrscr();
for(i=1;i<=1000;i++)
{ a=0,b=0,s=0;
num=i;
while(num>0)
{
a=num%10;
b=a*a*a;
s=s+b;
num=num/10;
}
if(s==i)
printf("%d ",s);
}
getch();
}

OUTPUT

1 153 370 371 407

37
/* Print characters with corresponding ASCII value */

#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
clrscr();
while(i<=255)
{
printf("\t %c %d",i,i);
i++;
}
getch();
}

OUTPUT

0 ☺ 1 ☻ 2 ♥ 3 ♦ 4 ♣ 5 ♠ 6 7 8 9
13 ♫ 14 ☼ 15► 16◄ 17 ↕ 18 ‼ 19 ¶ 20 § 21 ▬ 22 ↨
23 ↑ 24 ↓ 25← 27∟ 28↔ 29▲ 30 ▼ 31 32 ! 33 "

38
//Print the digits of a given number in words.

#include<stdio.h>
#include<conio.h>
void main()
{
int num,a,r=0,v,b;
clrscr();
printf("\n Enter the value.");
scanf("%d",&num);
while(num>0)
{
a=num%10;
r=r*10+a;
num=num/10;
}
v=r;
while(v>0)
{
b=v%10;
switch(b)
{
case 0: printf("ZERO ");
break;
case 1: printf("ONE ");
break;
case 2: printf("TWO ");
break;
case 3: printf("THREE ");
break;
case 4: printf("FOUR ");
break;
case 5: printf("FIVE ");
break;
case 6: printf("SIX ");
break;
case 7: printf("SEVEN ");
break;
case 8: printf("EIGHT ");
break;

39
case 9: printf("NINE ");
break;
}
v=v/10;
}
getch();
}

OUTPUT

Enter the value.123


ONE TWO THREE

40
/* Fibonacci Series */

#include<stdio.h>
#include<conio.h>
void main()
{
int n,a=0,b=1,fs;
clrscr();
printf("\n Enter the last value for n.");
scanf("%d",&n);
printf(" %d \n %d",a,b);
fs=a+b;
while(fs<n)
{
printf("\t %d",fs);
a=b;
b=fs;
fs=a+b;
}
getch();
}

OUTPUT

Enter the last value for n. 4


0 1 1 2 3

41
/*Print n natural numbers. */

#include<stdio.h>
#include<conio.h>
void main()
{
int num,a=1;
clrscr();
printf("\n Enter the last number.");
scanf("%d",&num);
while(a<=num)
{
printf("%d ",a);
a++;
}
getch();
}

OUTPUT

Enter the last number.5


12345

42
/* Print the integers omitting those which are divisible by 7. */

#include<stdio.h>
#include<conio.h>
void main()
{
int num,i=0;
clrscr();
printf("\n Enter an integer.");
scanf("%d",&num);
while(i<=num)
{
if(i%7!=0)
printf("\t\t %d",i);
i++;
}
getch();
}

OUTPUT

Enter an integer.6
1 2 3 4 5 6

43
/* Reversing a number using loop */

#include<stdio.h>
#include<conio.h>
void main()
{
int num,a,r=0;
clrscr();
printf("\n Enter the value.");
scanf("%d",&num);
while(num>0)
{
a=num%10;
r=r*10+a;
num=num/10;
}
printf("\n The reverse number is %d",r);
getch();
}

OUTPUT

Enter the value.7890


The reverse number is 987

44
/* Find the sum of the digits */

#include<stdio.h>
#include<conio.h>
void main()
{
int num,a,s=0;
clrscr();
printf("\n Enter a number.");
scanf("%d",&num);
while(num>0)
{
a=num%10;
s=s+a;
num=num/10;
}
printf("\n The sum of the digits is %d",s);
getch();
}

OUTPUT

Enter a number.123
The sum of the digits is 6

45
/* To print the series (1+(1+2)+...+(1+2+...+n))*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,j,sum=0;
clrscr();
printf("\n Enter the value for n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
sum=sum+j;
}
printf(" Sum is %d",sum);
getch();
}

OUTPUT

Enter the value for n: 45


16215

46
/* Compute the value of z. */

#include<stdio.h>
#include<conio.h>
void main()
{
float x,y,z;
clrscr();
for(x=-1.5;x<=1.5;x=x+0.5)
{
for(y=0;y<=3;y++)
z=3*x*x+2*y*y*y-25.5;
}
printf("The value of z is %f",z);
getch();
}

OUTPUT

The value of z is 35.250000

47
/* Add the numbers using functions.*/

#include<stdio.h>
#include<conio.h>
void add(int,int);
void main()
{
int a,b;
clrscr();
printf("\n Enter the value for a and b.");
scanf("%d %d",&a,&b);
add(a,b);
getch();
}
void add(int a,int b)
{
int sum;
sum=a+b;
printf("\n Sum of %d and %d is %d",a,b,sum);
}

OUTPUT

Enter the value for a and b. 67 89


Sum of 67 and 89 is 156

48
//Calculate the value of binomial expression using functions

#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,r;
int b;
clrscr();
printf("\n Enter the integer values for n and r");
scanf("%d %d",&n,&r);
b=fact(n)/(fact(r)*fact(n-r));
printf("\n The binomial value is %d",b);
getch();
}
int fact(int x)
{
int i,f=1;
for(i=x;i>0;i--)
f=f*i;
return f;
}

OUTPUT

Enter the integer values for n and r 1 2


The binomial value is 0

49
/*Factorial of a given number using functions.*/

#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int num,fact_exp;
clrscr();
printf("\n Enter a number.\t");
scanf("%d",&num);
fact_exp=fact(num);
printf("\n The factorial of %d is %d",num,fact_exp);
getch();
}
int fact(int n)
{
int i,fact=1;
for(i=n;i>0;i--)
fact=fact*i;
return fact;
}

OUTPUT

Enter a number. 3
The factorial of 3 is 6

50
/*Fibonacci Series using functions.*/

#include<stdio.h>
#include<conio.h>
void fact(int);
void main()
{
int n;
clrscr();
printf("\n Enter the last value for n.");
scanf("%d",&n);
fact(n);
getch();
}
void fact(int n)
{
int a=0,b=1,fs;
printf(" %d \n %d",a,b);
fs=a+b;
while(fs<n)
{
printf("\t\t %d",fs);
a=b;
b=fs;
fs=a+b;
}
}

OUTPUT

Enter the last value for n. 4


0 1 1 2 3

51
52
/* (N!)^N using functions. */

#include<stdio.h>
#include<conio.h>
#include<math.h>
int fact(int);
void main()
{
int num,fact_pow;
clrscr();
printf("\n Enter a number.\t");
scanf("%d",&num);
fact_pow=fact(num);
printf("\n The factorial of %d is %d",num,fact_pow);
getch();
}
int fact(int n)
{
int i,fact=1,f_p=1;
for(i=n;i>0;i--)
fact=fact*i;
f_p=pow(fact,n);
return f_p;
}

OUTPUT

Enter a number. 4
The factorial of 4 is 4096

53
/* Reversing a number using function */

#include<stdio.h>
#include<conio.h>
int reverse(int);
void main()
{
int num,rev_num;
clrscr();
printf("\n Enter the value.");
scanf("%d",&num);
rev_num=reverse(num);
printf("\n The reverse number is %d",rev_num);
getch();
}
int reverse(int n)
{
int a,r=0;
while(n>0)
{
a=n%10;
r=r*10+a;
n=n/10;
}
return r;
}

OUTPUT

Enter the value. 567


The reverse number is 765

54
/* To find the value of binomial expression using recursion */

#include<stdio.h>
#include<conio.h>
int be(int,int);
void main()
{
int n,r,bin_exp;
clrscr();
printf("\n Enter the value for n and r.");
scanf("%d %d",&n,&r);
bin_exp=be(n,r);
printf("\n The value for this binomial expression is %d",bin_exp);
getch();
}
int be(int n,int r)
{
if((n==r)||(r==0))
return 1;
else
return be(n-1,r-1)+be((n-1),r);
}

OUTPUT

Enter the value for n and r. 6 4


The value for this binomial expression is 15

55
/*Factorial of a given number using recursion.*/

#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int num,fact_exp;
clrscr();
printf("\n Enter a number.\t");
scanf("%d",&num);
fact_exp=fact(num);
printf("\n The factorial of %d is %d",num,fact_exp);
getch();
}
int fact(int n)
{
int f;
if(n==1)
return (1);
else
f=n*fact(n-1);
return f;
}

OUTPUT

Enter a number. 1
The factorial of 5 is 1

56
/*Sum of N natural numbers using recursion.*/

#include<stdio.h>
#include<conio.h>
int sum(int);
void main()
{
int num,sum_n;
clrscr();
printf("\n Enter last value for n.\t");
scanf("%d",&num);
sum_n=sum(num);
printf("\n The sum of %d natural numbers is %d",num,sum_n);
getch();
}
int sum(int n)
{
int s;
if(n==1)
return (1);
else
s=n+sum(n-1);
return s;
}

OUTPUT

Enter last value for n. 8


The sum of 8 natural numbers is 36

57
/* Access value of variable using pointers. */

#include<stdio.h>
#include<conio.h>
void main()
{
int n,*p;
clrscr();
printf("\n Enter the value for n: ");
scanf("%d",&n);
p=&n;
printf("\n The value of p is %d",*p);
printf("\n And the address at which n is stored is %d",p);
getch();
}

OUTPUT

Enter the value for n: 9


The value of p is 9
And the address at which n is stored is -12

58
/* Call by Reference */

#include<stdio.h>
#include<conio.h>
void add10(int*,int*);
void main()
{
int a,b;
clrscr();
printf("\n Enter the values for a and b :");
scanf("%d %d",&a,&b);
printf("\n Values before the function call :");
printf("a=%d , b=%d",a,b);
add10(&a,&b);
printf("\n Values after the function call :");
printf("a=%d, b=%d",a,b);
getch();
}
void add10(int *x,int *y)
{
*x=*x+10;
*y=*y+10;
printf("\n Values inside the function call :");
printf("a=%d, b=%d",*x,*y);
}

OUTPUT
Enter the values for a and b : 01 23
Values before the function call :a=1, b=23
Values inside the function call :a=11, b=33
Values after the function call :a=11, b=33

59
/* Call by Value */

#include<stdio.h>
#include<conio.h>
void add10(int,int);
void main()
{
int a,b;
clrscr();
printf("\n Enter the values for a and b :");
scanf("%d %d",&a,&b);
printf("\n Values before the function call :");
printf("a=%d , b=%d",a,b);
add10(a,b);
printf("\n Values after the function call :");
printf("a=%d , b=%d",a,b);
getch();
}
void add10(int x,int y)
{
x=x+10;
y=y+10;
printf("\n Values inside the function call :");
printf("a=%d, b=%d",x,y);
}

OUTPUT
Enter the values for a and b :45 67
Values before the function call :a=45 , b=67
Values inside the function call :a=55, b=77
Values after the function call :a=45 , b=67

60
/* Swap values using call by reference */

#include<stdio.h>
#include<conio.h>
int swap(int*,int*);
void main()
{
int a,b;
clrscr();
printf("\n Enter the values for a and b :");
scanf("%d %d",&a,&b);
swap(&a,&b);
printf("\n Values after swapping :");
printf("a=%d, b=%d",a,b);
getch();
}
int swap(int *x,int *y)
{
int *temp;
*temp=*x;
*x=*y;
*y=*temp;
return (*x,*y);
}

OUTPUT

Enter the values for a and b: 89 12


Values after swapping: a=12, b=89

61
/* Maximum and Minimum number. */

#include<stdio.h>
#include<conio.h>
void max(int[],int);
void min(int[],int);
void main()
{
int arr[100];
int n,i;
clrscr();
printf("Enter the value for an array:");
scanf("%d",&n);
printf("Enter %d values",n+1);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
max(arr,n);
min(arr,n);
getch();
}
void max(int arr[],int n)
{
int m,i;
m=arr[0];
for(i=0;i<n;i++)
{
if(m<arr[i])
m=arr[i];
}
printf("\n The maximum value is %d ",m);
}
void min(int arr[],int n)
{
int m,i;
m=arr[0];
for(i=0;i<n;i++)
{
if(m>arr[i])
m=arr[i];

62
}
printf("\n The minimum value is %d ",m);
}

OUTPUT
Enter the value for an array: 3
Enter 3 values 67 89 45
The maximum value is 89
The minimum value is 45

63
/* Read and print 2-D Array */

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,r,c,arr[100][100];
clrscr();
printf("\n Enter the number of rows and columns:");
scanf("%d %d",&r,&c);
for(i=0;i<r;i++)
{
printf("\n Enter %d values for %d row",c,i);
for(j=0;j<c;j++)
scanf("%d",&arr[i][j]);
}
printf("\n The 2-D Array formed is \n\n");
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
printf("%d \t",arr[i][j]);
}
getch();
}

OUTPUT

Enter the number of rows and columns: 2 3


Enter 3 values for 1 row 9 8 7
Enter 3 values for 2 row 6 5 4
The 2-D Array formed is
9 8 7
6 5 4

64
/* Bubble Sort */

#include<stdio.h>
#include<conio.h>
void BubbleSort(int[],int);
void main()
{
int ar[100],n,i;
clrscr();
printf("\n Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&ar[i]);
BubbleSort(ar,n);
printf("\n The sorted array is :");
for(i=0;i<n;i++)
printf("%d \t",ar[i]);
}
void BubbleSort(int ar[],int n)
{
int temp,i,j;
for(i=0;i<n;i++)
{
for(j=0;j<(n-1)-i;j++)
{
if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
}
} OUTPUT

Enter the number of elements: 4


Enter 4 elements 98 76 54 32
The sorted array is : 32 54 76 98

65
/* Insertion Sort */

#include<stdio.h>
#include<conio.h>
void InsSort(int[],int);
void main()
{
int ar[100],n,i;
clrscr();
printf("\n Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&ar[i]);
InsSort(ar,n);
printf("\n The sorted array is :");
for(i=0;i<n;i++)
printf("%d \t",ar[i]);
}
void InsSort(int ar[],int n)
{
int temp,i,j;
for(i=1;i<=n;i++)
{
temp=ar[i];
j=i-1;
while(temp<ar[j])
{
ar[j+1]=ar[j];
j--;
}
ar[j+1]=temp;
}
}
OUTPUT

Enter the number of elements: 5


Enter 5 elements 56 89 58 789 45
The sorted array is: 45 56 58 89 768

66
/* Selection Sort */

#include<stdio.h>
#include<conio.h>
void SelSort(int[],int);
void main()
{
int ar[100],n,i;
clrscr();
printf("\n Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&ar[i]);
SelSort(ar,n);
printf("\n The sorted array is :");
for(i=0;i<n;i++)
printf("%d \t",ar[i]);
getch();
}
void SelSort(int ar[],int n)
{
int temp,i,small,pos,j;
for(i=0;i<n;i++)
{
small=ar[i];
pos=i;
for(j=i+1;j<n;j++)
{
if(ar[j]<small)
{
small=ar[j];
pos=j;
}
}
temp=ar[i];
ar[i]=ar[pos];
ar[pos]=temp;
}

67
}
OUTPUT

Enter the number of elements: 6

Enter 6 elements 54 43 32 46 52

The sorted array is: 32 43 46 52 54

68
/* Insertion Sort by Dynamic Memory Allocation */

#include<stdio.h>
#include<conio.h>
void main()
{
int n, i,*p, temp, j;
clrscr();
printf("\n Enter the number of elements:");
scanf("%d",&n);
p=(int*)malloc(n*sizeof(int));
printf("\n Enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",(p+i));
for(i=1;i<=n;i++)
{
temp=*(p+i);
j=i-1;
while(temp<*(p+j))
{
*(p+j+1)=*(p+j);
j--;
}
*(p+j+1)=temp;
}
printf("\n The sorted array is :");
for(i=0;i<n;i++)
printf("%d \t",*(p+i));
getch();
}

OUTPUT

Enter the number of elements: 5


Enter 5 elements 234 56 123 76 223
The sorted array is: 56 76 123 223 234

69
/* Maximum number by Dynamic Memory Allocation. */

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
int *p, n, i, max, m;
clrscr();
printf("Enter the value for n:");
scanf("%d",&n);
p=(int*)malloc(n*sizeof(int));
printf("Enter %d values",n);
for(i=0;i<n;i++)
scanf("%d",(p+i));
m=*(p+0);
for(i=0;i<n;i++)
{
if(m<*(p+i))
m=*(p+i);
}
printf("\n The maximum value is %d ",m);
getch();
}

OUTPUT

Enter the value for n: 5


Enter 5 values45 6 21 7 1
The maximum value is 45

70
/* Reverse the string using realloc() */

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
char str[100],rev_str,*p;
int len=0,i,j;
clrscr();
printf("\n Enter a string to be reversed: ");
gets(str);
len=strlen(str);
p=(char *)realloc(str,len);
printf("\n The reverse of the string is:");
for(i=len;i>0;i--)
{
rev_str=str[i-1];
printf("%c",rev_str);
}
getch();
}

OUTPUT

Enter a string to be reversed: Reversing a string using DMA


The reverse of the string is: AMD gnisu gnirts a gnisreveR

71
/* Selection Sort by Dynamic Memory Allocation*/

#include<stdio.h>
#include<conio.h>
void main()
{
int *p,n,i,temp,small,pos,j;
clrscr();
printf("\n Enter the number of elements:");
scanf("%d",&n);
p=(int*)calloc(n,sizeof(int));
printf("\n Enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",(p+i));
for(i=0;i<n;i++)
{
small=*(p+i);
pos=i;
for(j=i+1;j<n;j++)
{
if(*(p+j)<small)
{
small=*(p+j);
pos=j;
}
}
temp=*(p+i);
*(p+i)=*(p+pos);
*(p+pos)=temp;
}
printf("\n The sorted array is :");
for(i=0;i<n;i++)
printf("%d \t",*(p+i));
getch();
} OUTPUT

Enter the number of elements: 5


Enter 5 elements 45 74 23 87 35
The sorted array is: 23 35 45 74 87

72
/* To count the number of vowels */

#include<stdio.h>
#include<conio.h>
void main()
{
int v=0,i,len=0;
char str[50],ch;
clrscr();
printf("\n Enter a string:");
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
ch=str[i];
switch(ch)
{
case 'A':v++;
break;
case 'E':v++;
break;
case 'I':v++;
break;
case 'O':v++;
break;
case 'U':v++;
break;
case 'a':v++;
break;
case 'e':v++;
break;
case 'i':v++;
break;
case 'o':v++;
break;
case 'u':v++;
break;
}
}

73
printf("\n There are %d vowels",v);
getch();
}

OUTPUT

Enter a string: Hi! This is a magic of C. Enjoy!


There are 9 vowels.

74
/* To find the length of a string */

#include<stdio.h>
#include<conio.h>
int str_len(char *s);
void main()
{
int len;
char str[100];
clrscr();
printf("\n Enter a string : ");
gets(str);
len=str_len(str);
printf("\n String has %d characters.",len);
getch();
}
int str_len(char *s)
{
int length=0;
while(*s!='\0')
{
length++;
s++;
}
return length;
}

OUTPUT

Enter a string: The C compiler will automatically count the length of this
string

75
String has 65 characters.
/* To count a given letter in a string */

#include<stdio.h>
#include<conio.h>
void main()
{
char str[50],ch;
int len,i,count=0;
clrscr();
printf("\n Enter a string in small letters: ");
gets(str);
printf("\n Enter a letter you want to count: ");
scanf("%c",&ch);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]==ch)
count++;
}
if(count==0)
printf("\n No such letter exists in this string.");
else
printf("\n %c exists %d times.",ch,count);
getch();
}

OUTPUT

Enter a string in small letters: welcome to the world of c.


Enter a letter you want to count: t

76
t exists 2 times.
/* Adding 2 strings with user defined functions */

#include<conio.h>
#include<stdio.h>
void main()
{
char ch1[50],ch2[50],ch3[100];
int i=0,j=0;
clrscr();
printf("\nEnter 1st string: ");
gets(ch1);
printf("\nEnter 2nd string: ");
gets(ch2);
while(ch1[i]!='\0')
{
ch3[j]=ch1[i];
i++;
j++;
}
j++;
i=0;
while(ch2[i]!=NULL)
{
ch3[j]=ch2[i];
i++;
j++;
}
puts(ch3);
getch();
}

OUTPUT

Enter 1st string: This is IDEAL


Enter 2nd string: U r studying C here.

77
This is IDEAL U r studying C here.
/* Reverse a string. */

#include<stdio.h>
#include<conio.h>
void main()
{
char str[100],rev_str;
int len=0,i,j;
clrscr();
printf("\n Enter a string to be reversed: ");
gets(str);
len=strlen(str);
printf("\n The reverse of the string is:");
for(i=len;i>0;i--)
{
rev_str=str[i-1];
printf("%c",rev_str);
}
getch();
}

OUTPUT

Enter a string to be reversed: Reversing a string


The reverse of the string is: gnirts a gnisreveR

78
/* Print employee information who have salary<5000 and
experience >= 5 */

#include<stdio.h>
#include<conio.h>
struct employee
{
char emp_name[25];
int empno;
int exp;
int salary;
}emp[5];
void main()
{
int i,n;
clrscr();
printf("\n Enter the following details for 5 employees.");
for(i=0;i<5;i++)
{
printf("\n %d employee",i+1);
printf("\n Name : ");
scanf("%s",emp[i].emp_name);
printf("\n No. : ");
scanf("%d",&emp[i].empno);
printf("\n Experience : ");
scanf("%d",&emp[i].exp);
printf("\n Salary : ");
scanf("%d",&emp[i].salary);
}
clrscr();
printf("\n The enteries entered are:");
printf("\n CODE NAME EXPERIENCE SALARY");
for(i=0;i<5;i++)
{
printf("\n %d %s %d %d", emp[i].empno,
emp[i].emp_name, emp[i].exp, emp[i].salary);
}

79
printf("\n\n\n List of those employees who have their salaries less
than 5000 and experience more than 5 years.");
printf("\n CODE NAME EXPERIENCE SALARY");
for(i=0;i<5;i++)
{
if((emp[i].exp>=5)&&(emp[i].salary<5000))
{
printf("\n\n %d %s %d %d",
emp[i].empno, emp[i].emp_name, emp[i].exp,
emp[i].salary);
}
}
getch();
}

OUTPUT

Enter the following details for 5 employees.

1 employee
Name : Geeta
No. :1
Experience : 6
Salary : 2000

80
2 employee
Name :B
No. :2
Experience : 5
Salary : 1000

3 employee
Name :C
No. :3
Experience : 4
Salary : 500

4 employee
Name :D
No. :4
Experience : 3
Salary : 250

5 employee
Name :E
No. :5
Experience : 2
Salary : 125

The enteries entered are:


CODE NAME EXPERIENCE SALARY
1 Geeta 6 2000
2 B 5 1000
3 C 4 500
4 D 3 250
5 E 2 125

List of those employees who have their salaries less than 5000 and
experience more than 5 years.

CODE NAME EXPERIENCE SALARY

1 Geeta 6 2000

81
2 B 5 1000

82
/* Arrange the students in increasing order of their marks. */

#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char name[25];
int maths;
int b_com;
int c;
int fit;
int phy;
int total;
}stud[100];
void main()
{
int n,i,temp,j;
clrscr();
printf("Enter the number of students.");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the details for %d student",i+1);
printf("\n Roll number : ");
scanf("%d",&stud[i].roll);
printf("\n Name : ");
scanf("%s",stud[i].name);
printf("\n Enter marks out of 100: ");
printf("\n Mathematics : ");
scanf("%d",&stud[i].maths);
printf("\n Business Communication: ");
scanf("%d",&stud[i].b_com);
printf("\n C language : ");
scanf("%d",&stud[i].c);
printf("\n Fundamentals of IT : ");
scanf("%d",&stud[i].fit);
printf("\n Basics of Physics : ");
scanf("%d",&stud[i].phy);

83
stud[i].total=stud[i].maths + stud[i].b_com + stud[i].c +
stud[i].fit + stud[i].phy;
}
printf("Details entered by you are: \n");
printf("ROLL NAME TOTAL \n");
for(i=0;i<n;i++)
{
printf("%d",stud[i].roll);
printf(" %s",stud[i].name);
printf(" %d/500",stud[i].total);
printf("\n");
}
printf("\n\n\n List according to increasing order of total marks:");
for(i=0;i<n-1;i++)
{
for(j=1;j<n;j++)
{
if(stud[i].total>stud[j].total)
{
temp=stud[i].total;
stud[i].total=stud[j].total;
stud[j].total=temp;
}
}
}
printf("\nROLL NAME TOTAL \n");
for(i=0;i<n;i++)
{
printf("%d",stud[i].roll);
printf(" %s",stud[i].name);
printf(" %d/500",stud[i].total);
printf("\n");
}
getch();
}

84
OUTPUT

Enter the number of students.2

Enter the details for 1 student


Roll number :1
Name :A
Enter marks out of 100 :
Mathematics : 99
Business Communication: 98
C language : 97
Fundamentals of IT : 96
Basics of Physics : 95

Enter the details for 2 student


Roll number :2
Name :B
Enter marks out of 100 :
Mathematics : 94
Business Communication: 93
C language : 92
Fundamentals of IT : 91
Basics of Physics : 90

Details entered by you are:


ROLL NAME TOTAL
1 A 485/500
2 B 460/500

List according to increasing order of total marks:


ROLL NAME TOTAL
2 B 460/500
1 A 485/500

85
/* Copy the context of one file to another. */

#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
char sname[25],tname[25],ch;
FILE *sptr,*tptr;
clrscr();
printf("\n Enter source file name: ");
gets(sname);
printf("\n Enter target file name: ");
gets(tname);
sptr=fopen("sname.txt","r");
if(sptr==NULL)
{
puts("\n Cannot open file!");
exit(0);
}
tptr=fopen("tname.txt","w");
while(!feof(sptr))
{
ch=getc(sptr);
putc(ch,tptr);
}
rewind(tptr);
while(!feof(tptr))
{
ch=getc(tptr);
printf("%c",ch);
}
fclose(sptr);
fclose(tptr);
getch();
}

86
/* Count chars, spaces, tabs and new lines in a file. */

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp,*fptr;
char ch,s[100];
int noc=0,nos=0,not=0,non=0;
clrscr();
fptr=fopen("SAMPLE.TXT","w");
printf("\n Enter text:");
while(strlen(gets(s))>0)
{
fputs(s,fptr);
fputs("\n",fptr);
}
fclose(fptr);
fp=fopen("SAMPLE.TXT","r");
if(fp==NULL)
{
puts("Cannot open file!");
exit(0);
}
while(1)
{
ch=getc(fp);
if(ch==EOF)
break;
if(ch==' ')
nos++;
else
if(ch=='\n')
non++;
else
if(ch=='\t')
not++;
else
noc++;

87
}
fclose(fp);
printf("\n Number of characters : %d",noc);
printf("\n Number of blanks : %d",nos);
printf("\n Number of tabs : %d",not);
printf("\n Number of lines : %d",non);
getch();
}

OUTPUT

Enter text: This is IDEAL


And this program is based on C
This is not a magic
Cummon lets have a look
Number of characters: 67
Number of blanks : 15
Number of tabs :2

88
Number of lines :4

89
/* Read records from a file using structure */

#include<stdio.h>
#include<conio.h>
void main()
{
FILE * fptr;
struct student
{
char name[40];
int age;
float total;
};
struct student s;
clrscr();
fptr=fopen("student.dat","r");
if(fptr==NULL)
{
puts("Cannot open file.");
exit(0);
}
while(fscanf(fptr,"%s","%d","%f",s.name,&s.age,&s.total)!=EOF)
printf("\n %s %d %f",s.name,s.age,s.total);
fclose(fptr);
getch();
}

OUTPUT

A 17 490.0000
B 17 480.0000
C 17 470.0000

90
/* Writes records to a file using structure */

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
char ans='Y';
struct student
{
char name[40];
int age;
float total;
};
struct student s;
clrscr();
fptr=fopen("student.dat","w");
if(fptr==NULL)
{
puts("Cannot open file.");
exit(0);
}
while(ans=='Y')
{
printf("\n Enter name, age and total marks obtained.");
scanf("%s %d %f",s.name,&s.age,&s.total);
fprintf(fptr,"\n %s %d %f",s.name,s.age,s.total);
printf("Add another record?(Y/N) : ");
ans=getche();
}
fclose(fptr);
getch();
} OUTPUT
Enter name, age and total marks obtained. A 17 490
Add another record? (Y/N): Y
Enter name, age and total marks obtained B 17 480
Add another record? (Y/N): Y
Enter name, age and total marks obtained C 17 470
Add another record? (Y/N): N

91
/* Reads records from binary file and displays them on VDU */

#include<stdio.h>
#include<conio.h>
void main()
{
FILE * fp;
struct emp
{
char name[30];
int age;
float bs;
}e;
clrscr();
fp=fopen("EMP.DAT","rb");
if(fp==NULL)
{
puts("Cannot open file!");
exit(0);
}
while(fread(&e,sizeof(e),1,fp)==1)
printf("\n %s %d %f",e.name,e.age,e.bs);
fclose(fp);
getch();
}

OUTPUT

D 18 5000.000000
E 18 6000.000000
F 18 7000.000000
G 18 8000.000000

92
/* Receives records from keyboard and writes them to a file in
binary mode */

#include<stdio.h>
#include<conio.h>
void main()
{
FILE * fp;
char ans='Y';
struct emp
{
char name[30];
int age;
float bs;
}e;
clrscr();
fp=fopen("EMP.DAT","wb");
if(fp==NULL)
{
puts("Cannot open file!");
exit(0);
}
while(ans=='Y')
{
printf("\n Enter name, age and basic salary:");
scanf("%s %d %f",e.name,&e.age,&e.bs);
fwrite(&e,sizeof(e),1,fp);
printf("Add another record (Y/N):");
ans=getche();
}
fclose(fp);
getch();
}

OUTPUT

Enter name, age and basic salary:


D
18

93
5000
Add another record (Y/N): Y

Enter name, age and basic salary:


E
18
6000
Add another record (Y/N): Y

Enter name, age and basic salary:


F
18
7000
Add another record (Y/N): Y

Enter name, age and basic salary:


G
18
8000
Add another record (Y/N): N

94

You might also like