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

Experiments

Uploaded by

Dr D Suneetha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Experiments

Uploaded by

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

ASSIGNMENT QUESTIONS

UNIT-1

1. Explain block diagram of a computer with a neat sketch.


2. List and explain various input devices and output devices used in a
computer system.
3. What is a computer software? Explain types of software.
4. What is an algorithm? Write the various criteria used for judging an
algorithm?
5. What is a flow chart? uses of flowchart? Explain the different symbols used in
a flow chart.
6. What are the different steps followed in the program development.
7. Write about space requirements for variables of different types.
8. What is the purpose of type declarations? What are the components of type
declaration?
9. What are different types of integer constants,character constant,string
constant ,backslash character constant?
10. What is variable? How can variables be characterized? Give the rules for
variable declaration?
11. What is the associativity of various operators?
12. Explain the working of ternary operator with example?
13. What is meant by operator precedence?
14. State the rules that are applied while evaluating in automatic type
expression?
15. Expain the following functions with examples
getchar(),putchar(),scanf(),printf(),gets() and puts()
16. Write about C tokens with examples.
17. Explain about three generation languages in C
18. What is software engineering.

UNIT-2

16. What are various control structures present in c language with examples?
17. Explain about switch statement with program?
18. What is meant by looping? Describe any two different forms of looping with
examples?
19. What is the difference between break and continue statements? Explain with
examples?
20. What is the purpose of goto statement?How is the associated target statement
identified?
21. In what way array is different from an ordinary variable?
22. What conditions must be satisified by the entire elements of any given array?
23. what is an advantage of using arrays?
24. Define an array? What are the different types of arrays?Explain?
25. Write the syntax for declaring two dimensional array with examples?
26. What is a string? Explain declaration and initialization of arrays of char type.
27. Explain various string I/O functions with simple programs?
28. List and explain various string handling functions with suitable examples.

UNIT-3

29. What do you mean by functions? Give the structure of the functions and
explain about the arguments and their return values?
30. What is meant by function prototype? Give an example of function prototype.
31. what are the different types of functions? Explain function with no argument
and no return type with example.
32. What is the main() in C? Why is it necessary in each program?
33. Explain in detail about call-by-value and call-by-reference . Explain with a
simple program?
34. What are the different storage classes in C ? Explain?
35. Write about block structure and scope of variable.Illustrate with an example?
36. Distinguish between user defined and built in functions?
37. what is preprocessor directive?
38. Distinguish between function and preprocessor directive?
39. How does the undefining of a predefined macro done?

UNIT-4

40. Explain the process of declaring and initialization pointers. Give an example?
41. List out the reasons for using pointers?
42. Explain the process of accessing a variable through its pointer. Give an
example.
43. Write a C program that uses a pointer as a function argument?
44. Mention the difference between malloc() and calloc() functions?
45. Explain the command line arguments with example
46. Explain the concept of dynamic memory allocation with example program?

UNIT-5

47. Define structure and write the general format for declaring and accessing
members?
48. How are structured elements stored in memory?
49. How to copy one structure to another structure of a same data type,give an
example
50. How is structure different from an array?Explain.
51. Describe nested structure. Draw diagrams to explain nested structure.
52. How to compare structure variables? Give an example.
53. How are structure elements accessed using pointers? Which operator is
used? Give an example.
54. What is the general format of a union? Decalre a union and assign values to
it. Explain the process of accessing the union members.
55. Differentiate between a structure and union with respective allocation of
memory by the compiler. Give an example of each.
56. Compare arrays,structures and unions.
57. Explain various bit wise operators present in C language with examples.

UNIT-6

58. Write the syntax for opening a file with various modes and closing a file
59. Distinguish between text mode and binary mode operation of a file.
60. What is the significance of EOF
61. Define streams . Explain different types of files.
62. Explain about file handling functions with examples.

Tutorial Programs:

Tutorial-1

1. Write a program to find the maximum of three numbers using if


2. Write a program to find whether the given number is positive or negative
3. Calculates the area of circle or rectangle
4. To find the maximum of three numbers using if….else
5. To find whether the given number is even or odd
6. To find the biggest among two numbers
7. Program that accepts sales amount and then calculates discount as 10%
of sales amount if sales amount is more than 5000 otherwise as 5% of
sales amount
8. Program that checks whether a given number is vowel or not
9. To find maximum of three numbers using nested if…..

Tutorial-2

10. Program to check whether a character is an uppercase or lowercase alphabet


or a digit or a special symbol
11. Program to read three integers and display the largest of these numbers
12. Program that reads marks in three subjects,calculate average marks and
assign grade as per the following specifications:
If marks then grade
>=90 A
75-90 B
60-75 C
50-60 D
<50 fail

13. To calculate area of square/rectangle/circle/triangle depending upon user’s


choice
14. Program for temperature conversion depending upon user’s choice
15. Program to find the roots of quadratic equation
16. Program to read two integers and check whether first number is divisible by
second number
17. Using switch statement to read a day-number and print corresponding day of
the week

Tutorial-3

18. To find the sum of first n natural numbers using while loop
19. Program to find the series sum for 2+4+6+…..+n
20. Program to check whether a given number is armstrong number or not
21. Program to find length of a given positive integer i.e. number of digits in it.
Program should also display sum of digits of the given number.
22. Program to check whether a given number is palindrome or not
23. To find the sum of first n natural using loops
24. To find sum of N odd numbers
Tutorial-4
25. To find the factorial of a number
26. To generate Fibonacci sequence
27. To check whether a number is prime or not
28. Program to print prime numbers between a range
29. Program to display a pattern as shown below
*
**
***
*** *
30. Program to display following
pattern ABCDE
ABCD
ABC
AB
A
The above pattern also display with numbers
31. Program to print multiplication table using nested for loop
32.To find the maximum of N values

Department of BS&H Page


NRI Institute of Technology, Pothavarappadu 200
Tutorial -5
33. To accept a list of numbers into a 1-d array ,find their sum and average
34. Program to find maximum and minimum element from 1-d array
35. To search for a number in a list of numbers
36. To sort a list of numbers
37. To search for a number in a list of numbers
38. Write a program to perform addition of two matrices
39. Write a program to perform multiplication of two matrices
Tutorial-6
40. Program to print transpose of a matrix
41. Program to check a square matrix if it is symmetrical
42. Program to print row sum and column sum of a matrix
43. Program to find the sum of even numbers using arrays
44. Program to count number of even and odd numbers in a 1-d integer array
45. Program to find the number of occurrences of a character in a string
46. Program to find the length of a string
Tutorial-7
47. Program to find the copy one string to another string
48. Program to compare two strings
49. Program to concatenate two strings
50. Program to check whether the given string is palindrome or not
51. Write a program sorting of N names using string function
52. Write a program to counting characters,words,and lines from the text
53. to find out whether the given string is palindrome or not using the library
function
Tutorial-8
54. Program to count number of occurrences of a given character in a string
55. Program to count number of uppercase,lowercase characters,digits,and
special characters in a string.
56. Program to delete a substring from a string
57. Program to replace a substring with another substring in a given string
58. Program to sort a list of names alphabetically in the increasing order
59. Program for tax calculation without using function
60. Program for tax calculation using function
61. Write a program to find sum of given series by using function with argument
and return value
e=2+3/1!-6/2!+9/3!-12/4!+…………………..
Tutorial-9
62. Swap two numbers using pointers
63. Program to matrix multiplication using pointers
64. Program to illustrate malloc() : creation of an array dynamically

