CCP Lab Manual
CCP Lab Manual
1A)Design,develop and execute a program in c to find and output all the roots
of a given quadratic equation, for non-Zero coefficients.
#include<stdio.h>
/*preprocessor dirictives*/
#include<conio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
float a,b,c,root1,root2;
/* local declaration*/
float realp,imagp,d;
clrscr();
printf("Enter the values of A B and C \n");
scanf("%f%f%f",&a,&b,&c);
if (a==0||b==0||c==0)
{
printf("\n roots cannot be determined \n");
exit(1);
}
else
{
d=(b*b)-(4.0*a*c);
/* this block of code used to find the complex roots*/
if(d==0)
{
printf("\n roots are real and equal");
root1=-b/(2.0*a);
root2=-b/(2.0*a);
CSE
Page 1
}
/* this block of code used to find the equal roots*/
else if(d>0)
{
printf("\n roots are real and distinct \n");
root1=(-b+sqrt((b*b)-(4.0*a*c)))/(2.0*a);
root2=(-b-sqrt((b*b)-(4.0*a*c)))/(2.0*a);
printf("\n root1 = %f",root1);
printf("\n root2 = %f",root2);
}
/* this block of code used to find the distinct roots*/
else
{
printf("\n complex roots");
realp=-b/(2.0*a);
imagp=sqrt(fabs((b*b)-(4.0*a*c)))/(2.0*a); //abs() is used to get only positive
values
printf("\n root1 =%f + i %f",realp,imagp);
printf("\n root2 =%f - i %f",realp,imagp);
}
}
getch();
} /* End of main function */
CSE
Page 2
OUTPUT:
1.Enter the values of A B and C
1 2 3
roots are real and equal
root1= -1.000000
root2=-1.000000
2.Enter the values of A B and C
-1 2 3
roots are real and distinct
root1= -1.000000
root2=3.000000
3.Enter the values of A B and C
2 4 6
Complex roots
root1= -1.000000+i 1.414214
root2= -1.000000-i 1.414214
4.Enter the values of A B and C
0 1 2
roots cannot be determined.
CSE
Page 3
clrscr();
printf("Enter the values for M and N \n");
scanf("%d%d",&m,&n);
a=m;
b=n;
while(n!=0) //While loop to repeat till the value of n is zero
{
rem=m%n;
m=n;
n=rem;
}
gcd=m;
lcm=(a*b)/gcd;
printf("The GCD of %d and %d is %d\n",a,b,gcd);
printf("The LCM of %d and %d is %d\n",a,b,lcm);
getch();
}
CSE
Page 4
OUTPUT:
1.Enter the values for M and N
8 4
The GCD of 8 and 4 is 4.
The LCM of 8 and 4 is 8.
2.Enter the values for M and N
24 32
The GCD of 24 and 32 is 8.
The LCM of 24 and 32 is 96.
CSE
Page 5
clrscr();
printf("Enter the Number to be Reversed:");
scanf("%d",&n);
m=n;
rev=0;
/* This block will reverse the given number */
while(n!=0)
{
dig=n%10;
rev=rev*10+dig;
n=n/10;
}
printf("The given number is %d\n",m);
printf("The reversed number is %d\n",rev);
if(m==rev) // Check the equality of the given number and reversed number
{
printf("The given mumber is palindrome\n");
}
else
CSE
Page 6
OUTPUT:
1.Enter the number to be Reversed
1221
The given number is 1221
The reversed number is 1221
The given number is palindrome.
2.Enter the number to be Reversed
1234
The given number is 1234
The reversed number is 4321
The given number is not a palindrome.
CSE
Page 7
CSE
Page 8
CSE
Page 9
}
else
{
space=0;
CSE
Page 10
}
getch();
}
OUTPUT:
1.Enter string with spaces
CBIT
CSE
KOLAR
CSE
Page 11
Page 12
OUTPUT:
1.Enter the number of elements
5
Enter the array elements in ascending order:
10 20 30 40 50
Enter the elements to search:40
The elements is found at position:4
CSE
Page 13
Page 14
OUTPUT:
1.Enter n value
5
Enter array elements to be searched
50 20 45 10 12
Sorted array elements are:
10 12 20 45 50
2.Enter n value
6
Enter array elements to be searched
65 70 90 15 100 10
Sorted array elements are:
10 15 65 70 90 100
CSE
Page 15
void main()
{
int c;
clrscr();
c=wordlength();
printf("\n\nWordlength of machine is %d bits",c);
getch();
}
CSE
Page 16
CSE
Page 17
Page 18
OUTPUT:
Value after using User defined function
e^0.5000000=1.648721
Using library function
e^0.5000000=1.648721
CSE
Page 19
#include<stdio.h>
#include<conio.h>
void read(int a[20][20],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
}
void product(int a[20][20],int b[20][20],int c[20][20],int m,int q,int n)
{
int i,j,k;
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
CSE
Page 20
Page 21
CSE
Page 22
6 5 7
0 0 0
4 8 9
Matrix A:
2 4 3
4 5 0
2 3 4
Matrix B:
6 5 7
0 0 0
4 8 9
Matrix (A*B):
24 34 41
24 20 28
28 42 50
2.Enter the no of rows and columns in A matrix A(m*n):3 2
Enter the no of rows and columns in B matrix B(p*q):4 5
Matrix cannot be multiplied.
CSE
Page 23
Page 24
" , c[i]);
OUTPUT:
Enter the number of elements 5
Enter the element of 1st array 2 7 4 5 3
Enter the element of 2st array 3 2 5 1 6
The contents of array A
a[0]=2
a[1]=7
a[2]=4
a[3]=5
a[4]=3
The contents of array B
b[0]=3
b[1]=2
b[2]=5
b[3]=1
b[4]=6
c[0]=5,thread_id=0
c[1]=9,thread_id=1
c[2]=9,thread_id=2
c[3]=6,thread_id=0
c[4]=9,thread_id=1
The new array is====5 9 9 6 9
CSE
Page 25
}
return n;
}
void main()
{
unsigned int n,value;
int x;
clrscr();
printf("Enter an unsigned interger <=65535\n");
scanf("%d",&n);
printf("Enter how many bits to be rotated");
scanf("%d",&x);
printf("\nRight rotation of %d by %d bits is:",n,x);
CSE
Page 26
OUTPUT:
1.Enter an unsigned integer <=65535
58
Enter how many bits to be rotated
4
Right rotation of 58 by 4 bits is 3.
2.Enter an unsigned integer <=65535
8
Enter how many bits to be rotated
2
Right rotation of 8 by 2 bits is 2.
CSE
Page 27
#include<stdio.h>
#include<conio.h>
int isprime(int num)
{
int i;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
return 0;//number is not a prime
}
return 1;
}
void main()
{
int number;
clrscr();
printf("Enter the number\n");
scanf("%d",&number);
if(isprime(number))
printf("%d is a prime number\n",number);
else
CSE
Page 28
OUTPUT:
1.Enter the number
101
101 is a prime number.
1.Enter the number
21
21 s not a prime number.
CSE
Page 29
CSE
Page 30
CSE
Page 31
Page 32
OUTPUT:
1.Enter the string CBIT
String before reverse is CBIT.
String after reverse is TIBC.
2.Enter the string COMPUTER
String before reverse is COMPUTER.
String after reverse is RETUPMOC.
CSE
Page 33
location in the string s1 where any character from the string s2 occurs, or 1
if s1 contains no character from s2. Do not use the standard library function
which does a similar job! Invoke the function match any (s1. s2) from the
main for different strings and print both the strings and the return value from
the function match any (s1,s2).
#include<stdio.h>
#include<conio.h>
/*inbuilt fuction that does similar job is strcspn()*/
int matchany(char str1[],char str2[])
{
int i,j;
char symbol;
for(i=0;str2[i]!='\0';i++)
{
symbol=str2[i];
for(j=0;str1[j]!='\0';j++)
{
if(symbol==str1[j])
return j+1;
}
}
return -1;
}
void main()
{
char str1[20],str2[20];
CSE
Page 34
OUTPUT:
1.Enter the string
India
Enter the string to search
area
Match in character is found at pos 5.
2.Enter the string
Karnataka
Enter the string to search
me
No character in me is present in karnataka.
CSE
Page 35
CSE
Page 36