Page |1
Exercise 1
a)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.
#include<stdio.h>
#include<math.h>
main()
int a,b,c;
float s,Area;
printf("\n enter three vlaues ");
scanf("%d%d%d",&a,&b,&c);
s=(a+b+c)/2;
Area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\n Area = %f",Area);
Output :
Enter three values 2 2 2
Area = 1.732
b) Write a C-program to find largest of three numbers using ternary operator .
#include<stdio.h>
main()
int a,b,c,large;
clrscr();
Page |2
printf("enter a,b,c values ");
scanf("%d%d%d",&a,&b,&c);
large= (a>b) ? (a>c ? a : c) : (b>c ? b : c) ;
printf("largest among three numbers is %d ",large);
Output :
Enter a,b,c values 2 4 3
largest among three numbers is 4
c) Write a c-program to swap two numbers without using temporary variable.
#include<stdio.h>
main()
int a,b;
printf("\n enter two values ");
scanf("%d%d",&a,&b);
printf("\n Before swapping a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\n After swapping a=%d b=%d",a,b);
Output:Enter two values 2 3
Before swapping a=2 b=3
After swapping a=3 b=2
Page |3
Exercise 2
2a) 2’s complement of a number is obtained by scanning it from right to left and
complementing all the bits after the first appearance of a 1. Thus 2’s complement of
11100 is 00100. Write a C program to find the 2’s complement of a binary number.
/* C program to find the 2’s complement of a binary number*
#include<stdio.h>
main()
{
int a[10],i,n,pos;
printf("enter no of bits\n");
scanf("%d",&n);
printf("enter binary number\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=n-1;i>=0;i--)
{
if(a[i]==1)
{
pos=i;
break;
}
}
printf("the complement of binary number is \n");
for(i=0;i<n;i++)
{
if(i<pos)
{
if(a[i]==1)
{
a[i]=0;
printf("%d ",a[i]);
}
else
{
a[i]=1;
printf("%d ",a[i]);
}
}
Page |4
else
printf("%d ",a[i]);
}
}
Output:
enter no of bits
5
enter binary number
01010
the complement of binary number is
10110
2b)/* Roots of a quadratic equation */
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c,t;
double r1,r2;
printf("\n------ROOTS OF A QUADRATIC EQUATION------\n");
printf("\n enter the values of a,b,c\t");
scanf("%f%f%f",&a,&b,&c);
t=pow(b,2)-4*a*c;
if(t>=0 && a!=0)
{
r1=(-b+sqrt(t))/(2*a);
r2=(-b-sqrt(t))/(2*a);
printf("roots are r1=%lf\t r2=%lf",r1,r2);
}
else
printf("\tRoots are complex");
}
OUTPUT :
------ROOTS OF A QUADRATIC EQUATION------
enter the values of a,b,c 1 2 1
roots are r1=-1.000000 r2=-1.000000
Page |5
/*2C) Arithmetic operations using switch-case */
#include<stdio.h>
main()
int n1,n2,res;
float div;
char op;
printf("\n Enter two integers\t");
scanf("%d%d",&n1,&n2);
printf("\n Enter an operator(+,-,*,/,%)\t");
op=getchar();
switch(op)
case '+': res=n1+n2;
printf("\n ADDITION %2d+%2d=%2d",n1,n2,res);
break;
case '-': res=n1-n2;
printf("\n SUBSTRACTION %2d-%2d=%2d",n1,n2,res);
break;
case '*': res=n1*n2;
printf("\n MULTIPLICATION %2d*%2d=%2d",n1,n2,res);
break;
case '/': div=(float)n1/n2;
printf("\n DIVISION %2d/%2d=%f",n1,n2,div );
break;
case '%': res=n1%n2;
printf("\n MODULARDIVISON %d%%%d=%d",n1,n2,res);
Page |6
break;
default: printf("\n INVALID OPERATOR");
Output: Output:
Enter two integers 45
Enter an operator(+,-,*,/,%) *
MULTIPLICATION 4* 5=20
Exercise 3 a) Write a C program to find the sum of individual digits of a positive integer
and reverse of a give number.
3a) /* sum of individuals digits of a positive integer and reverse of given number */
#include<stdio.h>
#include<conio.h>
void main()
{
unsigned int n,sum=0,r,rev=0;
clrscr();
printf("\n--------SUM OF INDIVIDUAL DIGITS--------\n");
printf("\n Enter a positive number\t");
scanf("%u",&n);
while(n>0)
{
r=n%10;
sum+=r;
rev=rev*10+r;
n=n/10;
}
printf("\n sum of individual digits is %u",sum);
printf("\n reverse of given number is %u",rev);
getch();
}
OUTPUT:
--------SUM OF INDIVIDUAL DIGITS--------
Enter a positive number 123
sum of individual digits is 6
Page |7
reverse of given number is 321
3b) A Fibonacci sequence is defined as follows: the first and second terms in the
sequence are 0 and 1. Subsequent terms are found by adding the preceding two terms
in the sequence.
Write a C program to generate the first n terms of the sequence.
#include<stdio.h>
#include<conio.h>
main()
{
int f1=0,f2=1,f3=0,n,i=3;
clrscr();
printf("enter the n value\n");
scanf("%d",&n);
printf("%d\t%d",f1,f2);
while(i<=n)
{
f3=f1+f2;
printf("\t%d",f3);
f1=f2;
f2=f3;
i++;
}
getch();
}
OUTPUT:
enter the n value
6
0 1 1 2 3 5
3c) Write a C program to generate all the prime numbers between 1 and n, where n is
a value supplied by the user.
#include<stdio.h>
#include<conio.h>
main()
{
Page |8
unsigned int n,N,np,i;
clrscr();
printf("\n------PRIME NUMBERS BETWEEN 1 TO N-------\n");
printf("\n Enter a positive integer\t");
scanf("%u",&N);
printf("\n prime numbers from 1 to %d\n",N);
for(n=1;n<=N;n++)
{
np=0;
for(i=2;i<=(n/2);i++)
{
if(n%i= =0)
{
np++;
break;
}
}
if(np= =0)
printf("%u\t",n);
}
getch();
}
OUTPUT:
------PRIME NUMBERS BETWEEN 1 TO N-------
Enter a positive integer 9
prime numbers from 1 to 9
2 3 5 7
Exercise 4
a)Write a c-program to print the multiplication table of a given number n upto a given
value.
#include<stdio.h>
main()
{
int n,i,r;
printf("\n enter n value ");
scanf("%d",&n);
Page |9
printf("\n Multiplication Table");
for(i=1;i<=n;i++)
{
r=n*i;
printf("\n %d * %d = %d",n,i,r);
}
}
Output:
Enter n value 3
Multiplication Table
3*1=3
3*2=6
3*3=9
b)Write a c-program to enter a decimal number and calculate and display the binary
equivalent of that number.
#include<stdio.h>
void main()
{
int n,i,a[50],count=0;
printf("\n enter a value ");
scanf("%d",&n);
printf("\n binary equivalent of given number\n");
while(n!=0)
{
a[count++]=n%2;
n=n/2;
}
for(i=count-1;i>=0;i--)
printf("%d ",a[i]);
}
Output :
Enter a value 5
Binary equivalent of given number
101
P a g e | 10
c)Write a c-program to check whether the given number is Amstrong number or not.
#include<stdio.h>
main()
{
int n,num,r,sum=0;
printf("\n enter a number ");
scanf("%d",&n);
num=n;
do
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}
while(n>0);
if(num==sum)
printf("\n given number is Armstrong ");
else
printf("\n given number is not Armstrong ");
}
Output : Enter a number 153
Given number is Armstrong.
Exercise 5a)Write a c-program to interchange the largest and smallest numbers in the
array.
#include<stdio.h>
main()
int a[20],i,n,max,min,temp,minpos,maxpos;
clrscr();
printf("\n enter array size ");
scanf("%d",&n);
printf("\n enter array elements ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
P a g e | 11
printf("\n Before interchange largest and smallest elements in the array \n ");
for(i=0;i<n;i++)
printf("%d ", a[i]);
max=min=a[0];
for(i=0;i<n;i++)
if(min>a[i])
min=a[i];
minpos=i;
if(max<a[i])
max=a[i];
maxpos=i;
printf("\n max=%d at %d ",max,maxpos);
printf("\n min=%d at %d ",min,minpos);
temp=a[maxpos];
a[maxpos]=a[minpos];
a[minpos]=temp;
printf("\n after interchange largest and smallest elements in array\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
Output: enter array size 4
P a g e | 12
Enter array elements 4 5 3 2
Before interchange largest and smallest elements in array
4532
After interchange largest and smallest elements in array
4 235
5 b) Write a c-program to implement Linear search .
#include<stdio.h>
main()
int a[10],i,n,key,flag=0,pos;
printf("\n enter n value ");
scanf("%d",&n);
printf("\n enter %d array elements",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n enter key element to find ");
scanf("%d",&key);
for(i=0;i<n;i++)
if(key==a[i])
flag=1;
pos=i;
break;
P a g e | 13
if(flag==1)
printf("\n key element is found at %d",pos);
else
printf("\nkey element not found ");
Outut : Enter n value 5
Enter 5 elements
56437
Enter key element 4
Key element is found at 2
5c) Write a c-program to implement binary search .
#include<stdio.h>
main()
int a[10],n,i,beg,end,mid,key;
clrscr();
printf("\n enter size ");
scanf("%d",&n);
printf("\n enter array elements in ascending order ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n enter key element ");
P a g e | 14
scanf("%d",&key);
beg=0;
end=n-1;
mid=(beg+end)/2;
while(beg<=end && key!=a[mid])
if(key < a[mid])
end=mid-1;
else
beg=mid+1;
mid=(beg+end)/2;
if(key==a[mid])
printf(" \n %d is found at %d ",key,mid);
else
printf("\n not found ");
getch();
Output:enter size 5
Enter array elements in ascending order 1 2 3 4 5
Enter key element 4
4 is found at 3
Exercise 6
a) Write a c-program to implement Sorting of array of elements .
#include<stdio.h>
main()
P a g e | 15
int a[30],n,t,i,j;
printf("\n enter n value ");
scanf("%d",&n);
printf("\n enter %d elements ",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])
t=a[i];
a[i]=a[j];
a[j]=t;
printf("\n Array elements after sorting ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
Output:
Enter n value 5
Enter 5 elements
P a g e | 16
52431
Array elements after sorting 1 2 3 4 5
b)Write a c-program to input mxn matrices .check the compatibility and perform
addition and multiplication of them.
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],m,n,p,q,i,j,k,ch;
clrscr();
printf("\n menu \n");
printf("\n 1 . Matrix Addition ");
printf("\n 2. Matrix Multiplication ");
printf("\n enter choice ");
scanf("%d",&ch);
printf("\n enter order of 1st matrix ");
scanf("%d%d",&m,&n);
printf("\n enter order of 2nd matrix ");
scanf("%d%d",&p,&q);
if((m==p && n==q) || (n==p))
{
printf("\n enter elements of 1st matrix " );
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\n enter elements of 2nd matrix ");
for(i=0;i<p;i++)
for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
}
else
printf("\n wrong order ");
switch(ch)
{
case 1: printf("\n addition \n");
for(i=0;i<m;i++)
{
P a g e | 17
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
break;
case 2:printf("\n multiplication \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<p;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
printf("%d\t",c[i][j]);
}
printf("\n");
}
break;
default:printf("\n choice is invalid ");
}
getch();
}
Output : menu
1. Matrix addition
2. Matrix multiplication
Enter choice 1
Enter order of 1st matrix 2 2
Enter order of 2nd matrix 2 2
Enter elements of 1st matrix 1 2 3 4
Enter elements of 2nd matrix 1 2 3 4
Addition
24
68
P a g e | 18
Exercise 7Write a C program that uses functions to perform the following operations:
/* 7i) Insert substring from the position p */
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>
#define M 30
main()
{
char s[M],sub[M-10];
int n,p;
void insert(char [],char [],int);
printf("\n enter a string (1-%d): \t",M);
scanf(“%s”,s);
printf("\n enter substring(1-%d): \t",M-10);
gets(sub);
printf("\n enter the position to insert:\t");
scanf("%u",&p);
insert(s,sub,p-1);
printf("\n string after insertion is: %s",s);
}
void insert(char s[M],char sub[M-10],int p)
{
char t[M];
int i,j,l,sl,k;
l=strlen(s);
sl=strlen(sub);
if( (l+sl) >= M)
{
printf("\n cannot insert");
getch();
exit(1);
}
strcpy(t,s);
for(i=p,j=0; sub[j]!='\0' ;i++,j++)
{
s[i]=sub[j];
}
for(k=p; t[k]!= '\0'; k++,i++)
{
s[i]=t[k];
}
s[i]='\0';
return;
P a g e | 19
}
Output
enter a string (1-30): we friends
enter substring(1-20): are
enter the position to insert: 3
string after insertion is: we are friends
/* 7 ii)To delete n characters from position p in a given string */
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>
#define M 30
main()
{
char s[M];
int n,p;
void delete(char [],int,int);
clrscr();
printf("\n enter a string (1-%d): \t",M);
gets(s);
printf("\n enter the position:\t");
scanf("%d",&p);
printf("\n enter number of characters to delete:\t");
scanf("%d",&n);
delete(s,p-1,n);
printf("\n string after deletion is: %s",s);
getch();
}
void delete(char s[M],int p,int n)
{
int i,j,l;
l=strlen(s);
if( (p+n) > l)
{
printf("\n Invalid data");
getch();
exit(1);
P a g e | 20
}
for(i=p,j=p+n; s[j]!='\0' ;i++,j++)
{
s[i]=s[j];
}
s[i]='\0';
return;
}
Output
enter a string (1-30): welcome to bvcec
enter the position: 8
enter number of characters to delete: 9
string after deletion is: welcome
/* 7iii) Replace a single character from the given string */
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>
#define M 30
main()
{
char s[M],c;
int p;
void insert(char [],int,char);
clrscr();
printf("\n enter a string (1-%d): \t",M);
gets(s);
printf("\n enter the position in string:\t");
scanf("%d",&p);
printf("\n Enter new character:");
fflush(stdin);
scanf("%c",&c);
insert(s,p-1,c);
printf("\n string after new character insertion is: %s",s);
getch();
}
void insert(char s[M],int p,char c)
P a g e | 21
{
int i,l;
l=strlen(s);
if(l<p+1)
{
printf("\n cannot insert");
getch();
exit(1);
}
for(i=0;s[i]!='\0';i++)
{
if(i==p)
s[i]=c;
}
return;
}
Output
enter a string (1-30): sing
enter the position in string: 2
Enter new character:o
string after new character insertion is: song
Exercise 8
Write a C program that uses functions to perform the following operations using
Structure:
i) Reading a complex number ii) Writing a complex number
iii) Addition of two complex numbers iv) Multiplication of two complex numbers
/* 8. Operations on complex numbers */
#include <stdio.h>
#include <conio.h>
typedef struct
{
float real;
float img;
}COMPLEX;
main()
{
P a g e | 22
COMPLEX c1,c2,cadd,cmul;
void accept(COMPLEX*);
void write(COMPLEX );
COMPLEX add(COMPLEX ,COMPLEX );
void mul(COMPLEX ,COMPLEX ,COMPLEX *);
clrscr();
printf("\n---------OPERATIONS ON COMPLEX NUMBERS--------\n");
printf("\n enter first complex number");
accept(&c1);
printf("\n enter second complex number");
accept(&c2);
cadd=add(c1,c2);
mul(c1,c2,&cmul);
printf("\n first complex number \t");
write(c1);
printf("\n second complex number\t");
write(c2);
printf("\n addition of two complex numbers is\t");
write(cadd);
printf("\n multiplication of two complex numbers is \t");
write(cmul);
getch();
}
void accept(COMPLEX *cp)
{
printf("\n enter the real part\t");
scanf("%f",&cp->real);
printf("enter the imaginary part\t");
scanf("%f",&cp->img);
return;
}
void write(COMPLEX c)
{
printf("%.2f%+.2fi",c.real,c.img);
return;
}
COMPLEX add(COMPLEX c1,COMPLEX c2)
{
COMPLEX ca;
ca.real=c1.real+c2.real;
ca.img=c1.img+c2.img;
return(ca);
}
P a g e | 23
void mul(COMPLEX c1,COMPLEX c2,COMPLEX *cmp)
{
cmp->real=(c1.real)*(c2.real)-(c1.img)*(c2.img);
cmp->img=(c1.real)*(c2.img)+(c1.img)*(c2.real);
return;
}
Exercise 9 Write a c-program for the following string operations without using the
built in functions
/* a) To concatenate two strings. */
#include<stdio.h>
main()
char str1[20],str2[20],str3[20];
int i=0,j=0;
clrscr();
printf("\n enter a string ");
scanf("%s",str1);
printf("\n enter another string ");
scanf("%s",str2);
while(str1[i]!='\0')
str3[j]=str1[i];
i++;
j++;
i=0;
while(str2[i]!='\0')
P a g e | 24
str3[j]=str2[i];
i++;
j++;
str3[j]='\0';
printf("\n after concatenation %s",str3);
getch();
Output:
enter a string ram
enter another string sai
after concatenation ramsai
/* b) To append a string to another string. */
#include<stdio.h>
main()
char source[40],dest[40];
int i=0,j=0;
clrscr();
printf("\n enter source string ");
scanf("%s",source);
printf("\n enter destination string ");
P a g e | 25
scanf("%s",dest);
while(dest[i]!='\0')
i++;
while(source[j]!='\0')
dest[i]=source[j];
i++;
j++;
printf("\n destination string is %s" ,dest);
getch();
Output:
enter source string kamal
enter destination string nil
destination string is nilkamal
/* c)To compare two strings. */
/* compare two strings */
#include<stdio.h>
main()
char str1[20],str2[20];
int i=0,flag;
clrscr();
P a g e | 26
printf("\n enter a string ");
scanf("%s",str1);
printf("\n enter another string ");
scanf("%s",str2);
while(str1[i]!='0')
if(str1[i]==str2[i])
flag=1;
else if(str1[i]<str2[i])
flag=2;
break;
else
flag=0;
break;
i++;
if(flag==1)
printf("\n both strings are equal");
else if(flag==2)
printf("\n string1 comes before string2");
else
printf("\n string1 comes after string2");
P a g e | 27
getch();
Output:
enter a string ram
enter another string sai
string1 comes before string2
Exercise 10
Write a c-program for the following string operations with out using the built in functions
a) To find the length of a string.
b) To find whether a given string is palindrome or not.
/*10 a) To find the length of a string */
#include<stdio.h>
main()
char str[30],i;
printf("\n enter a string ");
gets(str);
i=0;
while(str[i]!='\0')
i++;
printf("\n length of the string is %d",i);
Output : enter a string welcome
Length of the string is 7
P a g e | 28
10 b) Checking whether the string is palindrome or not */
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define M 30
main()
{
char s[M];
int i,j,ne=0;
clrscr();
printf("\n enter a string:\t");
gets(s);
for(i=0,j=strlen(s)-1; j>=i;j--,i++)
{
if(s[i] != s[j])
{
ne++;
break;
}
}
if(ne==0)
printf("\n %s is palindrome",s);
else
printf("\n %s is not a palindrome",s);
getch();
}
Output:
enter a string: liril
liril is palindrome
Exercise 11
a) Write a C functions to find both the largest and smallest number of an array of
integers.
/* a) smallest and largest from a list of integers */
#include<stdio.h>
void maxarray(int[],int);
void minarray(int[],int);
P a g e | 29
int a[10],n,i;
main()
{
clrscr();
printf("\n enter no of elements");
scanf("%d",&n);
printf("\n enter array elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
maxarray(a,n);
minarray(a,n);
getch();
}
void maxarray(int a[],int n)
{
int max=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
}
printf("\n max element in given array is %d",max);
}
void minarray(int a[],int n)
{
int min=a[0];
for(i=1;i<n;i++)
{
if(min>a[i])
min=a[i];
}
printf("\n min element in given array is %d",min)
}
Output:
enter no of elements5
enter array elements2 1 3 5 4
max element in given array is 5
min element in given array is 1
P a g e | 30
c) Write a c-program illustrating call by value ad call by reference concepts.
/* call by value */
#include<stdio.h>
void inc(int,int);
void main()
{
int a,b;
clrscr();
printf("enter a,b values");
scanf("%d%d",&a,&b);
inc(a,b);
printf("\n actual parameters a=%d b=%d",a,b);
getch();
}
void inc(int x,int y)
{ x++;
y++;
printf("\n after incrementing formal parameters");
printf("\n formal parameters x=%d y=%d",x,y);
}
Output enter a.b values 4 5
after incrementing formal parameters
formal parameters x=5 y=6
actual parameters a=4 b=5
/* call by reference */
#include<stdio.h>
void inc(int *,int *);
void main()
{
int a,b;
clrscr();
printf("enter a,b values");
scanf("%d%d",&a,&b);
inc(&a,&b);
printf("\n actual paramaeters a=%d b=%d",a,b);
getch();
}
P a g e | 31
void inc(int *x,int *y)
{
++*x;
++*y;
printf("\n after incrementing formal parameters");
printf("\n formal parameters x=%d y=%d",*x,*y);
}
Output enter a.b values 4 5
after incrementing formal parameters
formal parameters x=5 y=6
actual parameters a=5 b=6
Exercise 12
a) Write C programs that use both recursive and non-recursive functions for the
following
/* i) factorial of a number (recursive and nonrecursive functions) */
#include <stdio.h>
#include <conio.h>
main()
{
unsigned int n;
unsigned long int nr,r;
unsigned long int fact_rec(unsigned int);
unsigned long int fact_nonrec(unsigned int);
clrscr();
printf("\n------FACTORIAL OF A NUMBER------\n");
printf("\n enter a number (positive integer) \t");
scanf("%u",&n);
nr=fact_nonrec(n);
printf("\n factorial (non recursive function) of %u is %lu ",n,nr);
r=fact_rec(n);
printf("\n\nfactorial (recursive function) of %u is %lu",n,r);
getch();
}
unsigned long int fact_nonrec(unsigned int n)
{
int i;
unsigned long int f=1;
for(i=n;i>=1;i--)
P a g e | 32
f=f*i;
return(f);
}
unsigned long int fact_rec(unsigned int n)
{
if(n==0)
return(1);
else
return(n*fact_rec(n-1));
}
Output:
------FACTORIAL OF A NUMBER------
enter a number (positive integer) 5
factorial (non recursive function) of 5 is 120
factorial (recursive function) of 5 is 120
/* ii) gcd of two numbers (recursive and nonrecursive functions) */
#include<stdio.h>
int rec_gcd(int,int);
int nonrec_gcd(int,int);
main()
{
int a,b;
clrscr();
printf("\n enter a,b values");
scanf("%d%d",&a,&b);
printf("\ngcd using recursion is %d",rec_gcd(a,b));
printf("\ngcd using nonrecursive is %d",nonrec_gcd(a,b));
getch();
}
int nonrec_gcd(int x,int y)
{
int r;
do
{
r=x%y;
P a g e | 33
x=y;
y=r;
}while(r!=0);
return x;
}
int rec_gcd(int x,int y)
{
int r;
if(y==0)
return x;
else
{
r=x%y;
return rec_gcd(y,r);
}
}
Output:
enter a,b values24 36
gcd using recursion is 12
gcd using nonrecursive is 12
-------------------------------------------------------------------------------------------------------------------
iii) /* to find Fibonacci sequence (recursive and nonrecursive functions) */
#include<stdio.h>
int rec_fib(int);
void nonrec_fib(int);
void main()
{
int n,i;
clrscr();
printf("\n enter n value");
scanf("%d",&n);
printf("\n FIBONACCI SERIES USING RECURSION \n");
for(i=1;i<=n;i++)
printf("%d ",rec_fib(i));
printf("\n FIBONACCI SERIES USING NON RECURSION \n");
nonrec_fib(n);
P a g e | 34
getch();
}
int rec_fib(int x)
{
if(x==1)
return 0;
else if(x==2)
return 1;
else
return rec_fib(x-1)+rec_fib(x-2);
}
void nonrec_fib(int x)
{
int i,f1=0,f2=1,f3;
printf("%d %d ",f1,f2);
for(i=3;i<=x;i++)
{
f3=f1+f2;
printf("%d ",f3);
f1=f2;
f2=f3;
}
}
Output: enter n value 5
FIBONACCI SERIES USING RECURSION
01123
FIBONACCI SERIES USING NON RECURSION
01123
Exercise 13
a) Write a c-program to reverse a string using pointers.
#include<stdio.h>
void main()
{
char str[20],*p;
int i=0;
clrscr();
printf("\n enter a string ");
scanf("%s",str);
P a g e | 35
while(str[i]!='\0')
i++;
p=&str[--i];
while(i>=0)
{
printf("%c",*p);
p--;
i--;
}
getch();
}
Output: enter a string karthik
kihtrak
b) Write a c-program to compare two arrays.
#include<stdio.h>
void main()
{
char str1[20],str2[20],*p,*q;
int flag=0;
clrscr();
printf("\n enter a string ");
scanf("%s",str1);
printf("\n enter a string ");
scanf("%s", str2);
p=&str1[0];
q=&str2[0];
while(*p!='\0'&&*q!='\0')
{
if(*p==*q)
flag=1;
else
{
flag=0;
break;
}
p++;
q++;
P a g e | 36
}
if(flag==1)
printf("\n both strings are equal ");
else
printf("\n both strings are not equal");
getch();
}
Output :enter a string ram
Enter a string ram
Both strings are equal
Exercise 14
/*14a) Pointer based function to exchange value of two integers using passing by
address */
#include<stdio.h>
void swap(int *,int *);
void (*ptr)(int *,int *);
void main()
{
int a,b;
clrscr();
printf("\n enter a,b values");
scanf("%d%d",&a,&b);
ptr=swap;
(*ptr)(&a,&b);
getch();
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
printf("\n after swappig x=%d y=%d",*x,*y);
}
Output: enter a,b values 4 5
After swapping x=5 y=4
P a g e | 37
/* b) Write a c-program to swap two numbers using pointers. */
#include<stdio.h>
void main()
{
int a,b,t,*x,*y;
clrscr();
printf("\n enter two values ");
scanf("%d%d",&a,&b);
x=&a;
y=&b;
t=*x;
*x=*y;
*y=t;
printf("\n After swapping x=%d y=%d",*x,*y);
getch();
}
Output: enter two values 4 5
After swapping x=5 y=4
Exercise 15
/*15a) write a program to define a structure and initialize its member variables*/
#include<stdio.h>
struct book
{
char name[20];
int pages;
float price;
};
main()
{
struct book b1={"cprogramming",250,200.00};
clrscr();
printf("\n book name %s",b1.name);
P a g e | 38
printf("\n no of pages %d",b1.pages);
printf("\n book price %f",b1.price);
getch();
}
Output:
book name c programming
no of pages 250
book price 200.000000
/*15b) Write a program to create user defined data type hours on int data type and
use it int he program */
#include<stdio.h>
#define H 60
main()
{
typedef int hours;
hours hrs;
clrscr();
printf("\n enter hours ");
scanf("%d",&hrs);
printf("\n minutes= %d",hrs*H);
printf("\n seconds= %d",hrs*H*H);
getch();
}
Output:
enter hours 2
minutes= 120
seconds= 7200
Exercise 16
/*16a) copies the contents of one file to another file */
#include<stdio.h>
#include<conio.h>
P a g e | 39
main()
{
char c,source[20];
FILE *fs,*fp;
clrscr();
printf("\n enter file name ");
gets(source);
fs=fopen(source,"r");
if(fs==NULL)
{
printf("source file cannot open ");
exit(0);
}
fp=fopen("dest.txt","w");
if(fp==NULL)
{
printf("dest file cannot open");
exit(0);
}
while((c=getc(fs))!=EOF)
{
putc(c,fp);
}
fclose(fp);
fp=fopen("dest.txt","r");
printf("\n copied contents are\n");
while((c=getc(fp))!=EOF)
{
putchar(c);
}
fclose(fs);
fclose(fp);
getch();
}
Output:
enter file name source.txt
copied contents are cprogramming
b) Write a c- program to count the number of characters and number of lines.
#include<stdio.h>
void main()
{
P a g e | 40
FILE *fp = fopen("myfile.txt","r");
int ch;
int charcount=0,linecount=1;
clrscr();
while(ch != EOF)
{
ch = fgetc(fp);
charcount++;
if( ch== '\n') linecount++;
}
fclose(fp);
printf("Total number of characters %d\n",charcount);
printf("Total number of lines %d\n",linecount);
getch();
}
Output:Total number of characters 28
Total number of lines 2
c)Write a c-program to merge two files
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
FILE *fs1, *fs2, *ft;
char ch, file1[20], file2[20], file3[20];
printf("Enter name of first file ");
gets(file1);
printf("Enter name of second file ");
gets(file2);
printf("Enter name of file which will store contents of two files ");
gets(file3);
fs1 = fopen(file1,"r");
fs2 = fopen(file2,"r");
P a g e | 41
if( fs1 == NULL || fs2 == NULL )
{
perror("Error ");
printf("Press any key to exit...\n");
getch();
exit(EXIT_FAILURE);
}
ft = fopen(file3,"w");
if( ft == NULL )
{
perror("Error ");
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while( ( ch = fgetc(fs1) ) != EOF )
fputc(ch,ft);
while( ( ch = fgetc(fs2) ) != EOF )
fputc(ch,ft);
printf("Two files were merged into %s file successfully.\n",file3);
fclose(fs1);
fclose(fs2);
fclose(ft);
getch();
return 0;
}