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

C Lab Solutions

This document contains examples of C programs to solve various problems. It includes programs to calculate the area of a triangle, find the largest of three numbers, swap two numbers without a temporary variable, find the 2's complement of a binary number, find the roots of a quadratic equation, perform basic mathematical operations on two numbers, find the sum of digits and reverse of a number, and generate the Fibonacci sequence. For each problem, it provides the aim, algorithm, expected output, source code, and actual output of running the program. The programs demonstrate basic programming concepts in C like input/output, conditional statements, loops, functions, and data types.

Uploaded by

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

C Lab Solutions

This document contains examples of C programs to solve various problems. It includes programs to calculate the area of a triangle, find the largest of three numbers, swap two numbers without a temporary variable, find the 2's complement of a binary number, find the roots of a quadratic equation, perform basic mathematical operations on two numbers, find the sum of digits and reverse of a number, and generate the Fibonacci sequence. For each problem, it provides the aim, algorithm, expected output, source code, and actual output of running the program. The programs demonstrate basic programming concepts in C like input/output, conditional statements, loops, functions, and data types.

Uploaded by

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

Problem solving using c lab

EXERCISE :1
a) Write a c program to calculate area of triangle.

AIM: Write a c program to calculate area of triangle.


Algorithm :
Step1: start.

Step2: declare base , height and area.

Step3: read the variables base, height and area=base*height*0.5.

Step4: print the value of area.

Step5: stop.

Expected output:
enter the value of base of triangle:4

enter the value of height of triangle:3

the area of triangle is :6


Source code:
#include<stdio.h>
void main()
{
float base,height,area;
clrscr();
printf("enter the value of base of triangle:");
scanf("%f",&base);
printf("enter the value of height of triangle:");
scanf("%f",&height);
area=base*height*0.5;
printf("the area of triangle is :%2f",area);
getch();
}

Actual output:
enter the value of base of triangle:6

enter the value of height of triangle:5

the area of triangle is :15


EX1 b) Write a c program to find the largest of three numbers
using temporary variable.

Aim: To Write a c program to find the largest of three numbers


using temporary variable.

Algorithm :
Step1 : start.

Step2 : declare variables A,B,C and largest.

Step3 : read variables A,B,C and largest.

Step4 : largest= A>B?(A>C;A:C);(B>C;?B:C).

Step5 : print the largest number

Step6 : stop

Expected output :
Enter 3 numbers :5 4 7 .

7 is the largest number .


Source code:
#include<stdio.h>
int main()
{
int a,b,c,largest;
clrscr();
printf("enter the values of 3 numbers: ");
scanf("%d%d%d",&a,&b,&c);
largest=a>b?(a>c?a:c):(b>c?b:c);
printf("%d is the largest number",largest);
return(0);
}

Actual output

enter the values of 3 numbers:1 2 3

3 is the largest number.


EX1 c) write a c program to swap two numbers
without using temporary variable.
Aim: To write a c program to swap two numbers
without using temporary variable.
Algorithm:
Step1 : start
Step2 : declare the variables a,b
Step3 : read the variables
Step4 : a=a+b , b=a-b, a=a-b.
Step5 : print the values after swapping a, b
Step6 : stop

Expected output :

enter two integers a, b:2 1


after swapping a=1,b=2
Source code:
#include<stdio.h>
int main()
{
int a,b;
clrscr();
printf("enter two integers a, b:");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("after swapping a=%d,b=%d\n",a,b);
return(0);
}

Actual output :
enter two integers a, b:5 6
after swapping a=6,b=5

EXERCISE:2
a)write a c program to find 2’s compliment of a binary
number.
Aim: To write a c program to find 2’s compliment of a
binary number.
Algorithm:
Step1 : start
Step 2: define size
Step3 : declare variable I,carry and character num[SIZE+1] ,one-
comp[SIZE+1] ,two-comp[SIZE+1]

Step4 : read the variable num


Step5 : else for loop to find one-comp, two comp
Step6 : print the value of one-comp and two-comp
Step7 : stop
Expected output :
Enter size bit binary value:1 0 1 0 1

Ones complement = 0 1 0 1 0

Twos complement = 0 1 0 1 1
Source code:
#include <stdio.h>

#define SIZE 8

int main()
{
char binary[SIZE + 1], onesComp[SIZE + 1], twosComp[SIZE + 1];
int i, carry=1;

printf("Enter %d bit binary value: ", SIZE);

/* Input 8-bit binary string */


gets(binary);

/* Find ones complement of the binary number */


for(i=0; i<SIZE; i++)
{
if(binary[i] == '1')
{
onesComp[i] = '0';
}
else if(binary[i] == '0')
{
onesComp[i] = '1';
}
}
onesComp[SIZE] = '\0';

/*
* Add 1 to the ones complement
*/
for(i=SIZE-1; i>=0; i--)
{
if(onesComp[i] == '1' && carry == 1)
{
twosComp[i] = '0';
}
else if(onesComp[i] == '0' && carry == 1)
{
twosComp[i] = '1';
carry = 0;
}
else
{
twosComp[i] = onesComp[i];
}
}
twosComp[SIZE] = '\0';

printf("Original binary = %s\n", binary);


printf("Ones complement = %s\n", onesComp);
printf("Twos complement = %s\n", twosComp);

return 0;
}

Actual output:
Enter a size bit binary number =1 0 0 0 0 1

