Você está na página 1de 57

BANNARI AMMAN INSTITUTE OF TECHNOLOGY

(Autonomous Institution Affiliated to Anna University-Coimbatore


Approved by AICTE - Accredited by NBA and NAAC with A Grade)

SATHYAMANGALAM 638 401 Erode District Tamil Nadu

DEPARTMENT OF ELECTRICAL AND ELECTRONICS


ENGINEERING

07 G 108 C PROGRAMMING LABORATORY

Prepared By,
P.UmaMaheswari
L/EEE
P.Selvabharathi
L/EEE

BANNARI AMMAN INSTITUTE OF TECHNOLOGY


(Autonomous Institution Affiliated to Anna University-Coimbatore
Approved by AICTE - Accredited by NBA and NAAC with A Grade)

SATHYAMANGALAM 638 401 Erode District Tamil Nadu


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING

07G108 C PROGRAMMING LABORATORIES


(Common to all branches)
0

0 3

Programs Using Decision-Making and Looping Statements:


1.
2.
3.
4.
5.
6.
7.
8.

Write a program to calculate the simple interest, given the principle, period and rate of interest (Simple
Interest = PNR /100)
Write a program to convert the temperature from Fahrenheit to Centigrade and vice versa.
( F = 1.8 x (C +32)) ; C = ( F- 32) /1.8 )
Write a program to find the largest of 3 numbers using the minimum possible checks. (Simple if)
Write a program to convert binary to decimal number using while loop.
Write a program to find all possible roots of quadratic equation using switch case.
Write a program to read a particular number and to check whether it is a prime number or not.
The Electricity Production company has to print up the bills for its customers at the following rate:
For the 1st 50 KWH
For the next 100 KWH
For the next 200 KWH
For more than 350 KWH

rate is Rs.2
rate is Rs.6
rate is Rs.7
rate is Rs.8

Write a program to do the above and the output should be in the following order Customer name, Number
of Units and the Total Bill.
Programs Using Functions:
9.

Write a program using function that will round a floating point number to an indicated decimal place. For
example, the number 12.758
10. Would Yield the value 12.76 when it is rounded off to two decimal places.
11. Write a function exchange to interchange the values of two variables say X and Y
12. Write a recursive function that will generate and print first n Fibonacci series.
Programs Using Arrays:
13. Write a program to merge two different sized arrays and eliminate the duplicate from the merged array.
14. Write a menu driven program for inserting an element into an array and for deleting an element from the
array. Program should also have the provision for deleting the duplicates in an array.
15. Write a program to multiply two matrices. Use separate functions to read, process and print the data in
matrix form. Also find the trace and transpose of the given matrix.
16. A list of N numbers is given. Write a program to find:
a. Their average and standard deviation.
b. The number of integers, which are greater than equal to a specified number in the list.

17. Using arrays write a program to arrange the given set of N numbers in ascending order and hence to pick
the greatest and the smallest number. And also find the presence of a specified number

String Handling Programs:


18. Write a program to count number of vowels, consonants, words, white spaces and other characters in a
given line of text.
19. Write a program to check whether the given string is a palindrome or not.
20. Write a program to find the occurrence of a sub string in a main string and if found replace it with new
string.
21. Write a program to sort the set of names in alphabetical order.
22. Write a program using gets O(Capital) and puts o (Small) which converts a given 'C' program typed in
uppercase to a program in lowercase
Programs Using Structures:
23. Create a structure to store the following details:
Rollno, Name, Mark1, Mark2, Mark3, Total, Average, Result and Class.
Write a program to read Rollno, name and 3 subject marks. Find out the total, result and class as follows:
a. Total is the addition of 3 subject marks.
b Result is "Pass" if all subject marks are greater than or equal to 50 else "Fail".
c. Class will be awarded for students who have cleared 3 subjects
i) Class "Distinction" if average >=75
ii) Class "First" if average lies between 60 to 74 (both inclusive)
iii) Class "Second" if average lies between 50 & 59 (both inclusive)
d. Repeat the above program to manipulate 10 students' details and sort the structures as per
rank obtained by them
24. Define a structure that can describe the employees with the fields Eno, Ename. Basic. Write a program to
calculate DA = 32% of Basic. HRA = 15% of Basic. CCA = 10% of BASIC,
PF = 150,0 of Basic and
print all details with Net pay All processing should be using pointer notation.
Programs Using Pointers:
25. Write a program to count the number of consonants, vowels, digits, white spaces and other characters in a
line of text using pointers
26. Write menu driven program to perform all string handling operations using pointers.
27. Write a program to sort a list of strings in an alphabetical order (using pointers with DMA)
28. Write a menu driven program to perform addition, subtraction and multiplication of matrices using
pointers.
29. Write a program to search for an element using binary search.
30. Write a program for encryption of a given sentence and decryption of the same sentence.

1. SIMPLE INTEREST
OBJECTIVE:
To calculate the simple interest by using the principle amount, no of years and
rate of interest
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
main()
{
float prin_amt,no_yrs,int_rate;
float SI;
printf("\n Enter the principle amt:");
scanf("%f",&prin_amt);
printf("\n Enter the no of years :");
scanf("%f",&no_yrs);
printf("\n Enter the interest rate :");
scanf("%f",&int_rate);
SI=(prin_amt*no_yrs*int_rate)/100;
printf("\n The simple interst is : %f",SI);
getch();
}

NPUT & OUTPUT :


Enter the principle amt:15000
Enter the no of years :15
Enter the interest rate :15
The simple interst is : 33750.000000

Viva Questions:
1)
2)
3)
4)
5)
6)
7)

C programming language was developed by___________


C was developed in the year ______________
C is a _______________ language
C language is available for which of the following Operating Systems?
Which of the following symbol is used to denote a pre-processor statement?
Which symbol is used as a statement terminator in C?
Character constants should be enclosed between _____

2. FAHRENHEIT TO CENTIGRADE AND VICE VERSA


OBJECTIVE:
To convert the temperature from Fahrenheit to Centigrade and vice versa.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
float C,F;
int ch;
clrscr();
printf("\n\t\t CONVERSION OF TEMPERATURE");
printf("\n\n 1. CEN to FAR");
printf("\n 2. FAR to CEN");
printf("\n\n ENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\t Enter the Centigrade value :");
scanf("%f",&C);
F=1.8*C+32;
printf("\n\t The correspondiding Fahrenheit value :%f F",F);
break;

case 2:
printf("\n\t Enter the Fahrenheit value :");
scanf("%f",&F);
C=(F-32)/1.8;
printf("\n\t The equivalent Centigrade value :%f C",C);
break;
default:
printf("\n\t Invalid Choice");
}
getch();
}

INPUT & OUTPUT :


CONVERSION OF TEMPERATURE
1. CEN to FAR
2. FAR to CEN
ENTER YOUR CHOICE : 2
Enter the Fahrenheit value :45
The equivalent Centigrade value :7.222222 C
Viva
1)
2)
3)

Questions:
The maximum length of a variable in C is ___
What will be the maximum size of a float variable?
A declaration float a,b; occupies ___ of memory
The size of a String variable is

3. LARGEST OF 3 NUMBERS
OBJECTIVE:
To find the largest of 3 numbers using the minimum possible checks .

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("\n LARGEST AMONG 3 NUMBERS ");

printf("\n\n Enter the 3 numbers A,B,C :\n");


scanf("%d%d%d",&a,&b,&c);
if((a>b) && (a>c))
printf("\n The largest no is A = %d",a);
else if(b>c)
printf("\n The largest no is B = %d",b);
else
printf("\n The largest no is C = %d",c);
getch();

INPUT & OUTPUT :


LARGEST AMONG 3 NUMBERS
Enter the 3 numbers A,B,C :
11
23
42
The largest no is C = 42
Viva Questions:
1)
2)
3)
4)
5)

The operator && is an example for ___ operator.


The operator & is used for
The operator / can be applied to___________
The equality operator is represented by__________
Operators have hierarchy. It is used to know which operator?

4. BINARY TO DECIMAL CONVERSION


OBJECTIVE:
To convert the given binary number into a decimal number using while loop.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long n,r,i=0,j,dec=0;
clrscr();
printf("\n Enter the binary number: ");
scanf("%ld",&n);
while(n!=0)
{
r=n%10 ;
dec=dec+r*pow(2,i);
n=n/10;
i++;
}
printf("The equivalent decimal number : %ld",dec);
getch();
}

INPUT & OUTPUT :


BINARY TO DECIMAL
Enter the binary number:

111111

10

The equivalent decimal number : 63


Viva
1)
2)
3)
4)

Questions:
The bitwise AND operator is used for__________
Which operator has the highest priority?
The type cast operator is represented as____________
Explicit type conversion is known as

11

5. ROOTS OF QUADRATIC EQUATION

OBJECTIVE:
To find all possible roots of a quadratic equation using switch case

SOURCE CODE:
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
float a,b,c,wsr,root1,root2,s1,s2;
int option;
clrscr();
printf("\n\n \t\t\t ROOTS OF A QUADRATIC EQUATION ");
printf("\n\n\nEnter the values of a,b,c:\n");
scanf("%f %f %f",&a,&b,&c);
wsr=pow(b,2)-(4*a*c);
printf("\n WSR =%f",wsr);
if(wsr==0)
option= 1;
if(wsr>0)
option=2;
if(wsr<0)
option=3;

