0% found this document useful (0 votes)
47 views15 pages

Lab Cycle Programs Works With E Box

The document contains code snippets from 12 lab cycle programs covering topics like: 1) Computing roots of a quadratic equation and implementing a simple calculator using switch statements. 2) Calculating electricity bill based on units consumed and implementing binary search on integers. 3) Matrix multiplication, Taylor series approximation, bubble sort algorithm, string operations using functions. 4) Structures to read and analyze student marks data, pointers to calculate statistics of an array, and recursive binary to decimal conversion. The programs demonstrate basic C programming concepts.
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)
47 views15 pages

Lab Cycle Programs Works With E Box

The document contains code snippets from 12 lab cycle programs covering topics like: 1) Computing roots of a quadratic equation and implementing a simple calculator using switch statements. 2) Calculating electricity bill based on units consumed and implementing binary search on integers. 3) Matrix multiplication, Taylor series approximation, bubble sort algorithm, string operations using functions. 4) Structures to read and analyze student marks data, pointers to calculate statistics of an array, and recursive binary to decimal conversion. The programs demonstrate basic C programming concepts.
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/ 15

//Lab cycle program -1

//Program to Compute the roots of a quadratic equation.


#include<stdio.h>
#include<math.h>

void main()
{
float d, x1, x2, r, img, a, b, c;
printf("Enter the coefficients of a, b and c :\n");
scanf("%f%f%f", &a, &b, &c);
d = b*b-4*a*c;
if(a == 0)
{
printf("invalid");
}
else if(d == 0)
{
x1 = -b/(2*a);
x2 = -b/(2*a);
printf("Roots are equal and x1 is %.2f", x1);
}
else if(d>0)
{
x1 = (-b+sqrt(d))/(2*a);
x2 = (-b-sqrt(d))/(2*a);
printf("%.2f and %.2f", x1, x2);
}
else if(d < 0)
{
r = -b/(2*a);
img = (sqrt(-d))/(2*a);
printf("%.2f+%.2fi and ",r,img);
printf("%.2f-%.2fi",r,img);
}

}
//Lab cycle program - 2
//Simple calculator using switch statement
#include<stdio.h>
int main()
{
int a,b,c;
char ch;
scanf("%d %c %d",&a,&ch,&b);
switch(ch)
{
case '+':
{
c=a+b;
printf("The sum is %d",c);
break;
}
case '-':
{
c=a-b;
printf("The differnce is %d",c);
break;
}
case '*':
{
c=a*b;
printf("The product is %d",c);
break;
}
case '/':
{
if(b==0)
{
printf("Divide by Zero error\n");
break;
}
else
{
c=a/b;
printf("The quotient is %d",c);
break;
}
}

case '%':
{
c=a%b;
printf("The remainder is %d",c);
break;

}
default:
printf("Invalid operator");
}
}
//Lab cycle program - 3
//Program to calculate electricity bill

#include<stdio.h>

int main()
{
int units;
char n[25];
float amount;
printf("Enter name\n");
scanf("%s",n);
printf("Enter units\n");
scanf("%d",&units);
if(units>=0 && units <= 200)
{
amount=100+(units*0.80);
}
else if(units>200 && units <= 300)
{
amount=100+160+(units-200)*0.9;
}
else if(units>300)
{
amount=100+160+90+(units-300)*1;
}
else
{
printf("Invalid Input\n");
return 0;
}

if(amount<400)
{
printf("Total amount charged to %s is %.2f",n,amount);
}
else if(amount>=400)
{
amount=amount+(amount*15)/100;
printf("Total amount charged to %s is %f",n,amount);
}
return 0;

}
// Lab cycle program - 5
//Implement Binary Search on Integers / Names.

#include<stdio.h>
int main()
{
int a[100]={0},high,low,mid,key,n,i;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the elements in sorted order\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the key to be searched\n");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
printf("The key is present at index %d in the array\n",mid);
return 0;
}
else if(key>a[mid])
{
low=mid+1;
}
else if(key<a[mid])
{
high=mid-1;
}
}
printf("The key is not present in the array\n");
return 0;
}
// Lab cycle program - 6
//Implement Matrix multiplication and validate the rules of multiplication.
#include<stdio.h>
void readmatrix(int a[10][10],int r,int c)
{
int i,j;
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
scanf("%d",&a[i][j]);
}
}
}
void printmatrix(int a[10][10],int r,int c)
{
int i,j;
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\n");
}

void multiplymatrix(int a[10][10],int b[10][10],int c[10][10],int m,int n,int p,int q)


{
int i,j,k;
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];
}
}
}
}
int main()
{
int a[10][10]={0},b[10][10]={0},c[10][10]={0},m,n,p,q;
printf("Enter the size of first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the size of second matrix\n");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("Enter the elements of first matrix\n");
readmatrix(a,m,n);
printf("Enter the elements of second matrix\n");
readmatrix(b,p,q);
//printf("Array elements of matrix A\n");
//printmatrix(a,m,n);
//printf("Array elements of matrix B\n");
//printmatrix(b,p,q);
multiplymatrix(a,b,c,m,n,p,q);
printf("The resultant matrix is\n");
printmatrix(c,m,q);
}
else
printf("Multiplication is not possible\n");
}
//Lab cycle program - 7
/*Compute sin(x)/cos(x) using Taylor series approximation.
Compare your result with the built-in library function. Print both the results with appropriate
inferences.*/