Ones complement = 0 1 1 1 1 0
EX2 b) write a c program to find the roots of a
quadratic equation.
Aim: To write a c program to find the roots of a
quadratic equation.
Algorithm :
Step1 : start
Step2 : declare variables a,b,c, root1,root2,imaginary
,and discriminant .
Step3: read coefficients a,b,c of quadratic equation
Step4 : find discriminant =(b*b)-4*a*c
Step5: if discriminant <0 , display roots are imaginary
and go to step 6
Find root1=(-b+sqrt(d))/2*a
root 2=(-b-sqrt(d))/2*a
Step6 : print values root1,root2
Step7 : stop

Expected output :
enter values of a,b,c of quadratic equation
(ax^2+bx+c):6 2 99
two distinct complex roots exists:-
0.17+i4.06,-0.17-i4.06

Source code:
#include<stdio.h>
#include<math.h>/*used for sqrt()*/
int main()
{
float a,b,c;
float root1,root2,imaginary;
float discriminant;
clrscr();
printf("enter values of a,b,c of quadratic equation
(ax^2+bx+c):");
scanf("%f%f%f",&a,&b,&c);
/*find discriminant of equation */
discriminant=(b*b)-(4*a*c);
/*find the nature of discriminant*/
if(discriminant>0)
{
root1=(-b+sqrt(discriminant))/(2*a);
root2=(-b-sqrt(discriminant))/(2*a);
printf("two distinct and real roots exists:%2f and
%2f",root1,root2);
}
else if(discriminant==0)
{
root1=root2=-b/(2*a);
printf("two equal and real roots exists:%2f and
%2f",root1,root2);
}
else if(discriminant<0)
{
root1=root2=-b/(2*a);
imaginary=sqrt(-discriminant)/(2*a);
printf("two distinct complex roots exists:%2f+i%2f
and %2f-i%2f",root1,imaginary,root2,imaginary);
}
return(0);
}

Actual output:
enter values of a,b,c of quadratic equation
(ax^2+bx+c):6 -8 -4
two distinct complex roots exists:1.72,-0.39

Ex2 c); write a c program which takes two integers


operands and one operator from user. Performs the
operation and then prints the result. (consider
operations +,-,*,/,% and use switch statement).
Aim: To write a c program which takes two integers
operands and one operator from user. Performs the
operation and then prints the result. (consider
operations +,-,*,/,% and use switch statement).
Algorithm :
Step 1: start
Step2 : read x,y variables.
Step3 : read option + or – or * or / or %.
Step4: if option is ‘+’ result is =x+y.
If option is ‘-’ result is =x-y.
If option is ‘*’result is =x*y.
If option is ‘/’ result is =x/y.
If option is ‘%’ result is =x%y.
Step5 : if option doesn’t match with (+) or (– )or (*) or
(/) or (%) then print select option +,-,*,/,% only
Step6 : print x, option y and result values
Step7 : stop.
Expected output :
1.addtion 2.subtraction 3.multiplication 4.division

enter the values of a and b:2 4

enter your choice:1

sum of 2 and 4 is 6

Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int op;
clrscr();

printf("1.addtion\n2.subtraction\n3.multiplication\n4.d
ivision\n");
printf("enter the values of a and b");
scanf("%d%d",&a,&b);
printf("enter your choice:");
scanf("%d",&op);
switch(op)
{
case 1:
printf("sum of %d and %d is %d",a,b,a+b);
break;
case 2:
printf("difference of %d and %d is %d",a,b,a+b);
break;
case 3:
printf("multiplication of %d and %d is %d",a,b,a*b);
break;
case 4:
printf("division of %d and %d is %d",a,b,a/b);
break;
default:
printf("enter the correct choice.");
break;

}
getch();
}

Actual output:
1.addtion 2.subtraction 3.multiplication 4.division

enter the values of a and b:5 20

enter your choice:4

division of 5 and 20 is 0.25

Exercise :3
a)Write a c program to find the sum of individual digits
of a positive integer and also find the reverse of the
given integer.
Aim : To Write a c program to find the sum of individual digits of
a positive integer and also find the reverse of the given number.

Algorithm:
Step1 : start
Step2 : read num as integer.
Step3: while (num>0)
Step4: print sum of digits.
Step5: stop.
Expected output:
enter a positive integer:16789

sum of individual digits:31

reverse of the given number:98761

Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int num,dup,sum,rev;
clrscr();
printf("enter a positive integer");
scanf("%d",&num);
dup=num;
sum=0;
while(num>0)
{
sum+=num%10;
num/=10;
}
printf("sum of individual digits:%d\n",sum);
rev=0;
while(dup>0)
{
rev=rev*10+(dup%10);
dup/=10;
}
printf("reverse of the given number:%d",rev);
getch();
}

Actual output:
enter a positive integer:2421

sum of individual digits:9

reverse of the given number:1242

ex3:b) write a c program to generate the first n terms


of the Fibonacci sequence.
Aim: write a c program to generate the first n terms of
the Fibonacci sequence.
Algorithm:
Step1: start

Step2: declare variables a,b,i,sum=o.

Step3: initialize the variables , a=0, b=1,and series=0.

Step4: print first two terms of series

Step4: use loop for the following

Sum=a+b

a=b, b=sum. Print the value of a

Step5: stop

Expected output:
enter the length of series:4

fibonacci series

0 1 1 2 3 5

Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,lengthofseries=0,counter,sum=0;
clrscr();
printf("enter the length of series:");
scanf("%d",&lengthofseries);
printf("fibonacci series\n");
printf("%d %d",a,b);
for(counter=2;counter<lengthofseries;counter++)
{
sum=a+b;
printf("%d",sum);
a=b;
b=sum;
}
getch();
}
Actual output:
enter the length of series:13

fibonacci series:

0 1 1 2 3 5 8 13 21 34 55 89 144

Ex3c) write a c program to generate all the prime


