[go: up one dir, main page]

0% found this document useful (0 votes)
32 views16 pages

C Lab Manual

The documents contain code snippets for simple calculator, quadratic equation solver, electricity bill calculator, binary search, Taylor series approximation, matrix multiplication, bubble sort, binary to decimal conversion, string operations and a structure program.

Uploaded by

Rakshitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views16 pages

C Lab Manual

The documents contain code snippets for simple calculator, quadratic equation solver, electricity bill calculator, binary search, Taylor series approximation, matrix multiplication, bubble sort, binary to decimal conversion, string operations and a structure program.

Uploaded by

Rakshitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1.

Simple calculator

#include <stdio.h>

#include<conio.h>

void main()

int a,b;

float result;

char op;

clrscr();

printf("enter the value for a\n");

scanf("%d",&a);

printf("enter the value for b\n");

scanf("%d",&b);

printf("enter the operation to be performed (+,-,*,/,%)\n");

scanf(" %c",&op);

result=0;

switch(op)

case '+': result=a+b;

break;

case '-': result=a-b;

break;

case '*': result=a*b;

break;

case '/':if(b==0)

printf("divide by zero error\n");

getch();
}

else

result=(float)a/(float)b;

break;

case '%': result=a%b;

break;

default:printf("inavlid operator\n");

getch();

printf("the result is %d%c%d=%f\n",a,op,b,result);

getch();

2. Quadratic equation

#include<stdio.h>

#include<math.h>

#include<conio.h>

void main( )

float x1, x2, a, b, c, d;

clrscr();

printf("Enter the coefficients a, b and c\n");

scanf("%f%f%f",&a,&b,&c);
if(a==0)

printf("the roots doesn't exist\n");

getch();

d=(b*b)-(4*a*c);

if(d == 0)

x1= -b/(2*a);

x2= -b/(2*a);

printf("The roots are real and equal\n");

printf("1st root = %f\n",x1);

printf("2nd root = %f\n",x2);

getch();

else if(d > 0)

x1=(-b+sqrt(d))/(2*a);

x2=(-b-sqrt(d))/(2*a);

printf("The roots are real and distinct\n");

printf("1st root = %f\n",x1);

printf("2nd root = %f\n",x2);

getch();

else

x1=-b/(2*a);

x2=sqrt(fabs(d))/(2*a);

printf("The roots are imaginary\n");


printf("1st root = %f + i%f\n",x1,x2);

printf("2nd root = %f - i%f\n",x1,x2);

getch();

3. Electricity program

#include<stdio.h>

#include<conio.h>

void main()

float units,total_amount;

float charge,surcharge=0;
char name[20];

clrscr();

printf("enter the name\n");

scanf("%s",&name);

printf("enter the number of units consumed\n");

scanf("%f",&units);

if(units>300)

charge=200*.080;

charge=charge+(100*0.90);

charge=charge+(units-300)*1;

else if(units>200)

charge=200*0.80;

charge=charge+(units-200)*0.90;

else

charge=units*0.80;

total_amount=charge+100;

if(total_amount>400)

surcharge=total_amount*0.15;

total_amount=total_amount+surcharge;

printf("total user amount is %f\n",total_amount);

getch();

}
4. Binary search

#include<stdio.h>

#include<conio.h>

void main()

int a[100], n, i, mid, low, high, found, key;

clrscr();

printf("\nEnter the number of elements\n");

scanf("%d",&n);

printf("\nEnter the elements in ascending order\n");

for(i=0; i<n; i++)

scanf("%d", &a[i]);

printf("\nEnter the key element\n");

scanf("%d", &key);

found = 0;

low = 0;

high = n-1;

while(low <= high)

mid = (low+high)/2;

