Laboratory Experiments
1. //Program to find the roots of a quadratic equation
#include <stdio.h>
#include <math.h>
void main()
{
int a,b,c,disc;
float r1,r2;
printf("Enter the co-efficient of a quadritic equation\n");
scanf("%d%d%d",&a,&b,&c);
disc=b*b-4*a*c; // finding the discriminant
if(disc>0)
{
printf("Roots are real and distinct\n");
r1=(-b+sqrt(disc))/(2.0*a);
r2=(-b-sqrt(disc))/(2.0*a);
printf("Root1 = %f\nRoot2 = %f\n",r1,r2);
}
else if(disc==0)
{
printf("Roots are real and equal\n");
r1=r2=-b/(2.0*a);
printf("Root1 = %f\nRoot2 = %f\n",r1,r2);
}
else
{
printf("Roots are Imaginary\n");
r1=-b/(2.0*a);
r2=sqrt(fabs(disc))/(2.0*a);
printf("Root1 = %f + i %f\nRoot2 = %f - i %f\n",r1,r2,r1,r2);
}
}
2. // Program to design a simple calculator for shop keeper
#include <stdio.h>
void main()
{
int a,b;
char op;
printf("Enter the any mathmetical expression (eg: 2+4)\n");
scanf("%d%c%d",&a,&op,&b);
switch(op)
{
case '+': printf("%d + %d = %d\n",a,b,a+b);
break;
case '-': printf("%d - %d = %d\n",a,b,a-b);
break;
case '*': printf("%d * %d = %d\n",a,b,a*b);
break;
case '/': printf("This operation gives the quotent\n");
printf("%d / %d = %f\n",a,b,(float)a/b);
break;
case '%': printf("This operation gives the Remainder\n");
printf("%d %% %d = %d\n",a,b,a%b);
break;
default: printf("Not a valid mathematical expression\n");
}
}
3. //Program to read the customer number, power consumed and display the amount to
be paid by the customer.
#include <stdio.h>
void main()
{
int cnum,power;
float amt;
printf("Enter the customer number and power consumed\n");
scanf("%d%d",&cnum,&power);
if(power<=200)
amt=0.50*power;
else if(power<=400)
amt=100+(0.65*(power-200));
else if(power<=600)
amt=230+(0.80*(power-400));
else
amt=390+(1.0*(power-600));
printf("Customer has to pay Rs. %.2f",amt);
}
4. //Program to find sine value for a given angle and also verify calculated sine value using builtin
function
#include <stdio.h>
#include <math.h>
void main()
{
int i,n,deg;
float x,sum=0,term;
printf("Enter the degree to find the sin value and number of terms in the series\n");
scanf("%d%d",°,&n);
x=3.142/180*deg;
term=x;
for(i=1;i<=n;i++)
{
sum=sum+term;
term*=-(x*x)/((2*i)*(2*i+1));
}
printf("Sin(%d) = %f\n",deg,sum);
printf("Using library function Sin(%d) = %f",deg,sin(x));
}
Output:
5. //Program to check the given number is palindrome or not
#include <stdio.h>
void main()
{
int num,rev=0,dig,x;
printf("Enter the 4 digit registration number\n");
scanf("%d",&num);
x=num;
while(num!=0)
{
dig=num%10;
rev=rev*10+dig;
num/=10;
}
if(rev==x)
printf("The number is palindrome");
else
printf("The number is not a palindrome");
}
Output:
6. //Program to find the student with a given weight
#include <stdio.h>
void main()
{
int i,n,f=0;
float weight[60],sweight;
printf("How many students weight you want enter\n");
scanf("%d",&n);
printf("Enter %d students weight\n",n);
for(i=0;i<n;i++)
scanf("%f",&weight[i]);
printf("Enter the weight of a student (whom you want to search)\n");
scanf("%f",&sweight);
for(i=0;i<n;i++)
if(sweight==weight[i])
{
printf("Student found at %d position\n",i+1);
f=1;
}
if(f==0)
printf("No sutdent is found with this weight");
}
Output:
7. //Program to check whether the matrices are multipliable, if so find the product matrix,
otherwise display suitable message
#include <stdio.h>
void main()
{
int i,j,k,m,n,p,q,a[5][5],b[5][5],c[5][5];
printf("Enter the order of first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the order of second matrix\n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Matrix multiplication is not possible\n");
}
else
{
printf("Enter the elements of first matrix of order %d X %d\n",m,n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the elements of second matrix of order %d X %d\n",p,q);
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;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("First matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("Second matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("%d\t",b[i][j]);
printf("\n");
}
printf("Resultant matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
}
Output:
8. /*Program to find the transpose of a given matrix and also find sum of upper triangle elements
and sum of lower triangle elements of the transposed
matrix*/
#include <stdio.h>
void main()
{
int a[5][5],i,j,m,n,t[5][5],usum=0,lsum=0;
printf("Enter the order for square matrix\n");
scanf("%d%d",&m,&n);
if(m!=n)
printf("Order is not for square matrix\n");
else
{
printf("Enter the elements of the matrix of order %d X %d\n",m,n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
t[j][i]=a[i][j];
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if(i<=j)
usum+=t[i][j]; //finding the sum of upper triangle
if(i>=j)
lsum+=t[i][j]; //finding the sum of lower triangle
}
printf("Given matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("Transpose matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",t[i][j]);
printf("\n");
}
printf("Sum of the elements of upper triangle is %d\n",usum);
printf("Sum of the elements of lower triangle is %d\n",lsum);
}
}
Sample output:
9. // Program to count the number of vowels and consonants in a given string
#include <stdio.h>
void main()
{
int vc=0,cc=0,i;
char str[50];
printf("Enter a string\n");
fgets(str,50,stdin); //instead of fgets() you can use scanf("%s",str);
for(i=0;str[i]!='\0';i++)
if(isalpha(str[i]))
switch(str[i])
{
case 'A':
case 'a':
case 'E':
case 'e':
case 'I':
case 'i':
case 'O':
case 'o':
case 'U':
case 'u':vc++;
break;
default:cc++;
}
printf("Number of vowels are %d\n",vc);
printf("Number of consonants are %d",cc);
}
Sample Output:
10. //Program to find nCr and nPr
#include <stdio.h>
void main()
{
int n,r;
long ncr,npr;
long fact(int);
printf("Enter the value for n and r (n>r)\n");
scanf("%d%d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
npr=fact(n)/fact(n-r);
printf("nCr = %ld and nPr = %ld",ncr,npr);
}
long fact(int n)
{
int i;
long f=1;
for(i=1;i<=n;i++)
f*=i;
return f;
}
Output:
11. //Program to sort a given set of integers using the function to swap 2 numbers
#include <stdio.h>
void main()
{
int i,j,n,a[25];
void swap(int *,int *);
printf("How many numbers you want to enter to sort\n");
scanf("%d",&n);
printf("Enter %d numbers\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
swap(&a[j],&a[j+1]);
printf("Given numbers in sorted order\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
void swap(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
Output:
12. //Program to assign students information and display them along with the total
#include <stdio.h>
#include <conio.h>
void main()
{
struct student
{
char name[50],usn[10];
int cie1,cie2,cie3,act1,act2,total;
}s[10];
int i,n;
clrscr();
printf("How many students information you want to enter\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
fflush(stdin);
printf("\nEnter %d student details\n",i+1);
printf("USN : ");
fgets(s[i].usn,50,stdin); //instead of fgets() you can use scanf("%s", s[i].usn); also
printf("Name : ");
fgets(s[i].name,50,stdin); //instead of fgets() you can use scanf("%s", s[i].name); also
printf("First CIE marks : ");
scanf("%d",&s[i].cie1);
printf("Second CIE marks : ");
scanf("%d",&s[i].cie2);
printf("Third CIE marks : ");
scanf("%d",&s[i].cie3);
printf("Activity-1 marks : ");
scanf("%d",&s[i].act1);
printf("Activity-2 marks : ");
scanf("%d",&s[i].act2);
if(s[i].cie1<s[i].cie2&&s[i].cie1<s[i].cie3)
s[i].total=s[i].cie2+s[i].cie3+s[i].act1+s[i].act2;
else if(s[i].cie2<s[i].cie3)
s[i].total=s[i].cie1+s[i].cie3+s[i].act1+s[i].act2;
else
s[i].total=s[i].cie1+s[i].cie2+s[i].act1+s[i].act2;
}
printf("\nStudents information is as follows\n");
for(i=0;i<n;i++)
{
printf("\nUSN : %s\t\t\tName : %s\n",s[i].usn,s[i].name);
printf("CIE1\tCIE2\tCIE3\tAct-1\tAct-2\tTotal\n");
printf(" %d\t %d\t %d\t %d\t %d\t %d\n",s[i].cie1,s[i].cie2,s[i].cie3,s[i].act1,s[i].act2,s[i].total);
}
getch();
}
Output: