C Manual 22 Scheme
C Manual 22 Scheme
Compute the roots of a quadratic equation by accepting the coefficients. Print appropriate messages.
Step 1: Start
Step 2: Read the coefficients
Step 3: if (a==0 && b== 0) then
print‘Invalid inputs’
goto step 8
Step 4: if(a==0) then
root1=-c/b
print ‘Linear roots’, root1
goto step 8
Step 5: d=b*b-4*a*c
if(d==0) then
root1=root2=-b/(2*a)
print ‘Roots are equal’, root1, root2
goto step 8
Step 6: if (d>0)
root1=(-b+(sqrt(d)))/ (2*a)
root2=(-b-(sqrt(d)))/ (2*a)
print ‘Roots are distinct’,root1,root2
goto step 8
Step 7: print ‘Roots are imaginary’
Step 8: Stop
#include <stdio.h>
#include<math.h>
int main()
{
float a,b,c,d,root1,root2,rpart,ipart;
printf("Enter the coefficients");
scanf("%f%f%f",&a,&b,&c);
if(a==0 && b==0)
printf("Invalid inputs");
else if(a==0)
{
printf("Linear roots\n");
root1=-c/b;
printf("Root is %.3f",root1);
}
else
{
d=b*b-4*a*c;
if(d==0)
{
printf("Roots are equal\n");
root1=root2=-b/(2*a);
printf("Root1=%.3f\n",root1);
printf("Root2=%.3f",root2);
}
else if(d>0)
{
printf("Roots are distinct\n");
root1=(-b+(sqrt(d)))/(2*a);
root2=(-b-(sqrt(d)))/(2*a);
printf("Root1=%.3f\n",root1);
printf("Root2=%.3f",root2);
}
else
{
printf("Roots are imaginary\n");
rpart=-b/(2*a);
ipart=sqrt(fabs(d))/(2*a);
printf("Root1=%.3f+i%.3f\n",rpart,ipart);
printf("Root2=%.3f-i%.3f",rpart,ipart);
}
}
return 0;
}
Out Put :
Enter the coefficients 1 -4 4
Roots are equal
Root1=2.000
Root2=2.000
An electricity board charges the following rates for the use of electricity: for the first 200 units 80 paise per
unit: for the next 100 units 90 paise per unit: beyond 300 units Rs. 1 per unit. All users are charged a
minimum of Rs. 100 as meter charge. If the total amount is more than Rs. 400, then an additional surcharge
of 15% of total amount is charged. Write a program to read the name of the user, number of units consumed
and print out thecharges.
Step 1: Start
billAmount = 100
if(unit<=200) then
billAmount= billAmount+(unit*0.80)
goto Step 4
billAmount= billAmount+(200*0.80)+((unit-200)*0.90);
goto Step 4
billAmount= billAmount+(200*0.80)+(100*0.90)+((unit-300)*1);
goto Step 4
if(billAmount>400) then
billAmount=billAmount+(billAmount*0.15);
goto Step 4
Out Put :
Enter the customer name: Shambu
Enter the total units consumed: 493
Customer name:Shambu
Total units consumed:493
Total Bill Amount:624.45
Lab Experiment 4:
Write a C Program to display the following by reading the number of rows as input:
121
12321
1234321
Step 1: Start
for j= 1 to row-i
print" "
forj=1 to i
print j
forj=i-1 down to 1
print j
Out Put :
Enter number of rows :3
1
121
12321
Lab Experiment 5:
Out Put :
Step 1: Start
Step 2: Read the order of the matrix A
Read m,n
Step 3: Read the order of the matrix B
Read p,q
Step 4: To check for compatibility condition of matrix multiplication
if(n!=p)
print “Matrix multiplication not possible”
goto step 9
Step 5: Read the elements of matrix A
Repeat through step 5 for i=0 to m-1
Repeat for j=0 to n-1
Read A[i][j]
Step 6: Read the elements of matrix B
Repeat through step 6 for i=0 to p-1
Repeat for j=0 to q-1
Read B[i][j]
Step 7: Calculate the product of two given matrices
Repeat through step 7 for i=0 to m-1
Repeat for j=0 to q-1
C[i][j]=0
Repeat for k=0 to n-1
C[i][j]=C[i][j]+A[i][k]*B[k][j]
Step 8: Print the resultant matrix
Repeat step 8 for i0 to m-1
Repeat for j0 to n-1
Print C[i][j]
Step 9: Stop
#include<stdio.h>
void main()
{
int m,n,p,q,i,j,k,A[10][10],B[10][10],C[10][10];
printf("Enter the order of matrix A \n");
scanf("%d%d",&m,&n);
printf("Enter the order of matrix B \n");
scanf("%d%d",&p,&q);
if(n!=p)
printf("Matrix multiplication is not possible\n");
else
{
printf("Enter the elements of matrix A \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&A[i][j]);
printf("Enter the elements of matrix B \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<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 C is \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",C[i][j]);
printf("\n");
}
}
}
Out Put :
Enter the order of matrix A
33
Enter the order of matrix B
33
Enter the elements of matrix A
111
111
111
Enter the elements of matrix B
111
111
111
The resultant matrix C is
3 3 3
3 3 3
3 3 3
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.
Step 1: Start
Step 2: Input the value of degree
Read degree
Step 3: Initialization
x=degree*(PI/180)
nume=x
fact=1
i=2
Step 4: Calculate the sum
term=nume/fact
nume=-nume*x*x
fact=fact*i*(i+1)
sum=sum+term
i=i+2
Repeat the step 4 until while (fabs(term)>=0.00001)
Step 5: Print the result
Print sum
Step 6: End
#include<stdio.h>
#include<math.h>
#define PI 3.142
void main()
{
int i,degree;
float x,sum=0,term,nume,fact;
printf("Enter value of degree \n");
scanf("%d",°ree);
x=degree*(PI/180);
nume=x;
fact=1;
i=2;
do
{
term=nume/fact;
nume=-nume*x*x;
fact=fact*i*(i+1);
sum=sum+term;
i=i+2;
}while(fabs(term)>=0.00001);
printf("The sine of %d is %.3f",degree,sum);
printf("Using inbuilt function sin(%d) is %.3f",degree,sin(x));
}
Out Put:
Enter value of degree:60
The sine of 60 is 0.866
Using inbuilt function sin(60) is 0.866
Lab Experiment: 8
Write functions to implement string operations such as compare, concatenate and find string length. Use
parameter passing techniques.
#include <stdio.h>
int length(char []);
int compare(char [], char []);
voidconcat(char [], char []);
void main()
{
char s1[1000], s2[1000];
int l1,l2;
printf("Enter a string1\n");
gets(s1);
printf("Enter a string2\n");
gets(s2);
l1 = length(s1);
l2 = length(s2);
printf("Length of %s = %d\n", s1, l1);
printf("Length of %s = %d\n", s2, l2);
if (compare(s1, s2) == 0)
printf("Strings are Equal\n");
else
printf("Strings are Unequal\n");
concat(s1, s2);
printf("After concatenation string is :%s", s1);
}
int length(char s1[])
{
int c = 0;
while (s1[c] != '\0')
c++;
return c;
}
Out Put:
Enter a string1 :
hello
Enter a string2:
hello
Length of hello=5
Length of hello=5
Strings are Equal
After concatenation string is :hellohello
Enter a string1 :
hello
Enter a string2:
yenepoya
Length of hello=5
Length of hello=8
Strings are Unequal
After concatenation string is :helloyenepoya
Lab Experiment: 10
Implement structures to read, write and compute average-marks of the students, list the students scoring
above and below the average marks for a class of N students.
#include <stdio.h>
struct student
{
char usn[50];
char name[50];
int marks;
} s[10];
void main()
{
int i,n,countav=0,countbv=0;
floatsum,average;
printf("Enter number of Students\n");
scanf("%d",&n);
printf("Enter information of students:\n");
for(i=0; i<n;i++)
{
printf("Enter USN: ");
scanf("%s",s[i].usn);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%d",&s[i].marks);
printf("\n");
}
printf("Displaying Information:\n\n");
for(i=0; i<n; i++)
{
printf("\nUSN: %s\n",s[i].usn);
printf("Name: %s\n ", s[i].name);
printf("Marks: %d",s[i].marks);
printf("\n");
}
for(i=0;i<n;i++)
{
sum=sum+s[i].marks;
}
average=sum/n;
printf("\nAverage marks: %f",average);
for(i=0;i<n;i++)
{
if(s[i].marks>=average)
countav++;
else
countbv++;
}
printf("\nTotal No of students above average= %d",countav);
printf("\nTotal No of students below average= %d",countbv);
}
Out Put :
Enter number of Students 2
Enter information of students
Enter USN :01
Enter name :pooja
Enter marks :50
Displaying Information:
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>
void main()
{
float a[10],*ptr,mean,std,sum=0,sumstd=0;
int n,i;
printf("Enter a value of n\n");
scanf("%d",&n);
printf("Enter real values\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+*ptr; ptr++;
}
mean=sum/n; ptr=a;
for(i=0;i<n;i++)
{
sumstd=sumstd+pow((*ptr-mean),2);
ptr++;
}
std=sqrt(sumstd/n); printf("Sum=%.3f\
n",sum); printf("Mean=%.3f\n",mean);
printf("Standard Deviation =%.3f",std);
}
Out Put :
Enter a value of n
5
Enter real values
34 88 32 12 10
Sum=176.000
Mean=35.200
Standard Deviation=28.188
Lab Experiment: 12
Write a C program to copy a text file to another, read both the input file name and target file name.
#include <stdio.h>
void main()
{
FILE *fptr1, *fptr2;
charch, fname1[20], fname2[20];
printf(" Enter the source file name : ");
scanf("%s",fname1);
fptr1=fopen(fname1,"r");
printf(" Enter the new file name : ");
scanf("%s",fname2);
fptr2=fopen(fname2, "w");
while(1)
{
ch=fgetc(fptr1);
if(ch==EOF)
{
break;
}
else
{
fputc(ch, fptr2);
}
}
printf(" The file %s copied successfully in the file %s. \n\n",fname1,fname2);
fclose(fptr1);
fclose(fptr2);
}
Out Put :
Enter the source file name :abc.txt