if(key == a[mid])
{

found = 1;

break;

else if(key < a[mid])

high = mid-1;

else

low = mid+1;

if(found==1)

printf("\nKey element %d found at position %d\n", key, mid+1);

else

printf("\nKey element not found\n");

getch();

5. Taylor series

#include<stdio.h>

#include<conio.h>
#include<math.h>

float my_sine(float x, int n);

int fact(int n);

void main()

int x, n;

float rad, res;

clrscr();

printf(" ENTER THE DEGREE");

scanf("%d",&x);

printf("ENTER THE NUMBER OF TERMS");

scanf("%d", &n);

rad=x*(3.142/180);

res=my_sine(rad , n);

printf(" my_sin(%d)=%f\n", x , res);

printf(" Library sine(%d)=%f", x ,sin(rad));

getch();

float my_sine(float x, int n)

int i;

float sum=0;

for(i=1; i<=n; i=i+2)

if((i-1)%4==0)

sum=sum+pow(x , i)/fact(i);

else

sum=sum-pow(x , i)/fact(i);

}
return sum;

int fact(int n)

if(n==0)

return 1;

else

return n*fact(n-1);

6. Matrix multiplication

#include<stdio.h>

#include<conio.h>

void main()

int i, j, a[10][10], b[10][10], c[10][10], k, m,n,p,q;

clrscr();

printf("Enter the array size of 1st matrix");

scanf("%d%d", &m,&n);

printf("Enter the array size of 2nd matrix");

scanf("%d%d",&p,&q);

if(n!=p)

printf("Matrix multiplication is not possible");


}

printf("Enter the elements of 1st matrix");

for(i=0; i<m; i++)

for(j=0;j<n;j++)

scanf("%d", &a[i][j]);

printf("Enter the elements of 2nd matrix");

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("The resultant matrix is\n");


for(i=0; i<m; i++)

for(j=0; j<q; j++)

printf("%d\t", c[i][j]);

printf("\n");

getch();

7. Bubble sort

#include<stdio.h>

#include<conio.h>

void main()

int a[10], n, i, j, temp;

clrscr();

printf("Enter the number of elements\n");

scanf("%d",&n);

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

for(i=0; i<n; i++)


{

scanf("%d", &a[i]);

for(j=1; j<n; j++)

for(i=0; i<n-j; i++)

if(a[i]> a[i+1])

temp=a[i];

a[i]=a[i+1];

a[i+1]=temp;

printf("The sorted array is\t");

for(i=0; i<n; i++)

printf("%d\t", a[i]);

getch();

}
8. Binary to decimal conversion

#include<stdio.h>

#include<conio.h>

int convert(int bin);

void main()

int bin, dec;

clrscr();

printf("\nEnter the binary value : ");

scanf("%d", &bin);

dec = convert(bin);

printf("\nDecimal equivalent of %d is %d\n", bin, dec);

getch();

int convert(int bin)

if(bin == 0 )

return bin;

else

return (bin%10+2*convert(bin/10));

}
9. String program

#include<stdio.h>

#include<string.h>

#include<conio.h>

void str_cmp(char str1[], char str2[])

if(strcmp(str1,str2)==0)

printf("strings are equal\n");

else

printf("strings are not equal\n");

return ;

void str_len(char str1[])

int n;

n=strlen(str1);

printf("the length of the string is %d\n",n);

return ;

void str_can(char str1[], char str2[])

strcat(str1,str2);

printf("the concatenated string is %s\n",str1);

return;

void main()

char str1[30], str2[30];


clrscr();

printf("enter String 1 :\n ");

scanf("%s", str1);

printf("enter String 2 : \n");

scanf("%s", str2);

str_cmp(str1,str2);

str_len(str1);

str_can(str1,str2);

getch();

10. Structure program

#include<stdio.h>

#include<conio.h>

struct student

int marks;

};

void main()

struct student s[10];

int i, n, sum=0;
float average;

clrscr();

printf("Enter the number of students : \n");

scanf("%d", &n);

printf("Enter the marks of Student \n");

for(i=0; i<n; i++)

scanf("%d ", &s[i].marks);

printf("the marks of Student are\n");

for(i=0; i<n; i++)

printf("%d\n ", s[i].marks);

for(i=0;i<n;i++)

sum=sum+s[i].marks;

average = (float)sum / n;

for(i=0; i<n; i++)

if(s[i].marks > average)

printf("The student %d has scored above average\n",i++);

else

printf("The student %d has scored below average\n", i++);

You might also like