Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
68 views

C LAB PROGRAMS With Ouputs PDF

The document provides C programs and outputs for various exercises involving basic programming concepts like calculating area of a triangle, finding the largest of three numbers, swapping two numbers, finding the 2's complement and roots of a quadratic equation. It also includes programs to perform arithmetic operations based on user input, find sum of digits and reverse of a number, generate Fibonacci series and prime numbers. Further programs demonstrate printing a multiplication table, converting decimal to binary and checking if a number is Armstrong number.

Uploaded by

Nikitha Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

C LAB PROGRAMS With Ouputs PDF

The document provides C programs and outputs for various exercises involving basic programming concepts like calculating area of a triangle, finding the largest of three numbers, swapping two numbers, finding the 2's complement and roots of a quadratic equation. It also includes programs to perform arithmetic operations based on user input, find sum of digits and reverse of a number, generate Fibonacci series and prime numbers. Further programs demonstrate printing a multiplication table, converting decimal to binary and checking if a number is Armstrong number.

Uploaded by

Nikitha Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

K.Anudeep Asst.

Prof (CSE),VIIT

CP LAB PROGRAMS (R13 REGULATION)


Exercise l
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
vi area.c
#include<stdio.h>
#include<math.h>
void main()
{
double a,b,c,s,area;
printf("Enter a,b,c values :");
scanf("%lf%lf%lf",&a,&b,&c);
s=(a+b+c)/2;
printf("s =%lf",s);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\nArea =%lf\n",area);
}
Complilation: gcc area.c –lm –o area
Execution: ./area
Output:
Enter a,b,c values :2 2 2
s =3.000000
Area =1.732051

b) Write a C program to find the largest of three numbers using ternary operator.
vi large.c
#include<stdio.h>
void main()
{
int a,b,c,large;
printf("\nEnter three values: ");
scanf("%d%d%d",&a,&b,&c);

1
K.Anudeep Asst.Prof (CSE),VIIT

large=a>b?(a>c?a:c):(b>c?b:c);
printf("Largest is %d",large);
}
Complilation: gcc large.c –o large
Execution: ./large
Output:
Enter three values: 5 6 8
Largest is 8
c) Write a C Program to swap two numbers without using a temporary variable.
#include<stdio.h>
void main()
{
int a,b;
printf("Enter two values: ");
scanf("%d%d",&a,&b);
printf("\nBefore swapping %d and %d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swapping %d and %d",a,b);
}
Output:
Enter two values: 56 80
Before swapping 56 and 80
After swapping 80 and 56
Exercise 2
a . Write a C program to find the 2‟s complement of a binary number.
#include<stdio.h>
void main()
{
char a[10];
int length=0,i,j,count=0;
printf("Enter a binary number");

2
K.Anudeep Asst.Prof (CSE),VIIT

scanf("%s",a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]!='0'&&a[i]!='1')
{
count++;
break;
}
}
if(count==0)
{
while(a[length]!='\0')
{
length++;
}
for(i=length-1;i>=0;i--)
{
if(a[i]=='1')
break;
}
for(j=i-1;j>=0;j--)
{
if(a[j]=='1')
a[j]='0';
else
a[j]='1';
}
printf("2's complement is %s",a);
}
else
printf("Not a binary number");
}

3
K.Anudeep Asst.Prof (CSE),VIIT

Output:
Enter a binary number100011
2's complement is 011101

b) Write a C program to find the roots of a quadratic equation.


#include<stdio.h>
#include<math.h>
void main()
{
double a,b,c,d,area,r1,r2,r,i;
printf("Enter a,b,c values: ");
scanf("%lf%lf%lf",&a,&b,&c);
d=(b*b)-(4*a*c);
printf("Discriminent is %lf",d);
if(d>0)
{
printf("\nRoots are real\n");
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("Roots are %lf and %lf",r1,r2);
}
else if(d<0)
{
printf("\nRoots are Imginary\n");
r=-b/(2*a);
i=sqrt(abs(d))/(2*a);
printf("Roots are %lf +i %lf and %lf -i %lf",r,i,r,i);
}
else{
printf("\nRoots are equal\n");
r=-b/(2*a);
printf("Root is %lf",r);

4
K.Anudeep Asst.Prof (CSE),VIIT

}
}
Output:
Enter a,b,c values: 2 5 3
Discriminent is 1.000000
Roots are real
Roots are -1.000000 and -1.500000