12

switch(option)
{
case 1:
printf("\n ROOTS ARE REAL AND EQUAL");
root1=(-b/(2*a));
root2=root1;
printf("\n ROOT 1 = %f, \n ROOT 2= %f",root1,root2);
break;
case 2:
printf("\n ROOTS ARE REAL AND UNEQUAL");
root1=-b+sqrt(wsr)/(2*a);
root2=-b-sqrt(wsr)/(2*a);
printf("\n ROOT 1 = %f, \n ROOT 2= %f",root1,root2);
break;
case 3:
printf("\n ROOTS ARE IMAGINARY");
s1=-b/2*a;
s2=sqrt(abs(wsr))/(2*a);
printf("\n ROOT 1=%f+ %fi",s1,s2);
printf("\n ROOT 2%f-%fi",s1,s2);
}
}

13

INPUT & OUTPUT :


ROOTS OF A QUADRATIC EQUATION
Enter the values of a,b,c:
1
-6
9
WSR =0.000000
ROOTS ARE REAL AND EQUAL
ROOT 1 = 3.000000,
ROOT 2= 3.000000
Viva Questions :
1) In the C language 'a represents__________
2) The number of the relational operators in the C language is_________
3) A compound statement is a group of statements included between a pair of________
4) In C, what is union?
5) A multidimensional array can be expressed in terms of__________

14

6. PRIME NUMBER OR NOT

OBJECTIVE:
To check whether the given number is a prime number or not.
SOURCE CODE:
#include<stdio.h>
#include<process.h>
#include<conio.h>
void main()
{
int n,prime,i;
clrscr();
printf("\n\t\t\t PRIME NO ");
printf("\n Enter the number : ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i!=0)
prime=1;
else
{
printf("\n\n The given number %d is not a prime number",n);
getch();

15

exit(0);
}
}
printf("\n\n The given number %d is a prime number\n",n);
getch();
}
INPUT & OUTPUT :
PRIME NO
Enter the number : 13
The given number 13 is a prime number

Viva Questions :
1) A pointer to a pointer in a form of__________
2) Pointers are of________
3) Maximum number of elements in the array declaration int a[5][8] is __________
4) Array subscripts in C always start at________
5) A Structure is defined as ______________

16

7. ELECTRICITY BILL

OBJECTIVE:
To print the electricity bill with Customer Name , Number of units and
Total amount
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int initial,final,consumed,amt,ch;
char name[20];
clrscr();
printf("\n\t\t ELECTRICITY BILL");
printf("\n\n Enter the Customer Name :");
scanf("%s",&name);
printf("\n Enter the Final unit

:");

scanf("%d",&final);
printf(" Enter the Initial unit :");
scanf("%d",&initial);
consumed=final-initial;
if(consumed<=50)
amt=consumed*2;
else if(consumed>50 & consumed<=150)
amt=consumed*6;
else if (consumed>150 & consumed<=350)

17

amt=consumed*7;
else if (consumed>350)
amt=consumed*8;

printf("\n Customer Name : %s",name);


printf("\n No of Units

: %d",consumed);

printf("\n Total amount

: %d",amt);

getch();
}

INPUT & OUTPUT :


ELECTRICITY BILL
Enter the Customer Name : Hema
Enter the Final unit :354
Enter the Initial unit :120
Customer Name : Hema
No of Units : 234
Total amount : 1638

Viva :
1. Header files in C contain what ?
2. The printf() function retunes which value when an error occurs?
3. The output of printf("%u", -1) is______
4. An Ampersand before the name of a variable denotes________
5. Null character is represented by_________
6. A statement differs from expression by terminating with a_________
18

8. ROUNDING OFF FLOATING POINT NUMBER

OBJECTIVE:
To round off a floating point number using function.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float round(float data,int dig);
void main()
{
float data,result;
int i,j,dig;
clrscr();
printf("\n\n\t\tROUNDING OFF FLOATING NUMBER\n");
printf("\n Enter the data

: ");

scanf("%f",&data);
printf("\n How many digits you want after decimal point? : ");
scanf("%d",&dig);
if(dig<=6)
result=round(data,dig);
else
result=data;
printf("\n The rounded off value of the data %f is: %g",data,result);
getch();

19

}
float round(float data,int dig)
{
long temp,test;
float result;
result=(pow(10,dig+1) * data);
temp=result;
test=temp%10;
if(test>=5)
temp=(temp/10)+1;
else
temp=temp/10;
result=temp/pow(10,dig);
return(result);
}

INPUT & OUTPUT :


ROUNDING OFF FLOATING NUMBER
Enter the data

: 12.456987

How many digits you want after decimal point? : 3


The rounded off value of the data 12.456987 is: 12.457
Viva :
What should be the expression return value for a do-while to terminate________
What for continue statement is used?
Which operator in C is called a ternary operator?
Which of the following is a key word is used for a storage class?
When the main function is called, it is called with the arguments?

20

9. INTERCHANGING THE VALUES OF TWO VARIABLES


OBJECTIVE:
To interchange the values in between two variables using function.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void swap(int x,int y);
void main()
{
int x,y;
clrscr();
printf("\n\n\t\t

INTERCHANGING THE VALUES OF TWO VARIABLES ");

printf("\n\n\nEnter the values for x & y :\n");


scanf("%d %d",&x,&y);
printf("\n The value of x & y before interchange : \n x=%d \n
y=%d\n",x,y);
swap(x,y);
getch();
}
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;

21

printf("\n The value of x & y after interchange : \n x=%d \n y=%d


\n",x,y);
}

INPUT & OUTPUT :


INTERCHANGING THE VALUES OF TWO VARIABLES
Enter the values for x & y :
45
23
The value of x & y before interchange :
x=45
y=23
The value of x & y after interchange :
x=23
y=45
Viva:
1) What is the difference between #include <file> and #include ?file?
2) What is the difference between far and near?
3) What is hashing?
4) How can I open a file so that other programs can update it at the same time?
5) When should a far pointer be used?