numbers between 1 and n, where n is the value
supplied by user.
Aim: to write a c program to generate all the prime numbers
between 1 and n, where n is the value supplied by user.
Algorithm:
Step1: start.

Step2: read n.

Step3: initialize i=1,c=0

Step4: if i<=n goto step 5

If not goto step10

Step5: initialize j=0

Step6: if j<=1 do as the follow if not goto step7

(i) If i%j==0 increment c


(ii) Increment j
(iii) Goto step6

Step7: if c==2 print i

Step8:increment i

Step9: goto step4

Step10: stop

Expected output:
prime no series:

enter any number:10

the prime nymbers between 1 to 10 are:

2 3 5 7

Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,count;
clrscr();
printf("prime no series\n");
printf("enter any number:");
scanf("%d",&n);
printf("the prime nymbers between 1 to %d are\n",n);
for(i=1;i<=n;i++)
{
count=0;
for(j=1;j<=i;j++)
if(i%j==0)
{
count++;
}
if(count==2)
{
printf("%d\t",i);
}
}
getch();
}

Actual output:
prime no series:

enter any number:24

the prime nymbers between 1 to 24 are :

2 3 5 7 11 13 17 19 23
exercise 4
a)write a c program to print the multiplication table of
a given number.
Aim: to write a c program to print the multiplication table of a
given number.
Algorithm:
Step1: start.
Step2: initialize

Step3: check whether i<n, the go to step 4 else goto

Step8

Step4: print x , i ,y

Step5: y=y+x

Step6: go to step 3

Step8: stop

Expected output:
enter a number: 9
enter the range: 3
9*1=9
9*2=18
9*3=27

Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,range;
clrscr();
printf("enter a number");
scanf("%d",&n);
printf("enter the range");
scanf("%d",&range);
for(i=1;i<=range;i++)
printf("%3d*%2d=%5d\n",n,i,n*i);
}

Actual output:
enter a number: 3
enter the range: 3
3*1=3
3*2=6
3*3=9

Ex 4b): write a program to read a decimal number and


find its equivalent binary number.
Aim: to write a program to read a decimal number and find its
equivalent binary number.

Algorithm:
Step 1: start

Step 2: declare variables n,c,k.

Step 3: read variables

Step 4: for loop( c=8;c>=0,c--)


Step 5: using if and else condition k=n>>c

If(k&1)→0

else→0

step 6: print ‘n’in binary system

step 7: stop

Expected output:
enter a integer in decimal number system:10

10 in binary system is:01010

Source code:
#include<stdio.h>
int main()
{
int n,c,k;
clrscr();
printf("enter a integer in decimal number system:\n");
scanf("%d",&n);
printf("%d in binary system is:\n",n);
for(c=8;c>=0;c--)
{
k=n>>c;
if(k&1)
printf("1");
else
printf("0");
}
printf("\n");
return(0);
}

Actual output:
enter a integer in decimal number system:108

108 in binary system is:01100100

Ex4 c): write a c program to check whether the given


number is Armstrong number or not.
Aim: To write a c program to check whether the given number
is Armstrong number or not.
Algorithm:
Step1: start

Step2: read number

Step3: set sum=0 and duplicate=number

Step4: remainder=number%10

Step5: sum=sum+(remainder*remainder*remainder)

Step6: number=number/10
Step7: repeat steps 4to6 until number>0

Step8: if sum=dup

Step9: display number is Armstrong

Step10: else

Step11: display number is not Armstrong number.

Step12: stop

Expected output:
enter a number: 153

153 is a armstromg

Source code:
#include<stdio.h>
int main()
{
int n,temp,rem,result=0;
clrscr();
printf("enter a number:");
scanf("%d",&n);
temp=n;
while(n!=0)
{
rem=n%10;
result = result+(rem*rem*rem);
n=n/10;
}
if(temp==result)
printf("%d is a armstromg\n",temp);
else
printf("%d is not a armstrong\n",temp);
return(0);
}
Actual output:
enter a number:1634

1634 is a armstromg

Exercise 5
a) Write a c program to interchange largest and
smallest numbers in the given array
Aim: to Write a c program to interchange largest and
smallest numbers in the given array.
Algorithm:
Step1: start

Step2: initialize max,min,maxpos,minpos,i,temp.

Step3: read a[i]

Step4: apply for lop as

For(i=0;i<n;i++)

if(a[i]>max)

Max=a[i]
Maxpos=i

If(a[i]<min)

Min=a[i];

Minpos=i;

Step5: temp=a[maxpos]

a[maxpos] =a[minpos]

a[minpos]= temp

Step6: print the result after interchanging the largest and smallest
integers.

Step7: stop

expected output:
enter 5 integers:1 2 3 4 5

after interchanging array elements are:5 2 3 4 1

source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],n,max,min,maxpos,minpos,i,temp;
clrscr();
printf("enter 5 integers:");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
max=a[0];
min=a[0];
maxpos=0;
minpos=0;
for(i=1;i<5;i++)
{
if(a[i]>max)
{
max=a[i];
maxpos=i;
}
if(a[i]<min)
{
min=a[i];
minpos=i;
}
}
temp=a[maxpos];
a[maxpos]=a[minpos];
a[minpos]=temp;
printf("\n after interchanging array elements
are:");
for(i=0;i<5;i++)
printf("\n %d",a[i]);
getch();
}
actual output:
enter 5 integers:2 5 4 1 3

after interchanging array elements are:2 1 4 5 3


Ex5 b) write a c program to implement linear search
Aim: to write a c program to implement linear search.
Algorithm:
Step1: start

Step2: declare variables num,n , I,arr[10],found=0,pos=1

Step3: read n as number of elements

Step4: read array elements

Step5: read the variables num as the key to be searched

Step6: run for loop

For(i=0;i<n;i++)

If( arr[i]==num)

Found=1;

Pos=I;

Print the number is found at pos


Step7: if ( found==0)

Print the number is not found

Step8: stop

Expected output:
enter number of elements:3

enter elements

a[o]=3

a[1]=4

a[2]=5

enter an element to be searched: 0

0 is not found

Source code:
#include<stdio.h>
int main()
{
int a[100],n,i,key,flag=0;
clrscr();
printf("enter number of elements");
scanf("%d",&n);
printf("enter elements\n");
/*read array elements*/
for(i=0;i<n;i++)
{
printf("enter a[%d]=",i);
scanf("%d",&a[i]);
}
printf("enter an element to be searched:");
scanf("%d",&key);
/*linear search starts here*/
for(i=0;i<n;i++)
{
if(key==a[i])
{
printf("%d is found at position%d\n",key,i);
flag=1;
break;
}
}
if(flag==0)
printf("%d is not found\n",key);
return(0);
}

Actual output:
enter number of elements:2

enter elements

a[o]=5

a[1]=4

enter an element to be searched: 4

4 is found at position 1
Ex5 c) write a c program to implement binary search.
Aim: to write a c program to implement binary search
Algorithm:
Step1: start

Step2: initialize set first, last, middle ,n search

Step3: repeat step 4 and step 5 if first=last

Step4: middle=first+last/2

Step5: if a[middle]= search then print the given number is found at


location middle+1 and goto step 7

Step6: if first>last then print the given number is not found

Step7: stop

Expected output:
enter number of elements: 3

enter 3 integers: 1 2 3

enter a value to find:7

not found! 7 isn't presnt in the list.

Source code:
#include<stdio.h>
int main()
{
int c,first,last,middle,n,search,array[100];
printf("enter number of elements\n",n);
scanf("%d",&n);
printf("enter %d integers\n",n);
for(c=0;c<n;c++)
scanf("%d",&array[c]);
printf("enter a value to find\n",n);
scanf("%d",&search);
first=0;
last=n-1;
middle=(first+last)/2;
while(first<=last)
{
if(array[middle]==search)
{
printf("%d found at location%d.\n",search,middle+1);
break;
}
else
last=middle-1;
middle=(first+last)/2;
}
if(first>last)
printf("not found! %d isn't presnt in the list.\n",search);
return(0);
}

Actual output:
enter number of elements:4

enter 4 integers:5 4 3 2

enter a value to find 3

3 found at location 2
Exercise 6
a)write a c program to implement sorting of an array
aim: to write a c program to implement sorting of an array.
Algorithm:
Step1: start
Step2: declare array[SIZE],n,i,j,t
Step3: declare variable num
Step4: read variable num
Step5: use for loop for(i=0;i<n;i++)
Step6: use for loop for(j=0; j<n-1;j++)
Step7: if (a[i]>a[j])
t=a[i]
a[i]=a[s]
a[j]=t
Step8: print the sorted elements
Step9: stop
Expected output:
enter number of elements:3

enter a[0]=1

enter a[1}=3

enter a[2]=2

sorted elements… 1 2 3

Source code:
#include<stdio.h>
int main()
{
int a[100],i,j,n,temp;
clrscr();
printf("enter number of elements:");
scanf("%d",&n);
printf("enter elements\n");
/* read array elements */
for(i=0;i<n;i++)
{
printf("enter a[%d]=",i);
scanf("%d",&a[i]);
}
/*bubble sort logic starts here */
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
if(a[i]>a[j]) //*for ascending order<for decending order
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("sorted elements...\n");
for(i=0;i<n;i++)
printf("%3d",a[i]);
printf("\n");
getch();
}
Actual output:
enter number of elements:4

enter a[0]=1

enter a[1]=5

enter a[2]=6

enter a[3]=2

sorted elements...1 2 5 6
Ex6 b)write a c program to add and multiply two m*n
matrices
aim: to write a c program to add and multiply two m*n
matrices
algorithm:
Step1: start

Step2: declare matrix A[i][j],B[i][j];c[i][j];D[i][j] rows= m columns=n;

Matrix addition:
Step3: read m,n,p,q,A,B

Step4: declare variables i=0;j=0

Step5: if m!=p,n!=q then print matrix addition is not possible else

Step6: c[i][j]=A[i][j]+B[i][j]

Print c[i][j]

Matrix multiplication
Step7: check no of rows and columns of 1st and 2nd matrix

Step8: if no of rows in 1st matrix= no of columns of 2nd matrix go to step


9 else print matrix multiplication is not possible

Step9: multiply the matrices using nested loops

Step10: print the product of the matrices

Step11: stop

expected output:
enter no. of rows and coloums of matrix A:2 2

enter the no. of rows and coloums of matrix B: 2 2

enter elements of matrix A

2 2

2 2

enter elements of matrix B

2 2

2 2

result of matrix addition

4 4

4 4

result of matrix addition

8 8

8 8

source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3]={0},d[3][3]={0};
int i,j,k,m,n,p,q;
clrscr();
printf("enter no. of rows and coloums of matrix A:");
scanf("%d%d",&m,&n);
printf("enter the no. of rows and coloums of matrix B:");
scanf("%d%d",&p,&q);
if(m!=p||n!=q)
{
printf("matrix addtion is not possible");
return;
}
else if(n!=p)
{
printf("matrix multiplication not possible");
return;
}
else
{
printf("enter elements of matrix A");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("enter elements of matrix B");
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<n;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\n result of matrix addition\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d",c[i][j]);
printf("\n");
}
for(i=0;i<m;i++)
for(j=0;j<q;j++)
for(k=0;k<p;k++)
d[i][j]+=a[i][k]*b[k][j];
printf("\n result of matrix multiplication\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d",d[i][j]);
printf("\n");
}
}
getch();
}

actual output:
enter no. of rows and coloums of matrix A:2 2

enter the no. of rows and coloums of matrix B:2 2


enter elements of matrix A

1 1

1 1

enter elements of matrix B

2 2

2 2

result of matrix addition

3 3

3 3

result of matrix addition

4 4

4 4

Exercise7
a)write a c program that uses functions to perform the
following operations:
(i) to insert a substring into given main string at a given
position
(ii) to delete n characters from a given position in a
given string
(iii) to replace a character of string either from
beginning or ending or at a specified location
Aim: to insert a substring into given main string at a given
position
Algorithm:
Step1: start

Step2: read main string and sub string

Step3: find the length of the main string

Step4: find the length of the substring

Step6: copy main string into substring

Step7: read the position to insert the substring (p)

Step8: copy temporary string into main string from position p+n

Step9: print the strings

Step10: stop

Expected output:
Enter First String: coter
Enter second String: mpu

Enter the position where the item has to be inserted: 3

computer

Source code:
1. #include <stdio.h>
2. #include <conio.h>
3. #include <string.h>
4.
5. void main()
6. {
7. char a[10];
8. char b[10];
9. char c[10];
10. int p=0,r=0,i=0;
11. int t=0;
12. int x,g,s,n,o;
13. clrscr();
14.
15. puts("Enter First String:");
16. gets(a);
17. puts("Enter Second String:");
18. gets(b);
19. printf("Enter the position where the item has to be inserted: ");
20. scanf("%d",&p);
21. r = strlen(a);
22. n = strlen(b);
23. i=0;
24.
25. // Copying the input string into another array
26. while(i <= r)
27. {
28. c[i]=a[i];
29. i++;
30. }
31. s = n+r;
32. o = p+n;
33.
34. // Adding the sub-string
35. for(i=p;i<s;i++)
36. {
37. x = c[i];
38. if(t<n)
39. {
40. a[i] = b[t];
41. t=t+1;
42. }
43. a[o]=x;
44. o=o+1;
45. }
46.
47. printf("%s", a);
48. getch();
49. }

Actual output:
Enter First String: program9

Enter second String: ming

Enter the position where the item has to be inserted: 7

Programming9

ii) write a c program to delete n characters from a given


position in a given string
aim: to write a c program to delete n characters from a given
position in a given string.
algorithm:
Step1: start
Step2: initialize set from i=0& j=0

Step3: repeate step3 to 6 while*text[i]!=’\0’

Step4:if i=n,then

Repeat step4 while n>=0

Set i=i+1

Set n=n-1

Step5: set new str[j]= text[i]

Step6: set new str[j]=text[i]

Step7: set j=j+1

Step8: set i=i+1

Step9: set new str[j]=’\0’

Step10: stop

expected output:
enter the string:hello world

enter the index position:6

enter the no.of character:5

after deletion the string: hello

source code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st[20],temp[20];
int pos,i,j,ct=0,n;
clrscr();
printf("enter the string");
gets(st);
printf("\nenter the index position:");
scanf("%d",&pos);
printf("\n enter the no.of character:");
scanf("%d",&n);
if(pos<=strlen(st))
{
for(i=0;i<=strlen(st);i++)
{
if(i==pos)
{
for(j=0;st[i]!='\0';j++)
{
temp[j]=st[i];
i++;
}
temp[j]='\0';
i=pos;
for(j=0;temp[j]!='\0';j++)
{
ct++;
if(ct>n)
{
st[i]=temp[i];
ct++;
if(ct>n)
{
st[i]=temp[j];
i++;
}
}
st[i]='\0';
}
}
printf("\n\n after deletion the string: %s",st);
}
printf("\nsorry we cannot delete from that
position");
getch();
}
}
actual output:
enter the string: c programming

enter the index position:6

enter the no.of character:8

after deletion the string: c pro

ex 7c) write a cprogram to replace a character of


string either from beginning or ending or at a specified
location.
aim: to write a cprogram to replace a character of string either
from beginning or ending or at a specified location.
algorithm:
Step1: start
Step
Step
Step
Step
Step
Step
Step
expected output:
source code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50],ch;
int opt,pos,i,len=0;
clrscr();
printf("\nenter string:\n");
gets(str);
printf("\n enter charcator to replace:\n");
ch=getche();
for(i=0;str[i]!='\0';i++)
len++;
printf("\n menu:\n 1.begining\n 2.ending\n
3.specified location \n");
opt:printf("\nenter option:");
scanf("%d",&opt);
switch(opt)
{
case-1:str[0]=ch;
break;
case-2:str[len-1]=ch;
break;
case-3:printf("\n enter position:\n");
scanf("%d",&pos);
if(pos>len)
printf("\n replacing not possible");
else
str[pos]=ch;
break;
default:printf("\n wrong option");
}
printf("\n after replacing characters,string is:\n");
puts(str);
}

actual output:

exercise 8
a) write a c program that uses following functions to
perform the following operation using structure
i) reading complex number ii) writing a complex number
iii) addition of two complex numbers iv) multiplication
of two complex numbers
aim: to write a c program that uses following functions to
perform the following operation using structure
i) reading complex number ii) writing a complex number
iii) addition of two complex numbers iv) multiplication of two
complex numbers
algorithm:
Step1: start

Step2: declare structure for complex number

Step3: read the complex number

Complex numbers addition


Step4: w.img part=w1 img part part +w2 img part

W.real part= w1real part+w2 real part

Multiplication
Step5: w real part= (w1 real part*w2 real part)-(w1 img
part*w2img part)

Step6: print addition and multiplication of the complex numbers

Step7: stop

expected output:
press 1 to add two complex numbers.

press 2 to subtract two complex numbers.

press 3 to multiply two complex numbers.

press 4 to exit.

enter your choice:1

enter a and b where a+ib is the first complex number.

A=4 b=5
C=4 d= 3

sum of the complex numbers=8+8i

source code:
#include<stdio.h>
#include<stdlib.h>
struct complex
{
int real,img;
};
int main()
{
int choice,x,y,z;
struct complex a,b,c;
while(1)
{
printf("press 1 to add two complex numbers.\n");
printf("press 2 to subtract two complex numbers.\n");
printf("press 3 to multiply two complex numbers.\n");
printf(“press 4 to exit\n”)
printf("c\n");
printf("enter your choice \n");
scanf("%d",&choice);
if(choice==5)
exit(0);
if ( choice>=1 && choice<=4)
{
printf("enter a and b where a+ib is the first complex
number.");
printf("\na=");
scanf("%d",&a.real);
printf("b =");
scanf("%d",&a.img);
printf("enter c and d where c+id is the second complex
number.");
printf("\nc =");
scanf("%d",&b.real);
printf("d =");
scanf("%d",&b.img);
}
if(choice==1)
{
c.real=a.real+b.real;
c.img=a.img+b.img;
if(c.img>=0)
printf("sum of the complex numbers=%d+%di",c.real,c.img);
else
printf("sum of the complex numbers=%d%d",c.real,c.img);
}
else if(choice==2)
{
c.real=a.real-b.real;
c.img=a.img-b.img;
if(c.img>=0)
printf("difference of two complex
numbers=%d+%di",c.real,c.img);
else
printf("difference of two complex
numbers=%d%d",c.real,c.img);
}
else if(choice==3)
{
c.real=a.real*b.real-a.img*b.img;
c.img=a.real*b.img+a.img*b.img;
if(c.img>=0)
printf("multiplication of two complex numbers
=%d+%di",c.real,c.img);
else
printf("multiplication of two complex number is
%d%d",c.real,c.img);
}

else
printf("invalid choice.");
printf("\n press any key to enter choice again...\n");
}
}

actual output:
press 1 to add two complex numbers.

press 2 to subtract two complex numbers.


press 3 to multiply two complex numbers.

press 4 to exit.

Enter your choice:2

enter a and b where a+ib is the first complex number.

A=-1 b=11

C=1 d=1

multiplication of two complex number is-10+12i

Exercise: 9
a) Write a C program for the following string operations without using built in functions.

i. to concatenate two strings

ii. to append a string to another string

iii. to compare two strings

i. . To concatenate two strings :- ii. to append a string to another string

Aim: To write a C program for the following string operations without


using built in functions

i. to concatenate two strings

ii. to append a string to another string without using built in functions .

Algorithm:
Step1: Start

Step2: Declare variables i,j and strings str1[50],str2[50]

Step3: Enter the input of both strings from the user


Step4: Read the input strings

Step5: Run for loop

for(i=0;str[i]=’\0’;++i)

for(j=0;str[j]=’\0’;++j)

Str1[i]=str2[j];

Step6: Assign null character to ‘\0’ to str1[i]

Step7: Display output as str1[i]

Step8: Stop

Expected Output:
Enter First string : Hello

Enter Second string : World

After Concatenation : HelloWorld

Source code:
#include<stdio.h>
int main()
{
char str1[50],str2[50],i,j;
clrscr();
printf("\nenter first string");
scanf("%s",str1);
printf("\nenter second string");
scanf("%s",str2);
for(i=0;str1[i]!='\0';++i)
for(j=0;str2[j]!='\0';++j,++i)
{
str1[i]=str2[j];
}
str1[i]='\0';
printf("\noutput:%s",str1);
return(0);
}

Actual Output:
Enter First string : C

Enter Second string : programming

After Concatenation : Cprogramming


Ex 9)iii. To compare two strings
Aim:To write C program for the comparision of two strings
Algorithm:
Step1: Start

Step2: Declare two input strings from the user and store it to some
variable names

Step3: Declare another variables i,j,flag and initiate them to 0

Step4:Enter input string for name 1 and name 2

Step5: Read the input strings

Step6: Using while loop check if the trings are equal or not

Step7:Using for loop check if flag is equal to 1

Then both strings will be equal elseif flag is equal to 0 ,then both
stings are not equal
Step8: Display result

Step9: Stop
Expected Output:
Enter name 1 : Computer

Enter name 2: Computer

Both names are equal

Source code:
#include<stdio.h>
int main()
{
char str1[100],str2[100];
int result,i;
clrscr();
printf("\nenter first string");
gets(str1);
printf("\nenter second string");
gets(str2);
for(i=0;str1[i]==str2[i]&&str1[i]!='\0';++i)
if(str1[i]<str2[i])
{
printf("\nstr1 is less than str2");
}
else if(str1[i]>str2[i])
{
printf("\n str2 is less than str1");
}
else
{
printf("\n str1 is equal to str2");
}
return(0);
}

Actual Output:
Enter name 1 : Computer

Enter name 2: programming

Both names are not equal


Exercise: 10
a) write a c program to find no of characters in a
given string including spaces
aim: write a c program to find no of characters in a given
string including spaces
algorithm:
Step1: start

Step2: initialize string

Step3: declare variables count and int

Step4: run for loop to find no of characters in the string

Step5: display the count value

Step6: stop

expected output:
Total number of characters in a string: 19
source code:
1. #include <stdio.h>
2. #include <string.h>
3.
4. int main()
5. {
6. char string[] = "The best of both worlds";
7. int count = 0;
8.
9. //Counts each character except space
10. for(int i = 0; i < strlen(string); i++) {
11. if(string[i] != ' ')
12. count++;
13. }
14.
15. //Displays the total number of characters present in the given string
16. printf("Total number of characters in a string: %d", count);
17.
18. return 0;
19. }

actual output:
Total number of characters in a string: 19
b) Write a C program to copy the contents of one string to
another string without using
string handling function.

Aim: To write a C program to copy the contents of one string to


another string without using string handling function.

Algorithm:
Step 1: Start

Step 2: Declare strings s1[ ]=”Bhavya”, s2[100]

Step 3: Declare variable i

Step 4: Print String s1

Step 5: Run for loop

for(i=0;s1[i]!=’\o’;i++)

s2[i]=s1[i];

Step 6: Assign s2[i] as null character ‘\o’.

Step 7: Print string s2

Step 8: Stop
Expected Output:
String s1: Computer

String s2: Computer

Source code:
#include<stdio.h>

int main()
{
char s1[100], s2[100];
int i;

printf("\nEnter the string :");


gets(s1);

i = 0;
while (s1[i] != '\0') {
s2[i] = s1[i];
i++;
}

s2[i] = '\0';
printf("\nCopied String is %s ", s2);

return (0);
}

ACTUAL Output:
String s1: Program

String s2: Program


Ex 10c) Write a C program to find whether a given string is
Palindrome or not.

Aim: To write C program to find whether a given string is Palindrome or not.


ALgoRithm:
Step 1: Start

Step 2: Declare variables i,len,flag,str[100]

Step 3: Initialize flag to 0

Step 4: Read the string

Step 5: Assign len as strlen(str);

len=strlen(str);

Step 6: Run for loop

for(i=0;i<len;i++)

if(str[i]!=str[len-i-1])

flag=1;

break;

Step 7: if flag=0 Print string is palindrome

else print string is not palindrome.

Step 8: Stop
Expected Output:
Please enter any string: Racecar

Racecar is palindrome string

Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
int i,len,flag;
flag=0;
clrscr();
printf("\nplease enter any string:");
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]!=str[len-i-1])
{
flag=1;
break;
}
}
if(flag==0)
{
printf("\n %s is a palidrome string",str);
}
else
{
printf("\n %s is not a palidrome string",str);
}
return(0);
}

ACTUAL Output:
Please enter any string: MOM

MOM is palindrome string

Exercise 11
write a c program using recursion for the following.
a)to display sum of digits of given number.
Aim: to write a c program using recursion to display sum of digits of
given number.

Algorithm:
Step1: start

Step2: declare the function int sum(int a) in global declaration

Step3: declare the variables num ,result

Step4: read variables num as required number that to be taken from


user

Step5: assign sum(num) to result

Step6: display result as result variable

Step7: sum(int num) function is as follows

(i) If num!=0 , return (n%10+sum(1/10))


(ii) Else return(0).

Step8: stop.

Expected output:
enter the number:123

sum of digits in 123 is 6

Source code:
#include<stdio.h>
int sum(int a);
int main()
{
int num,result;
printf("enter the number:");
scanf("%d",&num);
result=sum(num);
printf("sum of digits in %d is %d\n",num,result);
return(0);
}
int sum(int num)
{
if(num!=0)
{
return(num%10+sum(num/10));
}
else
{
return(0);
}
}

Actual output:
enter the number:196

sum of digits in 196 is 16

ex11 b)write a c program using recursion to find


factorial of a given integer
aim: to write a c program using recursion to find factorial of a
given integer
algorithm:
Step1: start
Step2: declare the function prod()

Long int prod(int n)in global declaration

Step3: declare the variable n

Step4: read the variables n as the input from the user

Step 5: display result as prod(n)

Step 6:stop

Step 7: function prod() is as follows

(i) Long int prod(int n)


(ii) If n>=1 return n*prod(n-1)
(iii) Else return 1

expected output:
enter positive integer:4

factorial of 4 is 24

source code:
#include<stdio.h>
long int multiplynumber(int n);
int main()
{
int n;
clrscr();
printf("enter positive integer:");
scanf("%d",&n);
printf("factorial of %d is %1d",n,multiplynumber(n));
return(0);
}
long int multiplynumber(int n)
{
if(n>=1)
return n*multiplynumber(n-1);
else
return 1;
}

actual output:
enter positive integer:5

factorial of 5 is 120

11 c)write a c program using recursion to find gcd of


two given integers
aim: to write a c program using recursion to find gcd of two
given integers .
algorithm:
Step1: start

Step2: declare functions hcf() in global declaration int hcf (int n1,int n2)

Step3: declare variables n1 and n2 as integers values from the user


Step4: read variables n1 and n2 as integers values from user

Step5: display the gcd of two numbers as hcf(n1,n2)

Step6: stop

Step7: function hcf() is follows

(i) Int hcf(int n1,int n2)


(ii) If n2!=0 return hcf(n2,n1%n2)
(iii) Else return 1

expected output:
enter two positive integers: 1 9

G.C.D of 1 and 9 is 1

source code:
#include<stdio.h>
int hcf(int n1,int n2);
int main()
{
int n1,n2;
clrscr();
printf("enter two positive integers:");
scanf("%d%d",&n1,&n2);
printf("G.C.D of %d and %d is %d",n1,n2,hcf(n1,n2));
return(0);
}
int hcf(int n1,int n2)
{
if(n2 !=0)
return hcf(n2,n1%n2);
else
return n1;
}
actual output:
enter two positive integers: 360 60

G.C.D of 360 and 60 is 6

Ex 11 d) write a c program using recursion for the


finding Fibonacci sequence
Aim: to write a c program using recursion for the finding
Fibonacci sequence
Algorithm:
Step1: start

Step2: declare the function Fibonacci() In global declaration int


Fibonacci(int)

Step3: declare variable n,i=0,c;

Step4: read the variable n


Step5: run for loop

For(c=1;c<=n;c++)

Print Fibonacci(i)

i++

Step6: stop

Step7: function Fibonacci() is as follows

(i) Int Fibonacci(int n)


(ii) If n==0,return0
(iii) Else if n<=1,return 1
(iv) Else ,return( Fibonacci(n-1)+Fibonacci(n-2)

Expected output:
enter number of terms:3

fibonacci series 1 1 2

Source code:
#include<stdio.h>
int fibonacci(int);
int main()
{
int n,i=0,c;
printf("enter number of terms");
scanf("%d",&n);
printf("fibonacci series...\n");
for(c=1;c<=n;c++)
{
printf("%d\n",fibonacci(i));
i++;
}
return(0);
}
int fibonacci(int n)
{
if(n==0)
return(0);
else if(n==1)
return 1;
else
return(fibonacci(n-1)+fibonacci(n-2));
}

Actual output:
enter number of terms:5

fibonacci series:1 1 2 3 5.

Exercise 12
a)write a c program to reverse a string using pointers
aim: to write a c program to reverse a string using pointers
algorithm:
Step1:start

Step2: declare the characters variable *s

Step3: declare the integer variables len,i

Step4: enter the input of the string

Step5:read the string

Step6: assign strlen(s) to len


Step7: display reverse of string using for loop

Step 8: stop

expected output:
enter a string: computer

the reverse of string is: retupmoc

source code:
#include<stdio.h>
#include<conio.h>
void main()
{
char*s;
int len,i;
clrscr();
printf("\n enter a string:");
gets(s);
len=strlen(s);
printf("\n the reverse of string is:");
for(i=len;i>=0;i--);
printf("%c",*(s+1));
getch();
}

actual output:
enter a string: math

the reverse of string is: htam


Ex12 b) write a c program to compare two 2d arrays
using pointers
Aim: to write a c program to compare two 2d arrays using
pointers
Algorithm:
Step1:start

Step2: declare the function compare(int*


a1,int*a2,int*n)

Step3: declare variables flag=0,i

Step4: run for loop for (i=0,i<n,i++)

if(*a1!=*a2) the flag=1

Step5: a1++, a2++

Step6:if flag=1 print both are not equal

Step7: else print both are equal


Step8: function main(int arg c,char **argv)

Step9: declare variables n,i

Step10:take input as number between 1 to 10

Step11: take array elements of array 1,and array 2

Step12: run for loop

Step13: compare(a1,a2,n)

Step14: stop

Expected output:
Enter a number between 1 and 10: 4

Enter 4 numbers for array 1: 3 4 5 6

Enter 4 numbers for array 2: 4 5 6 7

Both arrays are not equal

Source code:
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>

void compare(int *a1, int *a2, int n)


{
int i, flag = 0;
for(i = 0; i < n; i++)
{
if(*a1 != *a2)
{
flag = 1;
break;
}
a1++;
a2++;
}
if(flag == 1)
printf("\nBoth arrays are not equal");
else
printf("\nBoth arrays are equal");
}

int main(int argc, char **argv)


{
int a1[10], a2[10];
int n, i;
printf("Enter a number between 1 and 10: ");
scanf("%d", &n);
printf("Enter %d numbers for array 1: ", n);
for(i = 0; i < n; i++)
scanf("%d", &a1[i]);
printf("Enter %d numbers for array 2: ", n);
for(i = 0; i < n; i++)
scanf("%d", &a2[i]);
compare(a1, a2, n);
getch();
return 0;
}

Actual output:
Enter a number between 1 and 10: 3

Enter 3 numbers for array 1: 1 2 3

Enter 3 numbers for array 2: 1 2 3

Both arrays are equal


Ex12 c)write a c program consisting of pointers based
function to exchange value of two integers using
passing by address.
Aim: to write a c program consisting of pointers based
function to exchange value of two integers using passing by
address.
Algorithm:
Step1: start

Step2:declare void swap(int *a ,int*b)

(i) Declare variable t


(ii) Assign *a to t
(iii) Assign *b to *a
(iv) Assign t to *b

Step3: declare num1,num2

Step4:take values num1,num2 from user

Step5:read the variable num1 and num2

Step6: print the variables num1 and num2 before swapping as


num1,num2
Step7:swap (&num1,&num2)

Step8:print the output

Step9: stop

Expected output:
enter the value of num1:1

enter the value of num2:3

before swapping:num1=1,num2=3

after swapping:num1=3,num2=1

Source code:
#include<stdio.h>
void swap(int*a,int*b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
int main()
{
int num1,num2;
clrscr();
printf("enter the value of num1:");
scanf("%d",&num1);
printf("enter the value of num2:");
scanf("%d",&num2);
printf("before swapping:num1=%d,num2=%d\n",num1,num2);
swap(&num1,&num2);
printf("after swapping:num1=%d,num2=%d\n",num1,num2);
return(0);
}
Actual output:
enter the value of num1: 6

enter the value of num2: 5

before swapping:num1=6,num2=5

after swapping:num1=5,num2=6

You might also like