c)Write a C program, which takes two integer operands and one operator form the user,
performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use
Switch Statement)
#include<stdio.h>
void main()
{
int a,b;
char ch;
printf("Enter two operands: ");
scanf("%d%d",&a,&b);
printf("Enter an operator : ");
scanf("%c",&ch);//To read the space or new line
scanf("%c",&ch);
switch(ch)
{
case '+':
printf("Addition is %d\n",a+b);
break;
case '-':
printf("Subtraction is %d\n",a-b);
break;
case '*':
printf("Multiplication is %d\n",a*b);
break;
case '/':

5
K.Anudeep Asst.Prof (CSE),VIIT

printf("Division is %d\n",a/b);
break;
case '%':
printf("Quotient is %d\n",a%b);
break;
default:
printf("Invalid Operator\n");
}
}
Output:
Enter two operands: 12 45
Enter an operator : +
Addition is 57
Enter two operands: 30 6
Enter an operator : /
Division is 5
Enter two operands: 12 3
Enter an operator : %
Quotient is 0

Exercise 3
a) Write a C program to find the sum of individual digits of a positive integer and find the
reverse of the given number.
#include<stdio.h>
void main()
{
int n,r,temp,sum,rev;
printf("Enter the number :");
scanf("%d",&n);
if(n<0)
{
printf("Number should be postive number");
}

6
K.Anudeep Asst.Prof (CSE),VIIT

else
{
sum=0;
temp=n;
rev=0;
while(temp>0)
{
r=temp%10;
sum=sum+r;
rev=rev*10+r;
temp=temp/10;
}
printf("Sum is %d ",sum);
printf("\nThe reverse of %d is %d\n",n,rev);
}
}
Output:
Enter the number :1234
Sum is 10
The reverse of 1234 is 4321

b)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.
#include<stdio.h>
void main()
{
int a=0,b=1,c,n,i;
printf("How many fibonacci sequence you want: ");
scanf("%d",&n);
if(n>2)
{
printf("\n%d\t%d",a,b);
for(i=0;i<(n-2);i++)

7
K.Anudeep Asst.Prof (CSE),VIIT

{
c=a+b;
printf("\t%d",c);
a=b;
b=c;
}
}
else
printf("For fibonacci series minimum no is 3");
}
Output:
How many fibonacci sequence you want: 15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

C) 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>
void main()
{
int n,i,count ,p;
printf("Enter the range for prime numbers :");
scanf("%d",&p);
for(n=2;n<p;n++)
{
count=0;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
count++;
}
}
if(count==0)

8
K.Anudeep Asst.Prof (CSE),VIIT

{
printf("%d \n",n);
}
}
}
Output:
Enter the range for prime numbers :30
2 3 5 7 11 13 17 19 23 29

Exercise 4
a) Write a C Program to print the multiplication table of a given number n up to a given
value, where n is entered by the user.
#include<stdio.h>
void main()
{
int n,i,range;
printf("Enter which table number you want and range: ");
scanf("%d%d",&n,&range);
for(i=1;i<=range;i++)
{
printf("\n%d x %d=%d",n,i,n*i);
}
}
Output:
Enter which table number you want and range: 15 5

15 x 1=15
15 x 2=30
15 x 3=45
15 x 4=60
15 5=75

9
K.Anudeep Asst.Prof (CSE),VIIT

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,temp,q,a[10],i=0,j;
printf("Enter a decimal number: ");
scanf("%d",&n);
temp=n;
while(temp>=2)
{
a[i]=temp%2;
i++;
temp=temp/2;
}
a[i]=1;
printf("Decimal eqivalent of %d is ",n);
for(j=i;j>=0;j--)
printf("%d",a[j]);
}
Output:
Enter a decimal number: 16
Decimal eqivalent of 16 is 10000