22

10. FIBONACCI SERIES


OBJECTIVE:
To generate first n Fibonacci series using recursive function .
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
long int fibo(int n);
void main()
{
long int n,i;
clrscr();
printf("\n\n\t\t\t FIBONACCI SERIES");
printf("\n\nEnter the number n: ");
scanf("%ld",&n);
for(i=1;i<=n;i++)
{
printf("%10ld",fibo(i));
}
getch();
}

long int fibo(int i)


{
if(i==1)
return 0;
else
{
if(i==2)

23

return 1;
else
return(fibo(i-1)+fibo(i-2));
}
}

INPUT & OUTPUT :

FIBONACCI SERIES
Enter the number n: 15
0

21

34

55

89

144

233

377

13

Viva:
1.

why n++ executes faster than n+1?

2. Why doesn?t strcat (string, ?!?); work ?


3. Is NULL always defined as 0?
4. when should the volatile modifier be used?
5. What is page thrashing?

24

11. ARRAY MERGING


OBJECTIVE:
To merge two different sized arrays and to eliminate the duplicate data.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],b[50],c[100],n1,n2,i,j,m=0,n=0,dup=0;
clrscr();
printf("\n\t\t MERGING OF ARRAYS WITHOUT DUPLICATE\n");
printf("\nEnter the first array max value: ");
scanf("\t%d",&n1);
printf("\nEnter the first array element: \n");
for(i=0;i<n1;i++)
{
scanf(" %d",&a[i]);
c[m]=a[i];
m++;
}
printf("\nEnter the second array max value: ");
scanf(" %d",&n2);
printf("\nEnter the second array element: \n");
for(i=0;i<n2;i++)
{
scanf(" %d",&b[i]);
c[m]=b[i];
m++;

25

}
for(i=0;i<m;i++)
{
for(j=i+1;j<m;j++)
{
if(c[i]==c[j])
{
dup=1;
break;
}
}
if(dup==0)
{
c[n]=c[i];
n++;
}
dup=0;
}
printf("\nResultant array without duplicate : ");
for(i=0;i<n;i++)
printf("\n %d",c[i]);
getch();
}

26

INPUT & OUTPUT :


MERGING OF ARRAYS WITHOUT DUPLICATE

Enter the first array max value: 4


Enter the first array element:
10
20
30
10
Enter the second array max value: 5
Enter the second array element:
50
20
60
70
30
Resultant array without duplicate :
10
50
20
60
70
30

27

13. MATRIX MULTIPLICATION & TRANSPOSE


OBJECTIVE:
To multiply two matrices and to find the transpose of a resultant matrix using array.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int a[10][10],b[10][10],c[10][10],d[10][10],r1,c1,r2,c2,i,j,k;
clrscr();
printf("\n\t\t MATRIX MULTIPLICATION & TRANSPOSE\n");
printf("\nEnter the row & column size of first matrix: ");
scanf("%d %d",&r1,&c1);
printf("\nEnter the row & column size of second matrix: ");
scanf("%d %d",&r2,&c2);

if(c1!=r2)
{
printf("\n \"Matrix multiplication is not possible.Because
R1!=C2\"");
getch();
exit(0);
}

28

else
{
printf("\nEnter the first matrix: \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf(" %d",&a[i][j]);
}
}

printf("\nEnter the second matrix: \n");


for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf(" %d",&b[i][j]);
}
}

for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}

29

printf("\nResultant multiplied matrix:\n");


for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf(" \t%d",c[i][j]);
d[j][i]=c[i][j];
}
printf("\n");
}

printf("\nResultant transpose matrix:\n");


for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf(" \t%d",d[i][j]);
}
printf("\n");
}
}
getch();
}

30

INPUT & OUTPUT :


MATRIX MULTIPLICATION & TRANSPOSE
Enter the row & column size of first matrix: 2 2
Enter the row & column size of second matrix: 2 3
Enter the first matrix:
2 4
4 6
Enter the second matrix:
1 2 3
6 2 1
Resultant multiplied matrix:
26

12

10

40

20

18

Resultant transpose matrix:


26

40

12

20

10

18

Viva
1) Explain one method to process an entire string as one unit?
2) Difference between calloc() and malloc()?
3) What does it mean when a pointer is used in an if statement?
4) How can you avoid including a header more than once?

31

14. STANDARD DEVIATION


OBJECTIVE:
To find the following using arrays
a). To find the average and standard deviation.
b). To find the number of integers, which are greater than equal to a specified number in the list.

SOURCE CODE:
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
float n,i,sum=0,sqrsum=0,mean,stddev,check;
float data[20],deviation[20],deviasqr[20];
clrscr();
printf("\n \t\t STANDARD DEVIATION \n");
printf("\n Enter the number of elements in data array : ");
scanf("%g",&n);
printf("\n Enter the elements of data array one by one : \n");
for(i=1;i<=n;i++)
scanf(" %g",&data[i]);
for(i=1;i<=n;i++)
sum=sum+data[i];
mean=sum/n;
for(i=1;i<=n;i++)
{
deviation[i]=data[i]-mean;
32

deviasqr[i]=pow(deviation[i],2);
sqrsum=sqrsum+deviasqr[i];
}
stddev=sqrt(sqrsum/n);
printf("\n D\t M\t (D-M)\t

(D-M)(D-M)");

printf("\n _____________________________________");
for(i=1;i<=n;i++)
printf("\n %g\t %g\t %6.2f\t %8.4f", data[i],mean,deviation[i],deviasqr[i]);
printf("\n Sum of deviation square :%g",sqrsum);
printf("\n\n\t\t a). MEAN or AVG = %g \t STDDEVIATION = %g" ,mean,stddev);
printf("\n\n Enter the number to check: ");
scanf("%g",&check);
printf(" \t\t b).The following numbers are greater than the given number :");
for(i=1;i<=n;i++)
{
if(data[i]>check)
printf("\n\t\t

data[%g]=%g",i,data[i]);

}
getch();
}

33

INPUT & OUTPUT :


STANDARD DEVIATION
Enter the number of elements in data array : 5
Enter the elements of data array one by one :
01
23
12
45
03
D

(D-M)

(D-M)(D-M)

_____________________________
1

16.8

-15.80

249.6400

23

16.8

6.20

38.4400

12

16.8

-4.80

23.0400

45

16.8

28.20

795.2401

16.8

-13.80

190.4400

Sum of deviation square :1296.8


a). MEAN or AVG = 16.8

STDDEVIATION = 16.1047

Enter the number to check: 10


b) The following numbers are greater than the given number :
data[2]=23
data[3]=12
data[4]=45

34

Viva
1. When would you use a pointer to a function
2. What is static identifier? 2)Where are the auto variables stored?
3. When does the compiler not implicitly generate the address of the first element of an array?
4. What are the advantages of auto variables?

35

15. ARRAY ASCENDING AND FINDING