Department of BS&H Page 201


NRI Institute of Technology, Pothavarappadu
Tutorial-10
65. Write a program using structures to display the following information for each
customer name,account number,street,city,old balance,current payment,new
balance,account status.
66. Write a C program to calculate student wise total for three students using
array of structures.
67. Write a C program to print maximum marks in each subject along with
the student name by using structures. Take 3 subjects and 3 students
records.
68. Write a C program to compute the monthly pay of 50 employees using each
employee’s name,basicpay. The DA is computed as 72% of basic pay. Gross
salary (basic pay+DA). Print the employee’s name and gross salary.
Tutorial-12
69. Write a C program to copy the one file data into another file.
70. Write a C program to merge the content of two files into a third file
71. Write a program to reverse the first n characters in a file.
72. Write a C program to count the number of words in a given file
73. Write a C program to count characters and lines in a given file.

Write a C Program to calculate the area of triangle using the formula area = ( s (s-a)
(s-b)(s-c))1/2 where s= (a+b+c)/2.

Aim: Program to calculate the area of triangle using the given formula.

Source Code:

/******************PROGRAM TO CALCULATE THE AREA OF TRIANGLE***************/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
double s,area;
clrscr();
printf("\n Enter the values of a,b and c:");
scanf("%d%d%d",&a,&b,&c);
s=(a+b+c)/2.0;

Department of BS&H Page 202


NRI Institute of Technology, Pothavarappadu
area=(s*(s-a)*(s-b)*(s-c));
printf("\n The Area is: %lf",sqrt(area));
getch();
}

Result : The program executed successfully and it gives a right output.

Input:
Enter the values of a,b and c:1 2 3

Output:
The Area is:0.000000

Department of BS&H Page 203


NRI Institute of Technology, Pothavarappadu
Write a C program to find the largest of three numbers using ternary operator.

Aim: Program to find the largest of three numbers using ternary operator.

Source Code:
/*****PROGRAM TO FIND THE LARGEST OF THREE NUMBERS USING TERNARY OPERATOR***/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,l;
clrscr();
printf("\n Enter the values of a,b and c:");
scanf("%d%d%d",&a,&b,&c); l=(a>b)?(a>c)?a:
(b>c)?b:c:(b>c)?b:(a>c)?a:c; printf("\n The
largest is: %d",l);
getch();
}

Result : The program executed successfully and it gives a right output.

Input: Enter the values of a,b and c:3 4 1

Output: The largest is:4

Department of BS&H Page 204


NRI Institute of Technology, Pothavarappadu
Program to swap two numbers without using temporary variable

Aim: Program to swap two numbers without using temporary variable.

Source Code:

/*******PROGRAM TO SWAP TWO NUMBERS WITHOUT USING TEMPORARY VARIABLE********/

/* */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("\n Enter the values of a and b:");
scanf("%d%d",&a,&b);
printf("\n Before Swapping a: %d\t b: %d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\n After Swapping a: %d\t b: %d",a,b);
getch();
}
Result : The program executed successfully and it gives a right output.

Input:
Enter the values of a and b: 10 20
Output:
Before Swapping a: 10 b: 20

After Swapping a: 20 b: 10

Department of BS&H Page 205


NRI Institute of Technology, Pothavarappadu
2’s complement of a number is obtained by scanning it from right to left and complementing all the bit

Aim: Program to find 2’s complement of a binary number.

Source Code:

/**********************PROGRAM TO FIND 2’s COMPLEMENT***********************/

#include<stdio.h>
#include<conio.h>
void main()
{
char bi[20],res;
int flag=0,i;
clrscr();
printf("\n Enter Binary no : ");
scanf("%s",bi);
for(i=strlen(bi)-1;i>=0;i--)
{
if(flag==0)
{
if(bi[i]==’0’)
continue;
else
flag=1;
}
else
{
if(bi[i]==’0’)
bi[i]=’1’;
else
bi[i]=’0’;
}
}
printf(“\n 2’s complement for the given binary no is: %s”,bi);
getch();
}

Result : The program executed successfully and it gives a right output.

Input:
Enter Binary no: 11100
Output:
2’s complement for the given binary no is: 00100

Department of BS&H Page 206


NRI Institute of Technology, Pothavarappadu
Write a C program to find the roots of a quadratic equation.

Aim: Program to find the roots of a quadratic equation.

Source Code:

/************PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION**************/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c,d;
float r1,r2;
clrscr();
printf("\n Enter the values of a,b,c: \n");
scanf("%d %d %d",&a,&b,&c);
d=((b*b)-4*a*c);
if(d==0)
{
printf("\n Roots are equal \n");
r1=(-b)/2*a;
r2=(-b)/2*a;
printf("\n The roots of quadratic equation are %f %f",r1,r2);
}
else if(d>=0)
{
printf(“\n Roots are real \n”);
r1=(-b+sqrt(b*b-4*a*c))/2*a;
r2=(-b-sqrt(b*b-4*a*c))/2*a;
printf("\n The roots of quadratic equation are %f %f",r1,r2);
}
else
{
printf(“\n Roots are imaginary”);
}
getch();
}

Result : The program executed successfully and it gives a right output.

Input:
Enter the values of a,b,c: 1 -5 6
Output:
The roots of quadratic equation are 3.000000 2.000000

Department of BS&H Page 207


NRI Institute of Technology, Pothavarappadu
Write a C program, which takes two integer operands and one operator from the user, performs
(Consider the operators +,-,*, /, % and use Switch Statement)

Aim: Program which takes two integer operands and one operator from the user, perform the operation.
Source Code:
/**********PROGRAM PERFORMS OPERATION BY BASED ON USERS CHOICE**************/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,n;
clrscr();
printf("Enter your choice\n 1.)addition \n
2.)substraction \n 3.)multiplication \n 4.)division \n 5.)modulus");
Scanf(“&d”,&n);
printf("\n Enter any two integer values : ");
scanf("%d%d",&a,&b);
switch(n)
{
case 1: printf("\n Addition of %d and %d is %d\n",a,b,a+b);
break;
case 2: printf("\n Subtraction of %d and %d is %d\n",a,b,a-b);
break;
case 3: printf("\n Multiplication of %d and %d %d\n",a,b,a*b);
break;
case 4: printf("\n Division of %d and %d is %d\n",a,b,a/b);
break;
case 5: printf("\n Modules of %d and %d is %d",a,b,a%b);
break;
default : printf(“\n please enter correct choice”);
}
getch();
}
Result : The program executed successfully and it gives a right output.
Input:
Enter your choice
1.)addition
2.)subtraction
3.)multiplication
4.)division
5.)modulus
5
Enter any two integer values : 2 5
Output:
Modules of 2 and 5 is 2

