Experiments
Experiments
UNIT-1
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
Tutorial-2
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
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:
#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;
Input:
Enter the values of a,b and c:1 2 3
Output:
The Area is:0.000000
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();
}
Source Code:
/* */
#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
Source Code:
#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();
}
Input:
Enter Binary no: 11100
Output:
2’s complement for the given binary no is: 00100
Source Code:
#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();
}
Input:
Enter the values of a,b,c: 1 -5 6
Output:
The roots of quadratic equation are 3.000000 2.000000
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
Source Code:
#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();
}
Input:
Enter positive integer:345
Output:
The sum of digits of givem number =12
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();
}
Input:
Enter the number of elements :5
Output:
The fibonacci series is :
0 1 1 2 3
Aim: Program to find the value SIN(x) using summing series method.
Source Code:
#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));
}
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
Aim: Program to find the value COS(x) using summing series method.
Source Code:
#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));
}
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
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();
}
Input:
Enter the value of x: 2
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();
}
Input:
Enter the range : 10
Output:
2 3 5 7
Aim: Program to print the multiplication table of a given number n up to a given value.
Source Code:
#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();
}
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
Source Code:
#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();
Input:
Enter the value of n: 32767
Output:
32767 in binary number system are: 0111111111111111
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
Aim: Program to interchange the largest and smallest numbers in the array.
Source Code:
#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
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)
#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
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
Source Code:
#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");
}
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++)
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
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;
}
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
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”);
}
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;
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
Aim: Write C Programs for the following string operations without using the built in functions
- to concatenate two strings
Source Code:
#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();
}
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:
#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();
}
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();
}
Input:
Enter the string1: never
Enter the String2: quit
Output:
Both strings are not same
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();
}
Input:
Enter string whose length is to be found: surya kiran
Output:
Length of the string surya kiran is 11
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();
}
Input:
Enter any string: madam
Output:
Palindrome.
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])
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
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();
}
Input:
Enter num1 and num2
values: 510
Output:
Number 1 : 5
Number 2 : 10
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();
}
Input:
Enter num1 and num2
values: 510
Output:
Number 1 : 10
Number 2 : 5
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);
}
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) );
}
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
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
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();
}
Input:
Enter the value of x and y: 2 3
Output:
Before swapping
X=2
Y=3
After
swapping X=3
Y=2
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();
}
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();
}
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
Aim: Program to count the number of characters and number of lines in a file.
Source Code:
#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);
}
c) Write a C Program to merge two files into a third file. The names of the files must be entered
<
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
*
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
f
o
p
e
n
(
a
r
r
[
3
]
,
"
w
"
)
;
i
f
(
f
t
=
=
N
U
)
{
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..