All Lab Programs.
All Lab Programs.
PROGRAMS
1. Write a C program to find and output all the roots of a given quadratic equation, for
nonzero coefficients. (using if….else statement)
Program
#include<stdio.h>
#include<math.h> // use cc lm ______.c
#include<stdlib.h>
int main()
{
float a,b,c,disc,root1,root2;
printf("\nEnter the coefficients:\n");
scanf("%f%f%f",&a,&b,&c);
disc=b*b4*a*c; // ‘disc’ indicates discriminant
//Find the distinct roots
if(disc>0)
{
root1=(b + sqrt(disc)) / (2*a);
root2=(b sqrt(disc)) / (2*a);
printf("\n Roots are real & distinct! \n");
printf("\n The roots are: \n%f\n%f\n",root1,root2);
}
else if(disc==0) //Find the equal roots
{
root1=root2= b / (2*a);
printf("\n Roots are real & equal! \n");
printf("\n The roots are \n%f\n%f\n",root1,root2);
}
1
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
else
{
//Find the complex roots
root1= b / (2*a);
root2= sqrt(abs(disc)) / (2*a);
printf("\n The roots are imaginary!\n");
printf("\n The first root is %f + i%f \n",root1,root2);
printf("\n The second root is %f i%f \n",root1,root2);
}
}
Output
Run 1:
Enter the coefficients:
1
2
1
Roots are real & equal!
The roots are
1.000000
1.000000
Run 2:
Enter the coefficients:
1
5
4
2
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
Roots are real & distinct!
The roots are:
1.000000
4.000000
Run 3:
Enter the coefficients:
2
4
6
The roots are imaginary!
The first root is 1.000000 + i1.414214
The second root is 1.000000 i1.414214
opera ons like addi on, subtrac on, mul plica on, and division only on integers.
Program
#include<stdio.h>
#include<math.h>
int main()
int a,b,c;
3
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
// input sec on
scanf("%d%d",&a,&b);
prin ("\n\n");
scanf("%d",&ch);
switch(ch)
case 1: c=a+b;
break;
case 2: c=a-b;
break;
case 3: c=a*b;
break;
case 4: if(b==0)
break;
4
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
else
break;
break;
------------------------------------------------------------------------------------------------------------
Output
Run 1:
1-ADD
2-SUB
3-MULTI
5
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
4-DIVIDE
The result is 12
Run 2:
1-ADD
2-SUB
3-MULTI
4-DIVIDE
The result is 5
Run 3:
6
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
1-ADD
2-SUB
3-MULTI
4-DIVIDE
The result is 30
Run 4:
1-ADD
2-SUB
3-MULTI
4-DIVIDE
The result is 3
7
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
Run 5:
1-ADD
2-SUB
3-MULTI
4-DIVIDE
Run 6:
1-ADD
2-SUB
3-MULTI
8
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
4-DIVIDE
Invalid choice!
------------------------------------------------------------------------------------------------------------
3. Write a C program
i) To check whether a given integer number is a Palindrome number or not.
ii) To check whether a given integer number is an Armstrong number or not.
Output the given number with suitable message (using looping constructs).
i) To check whether a given integer number is a Palindrome number or not.
Program
#include <stdio.h>
main()
int temp,n,rev,dig;
scanf("%d",&n );
temp=n;
rev=0;
while(n!=0)
9
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
/* "shi " the exis ng digits one place to the le and add new digit in the units
place */
dig=n%10;
n=n/10;
rev=(rev*10)+dig;
if(temp==rev)
else
------------------------------------------------------------------------------------------------------------
Output
Run 1:
1551
Number is a palindrome!
Run 2:
10
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
5678
ii) To check whether a given integer number is an Armstrong number or not.
#include <stdio.h>
#include <math.h>
int main()
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0)
originalNumber /= 10;
++n;
}
11
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
originalNumber = number;
while (originalNumber != 0)
remainder = originalNumber%10;
originalNumber /= 10;
if(result == number)
else
return 0;
output:
4. Write a C program
i) To generate and print first N Fibonacci numbers
ii) To find GCD and LCM of two integer numbers.
12
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
To generate and print first N Fibonacci numbers
#include<stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
return 0;
}
output:
Enter the number of terms
6
First 6 of Fibonacci series are:
0
1
1
2
3
5
To find GCD and LCM of two integer numbers.
#include <stdio.h>
13
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
void main()
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;
printf("Enter two numbers\n");
scanf("%d %d", &num1, &num2);
if (num1 > num2)
{
numerator = num1;
denominator = num2;
}
else
{
numerator = num2;
denominator = num1;
}
remainder = numerator % denominator;
while (remainder != 0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
printf("GCD of %d and %d = %d\n", num1, num2, gcd);
printf("LCM of %d and %d = %d\n", num1, num2, lcm);
}
output:
14
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
Enter two numbers
62
12
GCD of 62 and 12 = 2
LCM of 62 and 12 = 372
5. Write a C program to generate Pascal's triangle and Flyod’s Triangle.
/*floyds triangle*/
# include <stdio.h>
void main()
{
int r, i, j, c = 1 ;
printf("Enter the number of rows : ") ;
scanf("%d", &r) ;
printf("\nFloyd's triangle is : \n\n") ;
for(i = 0 ; i < r ; i++)
{
for(j = 0 ; j <= i ; j++)
{
printf("%6d", c) ;
c++ ;
}
printf("\n\n") ;
}
}
OUTPUT:
Enter the number of rows : 5
Floyd's triangle is :
1
2 3
4 5 6
7 8 9 10
11 12 13 14
Enter the number of rows : 3
Floyd's triangle is :
15
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
2 3
4 5 6
/*pascals triangle*/
#include<stdio.h>
main()
int b,p,q,r,x;
b=1;
q=0;
scanf("%d",&r);
while(q<r)
for(p=40-3*q;p>0;--p)
for(x=0;x<=q;++x)
if((x==0)||(q==0))
b=1;
else
b=(b*(q-x+1))/x;
prin ("%6d",b);
prin ("\n\n");
++q;
16
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
Output:
Pascals triangle
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Pascals triangle
1 1
1 2 1
1 3 3 1
17
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
array. Sort them in an ascending order using bubble sort technique. Print
both the given array and sorted array with suitable headings.
Program
#include<stdio.h>
#include<conio.h>
void main()
18
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
int a[10],n,i,j,temp;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
// Bubble Sor ng
if(a[j]>=a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
19
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
for(i=0;i<n;i++)
prin ("%d\n",a[i]);
getch();
------------------------------------------------------------------------------------------------------------
Output
Run 1:
65
84
91
20
20
65
84
91
20
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
------------------------------------------------------------------------------------------------------------
7. Write a C program to read two matrices A(MxN) and B(PxQ) and compute the product of A and B
a er checking compa bility for mul plica on. Output the input matrices and the resultant matrix
with suitable headings and format. (Using two dimension arrays where array size M, N, P, Q<=3).
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
int i,j,k,m,n,p,q,a[10][10],b[10][10],c[10][10];
void inputa()
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
21
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
void inputb()
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
void mulmatrix()
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]);
22
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
void output()
for(i=0;i<m;i++)
for(j=0;j<q;j++)
prin ("%d\t",c[i][j]);
prin ("\n");
void main()
clrscr();
scanf("%d%d",&m,&n);
inputa();
23
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
scanf("%d%d",&p,&q);
inputb();
if(n!=p)
prin ("\n The matrices are not compa ble for mul plica on! \n");
getch();
exit();
mulmatrix();
output();
getch();
------------------------------------------------------------------------------------------------------------
Output
Run 1:
24
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
123
456
111
222
333
14 14 14
32 32 32
------------------------------------------------------------------------------------------------------------
8 Write a C program to read a matrix A(M x N) and to find the following
i. Sum of the elements of the row
ii. Sum of the elements of the column
iii. Sum of all the elements of the matrix
iv. Sum of both diagonal elements of a matrix
v. Transpose of a matrix.
Output the computed results with suitable headings.
/*transpose*/
#include<stdio.h>
main()
{
int i,j,a[10][10],m,n,s,p,q,r,b[10][10],sum=0,c;
25
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
printf("Enter the size of the matrix\n");
scanf("%d%d",&m,&n);
printf("enter the elements of matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("enter the row you need to find the sum of \n");
scanf("%d",&p);
for(j=0;j<n;j++)
sum=sum+a[p1][j];
printf("sum of elementsof the row is %d \n",sum);
sum=0;
printf("enter the column you need to find the sum of \n");
scanf("%d",&q);
for(i=0;i<m;i++)
sum+=a[i][q1];
printf("sum of the elements of the column %d is %d ",q,sum);
sum=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
sum+=a[i][j];
printf("\nsum of all the elemetns of the matrix is %d",sum);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[j][i]=a[i][j];
}
}
sum=c=0;
26
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
if(m==n)
{
for (i = 0; i < m; ++i)
{
sum = sum + a[i][i];
c = c + a[i][m i 1];
}
printf("\nThe sum of the main diagonal elements is = %d\n", sum);
printf("The sum of the off diagonal elemets is = %d\n",c);
}
else
printf("\nThe matrix is not a square matrix hence can not find the diagonal sum\n");
printf("\ntranspose of matrix is \n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
}
OUTPUT:
1. enter the size of the matrix
2
3
enter the elements of matrix
1
1
1
2
2
2
enter the row you need to find the sum of
27
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
2
sum of elements of the row is 6
enter the column you need to find the sum of
2
sum of the elements of the column 2 is 3
sum of all the elements of the matrix is 9
The matrix is not a square matrix hence can not find the diagonal sum
transpose of matrix is
1 2
1 2
9. Write C user defined functions
(i) To input N real numbers into a single dimension array.
(ii) Compute their mean.
(iii) Compute their variance
(iv) Compute their standard deviation
#include<stdio.h>
#include<math.h>
int n,i,a[20];
float sum=0,mean,var,dev,sum1;
void input()
{
printf("enter the number of elements\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void find_mean()
{
28
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
sum=0;
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
mean=sum/n;
printf("mean= %f\n",mean);
}
void find_var()
{
sum1=0;
for(i=0;i<n;i++)
{
sum1=sum1+(a[i]mean)*(a[i]mean);
}
var=sum1/n;
printf("variance is = %f\n",var);
}
void find_dev()
{
dev=(float)sqrt(var);
printf("standard deviation = %f\n",dev);
}
void main()
{
input();
find_mean();
find_var();
find_dev();
}
29
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
output:
enter the number of elements
6
enter the elements
1
2
3
4
5
6
mean=3.500000
variance is= 2.916667
standard deviation=1.707825
10. Write a C program
i) To check whether a given input string is a palindrome or not.
ii) To find the number of vowels, consonants, digits and white space in a string.
To check whether a given input string is a palindrome or not.
#include<stdio.h>
#include<string.h>
main()
{
char test[100];
int begin,middle,end,length=0;
printf(“Enter a string to check whether palindrome or not \n”);
scanf("%s",test);
30
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
while(test[length]!='\0')
length++;
end=length1;middle=length/2;
for(begin=0;begin<middle;begin++)
{
if(test[begin]!=test[end])
printf("not a palindrome\n");
break;
end;
}
if(test[begin]==test[end])
printf("palindrome\n");
return 0;
}
output:
1. Enter a string to check whether palindrome or not
RVCE
not a palindrome
2. Enter a string to check whether palindrome or not
NAYAN
palindrome
To find the number of vowels, consonants, digits and white space in a string.
#include<stdio.h>
int main(){
char line[150];
int i,v,c,ch,d,s,o;
o=v=c=ch=d=s=0;
printf("Enter a line of string:\n");
gets(line);
31
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
for(i=0;line[i]!='\0';++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
++v;
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
++c;
else if(line[i]>='0'&&c<='9')
++d;
else if (line[i]==' ')
++s;
}
printf("Vowels: %d",v);
printf("\nConsonants: %d",c);
printf("\nDigits: %d",d);
printf("\nWhite spaces: %d",s);
return 0;
}
output:
Enter a line of string:
This program is easy 2 understand
Vowels: 9
Consonants: 18
Digits: 1
White spaces: 5
11. Write C user defined func ons
32
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
Program
#include<stdio.h>
#include<conio.h>
int n,a[20],key;
void input();
int i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&key);
void binarysearch()
int temp,key,low,mid,high;
low=0;
high=n-1;
while(low<=high)
33
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
getch();
exit();
else if(key>a[mid])
low=mid+1;
else
high=mid-1;
void main()
input();
binarysearch();
------------------------------------------------------------------------------------------------------------
Output
Run 1:
34
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
25
35
45
55
65
45
Run2:
10
20
30
40
50
60
80
------------------------------------------------------------------------------------------------------------
Program
#include<stdio.h>
#include<conio.h>
int n,i,j,temp,a[10],pos;
void input()
36
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
for(i=0;i<n;i++)
scanf("%d",&a[i]);
void output()
for(i=0;i<n;i++)
prin ("%d\n",a[i]);
void selsort()
pos=i;
if(a[j]>a[pos])
37
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
pos=j;
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
void main()
clrscr();
scanf("%d",&n);
input();
selsort();
output();
getch();
38
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
-----------------------------------------------------------------------------------------------------------
Output
Run 1:
20
57
80
45
93
62
Sorted list:
93
80
62
57
45
20
------------------------------------------------------------------------------------------------------------
13. Create a structure called student with the following members student name, roll
39
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
Program
#include<stdio.h>
#include<conio.h>
struct student
char name[30];
char rollno[20];
int marks[3];
};
void main()
stud a[20];
int i,n,ch,high1,high2,high3;
float avg1,avg2,avg3;
char rno[20];
40
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
clrscr();
prin ("\n\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",&a[i].name);
scanf("%s",&a[i].rollno);
scanf("%d",&a[i].marks[0]);
scanf("%d",&a[i].marks[1]);
scanf("%d",&a[i].marks[2]);
prin ("\n\n");
41
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
scanf("%d",&ch);
switch(ch)
scanf("%s",&rno);
for(i=0;i<n;i++)
if(strcmp(a[i].rollno,rno)==0)
break;
avg1=avg2=avg3=0;
for(i=0;i<n;i++)
avg1=avg1+a[i].marks[0];
avg2=avg1+a[i].marks[1];
avg3=avg1+a[i].marks[2];
42
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
break;
high1=a[0].marks[0];
high2=a[0].marks[1];
high3=a[0].marks[2];
for(i=1;i<n;i++)
if(a[i].marks[0]>high1)
high1=a[i].marks[0];
if(a[i].marks[1]>high2)
high1=a[i].marks[1];
if(a[i].marks[2]>high3)
high1=a[i].marks[2];
break;
43
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
break;
getch();
------------------------------------------------------------------------------------------------------------
Output
Run 1:
Details of 1 Student:
Enter rollno: 45
Enter marks1: 20
Enter marks2: 21
Enter marks3: 22
44
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
Details of 2 Student:
Enter rollno: 60
Enter marks1: 22
Enter marks2: 23
Enter marks3: 24
Details of 3 Student:
Enter rollno: 18
Enter marks1: 21
Enter marks2: 25
Enter marks3: 26
1-SEARCH
45
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
2-AVERAGE
3-HIGHEST
SEARCH SUCCESSFUL:
PQR 60 22 23 24
Run 2:
Details of 1 Student:
Enter rollno: 06
46
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
Enter marks1: 23
Enter marks2: 25
Enter marks3: 24
Details of 2 Student:
Enter rollno: 57
Enter marks1: 24
Enter marks2: 21
Enter marks3: 23
1-SEARCH
2-AVERAGE
3-HIGHEST
SEARCH FAILED!
Run 3:
Details of 1 Student:
Enter rollno: 45
Enter marks1: 20
Enter marks2: 21
Enter marks3: 22
Details of 2 Student:
Enter rollno: 65
Enter marks1: 22
Enter marks2: 23
48
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
Enter marks3: 24
Details of 3 Student:
Enter rollno: 80
Enter marks1: 24
Enter marks2: 25
Enter marks3: 20
1-SEARCH
2-AVERAGE
3-HIGHEST
Run 4:
Details of 1 Student:
Enter rollno: 25
Enter marks1: 25
Enter marks2: 22
Enter marks3: 21
Details of 2 Student:
Enter rollno: 15
Enter marks1: 23
50
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
Enter marks2: 24
Enter marks3: 22
1-SEARCH
2-AVERAGE
3-HIGHEST
Run 5:
Details of 1 Student:
51
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
Enter rollno: 56
Enter marks1: 23
Enter marks2: 24
Enter marks3: 25
Details of 2 Student:
Enter rollno: 12
Enter marks1: 20
Enter marks2: 21
Enter marks3: 22
1-SEARCH
2-AVERAGE
3-HIGHEST
52
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
Invalid choice!
------------------------------------------------------------------------------------------------------------
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char str1[20];
char str2[20];
char str3[20];
int i,ch,diff;
char *ptr1=str1;
char *ptr2=str2;
char *ptr3=str3;
53
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
clrscr();
prin ("\n\n");
scanf("%d",&ch);
prin ("\n");
switch(ch)
scanf("%s",str1);
break;
54
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
scanf("%s",str1);
scanf("%s",str2);
break;
ptr1++;
ptr2++;
if(diff==0)
else if(diff>0)
else
break;
scanf("%s",str1);
55
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
scanf("%s",str2);
*ptr3++ = *ptr1++;
while(*ptr2)
*ptr3++=*ptr2++;
break;
break;
getch();
------------------------------------------------------------------------------------------------------------
56
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
Output
Run 1:
1-STRING COPY
2-STRING COMPARE
3-STRING CONCATENATE
Run 2:
57
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
1-STRING COPY
2-STRING COMPARE
3-STRING CONCATENATE
Run 3:
1-STRING COPY
2-STRING COMPARE
3-STRING CONCATENATE
58
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
Run 4:
1-STRING COPY
2-STRING COMPARE
3-STRING CONCATENATE
59
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
Run 5:
1-STRING COPY
2-STRING COMPARE
3-STRING CONCATENATE
Run 6:
1-STRING COPY
60
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
2-STRING COMPARE
3-STRING CONCATENATE
Invalid choice!
------------------------------------------------------------------------------------------------------------
15 Write a C program to count no of lines, blank lines and comments in a given program
using files.
#include <stdio.h>
void main()
{
int line_count = 0, n_o_c_l = 0, n_o_n_b_l = 0, n_o_b_l = 0, n_e_c = 0;
FILE *fp1;
char ch;
fp1 = fopen("file.txt", "r");
while (((ch = fgetc(fp1)))!= EOF)
{
if (ch == '\n')
61
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
{
line_count++;
}
if (ch == '\n')
{
if ((ch = fgetc(fp1)) == '\n')
{
fseek(fp1, 1, 1);
n_o_b_l++;
}
}
if (ch == ';')
{
if ((ch = fgetc(fp1)) == '\n')
{
fseek(fp1, 1, 1);
n_e_c++;
}
}
}
fseek(fp1, 0, 0);
while ((ch = fgetc(fp1))!= EOF)
{
if (ch == '/')
{
if ((ch = fgetc(fp1)) == '/')
{
n_o_c_l++;
}
}
62
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
}
printf("Total no of lines: %d\n", line_count);
printf("Total no of comment line: %d\n", n_o_c_l);
printf("Total no of blank lines: %d\n", n_o_b_l);
printf("Total no of non blank lines: %d\n", line_countn_o_b_l);
printf("Total no of lines end with semicolon: %d\n", n_e_c);
}
input file contents: file.txt
department of
CSE RVCE
//this is a comment
output:
Total no of lines: 5
Total no of comment line:1
Total no of blank lines:2
Total no of non blank lines:3
Total no of lines end with semicolon:0
PART B PROGRAMS
63
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
1)STRINGS
1 a) Write a C program To remove all characters in a string except alphabets.
#include<stdio.h>
int main()
{
char line[150];
int i, j;
printf("Enter a string: ");
gets(line);
for(i = 0; line[i] != '\0'; ++i)
{
while (!( (line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' &&
line[i] <= 'Z') || line[i] == '\0') )
{
for(j = i; line[j] != '\0'; ++j)
{
line[j] = line[j+1];
}
line[j] = '\0';
}
}
printf("Output String: ");
puts(line);
return 0;
}
2) ARRAYS
Write a C program to insert an element in a specified position in an integer
array and print the array with inserted element.
1. #include <stdio.h>
2.
3. void main()
4. {
5. int array[10];
6. int i, j, n, m, temp, key, pos;
7.
8. printf("Enter how many elements \n");
9. scanf("%d", &n);
10. printf("Enter the elements \n");
11. for (i = 0; i < n; i++)
12. {
13. scanf("%d", &array[i]);
14. }
64
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
15. printf("Input array elements are \n");
16. for (i = 0; i < n; i++)
17. {
18. printf("%d\n", array[i]);
19. }
20. for (i = 0; i < n; i++)
21. {
22. for (j = i + 1; j < n; j++)
23. {
24. if (array[i] > array[j])
25. {
26. temp = array[i];
27. array[i] = array[j];
28. array[j] = temp;
29. }
30. }
31. }
32. printf("Sorted list is \n");
33. for (i = 0; i < n; i++)
34. {
35. printf("%d\n", array[i]);
36. }
37. printf("Enter the element to be inserted \n");
38. scanf("%d", &key);
39. for (i = 0; i < n; i++)
40. {
41. if (key < array[i])
42. {
43. pos = i;
44. break;
45. }
46. if (key > array[n1])
47. {
48. pos = n;
49. break;
50. }
51. }
52. if (pos != n)
53. {
54. m = n pos + 1 ;
55. for (i = 0; i <= m; i++)
56. {
57. array[n i + 2] = array[n i + 1] ;
58. }
59. }
60. array[pos] = key;
61. printf("Final list is \n");
62. for (i = 0; i < n + 1; i++)
63. {
64. printf("%d\n", array[i]);
65. }
66. }
$ cc pgm41.c
$ a.out
Enter how many elements
5
Enter the elements
76
90
65
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
56
78
12
Input array elements are
76
90
56
78
12
Sorted list is
12
56
76
78
90
Enter the element to be inserted
61
Final list is
12
56
61
76
78
90
3) RECURSION
Write a C program to find factorial of a number using recursion.
1. #include <stdio.h>
2.
3. int factorial(int);
4.
5. int main()
6. {
7. int num;
8. int result;
9.
10. printf("Enter a number to find it's Factorial: ");
11. scanf("%d", &num);
12. if (num < 0)
13. {
14. printf("Factorial of negative number not possible\n");
15. }
16. else
17. {
18. result = factorial(num);
19. printf("The Factorial of %d is %d.\n", num, result);
20. }
21. return 0;
22. }
23. int factorial(int num)
24. {
25. if (num == 0 || num == 1)
26. {
27. return 1;
28. }
29. else
66
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
30. {
31. return(num * factorial(num 1));
32. }
33. }
$ cc pgm5.c
$ a.out
Enter a number to find it's Factorial: 6
The Factorial of 6 is 720.
4) STACKS
Write a C program to implement basic operations(push,pop,display) of stack
using arrays.
#include <stdio.h>
#include<conio.h>
#define MAX 5
int top, status;
/*PUSH FUNCTION*/
void push (int stack[], int item)
{ if (top == (MAX1))
status = 0;
else
{ status = 1;
++top;
stack [top] = item;
}
}
/*POP FUNCTION*/
int pop (int stack[])
{
int ret;
if (top == 1)
{ ret = 0;
status = 0;
}
else
{ status = 1;
ret = stack [top];
top;
}
67
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
return ret;
}
/*FUNCTION TO DISPLAY STACK*/
void display (int stack[])
{ int i;
printf ("\nThe Stack is: ");
if (top == 1)
printf ("empty");
else
{ for (i=top; i>=0; i)
printf ("\n\n|%3d |\n",stack[i]);
}
printf ("\n");
}
/*MAIN PROGRAM*/
void main()
{
int stack [MAX], item;
int ch;
clrscr ();
top = 1;
do
{ do
{ printf ("\NMAIN MENU");
printf ("\n1.PUSH (Insert) in the Stack");
printf ("\n2.POP (Delete) from the Stack");
printf ("\n3.Exit (End the Execution)");
printf ("\nEnter Your Choice: ");
scanf ("%d", &ch);
if (ch<1 || ch>3)
printf ("\nInvalid Choice, Please try again");
}while (ch<1 || ch>3);
switch (ch)
{case 1:
printf ("\nEnter the Element to be pushed : ");
scanf ("%d", &item);
printf (" %d", item);
push (stack, item);
if (status)
{ printf ("\nAfter Pushing ");
display (stack);
if (top == (MAX1))
68
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
printf ("\nThe Stack is Full");
}
else
printf ("\nStack overflow on Push");
break;
case 2:
item = pop (stack);
if (status)
{ printf ("\nThe Popped item is %d. After Popping:
");
display (stack);
}
else
printf ("\nStack underflow on Pop");
break;
default:
printf ("\nEND OF EXECUTION");
}
}while (ch != 3);
getch();
}
5)QUEUE
Write a C program to implement basic operations(insert,delete,display) of
queues using arrays.
1. #include <stdio.h>
2.
3. #define MAX 50
4. int queue_array[MAX];
5. int rear = 1;
6. int front = 1;
7. main()
8. {
9. int choice;
10. while (1)
11. {
12. printf("1.Insert element to queue \n");
13. printf("2.Delete element from queue \n");
14. printf("3.Display all elements of queue \n");
15. printf("4.Quit \n");
16. printf("Enter your choice : ");
17. scanf("%d", &choice);
18. switch (choice)
19. {
20. case 1:
21. insert();
22. break;
69
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
23. case 2:
24. delete();
25. break;
26. case 3:
27. display();
28. break;
29. case 4:
30. exit(1);
31. default:
32. printf("Wrong choice \n");
33. } /*End of switch*/
34. } /*End of while*/
35. } /*End of main()*/
36. insert()
37. {
38. int add_item;
39. if (rear == MAX 1)
40. printf("Queue Overflow \n");
41. else
42. {
43. if (front == 1)
44. /*If queue is initially empty */
45. front = 0;
46. printf("Inset the element in queue : ");
47. scanf("%d", &add_item);
48. rear = rear + 1;
49. queue_array[rear] = add_item;
50. }
51. } /*End of insert()*/
52.
53. delete()
54. {
55. if (front == 1 || front > rear)
56. {
57. printf("Queue Underflow \n");
58. return ;
59. }
60. else
61. {
62. printf("Element deleted from queue is : %d\n",
queue_array[front]);
63. front = front + 1;
64. }
65. } /*End of delete() */
66. display()
67. {
68. int i;
69. if (front == 1)
70. printf("Queue is empty \n");
71. else
72. {
73. printf("Queue is : \n");
74. for (i = front; i <= rear; i++)
75. printf("%d ", queue_array[i]);
76. printf("\n");
77. }
78. } /*End of display() */
$cc pgm.c
$ a.out
1.Insert element to queue
70
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 10
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 15
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 20
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 30
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Element deleted from queue is : 10
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 3
Queue is :
15 20 30
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 4
6)STRUCTURES
Write C program to create a structure called complex with real and imaginary
as members. Based on users choice add two or subtract two complex numbers.
#include <stdio.h>
#include <stdlib.h>
struct complex
{
int real, img;
};
int main()
71
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
{
int choice, temp1, temp2, temp3;
struct complex a, b, c;
while(1)
{
printf("Press 1 to add two complex numbers.\n");
printf("Press 2 to subtract two complex numbers.\n");
printf("Press 3 to multiply two complex numbers.\n");
printf("Press 4 to divide two complex numbers.\n");
printf("Press 5 to exit.\n");
printf("Enter your choice\n");
scanf("%d",&choice);
if( choice == 5)
exit(0);
if(choice >= 1 && choice <= 4)
{
printf("Enter a and b where a + ib is the first complex number.");
printf("\na = ");
scanf("%d", &a.real);
printf("b = ");
scanf("%d", &a.img);
printf("Enter c and d where c + id is the second complex number.");
printf("\nc = ");
scanf("%d", &b.real);
printf("d = ");
scanf("%d", &b.img);
}
if ( choice == 1 )
{
c.real = a.real + b.real;
c.img = a.img + b.img;
if ( c.img >= 0 )
printf("Sum of two complex numbers = %d + %di",c.real,c.img);
else
printf("Sum of two complex numbers = %d %di",c.real,c.img);
}
else if ( choice == 2 )
{
c.real = a.real b.real;
c.img = a.img b.img;
if ( c.img >= 0 )
printf("Difference of two complex numbers = %d +
%di",c.real,c.img);
else
printf("Difference of two complex numbers = %d %di",c.real,c.img);
}
else if ( choice == 3 )
{
c.real = a.real*b.real a.img*b.img;
c.img = a.img*b.real + a.real*b.img;
if ( c.img >= 0 )
printf("Multiplication of two complex numbers = %d +
%di",c.real,c.img);
else
72
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
printf("Multiplication of two complex numbers = %d
%di",c.real,c.img);
}
else if ( choice == 4 )
{
if ( b.real == 0 && b.img == 0 )
printf("Division by 0 + 0i is not allowed.");
else
{
temp1 = a.real*b.real + a.img*b.img;
temp2 = a.img*b.real a.real*b.img;
temp3 = b.real*b.real + b.img*b.img;
if ( temp1%temp3 == 0 && temp2%temp3 == 0 )
{
if ( temp2/temp3 >= 0)
printf("Division of two complex numbers = %d +
%di",temp1/temp3,temp2/temp3);
else
printf("Division of two complex numbers = %d
%di",temp1/temp3,temp2/temp3);
}
else if ( temp1%temp3 == 0 && temp2%temp3 != 0 )
{
if ( temp2/temp3 >= 0)
printf("Division of two complex numbers = %d +
%d/%di",temp1/temp3,temp2,temp3);
else
printf("Division of two complex numbers = %d
%d/%di",temp1/temp3,temp2,temp3);
}
else if ( temp1%temp3 != 0 && temp2%temp3 == 0 )
{
if ( temp2/temp3 >= 0)
printf("Division of two complex numbers = %d/%d +
%di",temp1,temp3,temp2/temp3);
else
printf("Division of two complex numbers = %d
%d/%di",temp1,temp3,temp2/temp3);
}
else
{
if ( temp2/temp3 >= 0)
printf("Division of two complex numbers = %d/%d +
%d/%di",temp1,temp3,temp2,temp3);
else
printf("Division of two complex numbers = %d/%d
%d/%di",temp1,temp3,temp2,temp3);
}
}
}
else
printf("Invalid choice.");
printf("\nPress any key to enter choice again...\n");
}
}
7) POINTERS
73
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
Write a C program to find the average of array elements calling functions
through call by reference.
1. #include <stdio.h>
2. #define MAXSIZE 10
3.
4. void main()
5. {
6. int array[MAXSIZE];
7. int i, num, negative_sum = 0, positive_sum = 0;
8. float total = 0.0, average;
9.
10. printf ("Enter the value of N \n");
11. scanf("%d", &num);
12. printf("Enter %d numbers (ve, +ve and zero) \n", num);
13. for (i = 0; i < num; i++)
14. {
15. scanf("%d", &array[i]);
16. }
17. printf("Input array elements \n");
18. for (i = 0; i < num; i++)
19. {
20. printf("%+3d\n", array[i]);
21. }
22. /* Summation starts */
23. for (i = 0; i < num; i++)
24. {
25. if (array[i] < 0)
26. {
27. negative_sum = negative_sum + array[i];
28. }
29. else if (array[i] > 0)
30. {
31. positive_sum = positive_sum + array[i];
32. }
33. else if (array[i] == 0)
34. {
35. ;
36. }
37. total = total + array[i] ;
38. }
39. average = total / num;
40. printf("\n Sum of all negative numbers = %d\n", negative_sum);
41. printf("Sum of all positive numbers = %d\n", positive_sum);
42. printf("\n Average of all input numbers = %.2f\n", average);
43. }
$ cc pgm19.c
$ a.out
Enter the value of N
10
Enter 10 numbers (ve, +ve and zero)
8
9
100
80
90
45
23
1
0
74
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
16
Input array elements
8
+9
100
80
+90
+45
23
1
+0
+16
Sum of all negative numbers = 212
Sum of all positive numbers = 160
Average of all input numbers = 5.20
Call by reference ,
Ex : #include <stdio.h>
/* function declaration */
void swap(int *x, int *y);
int main () {
/* local variable definition */
int a = 100;
int b = 200;
printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b );
/* calling a function to swap the values.
* &a indicates pointer to a ie. address of variable a and
* &b indicates pointer to b ie. address of variable b.
*/
swap(&a, &b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
return 0;
}
8) FILES
Write a C program to copy the contents of one file to another file.
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr1, *fptr2;
75
FINDING ROOTS OF A QUADRATIC EQUATION PIC LAB MANUAL
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}
Output:
Enter the filename to open for reading
a.txt
Enter the filename to open for writing
b.txt
Contents copied to b.txt
76