Department of BS&H Page


NRI Institute of Technology, Pothavarappadu 208
Write a C program to find the sum of individual digits of a positive integer

Aim: Program to find the sum of individual digits of a positive integer.

Source Code:

/*****PROGRAM TO FIND THE SUM OF INDIVIDUAL DIGITS OF A POSITIVE INTEGER****/

#include<stdio.h>
#include<conio.h>
void main()
{
unsigned int n;
int s=0;
clrscr();
printf("\n Enter positive integer: ");
scanf("%u",&n);
while(n!=0)
{
s=s+n%10;
n=n/10;
}
printf("\n The sum of digits of givem number = %d",s);
getch();
}

Result : The program executed successfully and it gives a right output.

Input:
Enter positive integer:345
Output:
The sum of digits of givem number =12

Department of BS&H Page 209


NRI Institute of Technology, Pothavarappadu
A Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0 and 1. Sub

Aim: Program to generate the first n terms in the Fibonacci sequence.

Source Code:
/******PROGRAM TO GENERATE THE FIRST N TERMS IN THE FIBONACCI SEQUENCE******/

#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,n,i;
clrscr();
printf("\n Enter the number of elements : ");
scanf("%d",&n);
printf("\n The fibonacci series is : \n%d\t%d",a,b);
for(i=2;i<n;i++)
{
c=a+b; printf("\t
%d",c); a=b;
b=c;
}
getch();
}

Result : The program executed successfully and it gives a right output.

Input:
Enter the number of elements :5
Output:
The fibonacci series is :
0 1 1 2 3

Department of BS&H Page 210


NRI Institute of Technology, Pothavarappadu
Use summing series method to compute the value of SIN(x).

Aim: Program to find the value SIN(x) using summing series method.

Source Code:

/**********************PROGRAM TO FIND THE VALUE OF SIN(x) *****************/

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>

void main()
{
int n, x1;
float acc, term, den, x, sinx=0, sinval;
clrscr();
printf("\n Enter the value of x (in degrees):");
scanf("%f",&x);
x1 = x;
x = x*(3.142/180.0);
sinval = sin(x);
printf("\n Enter the accuary for the result:");
scanf("%f", &acc);
term = x;
sinx = term;
n = 1;
do
{
den = 2*n*(2*n+1);
term = -term * x * x / den;
sinx = sinx + term;
n = n + 1;
} while(acc <= fabs(sinval - sinx)); printf("\
n Sum of the sine series = %f", sinx);
printf("\n Using Library function sin(%d) = %f", x1,sin(x));
}

Result : The program executed successfully and it gives a right output.

Input:
Enter the value of x (in degrees): 30
Enter the accuary for the result: 0.000001

Output:
Sum of the sine series = 0.500059
Using Library function sin(30) = 0.500059

Department of BS&H Page 211


NRI Institute of Technology, Pothavarappadu
Use summing series method to compute the value of COS(x).

Aim: Program to find the value COS(x) using summing series method.

Source Code:

/**********************PROGRAM TO FIND THE VALUE OF COS(x) *****************/

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int n,x1,i,j;
float x,sign,cosx,fact;
printf("Enter the number of the terms in aseries\n");
scanf("%d",&n);
printf("Enter the value of x(in degrees)\n");
scanf("%f",&x);
x1=x;
x=x*(3.142/180.0);
cosx=1;
sign=-1;
for(i=2;i<=n;i=i+2)
{
fact=1;
for(j=1;j<=i;j++)
{
fact=fact*j;
}
cosx=cosx+(pow(x,i)/fact)*sign;
sign=sign*(-1);
}
printf("Sum of the cosine series = %7.2f\n",cosx);
printf("The value of cos(%d) using library function = %f\n",x1,cos(x));
}

Result : The program executed successfully and it gives a right output.

Input:
Enter the number of the terms in aseries:5
Enter the value of x(in degrees):60

Output:
Sum of the cosine series = 0.50
The value of cos(60) using library function = 0.499882

Department of BS&H Page 212


NRI Institute of Technology, Pothavarappadu
Use summing series method to compute the value of ex.

x
Aim: Program to find the value e using summing series method.

Source Code:
^x
/**********************PROGRAM TO FIND THE VALUE OF e *****************/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,x,i,j;
double s=1.0,f=1.0;
clrscr();
printf("\n Enter the value of x:");
scanf("%d",&x);
printf("\n Enter the accuracy(No.of terms):");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
f=f*j;
s=s+pow(x,i)/f;
}
printf("\n The sum of the series is(e pow x):%lf",s);
getch();
}

Result : The program executed successfully and it gives a right output.

Input:
Enter the value of x: 2

Enter the accuracy (No. of terms):5


Output:
The sum of the series is (e pow x):5.723148

Department of BS&H Page 213


NRI Institute of Technology, Pothavarappadu
Write a C program to generate all the prime numbers between 1 and n, where n is a value suppl

Aim: Program to generate all the prime numbers between 1 and n.

Source Code:
/**********PROGRAM TO GENERATE ALL THE PRIME NUMBERS BETWEEN 1 AND N********/

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

Result : The program executed successfully and it gives a right output.

Input:
Enter the range : 10
Output:
2 3 5 7

Department of BS&H Page 214


NRI Institute of Technology, Pothavarappadu
Write a C Program to print the multiplication table of a given number n up to a given value. Wh

Aim: Program to print the multiplication table of a given number n up to a given value.

Source Code:

/*******PROGRAM TO PRINT THE MULTIPLICATION TABLE OF A GIVEN NUMBER*********/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,u,i;
clrscr();
printf("\n Enter the value of n:");
scanf("%d",&n);
printf("\n Enter the table range:");
scanf("%d",&u)
printf("\n The multiplication Table for %d up to %d is \n",n,u);
for(i=1;i<=u;i++)
{
printf("\n %2d * %2d = %2d",n,i,n*i);
}
getch();
}

Result : The program executed successfully and it gives a right output.

Input:
Enter the value of n:5
Enter the table range:11
Output:
The multiplication Table for 5 up to 11 is
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
5 * 11 = 55

Department of BS&H Page 215


NRI Institute of Technology, Pothavarappadu
Write a C Program to enter a decimal number, and calculate and display the binary equivalent o

Aim: Program to convert decimal number into binary equivalent .

Source Code:

