Lab Cycle Programs Works With E Box
Lab Cycle Programs Works With E Box
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");
}
#include<stdio.h>
#include<math.h>
int main()
{
float num,den,term,deg,x;
int i;
printf("Enter degree and number \n");
scanf("%f",°);
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 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>
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;
}