You are on page 1of 29

Experiment 5.

Write a C program to find the roots of a quadratic equation for non-zero coefficients
(Using if else statement).
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,root1,root2,partr,parti,disc;
clrscr();
printf("Input the coefficients of the quadratic equation\n");
scanf("%f%f%f",&a,&b,&c);
disc=b*b-4*a*c;
if(disc==0)
{
printf("The roots are real and equal\n");
root1=-b/(2.0*a);
root2=root1;
printf("\n root1=%f\t root2=%f\n",root1,root2);
}
else if(disc>0)
{
printf("The roots are distinct\n");
root1=(-b+sqrt(disc))/(2.0*a);
root2=(-b-sqrt(disc))/(2.0*a);
printf("root1=%f\t root2=%f\n",root1,root2);
}
else
{
printf("The roots are complex\n");
partr=-b/(2.0*a);
parti=sqrt(-disc)/(2.0*a);
printf("\n root1=%f+i%f\t",partr,parti);
printf("\n root2=%f-i%f\t",partr,parti);
}
getch();
}

Nitin Kumar
E1-13
Output:

Input the coefficients of the quadratic equation


1 -4 4
The roots are real and equal
root 1 =2.000000 root2=2.000000

Input the coefficients of the quadratic equation


1 -5 6
The roots are distinct
root 1=3.000000 root2=2.000000

Input the coefficients of the quadratic equation


6 2 4
The roots are complex
root 1=-0.1 66667+i0.799305
root2=-0. 1 66667-i0.799305

Nitin Kumar
E1-13
Experiment 6.
Write a C program to simulate a simple calculator that performs arithmetic
operations like addition, subtraction, multiplication and division only on integers.
Error message should be reported, if any attempt is made to divide by zero.
(Use switch statement).

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,res,op;
clrscr();
printf("Enter the values for a and b\n");
scanf("%d%d",&a,&b);
printf("Enter the choice\n");
scanf("%d",&op);
switch(op)
{
case 1:res=a+b;
printf("The addition of two numbers is %d",res);
break;
case 2:res=a-b;
printf("The subtraction of two numbers is %d",res);
break;
case 3:res=a*b;
printf("The multiplication of two numbers is %d",res);
break;
case 4:if(b!=0)
{
res=a/b;
printf("The division of two numbers is %d",res);
break;
}
else
{
printf("Divide by zero error");
getch();
exit(0);
}
default:printf("Invalid Choice\n");
exit(0);
}
getch();
}

Nitin Kumar
E1-13
Output:

Enter the values for a and b


2 3
Enter the choice
1
The addition of two numbers is 5

Enter the values for a and b


4 5
Enter the choice
2
The subtraction of two numbers is -1

Enter the values for a and b


6 8
Enter the choice
3
The multiplication of two numbers is 48

Enter the values for a and b


10 2
Enter the choice
4
The division of two numbers is 5

Enter the values for a and b


2 0
Enter the choice
4
Divide by zero error

Nitin Kumar
E1-13
Experiment 7.
Write a C program to generate and print first n Fibonacci numbers (Using looping
constructs).
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,term1,term2,term;
clrscr();
printf("Enter the value of n\n");
scanf("%d",&n);
term1=0;
term2=1;
if(n==1)
printf("%d",n);
else
{
printf("%d%2d",term1,term2);
for(i=3;i<=n;i++)
{
term=term1+term2;
printf("%3d",term);
term1=term2;
term2=term;
}
}
getch();
}

Output:

Enter the value of n:


5
01 1 2 3
Enter the value of n:
10
0 1 1 2 3 5 8 13 21 34

Nitin Kumar
E1-13
Experiment 8.
Write a C program to find the GCD and LCM of two integers output the result along
with the given integer using Euclids algorithm.
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,p,q,r,gcd,lcm;
clrscr();
printf("Input two integers\n");
scanf("%d%d",&m,&n);
p=m;
q=n;
while(n!=0)
{
r=m%n;
m=n;
n=r;
}
gcd=m;
lcm=(p*q)/gcd;
printf("gcd(%d,%d)=%d\n",p,q,gcd);
printf("Icm(%d,%d)=%d\n",p,q,lcm);
getch();
}

Output:

Input two integers


4 5

gcd(4,5)= 1

lcm(4,5)=20

Nitin Kumar
E1-13
Experiment 9.
Write a C program to reverse a given four digit number and check whether it is a
palindrome or not. Output the given number with a suitable message (Using looping
constructs).

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,rev,digit;
clrscr();
printf("Enter a four digit number\n");
scanf("%d",&n);
m=n;
rev=0;
while(n!=0)
{
digit=n%10;
n=n/10;
rev=rev*10+digit;
}
printf("The reverse is %d\n",rev);
if(m==rev)
printf("The number is a palindrome");
else
printf("The number is not a palindrome");
getch();
}

Output:

Enter a four digit number


1221
The reverse is 1221
The number is a palindrome

Enter a four digit number


5678
The reverse is 8765
The number is not a palindrome

Nitin Kumar
E1-13
Experiment 10.
Write a C program to find whether a given number is prime or not. Output the given
number along with a suitable message (Using looping construct).

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,fact=0;
clrscr();
printf("Enter a number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
fact=fact+1;
}
if(fact<=2)
printf("%d is a prime number\n",n);
else
printf("%d is not a prime number\n",n);
getch();
}

Output:

Enter a number:
2
2 is a prime number

Enter a number:
6
6 is not a prime number

Nitin Kumar
E1-13
Experiment 11.
Write a C program to input N real numbers in ascending order into a single
dimensional array. Conduct a binary search for a given key integer number and
report success or failure in the form of a suitable message.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,low,high,mid,key;
clrscr();
printf("Input the size of the array\n");
scanf("%d",&n);
printf("Input the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the number to be searched\n");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
printf("Element %d is found \n",key);
getch();
exit(0);
}
if(key>a[mid])
low=mid+1;
else
high=mid-1;
}
printf("Element %d is not found",key);
getch();
}

Nitin Kumar
E1-13
Output:

Input the size of the array


5

Input the array elements


1 2 3 4 5

Enter the number to be searched


5

Element 5 is found

Input the size of the array


5

Input the array elements


5 6 7 8 9

Enter the number to be searched


3

Element 3 is not found

Nitin Kumar
E1-13
Experiment 12.
Write a C program to input N integer numbers into a single dimensional array. Sort
them In ascending order using bubble sort technique. Print both the given array arid
sorted array with suitable headings.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,j,temp;
clrscr();
printf("Input the size of the array\n");
scanf("%d",&n);

printf("Input the array elements\n");


for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe original array is\n");

for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nThe sorted array is\n\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

Nitin Kumar
E1-13
Output:

Input the size of the array


5

Input the array elements


9 5 7 6 2

The original array is:


9 5 7 6 2

The sorted array is:


2 5 6 7 9

Nitin Kumar
E1-13
Experiment 13.
Write a C program to evaluate the given polynomial f(x)=a4x4 a3x3 a2x2 +a1x
+a0x for given value of x and the coefficients using Homers method. (Using single
dimension array to store coefficients).

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
float a[10],sum=0,x;
clrscr();
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter the value of x\n");
scanf("%f",&x);
printf("Enter n+1 values into the array\n");
for(i=0;i<=n;i++)
scanf("%f",&a[i]);
sum=a[n]*x;
for(i=n-1;i>0;i--)
{
sum=(sum+a[i])*x;
}
sum=sum+a[0];
printf("Evaluated result %f\n",sum);
getch();
}

Output:

Enter the value of n


3
Enter the value of x
2
Enter n+1 values into the array
1 2 3 4
Evaluated result=49.000000

Nitin Kumar
E1-13
Experiment 14.
Write a C program to read two matrices A(MxN) and B(PxQ) and compute the
product of A and B after checking compatibility for multiplication. Output the input
matrices and the resultant matrix with suitable headings and format. (Using two
dimension arrays where array size M, N,P,Q<3).

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,p,q,k,m,n,sum;
clrscr();
printf("Input the size of the matrix1\n");
scanf("%d%d",&m,&n);
printf("Input the size of matrix2\n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Matrix multiplication is not possible\n");
getch();
exit(0);
}
printf("Input the elements of matrix1\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Input elements of matrix2\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<n;j++)
{
sum=0;
for(k=0;k<n;k++)
{
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
}
}

Nitin Kumar
E1-13
printf("Product is \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("\t%d",c[i][j]);
}
printf("\n");
}
getch();
}

Output:

Input the size of the matrix1


2 2
Input the size of matrix2
2 2
Input the elements of matrix1
1 2 3 4
Input elements of matrix2
1 2 3 4
Product is
7 10
15 22

Nitin Kumar
E1-13
Experiment 15.
Write C user defined functions:
1. To input N integer numbers into a single dimension array.
2. To conduct a linear search.
Using these functions, write a C program to accept the N integer numbers & given
key integer number and conduct a linear search. Report success or failure in the
form of a suitable message.

#include<stdio.h>
#include<conio.h>
void getdata(int);
void search(int a[],int);
int a[10],n;
void main()
{
clrscr();
printf("Enter the size of the array\n");
scanf("%d",&n);
getdata(n);
search(a,n);
}
void getdata(int n)
{
int i;
printf("Input array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void search(int a[],int n)
{
int i,key;
printf("Input key element \n");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{

Nitin Kumar
E1-13
printf("Element found\n");
getch();
exit(0);
}
}
printf("Element not found\n");
getch();
}

Output:

Enter the size of the array


5
Input array elements
1 8 9 5 4
Input key element
9
Element found

Enter the size of the array


4
Input array elements
5 9 87 32
Input key element
1
Element not found

Nitin Kumar
E1-13
Experiment 16.
Write C user defined functions:
1. To input N integers into a single dimension array.
2. To sort the integer numbers in ascending order using bubble sort technique.
3. To print the single dimension array elements.
Using these functions, write a C program to input N integer numbers
into a single dimension array, sort them in ascending order, and print both the given
array & the sorted array with suitable headings.

#include<stdio.h>
#include<conio.h>
void getdata();
void bubsort();
void putdata();
int a[10],i,n,temp,j;

void main()
{
clrscr();
getdata();
printf("The unsorted array is\n");
putdata();
bubsort();
printf("The sorted array is\n");
putdata();
getch();
}

void getdata()
{
printf("Enter the array size\n");
scanf("%d",&n);
printf("Input the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}

void putdata()
{
for(i=0;i<n;i++)
printf("\n%d\n",a[i]);
}

Nitin Kumar
E1-13
void bubsort()
{
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

Output:

Enter the array size


5

Input the array elements


9 8 7 6 5

The unsorted array is:


9
8
7
6
5

The sorted array is:


5
6
7
8
9

Nitin Kumar
E1-13
Experiment 17.
Write C user defined functions:
1. To input N integers into a single dimension array.
2. To sort the integer numbers in descending order using selection sort
Technique.
3. To print the single dimension array elements.
Using these functions, write a C program to input N integer numbers into a single
dimension array, sort them in descending order, and print both the given array &
the sorted array with suitable headings.

#include<stdio.h>
#include<conio.h>
void getdata();
void selsort();
void putdata();
int a[10],i,n,temp,j,minpos;
void main()
{
clrscr();
getdata();
printf("The unsorted array is\n");
putdata();
selsort();
printf("The sorted array is\n");
putdata();
getch();
}

void getdata()
{
printf("Enter the array size\n");
scanf("%d",&n);
printf("Input the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}

void putdata()
{
for(i=0;i<n;i++)
printf("\n%d\n",a[i]);
}

Nitin Kumar
E1-13
void selsort()
{
for(i=0;i<n-1;i++)
{
minpos=i;
for(j=i+1;j<n;j++)
{
if(a[j] > a[minpos])
{
minpos=j;
}
}
temp=a[minpos];
a[minpos]=a[i];
a[i]=temp;
}
}

Output:

Enter the array size


5

Input the array elements


8 6 4 9 3

The unsorted array is:


8
6
4
9
3

The sorted array is:


9
8
6
4
3

Nitin Kumar
E1-13
Experiment 18.
Write C user defined functions:
1. To input N integers into a single dimension array.
2. Compute their mean.
3. Compute their variance.
4. Compute their standard deviation.
Using these functions, write a C program to input N real integer numbers into a
single dimension array, and compute their mean, variance & standard deviation.
Output the computed results with suitable headings.

#include<stdio.h>
#include<conio.h>
#include<math.h>
int a[10],n,i;
float sum=0.0,sum1=0.0;
float mean1,var1,std1;
void mean();
void variance();
void stdev();
void getdata();

void main()
{
clrscr();
getdata();
mean();
variance();
stdev();
getch();
}

void getdata()
{
printf("Enter the array size\n");
scanf("%d",&n);
printf("Input the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}

Nitin Kumar
E1-13
void mean()
{
for(i=0;i<n;i++)
sum=sum+a[i];
mean1=sum/n;
printf("Mean=%f\n",mean1);
}

void variance()
{
for(i=0;i<n;i++)
sum1=sum1+(a[i]-mean1)*(a[i]-mean1);
var1=sum1/n;
printf("Variance=%f\n",var1);
}

void stdev()
{
std1=sqrt(var1);
printf("Deviation=%f\n",std1);
}

Output:

Enter the array size


5

Input the array elements


8 7 4 5 9

Mean=6.600000
Variance=3 .440000
Deviation=1 .854724

Nitin Kumar
E1-13
Experiment 19.
Write C user defined functions:
1. To read elements of a given matrix of size MxN.
2. To print elements of a given matrix of size MxN.
3. To compute the product of two matrices.
Using these functions, write a C program to read two matrices A(MxN) and B(PxQ)
and compute the product of A and B after checking compatibility for multiplication.
Output the input matrices and the resultant matrix with suitable headings and
forrnat.(Using two dimension arrays where array size M,N,P,Q<=3).

#include<stdio.h>
#include<conio.h>
int i,j,a[10][10],b[10][10],c[10][10],p,q,m,n,k;
void read(int a[10][10],int m,int n)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
}

void write(int c[10][10],int m,int q)


{
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}

Nitin Kumar
E1-13
void mul(int a[10][10],int m,int n,int b[10][10],int q)
{
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
}
void main()
{
printf("Enter the order of 1st the matrix\n");
scanf("%d%d",&m,&n);
printf("Enter elements of 1st matrix\n");
read(a,m,n);
printf("Enter order of 2nd matrix\n");
scanf("%d%d)",&p,&q);
printf("Enter the elements of the 2nd matrix\n");
read(b,p,q);
if(n!=p)
{
printf("Multiplication not possible\n");
exit(0);
}
mul(a,m,n,b,q);
printf("Product of two matrices\n");
write(c,m,q);
getch();
}

Nitin Kumar
E1-13
Output:

Enter the order of 1st matrix


2 3
Enter the elements of 1st matrix
1 2 3
4 5 6
Enter the order of 2nd matrix
3 4
Enter the elements of 2nd matrix
1 1 1 1
1 1 1 1
1 1 1 1
Product of matrices
6 6 6 6
15 15 15 15

Enter the order of 1st matrix


2 3
Enter the elements of 1st matrix
1 2 3
4 5 6
Enter the order of 2nd matrix
4 3
Enter the elements of 2nd matrix
1 1 1
1 1 1
1 1 1
1 1 1
Multiplication not possible

Nitin Kumar
E1-13
Experiment 20.
Write a C.program to read matrix A(MxN) and to find the following using user
defined functions:
1. Sum of the elements of the specified row.
2. Sum of the elements of the specified column.
3. Sum of all the elements of the matrix.
Output the computed results with suitable headings.

#include<stdio.h>
#include<conio.h>
int mat[10][10],i,j,m,n,asum,rsum,csum;
void read()
{
printf("Enter the elements of the matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&mat[i][j]);
}
void allsum()
{
asum=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
asum=asum+mat[i][j];
printf("Sum of all elements of the matrix=%d\n",asum);
}
void sumcol()
{
for(j=0;j<n;j++)
{
csum=0;
for(i=0;i<m;i++)
{
csum=csum+mat[i][j];
}
printf("\n sum of %d column is %d",j,csum);
}
}

Nitin Kumar
E1-13
void sumrow()

{
for(i=0;i<m;i++)
{
rsum=0;
for(j=0;j<n;j++)
{
rsum=rsum+mat[i][j];
}
printf("\n sum of %d row is %d",i,rsum);
}
}
void main()
{
clrscr();
printf("Enter the order of the matrix\n");
scanf("%d%d",&m,&n);
read();
allsum();
sumcol();
sumrow();
getch();
}

Nitin Kumar
E1-13
Output:

Enter the order of the matrix


3 2
Enter the elements of 1st matrix
5 6
2 6
3 4

Sum of all the elements of the matrix=26

Sum of 0 column is 10
Sum of 1 column is 16
Sum of 0 row is 11
Sum of 1 row is 8
Sum of 2 row is 7

Nitin Kumar
E1-13

You might also like