/*PROGRAM TO ENTER A DECIMAL NO AND CALCULATE AND DISPLAY BINARY EQUIVALENT*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,c,k;
clrscr();
printf("\n Enter the value of n:");
scanf("%d",&n);
printf("\n The Decimal number is: %d",n);
printf("\n %d in binary number system are:", n);
for (c = 15; c >= 0; c--)
{
k = n >> c;
if (k & 1)
printf("1");
else
printf("0");
}
getch();

Result : The program executed successfully and it gives a right output.

Input:
Enter the value of n: 32767
Output:
32767 in binary number system are: 0111111111111111

Department of BS&H Page 216


NRI Institute of Technology, Pothavarappadu
Write a C program to check whether the given number is Armstrong number or not.

Aim: Program to check whether the given number is Armstrong number or not.

Source Code:
/***PROGRAM TO CHECK WHETHER THE GIVEN NUMBER IS ARMSTRONG NUMBER OR NOT***/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,s=0,i,x;
clrscr();
printf("\n Enter the value of n:");
scanf("%d",&n);
x=n;
for(;n>0;n=n/10)
{
i=n%10;
s=s+i*i*i;
}
if(x==s)
printf("\n Armstrong Number");
else
printf("\n Not a Armstrong Number");
getch();

}
Result : The program executed successfully and it gives a right output.

Input:
Enter the value of n: 153
Output:
Armstrong Number

Department of BS&H Page 217


NRI Institute of Technology, Pothavarappadu
Write a C program to interchange the largest and smallest numbers in the array.

Aim: Program to interchange the largest and smallest numbers in the array.

Source Code:

/***PROGRAM TO INTERCHANGE THE LARGEST AND SMALLEST NUMBERS IN THE ARRAY****/

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,l=0,s=32767,li,si,t;
clrscr();
printf("\n Enter the size of an array:");
scanf("%d",&n);
printf("\n Enter array elements :");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
printf("%3d",a[i]);
}
for(i=0;i<n;i++){
if(l<a[i]){
l=a[i];
li=i;
}
}
for(i=0;i<n;i++){
if(s>a[i]){
s=a[i];
si=i;
}
}
t=a[li]; a[li]=a[si]; a[si]=t;
printf("\n After interchanging the Array Status is:");
for(i=0;i<n;i++)
printf("%3d",a[i]);
getch();

}
Result : The program executed successfully and it gives a right output.
Input: Enter the size of an array: 5
Enter array elements: 5 2 1 3 4

Output: 5 2 1 3 4
After interchanging the Array Status is: 1 2 5 3 4

Department of BS&H Page 218


NRI Institute of Technology, Pothavarappadu
Write a C program to implement a liner search.

Aim: Program to implement a linear search.

Source Code:
/*******************PROGRAM TO IMPLEMENT A LINEAR SEARCH *******************/

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,x,flag=0;
clrscr();
printf("\n Enter the size of an array:");
scanf("%d",&n);
printf("\n Enter array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter the search element:");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(a[i]==x)
{
flag=1;
break;
}
}
if(flag==1)
printf("\n Element found (position:%d)",++i);
else
printf("\n Element Not Found");
getch();

}
Result : The program executed successfully and it gives a right output.

Input:
Enter the size of an array: 5
Enter array elements: 4 3 1 6 5
Enter the search element: 6
Output:
Element found (position: 4)

Department of BS&H Page 219


NRI Institute of Technology, Pothavarappadu
Write a C program to implement a binary search.

Aim: Program to implement a binary search.


Source Code:
/*******************PROGRAM TO IMPLEMENT A BINARY SEARCH *******************/

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,x,flag=0,l,h,m;
clrscr();
printf("\n Enter the size of an array:");
scanf("%d",&n);
printf("\n Enter array elements (In Sorted Order):");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter the search element:");
scanf("%d",&x);
l=0;h=n-1;
while(l<=h)
{
m=(l+h)/2;
if(a[m]==x)
{
flag=1;
break;
}
else if(a[m]>x)
h=m-1;
else
l=m+1;
}
if(flag==1)
printf("\n Element found (position:%d)",++m);
else
printf("\n Element Not Found");
getch();
}
Result : The program executed successfully and it gives a right output.
Input:
Enter the size of an array: 5
Enter array elements (In Sorted Order): 1 2 3 4 5
Enter the search element: 6
Output:
Element Not Found

Department of BS&H Page 220


NRI Institute of Technology, Pothavarappadu
Write a C program to implement sorting of an array of elements.

Aim: Program to implement sorting of an array of elements.

Source Code:
/************PROGRAM TO IMPLEMENT SORTING OF AN ARRAY OF ELEMENTS***********/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,j,t;
clrscr();
printf("\n Enter the size of an array:");
scanf("%d",&n);
printf("\n Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Array elements before sorting:");
for(i=0;i<n;i++)
printf("%3d",a[i]);
for(i=0;i<n;i++){
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("\n Array elements after sorting:");
for(i=0;i<n;i++)
printf("\%3d",a[i]);
getch();

}
Result : The program executed successfully and it gives a right output.
Input: Enter the size of an array: 5
Enter array elements (In Sorted Order): 3 2 1 5 4
Output: Array elements before sorting: 3 2 1 5 4

Array elements after sorting: 1 2 3 4 5

Department of BS&H Page 221


NRI Institute of Technology, Pothavarappadu
Write a C program to input two m x n matrices, check the compatibility and perform addition

Aim: Program to find the addition of two matrices.

Source Code:

/****************PROGRAM TO FIND THE ADDITION OF TWO MATRICES**************/

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20],b[20][20],c[20][20],i,j,ra,ca,rb,cb;
clrscr();
printf("\n Enter order of matrix a:");
scanf("%d %d",&ra,&ca);
printf("\n Enter order of matrix b:");
scanf("%d %d",&rb,&cb);
if((ra==rb)&&(ca==cb))
{
printf("\n \t Enter matrix a\n");
for(i=0;i<ra;i++)
{
for(j=0;j<ca;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n\t Enter matrix b\n");
for(i=0;i<rb;i++)
{
for(j=0;j<cb;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n MATRIX ADDITION IS\n");
for(i=0;i<ra;i++)
{
for(j=0;j<cb;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%3d",c[i][j]);
}
printf("\n");
}

Department of BS&H Page 222


NRI Institute of Technology, Pothavarappadu
}
else
{
printf("\n Matrix Addition is Not Possible");
}
getch();
}

Result : The program executed successfully and it gives a right output.

Input: Enter order of matrix a: 3 2


Enter order of matrix b: 2 3

Output: Matrix Addition is Not Possible

Department of BS&H Page 223


NRI Institute of Technology, Pothavarappadu
Write a C program to input two m x n matrices, check the compatibility and perform multiplica

Aim: Program to find the multiplication of two matrices.

Source Code:
/*************PROGRAM TO FIND THE MULTIPLICATION OF TWO MATRICES************/

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20],b[20][20],c[20][20],r1,c1,r2,c2,i,j,k,ta,tb;
clrscr();
printf("\n Enter number of rows and columns of first matrix :"); scanf("%d
%d",&r1,&c1);
printf("\n Enter number of rows and columns of second matrix :"); scanf("%d
%d",&r2,&c2);
if(r2==c1)
{
ta=r1*c1;
printf("\n Enter %d elements of First matrix :",ta);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n First Matrix is :\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
tb=r2*c2;
printf("\n Enter the %d elments of second matrix :",tb); for(i=0;i<r2;i+
+)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n Second Matrix is:\n");
for(i=0;i<r2;i++)

Department of BS&H Page 224


NRI Institute of Technology, Pothavarappadu
{
for(j=0;j<c2;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n Multiplication of the Matrices:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<r2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
{
printf("\n Matrix multiplication is not possible");
}
getch();
}

Result : The program executed successfully and it gives a right output.

Input:
Enter number of rows and columns of first matrix :2 3
Enter number of rows and columns of second matrix :2 3
Output:
Matrix multiplication is not possible

Department of BS&H Page 225


NRI Institute of Technology, Pothavarappadu
Write a C program that uses functions to perform the following operations:
i. To insert a sub-string in to given main string from a given position.
Aim: Program that uses function to insert a sub-string in to given main string from a given position.
Source Code:
/**********PROGRAM TO INSERT A SUB STRING INTO A GIVEN MAIN STRING**********/
#include<stdio.h>
#include<conio.h>
void main()
{
void insert(char s[],char ss[],int n);
int n;
char s[80],ss[80];
clrscr();
printf("\n Enter the main string :");
gets(s);
printf("\n The given main string is :%s",s);
printf("\n Enter the sub string :");
gets(ss);
printf("\n The given sub string is :%s",ss);
printf("\n Enter the position :");
scanf("%d",&n);
insert(s,ss,n);
printf("\n String after insertion :");
puts(s);
getch();
}
void insert(char s[80],char ss[80],int n)
{
int i,j=0;
char temp[80];
for(i=n;s[i]!=NULL;i++)
{
temp[j++]=s[i];
temp[j]=NULL;
}
for(i=0;ss[i] != NULL;i++)
{
s[n++]=ss[i];
}
for(i=0;temp[i] != NULL;i++)
{
s[n++]=temp[i];
}
s[n]=’\0’;
}
Result : The program executed successfully and it gives a right output.
Input:
Enter the main string: Hi this is x
The given main string is: Hi this is x
Enter the sub string: Hello
The given sub string is: Hello
Enter the position: 2
Output:
String after insertion: HiHello this is x

Department of BS&H Page 226


NRI Institute of Technology, Pothavarappadu
Write a C program that uses functions to perform the following operations:
ii. To delete n Characters from a given position in a given string.

Aim: Program that uses function to delete n characters from a given position in a given string.

Source Code:
/*PROGRAM TO DELETE GIVEN NO OF CHARACTERS FROM THE GIVEN POSITION IN A STR*/

#include<stdio.h>
#include<conio.h>
void main()
{
void delete(char s[],int p,int n);
int p,n;
char s[80];
clrscr();
printf("\n Enter the main string :");
gets(s);
printf("\n The given main string is :%s",s);
printf("\n Enter the no.of characters :");
scanf("%d",&n);
printf("\n Enter the position :");
scanf("%d",&p);
delete(s,p,n);
printf("\n String after deletion :");
puts(s);
getch();
}
void delete(char s[80],int p,int n)
{
int i,j;
for(i=p,j=p+n;s[j] != NULL;i++,j++)
{
s[i]=s[j];
}
s[i]=NULL;
}

Result : The program executed successfully and it gives a right output.

Input:
Enter the main string: Hi Hello
The given main string is: Hi Hello
Enter the no. of characters: 2
Enter the position: 2
Output:
String after deletion: Hiello

Department of BS&H Page 227


NRI Institute of Technology, Pothavarappadu
Write a C program that uses functions to perform the following operations:
iii. To replace a character of string either from beginning or ending or at a specified loca

Aim: Program that uses function to replace a character of string from beginning or ending or at a specified position.

Source Code:
/*****PROGRAM TO REPLACE A CHARACTER OF STRING AT A SPECIFIED POSITION******/

#include<stdio.h>
#include<conio.h>
void main()
{
void replace(char s[],char e,char r,int p);
int p;
char s[80],e,r;
clrscr();
printf("\n Enter the main string :");
gets(s);
printf("\n The given main string is :%s",s);
printf("\n Enter the present character :");
scanf("%c",&e);
printf("\n Enter the replace character :");
r=getche();
printf("\n Enter the position :");
scanf("%d",&p);
replace(s,e,r,p);
printf("\n String after replace :");
puts(s);
getch();
}
void replace(char s[80],char e,char r,int p)
{
if(s[p]==e)
s[p]=r;
else
printf(“\n specified character is not existed”);
}

Result : The program executed successfully and it gives a right output.

Input: Enter the main string: Hi Hello


The given main string is: Hi Hello
Enter the present character: H
Enter the replace character: h
Enter the position: 3
Output: String after replace: Hi hello

Department of BS&H Page 228


NRI Institute of Technology, Pothavarappadu
Write a C program that uses functions to perform the following operations using
Structure:

i) Reading a complex number iii) Addition of two complex numbers

ii) Writing a complex number iv) Multiplication of two complex numbers

Aim: Program that uses functions to perform operations on complex numbers.

Source Code:
/***PROGRAM THAT USES FUNCTIONS TO PERFORM OPERATIONS ON COMPLEX NUMBERS***/

#include<stdio.h>
#include<conio.h>
struct complex
{
int re;
int im;
};
typedef struct complex complex;
void main()
{
complex Read();
void Write(complex);
complex Add(complex,complex);
complex Mul(complex,complex);
complex c1,c2,a,m;
clrscr();
c1=Read();
Write(c1);
c2=Read();
Write(c2);
printf("\n Addition of two complex numbers\n\t");
a=Add(c1,c2);
Write(a);
printf("\n Multiplication of two complex numbers\n\t");
m=Mul(c1,c2);
Write(m);
getch();
}
complex Read()
{
complex c;
printf("\n Enter Real part and Imaginary part of a Complex No:"); scanf("%d
%d",&c.re,&c.im);
return c;

Department of BS&H Page 229


NRI Institute of Technology, Pothavarappadu
}
void Write(complex c)
{
printf("\n The Complex No is :%d+i%d",c.re,c.im);
}
complex Add(complex x,complex y)
{
complex z;
z.re=x.re+y.re;
z.im=x.im+y.im;
return z;
}
complex Mul(complex x,complex y)
{
complex z;
z.re=(x.re*y.re-x.im*y.im);
z.im=(x.re*y.im+x.im*y.re);
return z;
}
Result : The program executed successfully and it gives a right output.

Input:
Enter Real part and Imaginary part of a Complex No:2 3
The Complex No is: 2+i3
Enter Real part and Imaginary part of a Complex No:1 2
The Complex No is: 1+i2

Output:
Addition of two complex numbers
The Complex No is: 3+i5
Multiplication of two complex numbers
The Complex No is:-4+i7

Department of BS&H Page 230


NRI Institute of Technology, Pothavarappadu
Write C Programs for the following string operations without using the built in functions
- to concatenate two strings

Aim: Write C Programs for the following string operations without using the built in functions
- to concatenate two strings

Source Code:

/******************PROGRAM TO CONCATENATE TWO STRINGS ***************/

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[25],str2[25];
int i=0,j=0;
clrscr();
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0') i+
+;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
getch();
}

Result : The program executed successfully and it gives a right output.

Input:
Enter First String:surya
Enter Second String:kiran

Output:
Concatenated String is suryakiran

Write C Programs for the following string operations without using the built in functions
D- epartment
to appendofaBS&H
string to another string. Page
231
Aim: Write C Programs for the following string operations without using the built in functions
to append a string to another string.

Source Code:

/******************PROGRAM TO APPEND A STRING TO ANOTHER STRING ************/

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[25],str2[25],str3[25];
int i=0,j=0;
clrscr();
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
{
str3[i]=str1[i];
i++;
}
while(str2[j]!='\0')
{
str3[i]=str2[j];
j++;
i++;
}
str3[i]='\0';
printf("\nappended String is %s",str3);
getch();
}

Result : The program executed successfully and it gives a right output.


Input:
Enter First String:surya
Enter Second String:kiran
Output:
appended String is suryakiran

Write C Programs for the following string operations without using the built in functions
Department of BS&H Page
232
Aim: Write C Programs for the following string operations without using the built in functions
- to compare two strings
Source Code:
/** Program to Compare Two Strings Without using strcmp() **/
#include<stdio.h>
void main()
{
char string1[25],string2[25];
int i,temp = 0;
printf("Enter the string1: \n");
gets(string1);
printf("\nEnter the String2: \n");
gets(string2);
for(i=0; string1[i]!='\0'; i++)
{
if(string1[i] == string2[i])
temp = 1;
else
temp = 0;
}
if(temp == 1)
printf("Both strings are same.");
else
printf("Both strings are not same.");
getch();
}

Result : The program executed successfully and it gives a right output.

Input:
Enter the string1: never
Enter the String2: quit
Output:
Both strings are not same

Department of BS&H Page 233


NRI Institute of Technology, Pothavarappadu
Write C Programs for the following string operations without using the built in functions- to find

Aim: Write C Programs for the following string operations without using the built in functions
- to find the length of a string

Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[25];
int len=0;
clrscr();
printf("\nEnter String whose length is to be
found:"); gets(str1);
while(str1[len]!='\0')
len++;
printf("\nLengt of the String %s is %d",str1,len);
getch();
}

Result : The program executed successfully and it gives a right output.

Input:
Enter string whose length is to be found: surya kiran

Output:
Length of the string surya kiran is 11

Department of BS&H Page 234


NRI Institute of Technology, Pothavarappadu
Write C Programs for the following string operations without using the built in functions- to find

AIM: To find whether a given string is palindrome or not

Source code:
#include <stdio.h>
#include <conio.h>
void main()
{
char text[100];
int begin, middle, end, length = 0;
clrscr();
printf("Enter any string:" );
gets(text);
while ( text[length] != '\0' )
length++;
end = length - 1;
middle = length/2;
for( begin = 0 ; begin < middle ; begin++ )
{
if ( text[begin] != text[end] )
{
printf("Not a palindrome.\n");
break;
}
end--;
}
if( begin == middle )
printf("Palindrome.\n");
getch();
}

Result : The program executed successfully and it gives a right output.

Input:
Enter any string: madam

Output:
Palindrome.

Department of BS&H Page 235


NRI Institute of Technology, Pothavarappadu
Write a C functions to find both the largest and smallest number of an array of integers

Aim: Program to find both the largest and smallest number of an array of integers.

Source Code:

/******* PROGRAM TO FIND BOTH THE LARGEST AND SMALLEST NUMBER OF AN ARRAY OF
INTEGERS ********/

#include<stdio.h>
#include<conio.h>
void main()
{

int n,i,a[100],max=0,min=0;
clrscr();
printf("\n Enter size of array");
scanf("%d",&n);
printf("\n Enter elements of array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
max=fmax(a,n);
min=fmin(a,n);
printf("\n %d is the max no of array\n",max);
printf("\n %d is the min no of array",min);
getch();
}
int fmax(int a[],int n)
{
int max=a[0],i;
for(i=1;i<n;i++)
{
if(max<a[i])
{
max=a[i];
}
}
return (max);
}
int fmin(int a[],int n)
{
int min=a[0],i;
for(i=1;i<n;i++)
{
if(min>a[i])

Department of BS&H Page 236


NRI Institute of Technology, Pothavarappadu
{
min=a[i];
}
}
return (min);
}

Result : The program executed successfully and it gives a right output.

Input:
Enter size of array: 4
Enter elements of array
4 1 3 8
Output:
8 is the max no of
array 1 is the min no
of array

Department of BS&H Page 237


NRI Institute of Technology, Pothavarappadu
Write C programs illustrating call by value and call by reference concepts.

AIM: Write C programs illustrating call by value

Source code:
#include<stdio.h>
#include<conio.h>
void interchange(int number1,int number2)
{
int temp;
temp = number1;
number1 = number2;
number2 = temp;
}

void main()
{
int num1,num2;
clrscr();
printf("Enter num1 and num2 values");
scanf("%d%d",&num1,&num2);
interchange(num1,num2); printf("\
nNumber 1 : %d",num1); printf("\
nNumber 2 : %d",num2); getch();
}

Result : The program executed successfully and it gives a right output.

Input:
Enter num1 and num2
values: 510
Output:
Number 1 : 5
Number 2 : 10

Write C programs illustrating call by value and call by reference concepts.

Department of BS&H Page


238
AIM: Write C programs illustrating call by Reference

Source code:
#include<stdio.h>
#include<conio.h>
void interchange(int *num1,int *num2)
{
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}
void main()
{
int num1,num2;
clrscr();
printf("Enter num1 and num2 values");
scanf("%d%d",&num1,&num2);
interchange(&num1,&num2); printf("\
nNumber 1 : %d",num1); printf("\
nNumber 2 : %d",num2); getch();
}

Result : The program executed successfully and it gives a right output.

Input:
Enter num1 and num2
values: 510
Output:
Number 1 : 10
Number 2 : 5

Department of BS&H Page 239


NRI Institute of Technology, Pothavarappadu
Write C programs that use both recursive and non-recursive functions for the following
i) To find the factorial of a given integer.

AIM: Write C programs that use both recursive and non-recursive functions for the following
i) To find the factorial of a given integer.
Source code:
/*FACTORIAL IN BOTH RECURSIVE AND NON RECURSIVE*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,ans1=0,ans2=0;
clrscr();
printf("enter a number");
scanf("%d",&n);
ans1=factR(n);
ans2=factNR(n);
printf("factorial of %d using recursive is %d\
n",n,ans1); printf("factorial of %d using non
recursive is %d",n,ans2);
getch();
}

int factR(int n)
{
if(n==0)
return 1;
else
return (n*factR(n-1));
}
int factNR(int n)
{
int i,fac=1;
for(i=1;i<=n;i++)
fac=fac*i;
return fac;
}
Result : The program executed successfully and it gives a right output.
Input:
Enter a number: 5
Output:
Factorial of 5 using recursive is 120
Factorial of 5 using non recursive is
120

Write C programs that use both recursive and non-recursive functions for the following
Department of BS&H Page
ii)
240To find the GCD (greatest common divisor) of two given integers.
Aim: To find the GCD (greatest common divisor) of two given
integers. Source code:
/*G.C.D IN BOTH RECURSIVE AND NON RECURSIVE*/
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,ans1=0,ans2=0;
clrscr();
printf("enter a,b");
scanf("%d %d",&a,&b);
ans1=GCDR(a,b);
ans2=GCDNR(a,b);
printf("GCD of %d & %d using recursive is %d\
n",a,b,ans1); printf("GCD of %d & %d using non recursive
is %d",a,b,ans2); getch();
}
int GCDR(int a,int b)
{
if(a%b==0)
return b;
else
return GCDR(b,a%b);
}