#include<stdio.h>
#include<math.h>
int main()
{
float num,den,term,deg,x;
int i;
printf("Enter degree and number \n");
scanf("%f",&deg);
x=deg;
x=x*(3.142/180);
num=x;
den=1;
term=num/den;
for(i=3; i<21; i=i+2)
{
num=num*(-x*x);
den=den*(i-1)*(i);
term=term+(num/den);
}
printf("sin(%.2f) = %.2f without using built-in function\n",deg,term);
printf("sin(%.2f) = %.2f using built-in function",deg,sin(x));
return 0;
}
//Lab cycle program - 8
//Program to sort array element using bubble sort algorithm

#include<stdio.h>
int main()
{
int a[50],i,j,n,t;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("\nEnter the elements to be sorted\n");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n-1; i++)
{
for(j=0; j<n-1; j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf("\nSorted elements\n");
for(i=0; i<n; i++)
{
printf("%d\t",a[i]);
}
}
//Lab cycle program - 9
//Write functions to implement string operations such as
//compare, concatenate, string length. Convince the parameter passing techniques.

#include <stdio.h>
#include <strings.h>

int stringlen(char a[])


{
int i;
for(i=0;; i++)
{
if(a[i]=='\0')
return i;
}
}
int stringcmp(char a[],char b[])
{
int i,c,l1;
l1=stringlen(b);
for(i=0; i<=l1; i++)
{
c=a[i]-b[i];
if(c==0)
{
c=a[i]-b[i];
}
else
break;
}
if(c==0)
{
printf("Strings are equal\n");
return 0;
}
else if(c>0)
{
printf("Stirngs are not equal\n");
return 1;
}
else if(c<0)
{
printf("Stirngs are not equal\n");
return -1;
}
return 0;
}

void stringcat(char a[],char b[])


{
int i,j;
j=stringlen(a);
for(i=0; b[i]!='\0'; i++,j++)
{
a[j]=b[i];
}

}
int main()
{
char s1[50]="\0", s2[50]="\0";
printf("Enter the string 1\n");
scanf("%s",s1);
printf("%s\n",s1);
printf("Enter the string 2\n");
scanf("%s",s2);
printf("%s\n",s2);
printf("Length of string 1 is %d\n",stringlen(s1));
stringcmp(s1,s2);
stringcat(s1,s2);
printf("Concatenated string is %s\n",s1);
return 0;
}
//Lab cycle program - 10
/*Implement structures to read, write and compute average- marks
and the students scoring above and below the average marks for a class of N students.*/

#include<stdio.h>
struct stu
{
char sname[50];
int rollno;
int marks;
}sturec[50];

void read_student_details(int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Enter the details of student %d\n",i+1);
printf("Enter the name\n");
scanf("%s",sturec[i].sname);
printf("Enter the rollnumber\n");
scanf("%d",&sturec[i].rollno);
printf("Enter the marks\n");
scanf("%d",&sturec[i].marks);
}
}

void find_above_below_avg(int n)
{
int i,ab=0,bl=0;
float ca;
for(i=0;i<n;i++)
{
ca=ca+sturec[i].marks;
}
ca=ca/n;
for(i=0;i<n;i++)
{
if(sturec[i].marks>=ca)
ab++;
else
bl++;
}
printf("Number of students who have above average is %d\n",ab);
printf("Number of students who have below average is %d\n",bl);
}

int main()
{
int n;
printf("Enter the number of students\n");
scanf("%d",&n);
read_student_details(n);
find_above_below_avg(n);
return 0;
}
//Lab cycle program - 11
/*Develop a program using pointers to compute the sum, mean and standard deviation
of all elements stored in an array of N real numbers. */
#include<stdio.h>
#include<math.h>

float mean(float *a,int n)


{
int i;
float sum=0,m=0;
printf("Enter the elements in the array\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
sum=sum+a[i];
}
m=sum/n;
printf("Sum is %.2f\n",sum);
return m;
}

void standard_deviation(float *b,int n)


{
float v=0,m=0;
int i;
m=mean(b,n);
printf("Mean is %.2f\n",m);
for(i=0;i<n;i++)
{
v=v+((b[i]-m)*(b[i]-m));
}
v=v/n;
printf("Standard deviation is %.2f",sqrt(v));
}

int main()
{
float x[50];
int n;
printf("Enter the number of elements in the array\n");
scanf("%d",&n);
standard_deviation(x,n);
}
//Lab cycle program - 12
//Implement Recursive functions for Binary to Decimal Conversion.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int convertBinaryToDecimal(long int bin)
{
long int n=0;
int rem=0;
static int i=0;
n=bin;
rem=bin%10;
n=n/10;
if(n==0)
return((bin%10)*pow(2,i++));

else
return(rem*pow(2,i++)+convertBinaryToDecimal(n));
}
int main()
{
char a[50];
long int bin;
int decimal=0,i;
printf("Enter a binary number\n");
scanf("%s",a);
//to check it's binary no
for(i=0; a[i]!='\0'; i++)
{
if(a[i]>'1')
{
//printf("You should enter only binary no\n");
return 0;
}
}
bin=atoi(a);// convert string to int
decimal=convertBinaryToDecimal(bin);
printf("%ld equivalent decimal value is %d\n",bin,decimal);
return 0;
}

You might also like