c) Write a C Program to check whether the given number is Armstrong number or not.
#include<stdio.h>
void main()
{
int n,temp,sum=0,r;
printf("Enter the number:\n");
scanf("%d",&n);
if(n<0)
{

10
K.Anudeep Asst.Prof (CSE),VIIT

printf("Number should be postive number");


}
else
{
temp=n;
while(temp>0)
{
r=temp%10;
sum=sum+(r*r*r);
temp=temp/10;
}
if(sum==n)
printf("%d is Armstrong\n",n);
else
printf("%d is not Armstrong:\n",n);
}
}
Output:
Enter the number:
153
153 is Armstrong
Enter the number:
156
156 is not Armstrong

Exercise 5
a) Write a C program to interchange the largest and smallest numbers in the array.
#include<stdio.h>
void main()
{
int n,a[10],i,temp,j,large,small;
printf("Enter size of Array :");

11
K.Anudeep Asst.Prof (CSE),VIIT

scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
large=0;
small=0;
for(i=0;i<n;i++)
{
if(a[i]>a[large])
large=i;
if(a[i]<a[small])
small=i;
}
printf("\nElements in the Array are: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\nLarge and small Indexes are %d and %d ",large,small);
temp=a[large];
a[large]=a[small];
a[small]=temp;
printf("\nArray after interchanging largest and smallest: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
Output:
Enter size of Array :5

12
K.Anudeep Asst.Prof (CSE),VIIT

Enter 5 elements: 12 45 67 8 5

Elements in the Array are: 12 45 67 8 5


Large and small Indexes are 2 and 4
Array after interchanging largest and smallest: 12 45 5 8 67

b) Write a C program to implement a liner search.


#include<stdio.h>
void main()
{
int a[20],n,i,element,count=0;
printf("Enter size of array:");
scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter element to search:");
scanf("%d",&element);
for(i=0;i<n;i++)
{
if(a[i]==element)
{
count++;
break;
}
}
if(count==1)
printf("Element found");
else
printf("Element not found");

13
K.Anudeep Asst.Prof (CSE),VIIT

}
Output:
Enter size of array:6
Enter 6 elements: 12 3 45 7 34 78
Enter element to search:45
Element found

c) Write a C program to implement binary search


#include<stdio.h>
void main()
{
int a[20],n,i,element,count=0,low=0,high,mid,j,temp;
printf("Enter size of array:");
scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter element to search:");
scanf("%d",&element);
//Sorting the Array before searching
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-i-1);j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}

14
K.Anudeep Asst.Prof (CSE),VIIT

}
}
high=n-1;
do{
mid=(low+high)/2;
if(a[mid]==element)
{
count++;
break;
}
else if(element>a[mid])
low=mid+1;
else
high=mid-1;
}while(low<=high);
if(count==1)
printf("Element found");
else
printf("Element not found");
}
Output:
Enter size of array:6
Enter 6 elements: 12 3 5 67 8 9
Enter element to search:67
Element found

Exercise 6
a) Write a C program to implement sorting of an array of elements .
#include<stdio.h>
void main()
{
int a[20],n,i,temp,j;

15
K.Anudeep Asst.Prof (CSE),VIIT

printf("Enter size of array:");


scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nElements before sorting: ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-i-1);j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nElements after sorting: ");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
Output:
Enter size of array:5
Enter 5 elements: 12 3 40 1 90
Elements before sorting: 12 3 40 1 90

16
K.Anudeep Asst.Prof (CSE),VIIT

Elements after sorting: 1 3 12 40 90

b) Write a C program to input two m x n matrices, check the compatibility and perform
addition and multiplication of them
#include<stdio.h>
void main()
{
int m,n,p,q,k,a[3][3],b[3][3],c[3][3],i,j;
printf("Enter the size of row and col of A: ");
scanf("%d%d",&m,&n);
printf("Enter the size of row and col of B: ");
scanf("%d%d",&p,&q);
printf("Enter the elements of matrix A\n ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of matrix B\n ");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;

17
K.Anudeep Asst.Prof (CSE),VIIT

for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("Multiplication of A and B is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
Output:
Enter the size of row and col of A: 2 3
Enter the size of row and col of B: 3 2
Enter the elements of matrix A
123
456
Enter the elements of matrix B
12
34
56
Multiplication of A and B is
22 28
49 64

Exercise 7
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.

18
K.Anudeep Asst.Prof (CSE),VIIT

#include <stdio.h>
void insertSubString(char str[],char substr[],int pos);
void main()
{
char str[20],substr[20];
int pos;
printf("Enter main string:");
scanf("%s",str);
printf("Enter sub string:");
scanf("%s",substr);
printf("Enter the position of where to insert:");
scanf("%d",&pos);
insertSubString(str,substr,pos);
}
void insertSubString(char str[],char substr[],int pos)
{
int l1,l2,i,j;
char fstr[40];
l1=strlen(str);
l2=strlen(substr);
if(pos<=l1)
{
for(i=0;i<pos;i++)
{
fstr[i]=str[i];
}
for(i=0,j=pos;i<l2;j++,i++)
{
fstr[j]=substr[i];
}
for(i=pos;i<l1;i++)
{

19
K.Anudeep Asst.Prof (CSE),VIIT

fstr[j++]=str[i];
}
fstr[j]='\0';
printf("The string is %s",fstr);
}
else
printf("Insertion is not possible");
}
Output:
Enter main string:anudeep
Enter sub string:koppolu
Enter the position of where to insert:7
The string is anudeepkoppolu

ii. To delete n Characters from a given position in a given string.


#include <stdio.h>
void delString(char str[],int pos,int n);
void main()
{
char str[20];
int pos,n;
printf("Enter main string:");
scanf("%s",str);
printf("Enter the position and no.of characters to delete:");
scanf("%d%d",&pos,&n);
delString(str,pos,n);
}
void delString(char str[],int pos,int n)
{
int len,i,j;
char fstr[40];
len=strlen(str);
if((pos+n-1)<len)

20
K.Anudeep Asst.Prof (CSE),VIIT

{
for(i=0;i<pos;i++)
{
fstr[i]=str[i];
}
for(j=(pos+n);j<len;j++)
{
fstr[i++]=str[j];
}
fstr[i]='\0';
printf("\nThe string is %s",fstr);
}
else
printf("\nDeletion is not possible");
}
Output:
Enter main string:anudeepkoppolu
Enter the position and no.of characters to delete:7 7
The string is anudeep

iii. To replace a character of string either from beginning or ending or at a specified location

#include <stdio.h>
void replaceString(char str[],char ch,int pos);
void main()
{
char str[20],ch;
int pos;
printf("Enter main string:");
scanf("%s",str);
printf("Enter character and position to replace:");
scanf("%c%c%d",&ch,&ch,&pos);
replaceString(str,ch,pos);

21
K.Anudeep Asst.Prof (CSE),VIIT

}
void replaceString(char str[],char ch,int pos)
{
if(pos<=strlen(str))
{
str[pos-1]=ch;
printf("The string is %s",str);
}
else
printf("Replacement is not possible:");
}
Output:
Enter main string:anudeep
Enter character and position to replace:A 1
The string is Anudeep

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
#include <stdio.h>
struct compnum
{
double real;
double img;
}c,c1,c2;
typedef struct compnum comp;
comp read();
int main()
{
int opt;
printf("Menu\n1.Read \n2. Write\n3.Add\n4.Multiplication\nEnter your option:");

22
K.Anudeep Asst.Prof (CSE),VIIT

scanf("%d",&opt);
switch(opt)
{
case 1:
printf("Enter a Complex Number:");
c=read();
break;
case 2:
printf("Enter a Complex Number:");
c=read();
printf("The complex number is %lf+%lfi",c.real,c.img);
break;
case 3:
printf("Enter First Complex Number:");
c1=read();
printf("Enter Second Complex Number:");
c2=read();
c.real=c1.real+c2.real;
c.img=c1.img+c2.img;
printf("The complex number is %lf+%lfi",c.real,c.img);
break;
case 4:
printf("Enter First Complex Number:");
c1=read();
printf("Enter Second Complex Number:");
c2=read();
c.real=c1.real*c2.real-c1.img*c2.img;
c.img=c1.img*c2.real+c1.real*c2.img;
printf("The complex number is %lf+%lfi",c.real,c.img);
break;
default:
printf("\nWrong option");

23
K.Anudeep Asst.Prof (CSE),VIIT

}
}
comp read()
{
printf("\nEnter real part and imaginary part:");
scanf("%lf%lf",&c.real,&c.img);
return c;
}
Output:
Menu
1.Read
2. Write
3.Add
4.Multiplication
Enter your option:3
Enter First Complex Number:
Enter real part and imaginary part:12 3
Enter Second Complex Number:
Enter real part and imaginary part:2 5
The complex number is 14.000000+8.000000i
Menu
1.Read
2. Write
3.Add
4.Multiplication
Enter your option:4
Enter First Complex Number:
Enter real part and imaginary part:1 2
Enter Second Complex Number:
Enter real part and imaginary part:5 6
The complex number is -7.000000+16.000000i

24
K.Anudeep Asst.Prof (CSE),VIIT

Exercise 9
a) Write C Programs for the following string operations without using the built in functions
- to concatenate two strings
#include<stdio.h>
void main()
{
char str[30],str1[20],str2[20];
int i,j,len1,len2;
printf("Enter first string:");
scanf("%s",str1);
printf("Enter second string:");
scanf("%s",str2);
len1=0;
len2=0;
for(i=0;str1[i]!='\0';i++)
{
len1++;
}
for(j=0;str2[j]!='\0';j++)
{
len2++;
}
printf("String1 length is %d ",len1);
printf("\nString2 length is %d ",len2);
for(i=0;i<len1;i++)
{
str[i]=str1[i];
}
for(j=0;j<len2;j++)
{
str[i++]=str2[j];
}

25
K.Anudeep Asst.Prof (CSE),VIIT

str[i]='\0';
printf("\nString after concatenation is %s",str);
}
Output:
Enter first string:anudeep
Enter second string:mtech
String1 length is 7
String2 length is 5
String after concatenation is anudeepmtech

- to compare two strings


#include<stdio.h>
void main()
{
char str[30],str1[20],str2[20];
int i,j,len1,len2,count=0;
printf("Enter first string:");
scanf("%s",str1);
printf("Enter second string:");
scanf("%s",str2);
len1=0;
len2=0;
for(i=0;str1[i]!='\0';i++)
{
len1++;
}
for(j=0;str2[j]!='\0';j++)
{
len2++;
}
printf("String1 length is %d ",len1);
printf("\nString2 length is %d ",len2);

26
K.Anudeep Asst.Prof (CSE),VIIT

if(len1!=len2)
printf("Strings are not equal");
else
{
for(i=0;i<len1;i++)
{
if(str1[i]!=str2[i])
{
count++;
break;
}
}
if(count==0)
printf("\nStrings are equal");
else
printf("Strings are not equal:");
}
}
Output:
Enter first string:anudeep
Enter second string:anudeep
String1 length is 7
String2 length is 7
Strings are equal

Exercise 10
a) Write C Programs for the following string operations without using the built in functions
- to find the length of a string
- to find whether a given string is palindrome or not
#include<stdio.h>
void main()
{

27
K.Anudeep Asst.Prof (CSE),VIIT

char a[20];
int length=0,i,j,count=0;
printf("Enter a string: ");
scanf("%s",a);
while(a[length]!='\0')
length++;
printf("\nThe length of the string is %d",length);
for(i=0,j=length-1;i<=length/2;i++,j--)
{
if(a[i]!=a[j])
{
count++;
break;
}
}
if(count==0)
printf("\n%s is palindrome",a);
else
printf("\n%s is not a palindrome",a);
}
Output:
Enter a string: madam
The length of the string is 5
madam is palindrome

Exercise 11
a) Write a C functions to find both the largest and smallest number of an array of integers.
#include<stdio.h>
int largest(int a[],int n);
int smallest(int a[],int n);
void main()
{

28
K.Anudeep Asst.Prof (CSE),VIIT

int a[10],n,large,small,i;
printf("Enter the size of Array: ");
scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
large=largest(a,n);
small=smallest(a,n);
printf("Largest value is %d and smallest is %d",large,small);
}
int largest(int a[],int n)
{
int large=a[0],i;
for(i=0;i<n;i++)
{
if(a[i]>large)
large=a[i];
}
return large;
}
int smallest(int a[],int n)
{
int small=a[0],i;
for(i=0;i<n;i++)
{
if(a[i]<small)
small=a[i];
}
return small;
}
Output:
Enter the size of Array: 6

29
K.Anudeep Asst.Prof (CSE),VIIT

Enter 6 elements: 12 5 7 75 34 778


Largest value is 778 and smallest is 5

b) Write C programs illustrating call by value and call by reference cncepts.


//Call by reference
#include<stdio.h>
void swap(int *a,int *b);
void main()
{
int x,y;
printf("Enter two values: ");
scanf("%d%d",&x,&y);
printf("\nValues Before swapping are %d and %d",x,y);
swap(&x,&y);
printf("\nValues in main() are %d and %d",x,y);
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
printf("\nValues after swapping are %d and %d",*a,*b);
}
Output:
Enter two values: 34 567
Values Before swapping are 34 and 567
Values after swapping are 567 and 34
Values in main() are 567 and 34
//call by value
#include<stdio.h>
void swap(int a,int b);

30
K.Anudeep Asst.Prof (CSE),VIIT

void main()
{
int x,y;
printf("Enter two values: ");
scanf("%d%d",&x,&y);
printf("\nValues Before swapping are %d and %d",x,y);
swap(x,y);
printf("\nValues in main() are %d and %d",x,y);
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("\nValues after swapping are %d and %d",a,b);
}
Output:
Enter two values: 45 55
Values Before swapping are 45 and 55
Values after swapping are 55 and 45
Values in main() are 45 and 55

Exercise 12
a) Write C programs that use both recursive and non-recursive functions for the following
i) To find the factorial of a given integer
#include<stdio.h>
int factorial(int);
void main()
{
unsigned int n,fact=1,i;
printf("Enter a number: ");

31
K.Anudeep Asst.Prof (CSE),VIIT

scanf("%d",&n);
printf("Iterative Approach:");
for(i=n;i>=1;i--)
{
fact=fact*i;
}
printf("\nFactorial of %d is %d",n,fact);
printf("\nRecursive Approach:");
fact=factorial(n);
printf("\nFactorial of %d is %d",n,fact);
}
int factorial(int n)
{
if(n==0)
return 1;
else
return n*factorial(n-1);
}
Output:
Enter a number: 5
Iterative Approach:
Factorial of 5 is 120
Recursive Approach:
Factorial of 5 is 120

ii) To find the GCD (greatest common divisor) of two given integers
#include<stdio.h>
int gcdRecursive(int m, int n);
void main()
{
unsigned int a,b,large,gcd,i;
printf("Enter two number:");

32
K.Anudeep Asst.Prof (CSE),VIIT

scanf("%d%d",&a,&b);
large=a>b?a:b;
printf("\nIterative approach for gcd calculation");
for(i=large;i>0;i--)
{
if(a%i==0&&b%i==0)
{
gcd=i;
break;
}
}
printf("\nGCD of %d and %d is %d",a,b,gcd);
printf("\nRecursive approach for gcd calculation");
gcd=gcdRecursive(a,b);
printf("\nGCD of %d and %d is %d",a,b,gcd);
}
int gcdRecursive(int a,int b)
{
if(b>a)
return gcdRecursive(b,a);
else if(b==0)
return a;
else
return gcdRecursive(b,a%b);
}
Output:
Enter two number:12 30
Iterative approach for gcd calculation
GCD of 12 and 30 is 6
Recursive approach for gcd calculation
GCD of 12 and 30 is 6

33
K.Anudeep Asst.Prof (CSE),VIIT

iii) To find Fibonacci sequence


#include<stdio.h>
int fibo(int n);
void main()
{
unsigned int n,a,b,c,i;
printf("Enter size of series:");
scanf("%d",&n);
printf("\nIterative approach for Fibonacci series");
if(n>2)
{
a=0;
b=1;
printf("\n%d\t%d\t",a,b);
for(i=0;i<(n-2);i++)
{
c=a+b;
printf("%d\t",c);
a=b;
b=c;
}
printf("\nRecursive approach for Fibonacci Series\n");
for(i=1;i<=n;i++)
{
printf("%d\t",fibo(i));
}
}
else
printf("Enter the minimum value of 3");
}
int fibo(int n)
{

34
K.Anudeep Asst.Prof (CSE),VIIT

if(n==1)
return 0;
else if(n==2)
return 1;
else
return fibo(n-1)+fibo(n-2);
}
Output:
Enter size of series:10
Iterative approach for Fibonacci series
0 1 1 2 3 5 8 13 21 34