int GCDNR(int a,int b)


{
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
return a;
}
Result : The program executed successfully and it gives a right output.
Input:
Enter a, b 12 16
Output:
GCD of 12 & 16 using recursive is 4
GCD of 12 & 16 using non recursive
is 4

Write C programs
Department that use both recursive and non-recursive functions for the
of BS&H Pagefollowing
241
iii) To find Fibonacci sequence
Aim: To find Fibonacci sequence

Source code:
/* Fibonacci Series non recursive */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,c,i=0;
clrscr();
printf("Enter the number of terms\
n"); scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
printf("result by non recursive function is:\n");
nrfib(n);
printf("result by recursive function is:\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", rfib(i));
i++;
}

getch();
}

nrfib(int n)
{
int first=0,second=1,next,c=0;
for(c=0;c<n;c++)
{
if ( c <= 1
) next =
c;
else
{
next = first + second;
first = second;
second = next;
}
Department of BS&H Page 242
NRI Institute of Technology, Pothavarappadu
printf("%d\n",next);
}
}

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

Result : The program executed successfully and it gives a right output.

Input:
Enter the number of terms 5
Output:
First 5 terms of Fibonacci series are:-
Result by non recursive function is: 0 1 1 2 3
Result by recursive function is: 0 1 1 2 3