OBJECTIVE:
To arrange the given set of N numbers in ascending order, to pick the greatest and the
smallest number and to find the presence of a specified number
.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,temp,a[100],x,found=0;
clrscr();
printf("\n\n\t ARRAY ASCENDING, FINDING GREATEST, SMALLEST AND SPECIFIED
NUMBER");
printf("\n\nEnter the no of datas in array (n) : ");
scanf("%d",&n);
printf("\nEnter the

datas for array :\n");

for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}

36

}
printf("\nThe sorted array is :");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
printf("\n\nThe minimum value of the array is :

%d",a[0]);

printf("\n\nThe maximum value of the array is :

%d",a[n-1]);

printf("\n\nEnter the data to find : ");


scanf("%d",&x);
for(i=0;i<n;i++)
{
if(x==a[i])
{
printf("Element Present in=%d Position\t",i+1);
found=1;
}
}
if(found==0)
printf("\n \"The data you are searching is not present\"");
getch();
}

37

INPUT & OUTPUT :


ARRAY ASCENDING, FINDING GREATEST, SMALLEST AND SPECIFIED NUMBER

Enter the no of datas in array (n) : 6


Enter the datas for array :
12
6
35
75
23
55
The sorted array is :
6
12
23
35
55
75
The minimum value of the array is : 6
The maximum value of the array is : 75
Enter the data to find : 55
Element Present in Position : 5
Viva :
1. What are advantages and disadvantages of external storage class?
2. How do you override a defined macro?
3. Can math operations be performed on a void pointer?
4. Where does global, static, local, register variables, free memory and C Program instructions
get stored?

38

16. STRING HANDLING OPERATIONS


OBJECTIVE:
To find the number of presence of numbers, consonants, words, white spaces & other
characters..

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int vow=0,num=0,cons=0,ws=0,oth=0,i,l;
char n[500];
clrscr();
printf(" \n\n\t\t\tCOUNTING THE NUMBER OF\n\t VOWELS, NUMBERS,
CONSONANTS, WORDS, WHITE SPACES & OTHER CHARACTERS");
printf("\n\n Enter a line of text:\n");
gets(n);
l=strlen(n);
for(i=0;i<=l;i++)
{
if(n[i]=='A'||n[i]=='E'||n[i]=='I'||n[i]=='O'||n[i]=='U'||
n[i]=='a'||n[i]=='e'||n[i]=='i'||n[i]=='o'||n[i]=='u')
vow++;
else if(n[i]>=65 && n[i]<=90 || n[i]>=97 && n[i]<=122)
cons++;
else if(n[i]>=48 && n[i]<=57)
num++;

39

else if(n[i]==32)
ws++;
else if(n[i]>=33 && n[i]<=127)
oth++;
}
printf("\n No of vowels inthe given text

: %d",vow);

printf("\n No of numbers in the given text

: %d",num);

printf("\n No of consonants in the given text

: %d",cons);

printf("\n No of white spaces in the given text

: %d",ws);

printf("\n No of words in the given text

: %d",ws+1);

printf("\n No of other characters in the given text : %d",oth);


getch();
}

INPUT & OUTPUT :

COUNTING THE NUMBER OF VOWELS


NUMBERS,CONSONANTS,WORDS,WHITE SPACES & OTHER CHARACTERS
Enter a line of text: hi$% how34 are you?
No of vowels in the given text

:6

No of numbers in the given text

:2

No of consonants in the given text

:5

No of white spaces in the given text

:3

No of words in the given text

:4

No of other characters in the given text : 3


Viva
1. How to print 1 to 100 without using any conditional loop?
2. Can math operations be performed on a void pointer?

40

3. Difference between : - 1)Global variable and Local variable , 2)Static variable and Global
variable ?
4. To which numbering system can the binary number 1101100100111100 be easily converted to?

17. STRING PALINDROME


OBJECTIVE:
To check whether the given string is a palindrome or not.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[25];
int len,i,j,notpal=0;
clrscr();
printf("\n\n\t\t STRING PALINDROME ");
printf("\n\nEnter the String: ");
scanf("%s",str);
len=strlen(str);
for(i=0,j=len-1;i<=len/2;i++,j--)
{
if(str[i]!=str[j])
{
notpal=1;
break;
}

41

if(notpal==1)
printf("\n\"The given string is not palindrome\"");
else
printf("\n\"The given string is palindrome\"");
getch();
}

INPUT & OUTPUT :


STRING PALINDROME
Enter the String: malayalam
"The given string is palindrome"

STRING PALINDROME
Enter the String: nalam
"The given string is not palindrome"
Viva:
1. How can we read/write structures from/to data files?
2. What is the quickest searching method to use?
3. Differentiate between a linker and linkage?

42

19. SORTING OF NAMES IN ALPHABETICAL ORDER


OBJECTIVE:
To sort the names in alphabetical order using string handling functions.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20][20],temp[50];
int i,j,n;
clrscr();
printf("\n\t\t SORTING OF NAMES IN ALPHABETICAL ORDER\n");
printf("\nEnter the number of names : ");
scanf("%d",&n);
printf("\nEnter the names one by one :\n");
for(i=0;i<n;i++)
scanf("%s",&name[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if((strcmp(name[i],name[j])>0))
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}

43

}
}
printf("\n\t\t SORTED NAMES \n");
for(i=0;i<n;i++)
printf("\n%s",name[i]);
getch();
}

INPUT & OUTPUT :


SORTING OF NAMES IN ALPHABETICAL ORDER
Enter the number of names : 05
Enter the names one by one :
Punitha.K
Saritha.R
Bindhu.N
Yasodha.K
Bindhu.C
SORTED NAMES
Bindhu.C
Bindhu.N
Punitha.K
Saritha.R
Yasodha.K
Viva:
1. What is the quickest searching method to use?
2. How to find GCD of four numbers?
3. What is Preprocessor?
4. What is the heap?
5. Can a variable be both const and volatile?
6. How can you check to see whether a symbol is defined?

44

20. STRING CASE CONVERSION


OBJECTIVE:
To convert the given word from uppercase to lowercase.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char upper[20],lower[20];
int len,i;
clrscr();
printf("\n\n\t CONVERSION FROM UPPER CASE TO LOWER CASE ");
printf("\n\nEnter the string in uppercase: ");
scanf("%s",upper);
len=strlen(upper);
for(i=0;i<len;i++)
lower[i]=upper[i]+32;
lower[i]='\0';
printf("\n\nThe lower case of \"%s\" is : \"%s\"",upper,lower);
getch();
}

45

INPUT & OUTPUT :

CONVERSION FROM UPPER CASE TO LOWER CASE


Enter the string in uppercase: HEMALATHA
The lower case of "HEMALATHA" is "hemalatha".

Viva:
1. Difference between :- 1) NULL pointer and NULL macro?
2. What is the difference between text and binary modes?
3. C Program to print the following series: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
4. How I can add two numbers in c language without using Arithmetic operators?

46

21.STUDENT DETAILS
OBJECTIVE:
To calculate the average of three marks of students and to store the Rollno, Name,
Mark1, Mark2, Mark3, Total, Average, Result and Class using structure.

SOURCE CODE:
#include<stdio.h>
#include<string.h>
#include<conio.h>
struct stud
{
int rollno,m1,m2,m3,total;
char name[30],grade[30];
float avg;
}s[50];
void main()
{
int i,n;
clrscr();
printf("\n\t\t\t STUDENTS DETAILS ");
printf("\n Enter the number of student:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the student %d details: ",i+1);
printf("\n Roll no :");
scanf("%d",&s[i].rollno);
printf("\n Name

:");

47

scanf("%s",&s[i].name);
printf("\n Mark1

:");

scanf("%d",&s[i].m1);
printf("\n Mark2

:");

scanf("%d",&s[i].m2);
printf("\n Mark3

:");

scanf("%d",&s[i].m3);
if(s[i].m1<50||s[i].m2<50||s[i].m3<50)
strcpy(s[i].grade,"fail");
else
{
s[i].total=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].total/3;
if(s[i].avg>=75)
strcpy(s[i].grade,"Distinction");
else if(s[i].avg>=60 && s[i].avg<=74)
strcpy(s[i].grade,"First");
else if(s[i].avg>=50 && s[i].avg<=59)
strcpy(s[i].grade,"Second");
}
}
printf("\n\t\t\t STUDENT MARK LIST \n");
printf("\nROLLNO\tNAME\t\tMARK1\tMARK2\tMARK3\tTOTAL\tAVERAGE(%)\t
GRADE");
for(i=0;i<n;i++)
printf("\n%d\t%s\t\t%d\t%d\t%d\t%d\t%g\t\t%s",s[i].rollno,
s[i].name,s[i].m1,s[i].m2,s[i].m3,s[i].total,s[i].avg,
s[i].grade);
getch();

48

INPUT & OUTPUT :


STUDENTS DETAILS
Enter the number of student:2
Enter the student 1 details:
Roll no :02
Name

:Bindhu.N

Mark1 :56
Mark2 :65
Mark3 :72
Enter the student 2 details:
Roll no :15
Name

:Shanthi.K

Mark1 :12
Mark2 :45
Mark3 :78
STUDENT MARK LIST
ROLLNO

NAME

MARK1 MARK2 MARK3 TOTAL AVERAGE GRADE

02

Bindhu.N

56

65

72

193

64.00

First

15

Shanthi.K

12

45

78

135

45.00

Fail

Viva:
1.
2.
3.
4.

How can I sort things that are too large to bring into memory?
What is the code for clrscr() function?
How do you print only part of a string?
What is the difference between goto and longjmp() and setjmp()?

49

22. EMPLOYEE SALARY DETAILS


OBJECTIVE:
To calculate the DA, HRA, CCA, PF of an employee and to print the Pay slip using
structure with pointer notation.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
struct emp
{
int eno;
char ename[50];
long int basic;
};
void main()
{
double da,hra,cca,pf,gross,net;
struct emp *e,a;
e=&a;
clrscr();
printf("\n\n\t\t EMPLOYEE SALARY DETAILS ");
printf("\n Enter emp no

: ");

scanf("%d",&e->eno);
printf("\n Enter name of the employee : ");
scanf("%s",&e->ename);
printf("\n Enter the basic pay

: ");

scanf("%ld",&e->basic);
da=e->basic*32/100;
hra=e->basic*15/100;

50

cca=e->basic*10/100;
pf=e->basic*15/100;
gross=e->basic+da+hra+cca;
net=gross-pf;
printf("\n\t\t\t PAY SLIP \n");
printf("\nEmployee No : %d",e->eno);
printf("\nName

: %s",e->ename);

printf("\nBasic salary: %ld",e->basic);


printf("\nHRA

: %lf",hra);

printf("\nDA

: %lf",da);

printf("\nPF

: %lf",pf);

printf("\nGross Salary: %lf",gross);


printf("\nNet Salary

: %lf",net);

getch();

INPUT & OUTPUT :


EMPLOYEE SALARY DETAILS
Enter emp no

: 12

Enter name of the employee : Sasikala.B


Enter the basic pay

: 15000

PAY SLIP
Employee No

: 12

Name

: Sasikala.B

Basic salary

: 15000

HRA

: 2250.000000

DA

: 4800.000000

PF

: 2250.000000

Gross Salary

: 23550.000000

Net Salary

: 21300.000000
51

Viva :
1. What is the purpose of realloc( )?
2. How do you print only part of a string?
3. What is the difference between goto and longjmp() and setjmp()?
4. How I can add two numbers in c language without using Arithmetic operators?
5. How much memory does a static variable takes?

52

23. STRING HANDLING OPERATIONS


OBJECTIVE:
To find the number of presence of numbers, consonants, words, white spaces & other
characters..

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int vow=0,num=0,cons=0,ws=0,oth=0,i,l;
char *strg;
clrscr();
printf(" \n\n\t\t\tCOUNTING THE NUMBER OF\n\t VOWELS, NUMBERS,
CONSONANTS, WORDS, WHITE SPACES & OTHER CHARACTERS");
printf("\n\n Enter a line of text:\n");
gets(strg);
while(*strg!=\0)
{
if(*strg=='A'||*strg=='E'||*strg=='I'||*strg=='O'||*strg=='U'||
*strg=='a'||*strg=='e'||*strg=='i'||*strg=='o'||*strg=='u')
vow++;
else if(*strg>=65 && *strg<=90 || *strg>=97 && *strg<=122)
cons++;
else if(*strg>=48 && *strg<=57)
num++;
else if(*strg==32)

53

ws++;
else if(*strg>=33 && *strg<=127)
oth++;
strg++;
}
printf("\n No of vowels inthe given text

: %d",vow);

printf("\n No of numbers in the given text

: %d",num);

printf("\n No of consonants in the given text

: %d",cons);

printf("\n No of white spaces in the given text

: %d",ws);

printf("\n No of words in the given text

: %d",ws+1);

printf("\n No of other characters in the given text : %d",oth);


getch();
}

INPUT & OUTPUT :

COUNTING THE NUMBER OF VOWELS


NUMBERS,CONSONANTS,WORDS,WHITE SPACES & OTHER CHARACTERS
Enter a line of text: hi$% how34 are you?
No of vowels in the given text

:6

No of numbers in the given text

:2

No of consonants in the given text

:5

No of white spaces in the given text

:3

No of words in the given text

:4

No of other characters in the given text : 3


Viva:
1. What is the purpose of main( ) function?

54

2. Is it better to use a macro or a function?


3. Is there anything you can do in C++ that you cannot do in C ?
4. What is indirection?
5. Tell me the difference between strdup and strcpy?
6. What use of structure and union?

55

24. STRING LENGTH & COPY USING POINTER


OBJECTIVE:
To find the length of the string and to copy it to another string using pointer.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
main()
{
char o_arr[12],*d_arr;
int len=0;
clrscr();
printf("\n\n\tSTRING LENGTH & COPYING FUNCTIONS USING POINTER");
printf("\n\n Enter the original array:");
gets(o_arr);
d_arr=o_arr;
printf("\n\n original array :%s",o_arr);
printf("\n\n duplicate array:%s",d_arr);
while(*d_arr!='\0')
{
len++;
d_arr++;
}
printf("\n\n length of the string : %d",len);
getch();
}

56

INPUT & OUTPUT :


STRING LENGTH & COPYING FUNCTIONS USING POINTER
Enter the original array: hi how are u?
original array : hi how are u?
duplicate array: hi how are u?
length of the string : 13

Viva:
1. what is the difference between the functions memmove() and memcpy()?
2. Write a code for implementation of doubly linked list with use of single pointer in each node
3. When should the register modifier be used? Does it really help?
4. How do you use a pointer to a function?
5. What is a null pointer?

57

Você também pode gostar