Recursive approach for Fibonacci Series


0 1 1 2 3 5 8 13 21 34

Exercise 13
a) Write C Program to reverse a string using pointers
#include<stdio.h>
void main()
{
char *str;
int length,i,j,n;
printf("Enter the size of the Array:");
scanf("%d",&n);
str=(char*)malloc(sizeof(char)*n);
printf("Enter a string: ");
scanf("%s",str);
length=0;
for(i=0;*(str+i)!='\0';i++)
{
length++;
}

35
K.Anudeep Asst.Prof (CSE),VIIT

printf("\nLength of the string is %d",length);


printf("\nReverse of the string is ");
for(j=length-1;j>=0;j--)
{
printf("%c",*(str+j));
}
}
Output:
Enter the size of the Array:10
Enter a string: civilbatch
Length of the string is 10
Reverse of the string is hctablivic

b) Write a C Program to compare two arrays using pointers


#include<stdio.h>
void main()
{
int *a,*b,len1,len2,count,i;
printf("Enter size of the first array:");
scanf("%d",&len1);
a=(int*)malloc(sizeof(int)*len1);
printf("Enter %d elements : ",len1);
for(i=0;i<len1;i++)
{
scanf("%d",(a+i));
}
printf("Enter size of the second array:");
scanf("%d",&len2);
b=(int*)malloc(sizeof(int)*len2);
printf("Enter %d elements : ",len2);
for(i=0;i<len2;i++)
{

36
K.Anudeep Asst.Prof (CSE),VIIT

scanf("%d",(b+i));
}
if(len1!=len2)
{
printf("Arrays are not equal");
}
else
{
count=0;
for(i=0;i<len1;i++)
{
if(*(a+i)!=*(b+i))
{
count++;
break;
}
}
if(count==0)
printf("\nArrays are equal");
else
printf("\nArrays are not equal");
}
}
Output:
Enter size of the first array:5
Enter 5 elements : 12 34 5 67 57
Enter size of the second array:5
Enter 5 elements : 393 933 939 4 56
Arrays are not equal

Exercise 14
a) Write a C program consisting of Pointer based function to exchange value of two integers
using passing by address.

37
K.Anudeep Asst.Prof (CSE),VIIT

#include<stdio.h>
void swap(int *a,int *b);
void main()
{
int x,y;
printf("Enter two values: ");
scanf("%d%d",&x,&y);
printf("\nValues Before swapping are %d and %d",x,y);
swap(&x,&y);
printf("\nValues in main() are %d and %d",x,y);
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
printf("\nValues after swapping are %d and %d",*a,*b);
}
Output:
Enter two values: 10 25
Values Before swapping are 10 and 25
Values after swapping are 25 and 10
Values in main() are 25 and 10

b) Write a C program to swap two numbers using pointers


#include<stdio.h>
void main()
{
int a,b,*x,*y;
printf("Enter two values: ");
scanf("%d%d",&a,&b);

38
K.Anudeep Asst.Prof (CSE),VIIT

x=&a;
y=&b;
printf("\nValues Before swapping are %d and %d",*x,*y);
*x=*x+*y;
*y=*x-*y;
*x=*x-*y;
printf("\nValues After swapping are %d and %d",*x,*y);
}
Output:
Enter two values: 12 35
Values Before swapping are 12 and 35
Values After swapping are 35 and 12

Exercise 15
Examples which explores the use of structures, union and other user defined variables Enter
todays date(dd mon yyyy): 13 jan 2015
//structures
#include<stdio.h>
struct Date
{
int day;
char month[10];
int year;
};
void main()
{
struct Date d;
printf("Enter todays date(dd mon yyyy):");
scanf("%d%s%d",&d.day,&d.month,&d.year);
printf("\nSize of the structure is %d",sizeof(d));
printf("\nTodays date is %d-%s-%d",d.day,d.month,d.year);
}
Output:

39
K.Anudeep Asst.Prof (CSE),VIIT

Enter todays date(dd mon yyyy): 13 jan 2015


Size of the structure is 14
Todays date is 12-jan-2015
//unions
#include<stdio.h>
union Date
{
int day;
char month[10];
float year;
};
void main()
{
union Date d;
printf("Enter todays date(dd mon yyyy):");
scanf("%d%s%d",&d.day,&d.month,&d.year);
printf("\nSize of the union is %d",sizeof(d));
printf("\nTodays date is %d-%s-%d",d.day,d.month,d.year);
}
Output:
Enter todays date(dd mon yyyy): 13 jan 2015
Size of the structure is 10
Todays date is 2015-n-0

//enumeration
#include<stdio.h>
enum currency{paise,rupee,hundred=100,thousand=1000};
void main()
{
printf("\nPaise value is %d",paise);
printf("\nRupee value is %d",rupee);
printf("\nHundred value is %d",hundred);

40
K.Anudeep Asst.Prof (CSE),VIIT

printf("\nThousand value is %d",thousand);


}
Output:
Paise value is 0
Rupee value is 1
Hundred value is 100
Thousand value is 1000

Exercise 16
a) Write a C program which copies one file to another
#include<stdio.h>
void main()
{
FILE *fp1,*fp2;
char ch;
fp1=fopen("test.txt","r");
if(fp1==NULL)
{
puts("Cannot open this file");
exit(1);
}

fp2=fopen("test1.txt", "w");
if(fp2==NULL)
{
puts("Not able to open this file");
fclose(fp1);
exit(1);
}
while((ch = fgetc(fp1))!= EOF)
{
fputc(ch,fp2);

41
K.Anudeep Asst.Prof (CSE),VIIT

}
printf("File copied successfully");
fclose(fp2);
fclose(fp1);
}
Output:
Test.txt
Happy
New Year
2015
Test1.txt
Happy
New Year
2015

b) Write a C program to count the number of characters and number of lines in a file.
#include <stdio.h>
int main()
{
FILE *fp;
int countl=1; // Line counter
int countc=0; // Characters counter
char filename[50];
char ch; // To store a character read from file
// Get file name from user. The file should be
// either in current folder or complete path should be provided
printf("Enter file name: ");//Extension is also required
scanf("%s", filename);

// Open the file


fp = fopen(filename, "r");

42
K.Anudeep Asst.Prof (CSE),VIIT

// Check if file exists


if (fp == NULL)
{
printf("Could not open file %s", filename);
exit(1);
}

// Extract characters from file and store in character ch


while((ch=getc(fp))!=EOF)
{
countc++;
if(ch=='\n')
countl++;
}

// Close the file


fclose(fp);
printf("The file %s has %d characters and %d lines ", filename,countc,countl);
getch();
return 0;
}
Output:
Test.txt
Happy
New Year
2015
Enter file name:test.txt
The file test.txt has 20 characters and 3 lines

c) Write a C Program t merge two files into a third file. The names of the files must be
entered using command line arguments.
#include <stdio.h>
void main(int argc,char* argv[])

43
K.Anudeep Asst.Prof (CSE),VIIT

{
FILE *fp1, *fp2, *fp;
char ch;
clrscr();
fp1=fopen(argv[1],"r");
fp2=fopen(argv[2],"r");
if(fp1== NULL||fp2==NULL)
{
perror("Error");
printf("Press any key to exit...\n");
getch();
exit(0);
}
fp=fopen(argv[3],"w");
if(fp==NULL)
{
perror("Error ");
printf("Press any key to exit...\n");
exit(0);
}
while((ch=fgetc(fp1))!=EOF)
{
fputc(ch,fp);
}
while((ch=fgetc(fp2))!=EOF)
{
fputc(ch,fp);
}
printf("Two files were merged into %s file successfully.\n",argv[3]);
fclose(fp1);
fclose(fp2);
fclose(fp);

44
K.Anudeep Asst.Prof (CSE),VIIT

getch();
}
Compilation: gcc merge.c –o mergetwofiles
Execution: ./mergetwofiles test.txt test1.txt merge.txt
Output:
Test.txt
Happy
New Year
Test1.txt
2015
Merge.txt
Happy
New Year2015

45

You might also like