Department of BS&H Page 243


NRI Institute of Technology, Pothavarappadu
a) Write C Program to reverse a string using pointers

Aim: Write C Program to reverse a string using pointers


Source code:
#include<stdio.h>
#include<conio.h>
void rev(char *);
void main()
{
char string[50];
clrscr();
printf("Enter a string to be reversed:");
gets(string);
printf("Before reversing the string %s\t",string);
rev(string);
printf("\nReverse String is %s",string);
getch();
}
void rev(char *str)
{
char *str1,temp;
str1=str; while(*str1!='\
0') str1++;
str1--;
while(str<str1)
{
temp=*str;
*str=*str1;
*str1=temp;
str++;
str1--;
}
}
Result : The program executed successfully and it gives a right output.
Input:
Enter a string to be reversed: SURYA
Output:
Before reversing the string SURYA
Reverse string is AYRUS

Department of BS&H Page 244


NRI Institute of Technology, Pothavarappadu
Aim: Write a C Program to compare two arrays using pointers
Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],x,y,i,flag=0,*p,*q;
clrscr();
printf("enter size of array A and B : ");
scanf("%d%d",&x,&y);
if(x==y)
{
printf("enter %d array elemnts of A:
",x); for(i=0;i<x;i++)
scanf("%d",&a[i]);
printf("\n enter %d array elemnts of b:
",y); for(i=0;i<y;i++)
scanf("%d",&b[i]);
p=a;
q=b;
for(i=0;i<x;i++)
{
if(*p++!=*q++)
{
flag=1;
}
}
if(flag==0)
printf("equal");
else
printf("not equal");
}
else
{
printf("number of array elements are not equal");
}
getch();
}
Result : The program executed successfully and it gives a right output.
Input:
Enter size of array A and B : 3 3
Enter 3 array elements of A : 1 2 3
Enter 3 array elements of B : 1 4 3
Output:
Not Equal

Department of BS&H Page 245


NRI Institute of Technology, Pothavarappadu
a) Write a C program consisting of Pointer based function to exchange value of two integers usin

Aim: Write a C program consisting of Pointer based function to exchange value of two integers using
passing by address.

Source code:
#include<stdio.h>
#include<conio.h>
main()
{
int x,y;
void swap(int *a,int
*b); clrscr();
printf("enter value of
x,y"); scanf("%d
%d",&x,&y);
swap(&x,&y);
printf("the value of x,y in main are %d
%d",x,y); getch();
}
void swap(int *a,int *b)
{
int *temp=0;
*temp=*a;
*a=*b;
*b=*temp;
printf("the value of x,y in functions are %d %d\
n",*a,*b); getch();
}
Result : The program executed successfully and it gives a right output.

Input:
Enter value of x, y: 4 5
Output:
The value of x, y in functions are 5 4
The value of x, y in main are 54

Department of BS&H Page


246
a) Write a C program to swap two numbers using pointers
.
Aim: Write a C program to swap two numbers using pointers

Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,*a,*b,temp;
clrscr();
printf("Enter the value of x and y\
n"); scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
printf("After Swapping\nx = %d\ny = %d\n", x, y);
getch();
}

Result : The program executed successfully and it gives a right output.

Input:
Enter the value of x and y: 2 3
Output:
Before swapping
X=2
Y=3
After
swapping X=3
Y=2

Department of BS&H Page 247


NRI Institute of Technology, Pothavarappadu
Examples which explores the use of structures, union and other user defined variables.

Aim Program to explain use of structures


Source Code:
/*PROGRAM TO EXPLAIN THE USE OF structures */
#include<stdio.h>
struct student
{
int rollno;
char name[10];
int m1;
int m2;
int m3;
};
void main()
{
struct student s;
int total;
clrscr();
printf("Enter the rollno");
scanf("%d",&s.rollno);
printf("Enter the name");
scanf("%s",s.name);
printf("Enter marks in m1");
scanf("%d",&s.m1);
printf("Enter marks in m2");
scanf("%d",&s.m2);
printf("Enter marks in m3");
scanf("%d",&s.m3);
total=s.m1+s.m2+s.m3;
printf("Student No:%d\n",s.rollno);
printf("Student Name:%s\n",s.name);
printf("Marks(M1):%d\n",s.m1);
printf("Marks(M2):%d\n",s.m2);
printf("Marks(M3):%d\n",s.m3);
printf("Total:%d",total);
getch();
}
Result : The program executed successfully and it gives a right output.
Input:
Enter the Rollno: 201
Enter the Name: krishna
Enter marks in m1: 55
Enter marks in m2: 45
Enter marks in m3: 67
Output:
Student Rollno: 201
Student Name: krishna
Marks(M1): 55
Marks(M2): 45
Marks(M3): 67
Total: 167

Department of BS&H Page 248


NRI Institute of Technology, Pothavarappadu
Aim: Program to explain use of union

Source Code:
/*PROGRAM TO EXPLAIN THE USE OF union */

#include<stdio.h>
union employee
{
float empid;
char ename[10];
float basic;
};
void main()
{
union employee e;
clrscr();
printf("Enter the Empid:");
scanf("%f",&e.empid);
printf("Employee Id:%f\n",e.empid);
printf("Enter the Employee name:");
scanf("%s",e.ename);
printf("Employee Name:%s\n",e.ename);
printf("Enter basic pay:");
scanf("%f",&e.basic);
printf("Basic Pay:%f\n",e.basic);
getch();
}

Result : The program executed successfully and it gives a right output.


Input:
Enter the Empid: 21
Enter the Employee name: Krishna
Enter basic pay: 2344
Output:
Employee Id: 21
Employee Name: Krishna
Basic Pay: 2344.00

Department of BS&H Page 249


NRI Institute of Technology, Pothavarappadu
Aim: Program to explain use of enum

Source Code:
/*PROGRAM TO EXPLAIN THE USE OF enum */

#include<stdio.h>
void main()
{ enum{monday, tuesday, wednesday, thursday, friday, saturday, sunday}
day; day = saturday;
if(day == saturday || day == sunday)
printf( "Day is a weekend day");
else if(day == wednesday)
printf("Day is hump day - middle of the work week");
getch();
}

Result : The program executed successfully and it gives a right output.


Output:
Day is a weekend day

Write a C program
Department which copies one file to another.
of BS&H Page
250
Aim: Program to copy one file to another using files.
Source Code:
/*PROGRAM TO COPY ONE FILE TO ANOTHER */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(int arg,char *arr[])
{
FILE *fs,*ft;
char ch;
clrscr();
if(arg!=3)
{
printf("Argument Missing ! Press key to exit.");
getch();
exit(0);
}
fs = fopen(arr[1],"r");
if(fs==NULL)
{
printf("Cannot open source file ! Press key to exit.");
getch();
exit(0);
}

ft = fopen(arr[2],"w");
if(ft==NULL)
{
printf("Cannot copy file ! Press key to exit.");
fclose(fs);
getch();
exit(0);
}
while(1)
{
ch = getc(fs);
if(ch==EOF)
{
break;
}
else
putc(ch,ft);
}
printf("File copied succesfully!");
fclose(fs);
fclose(ft);
}
Result : The program executed successfully and it gives a right output.

Output:
d:\> filecopy file1.txt file2.txt

Department of BS&H Page 251


NRI Institute of Technology, Pothavarappadu
b) Write a C program to count the number of characters and number of lines in a file.

Aim: Program to count the number of characters and number of lines in a file.

Source Code:

/*********PROGRAM TO FIND SUBSTRING IN MAIN STRING USING FUNCTIONS**********/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int linescount(char);
int wordscount(char);
int charcount(char)
char text[100];
int l=0,w=0,c=0;
clrscr();
printf("\n Enter a text:\n");
scanf("%[^z]s",text);
l=linescount(text);
w=wordscount(text);
c=charcount(text);
printf("%d are no of lines\n%d are no of words\n %d are no of characters\
n",l,w,c);
getch();
}
int linescount(char text[])
{
int l=0,i;
char ch;
for(i=0;text[i]!='\0';i++)
{
ch=text[i];
if(ch=='\n')
{
l++;
}
}
return(l);
}
int wordscount(char text[])
{
int w=0,i;
char ch;
for(i=0;text[i]!='\0';i++)
{
ch=text[i];
if((ch==' ')||(ch=='\n'))
{ w+
+;
}
}
Department of BS&H Page 252
NRI Institute of Technology, Pothavarappadu
return(w);
}
int charcount(char text[])
{
int i;
char ch;
for(i=0;text[i]!='\0';i++)
{
ch=text[i];
}
return(i);
}

Result : The program executed successfully and it gives a right output.


Input:
Enter a text: this is some thing
Hi
Hello z
Output:
no of lines 3
no of words 6
no of characters 28

c) Write a C Program to merge two files into a third file. The names of the files must be entered

Department of BS&H Page


NRI Institute of Technology, Pothavarappadu
Aim: To merge two files into a third file. The names of the files must be entered
using command line arguments. Source code:
#
i
n
c
l
u
d
e

<
s
t
d
i
o
.
h
>

#
i
n
c
l
u
d
e
<
c
o
n
i
o
.
h
>

#
i
n
c
l
u
d
e

<
s
t
d
l

NRI Institute of Technology, Pothavarappadu


i
b
.
h
>
void main(int arg,char *arr[])
{
F
I
L
E

*
f
s
1
,

*
f
s
2
,

*
f
t
;

c
h
a
r

c
h
;
c
l
r
s
c
r
(
)
;

i
f
(
a
r
g

NRI Institute of Technology, Pothavarappadu


!
=
4
)
{
printf("argument missing
! press key to exit.");
getch();
exit(0);
}
fs1 = fopen(arr[1],"r");
fs2 = fopen(arr[2],"r");
if( fs1 == NULL || fs2 == NULL )
{
perror("Error ");
printf("Press
any key to
exit...\n");
getch();
exit(EXIT_FAILURE);
}
f
t
=

f
o
p
e
n
(
a
r
r
[
3
]
,
"
w
"
)
;
i
f
(
f
t
=
=

N
U

NRI Institute of Technology, Pothavarappadu


L
L

)
{
perror("Error ");
printf("Press any key to exit...\
n"); exit(EXIT_FAILURE);
}
while( ( c
h=
fgetc(fs1)
) != EOF
)
fputc(ch,
ft);
while( ( c
h=
fgetc(fs2)
) != EOF
)
fputc(ch,
ft);
printf("Two files were merged into %s file
successfully.\n",file3); fclose(fs1);
f
c
l
o
s
e
(
f
s
2
)
;

f
c
l
o
s
e
(
f
t
)
;
}
Result : The program executed successfully
and it gives a right output. Input: open
command prompt.. move to your program location..

NRI Institute of Technology, Pothavarappadu


C:\software\TC>PROGNAME File1.txt File2.txt File3.txt
Output:
Two files were merged into File3.txt file successfully

NRI Institute of Technology, Pothavarappadu

You might also like

pFad - Phonifier reborn

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

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


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy