C Lab Solutions
C Lab Solutions
EXERCISE :1
a) Write a c program to calculate area of triangle.
Step5: stop.
Expected output:
enter the value of base of triangle:4
Actual output:
enter the value of base of triangle:6
Algorithm :
Step1 : start.
Step6 : stop
Expected output :
Enter 3 numbers :5 4 7 .
Actual output
Expected output :
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]
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;
/*
* 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';
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
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
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
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=a+b
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
Step2: read n.
Step8:increment i
Step10: stop
Expected output:
prime no series:
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:
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
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
Algorithm:
Step 1: start
If(k&1)→0
else→0
step 7: stop
Expected output:
enter a integer in decimal number system:10
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
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
Step10: else
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
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
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
For(i=0;i<n;i++)
If( arr[i]==num)
Found=1;
Pos=I;
Step8: stop
Expected output:
enter number of elements:3
enter elements
a[o]=3
a[1]=4
a[2]=5
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
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
Step4: middle=first+last/2
Step7: stop
Expected output:
enter number of elements: 3
enter 3 integers: 1 2 3
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
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
Matrix addition:
Step3: read m,n,p,q,A,B
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
Step11: stop
expected output:
enter no. of rows and coloums of matrix A:2 2
2 2
2 2
2 2
2 2
4 4
4 4
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
1 1
1 1
2 2
2 2
3 3
3 3
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
Step8: copy temporary string into main string from position p+n
Step10: stop
Expected output:
Enter First String: coter
Enter second String: mpu
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
Programming9
Step4:if i=n,then
Set i=i+1
Set n=n-1
Step10: stop
expected output:
enter the string:hello world
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
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
Multiplication
Step5: w real part= (w1 real part*w2 real part)-(w1 img
part*w2img part)
Step7: stop
expected output:
press 1 to add two complex numbers.
press 4 to exit.
A=4 b=5
C=4 d= 3
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 4 to exit.
A=-1 b=11
C=1 d=1
Exercise: 9
a) Write a C program for the following string operations without using built in functions.
Algorithm:
Step1: Start
for(i=0;str[i]=’\0’;++i)
for(j=0;str[j]=’\0’;++j)
Str1[i]=str2[j];
Step8: Stop
Expected Output:
Enter First string : Hello
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
Step2: Declare two input strings from the user and store it to some
variable names
Step6: Using while loop check if the trings are equal or not
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
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
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.
Algorithm:
Step 1: Start
for(i=0;s1[i]!=’\o’;i++)
s2[i]=s1[i];
Step 8: Stop
Expected Output:
String s1: Computer
Source code:
#include<stdio.h>
int main()
{
char s1[100], s2[100];
int i;
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
len=strlen(str);
for(i=0;i<len;i++)
if(str[i]!=str[len-i-1])
flag=1;
break;
Step 8: Stop
Expected Output:
Please enter any string: Racecar
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
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
Step8: stop.
Expected output:
enter the number:123
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
Step 6:stop
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
Step2: declare functions hcf() in global declaration int hcf (int n1,int n2)
Step6: stop
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
For(c=1;c<=n;c++)
Print Fibonacci(i)
i++
Step6: stop
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
Step 8: stop
expected output:
enter a string: computer
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
Step13: compare(a1,a2,n)
Step14: stop
Expected output:
Enter a number between 1 and 10: 4
Source code:
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
Actual output:
Enter a number between 1 and 10: 3
Step9: stop
Expected output:
enter the value of num1:1
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
before swapping:num1=6,num2=5
after swapping:num1=5,num2=6