0% found this document useful (0 votes)
96 views

All Lab Programs.

The document describes two C programs. The first program finds and outputs the roots of a quadratic equation by taking in coefficients and using if/else statements to check for real, equal, or complex roots. The second program simulates a basic calculator that performs addition, subtraction, multiplication, and division on integers using a switch statement and checks for divide by zero errors. It also includes programs to check if a number is a palindrome or Armstrong number using loops.

Uploaded by

Arpit Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

All Lab Programs.

The document describes two C programs. The first program finds and outputs the roots of a quadratic equation by taking in coefficients and using if/else statements to check for real, equal, or complex roots. The second program simulates a basic calculator that performs addition, subtraction, multiplication, and division on integers using a switch statement and checks for divide by zero errors. It also includes programs to check if a number is a palindrome or Armstrong number using loops.

Uploaded by

Arpit Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

PROGRAMS

1. Write a C program to find and output all the roots of a given quadratic equation, for 
non­zero 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*b­4*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
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­

2. Write a C program to simulate a simple calculator that performs arithme c

opera ons like addi on, subtrac on, mul plica on, and division only on integers.

Error message should be reported, if any a empt is made to divide by zero.

(Using switch statement).

Program

#include<stdio.h>

#include<math.h>

int main()

int a,b,c;

3
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

int ch; //stores the choice of opera on

// input sec on

prin ("\nEnter two numbers\n" );

scanf("%d%d",&a,&b);

prin ("\n\n");

prin ("\n 1-ADD\n");

prin ("\n 2-SUB\n");

prin ("\n 3-MULTI\n");

prin ("\n 4-DIVIDE\n");

prin ("\nEnter your choice of opera on\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)

prin ("\n Divide by zero error! \n");

break;

4
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

else

c=a/b; //perform division opera on

break;

default: //erroneous input

prin ("\nInvalid choice! \n");

break;

prin ("\nThe result is %d\n",c);

------------------------------------------------------------------------------------------------------------

Output

Run 1:

Enter two numbers


5
7

1-ADD

2-SUB

3-MULTI

5
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

4-DIVIDE

Enter your choice of opera on


1

The result is 12

Run 2:

Enter two numbers


9
4

1-ADD

2-SUB

3-MULTI

4-DIVIDE

Enter your choice of opera on


2

The result is 5

Run 3:

Enter two numbers


6
5

6
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

1-ADD

2-SUB

3-MULTI

4-DIVIDE

Enter your choice of opera on


3

The result is 30

Run 4:

Enter two numbers


9
3

1-ADD

2-SUB

3-MULTI

4-DIVIDE

Enter your choice of opera on


4

The result is 3

7
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

Run 5:

Enter two numbers


8
0

1-ADD

2-SUB

3-MULTI

4-DIVIDE

Enter your choice of opera on


4

Divide by zero error!

Run 6:

Enter two numbers


4
6

1-ADD

2-SUB

3-MULTI

8
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

4-DIVIDE

Enter your choice of opera on


5

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;

prin ("\n Enter the number \n");

scanf("%d",&n );

// ini al values for loop

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)

prin ("\n Number is a palindrome! \n",n);

else

prin ("\n Number is not a palindrome! \n",n);

------------------------------------------------------------------------------------------------------------

Output

Run 1:

Enter the number

1551

Number is a palindrome!

Run 2:

10
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

Enter the number

5678

Number is not a palindrome! 

­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­                  

ii) To check whether a given integer number is an Armstrong number or not. 

#include <stdio.h>

#include <math.h>

int main()

int number, originalNumber, remainder, result = 0, n = 0 ;

prin ("Enter an integer: ");

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;

result += pow(remainder, n);

originalNumber /= 10;

if(result == number)

prin ("%d is an Armstrong number.", number);

else

prin ("%d is not an Armstrong number.", number);

return 0;

output:

1. Enter an integer: 371


371 is an Armstrong number

2. Enter an integer: 121


121 is not an Armstrong number

3. Enter an integer: 1634


1634 is an Armstrong number

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;

prin ("Enter the number of rows you want to input\n");

scanf("%d",&r);

prin ("\n Pascals triangle\n");

while(q<r)

for(p=40-3*q;p>0;--p)

prin (" ");

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:

Enter the number of rows you want to input

Pascals triangle

1 1

1 2 1

1 3 3 1

1 4 6 4 1

Enter the number of rows you want to input

Pascals triangle

1 1

1 2 1

1 3 3 1

17
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

5. Write a C program to input N integer numbers into a single dimension

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();

prin ("\n Enter the size of n:\n" );

scanf("%d",&n);

prin ("\n Enter the array elements: \n");

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

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

// Bubble Sor ng

for(i=1;i<n;i++) /* ith smallest number bubbles up to its

right spot in ith itera on */

for(j=0;j<n-i;j++) /* bubbling starts from the "deepest numbers"

and proceeds upwards */

/* element at jth posi on is "lighter" than the one on top,

therefore jth element bubbles up */

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

prin ("\n The sorted array is: \n" );

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

prin ("%d\n",a[i]);

getch();

------------------------------------------------------------------------------------------------------------

Output

Run 1:

Enter the size of n:

Enter the array elements:

65

84

91

20

The sorted array is:

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();

prin ("\n Enter the dimensions of matrix A: \n");

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

prin ("\n Enter the array elements: \n");

inputa();

23
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

prin ("\n Enter the dimensions of matrix B: \n");

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

prin ("\n Enter the array elements: \n");

inputb();

if(n!=p)

prin ("\n The matrices are not compa ble for mul plica on! \n");

getch();

exit();

mulmatrix();

prin ("\n Product of two matrices A & B is: \n");

output();

getch();

------------------------------------------------------------------------------------------------------------

Output

Run 1:

Enter the dimensions of matrix A:

24
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

Enter the array elements:

123

456

Enter the dimensions of matrix B:

Enter the array elements:

111

222

333

Product of two matrices A & B is:

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[p­1][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][q­1];
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=length­1;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

i) To input N integer numbers into a single dimension array.


ii) To conduct a Binary search.
Using these func ons, write a C program to accept the N integer numbers & given key integer number
and conduct a Binary search. Report success or failure in the form of a suitable message.

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;

prin ("\n Enter the size of n: \n");

scanf("%d",&n);

prin ("\n Enter numbers in ascending order\n");

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

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

prin ("\n Enter number to be searched: \n");

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

mid=(low+high)/2; //Index of the middle element

if(key==a[mid]) // Key was found in the middle

prin ("\nSuccessful search! Key found at posi on %d\n",mid+1);

getch();

exit();

else if(key>a[mid])

low=mid+1;

else

high=mid-1;

prin ("\n Unsuccessful search! Key not found\n",key);

void main()

input();

binarysearch();

------------------------------------------------------------------------------------------------------------

Output

Run 1:

34
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

Enter the size of n:

Enter numbers in ascending order

25

35

45

55

65

Enter number to be searched:

45

Successful search! Key found at posi on 3

Run2:

Enter the size of n:

Enter numbers in ascending order

10

20

30

40

50

60

Enter number to be searched:


35
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

80

Unsuccessful search! Key not found

------------------------------------------------------------------------------------------------------------

                                                                                

                                                                                

                                                                                

                                                                                

                                                                                

                                                                                

12. Write C user defined func ons

a) To input N integer numbers into a single dimension array.


b) To sort the integer numbers in descending order using selec on sort technique.
c) To print the single dimension array elements.
Using these func ons, write a C program to input N integer numbers into a single dimension
array, sort them in descending order, and print both the given array & the sorted array with
suitable headings.

Program

#include<stdio.h>

#include<conio.h>

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

// Reads n integers from the console

void input()

36
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

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

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

// Writes n integers to the console

void output()

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

prin ("%d\n",a[i]);

// Sorts the n integers in a[] in descending order using selec on sort

void selsort()

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

pos=i;

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

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();

prin ("\n Enter the value of n: ");

scanf("%d",&n);

// Read n numbers into list

prin ("\n Enter the elements: \n");

input();

// Sort using selec on sort

selsort();

// Display sorted list

prin ( "\n Sorted list: \n" );

output();

getch();

38
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

-----------------------------------------------------------------------------------------------------------

Output

Run 1:

Enter the value of n: 6

Enter the elements:

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

no, marks in three tests. Write a C program to create N records and

39
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

a) Search on roll no and display all the records.


b) Average marks in each test.
c) Highest marks in each test.

Program

#include<stdio.h>

#include<conio.h>

//Maintains student name, rollno and marks in 3 tests

struct student

char name[30];

char rollno[20];

int marks[3];

};

typedef struct student stud;

//Calls the main func on to be performed

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 (" ****** STUDENTS RECORDS ****** ");

prin ("\n\n");

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

scanf("%d",&n);

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

prin ("\n\n Details of %d Student: \n",i+1);

prin ("\n Enter name: ");

scanf("%s",&a[i].name);

prin ("\n Enter rollno: ");

scanf("%s",&a[i].rollno);

prin ("\n Enter marks1: ");

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

prin ("\n Enter marks2: ");

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

prin ("\n Enter marks3: ");

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

prin ("\n\n");

prin ("\n 1-SEARCH\n");

prin ("\n 2-AVERAGE\n");

prin ("\n 3-HIGHEST\n");

prin ("\n Enter your choice: ");

41
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

scanf("%d",&ch);

switch(ch)

case 1: //Func on to search rollno & display all records

prin ("\n Enter rollno to be searched: ");

scanf("%s",&rno);

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

if(strcmp(a[i].rollno,rno)==0)

prin ("\n SEARCH SUCCESSFUL: \n");

prin ("\n Details of student is: \n");

prin ("\nNAME ROLLNO MARKS1 MARKS2 MARKS3: \n");

prin ("\n%s\t%s\t%d\t%d\t%d\t\n",a[i].name,a[i].rollno,a[i].marks[0],a[i].marks[1], a[i].marks[2]);

prin ("\n SEARCH FAILED! \n");

break;

case 2: //Func on to find average marks in each test

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

prin ("\n Average of test1= %f\n",avg1/n);

prin ("\n Average of test2= %f\n",avg2/n);

prin ("\n Average of test3= %f\n",avg3/n);

break;

case 3: //Func on to find highest marks in each test

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];

prin ("\n Highest marks in test1= %d\n", high1);

prin ("\n Highest marks in test2= %d\n", high2);

prin ("\n Highest marks in test3= %d\n", high3);

break;

default: //erroneous input

43
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

prin ("\n Invalid choice! \n");

break;

getch();

------------------------------------------------------------------------------------------------------------

Output

Run 1:

****** STUDENTS RECORDS ******

Enter the number of students: 3

Details of 1 Student:

Enter name: ABC

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 name: PQR

Enter rollno: 60

Enter marks1: 22

Enter marks2: 23

Enter marks3: 24

Details of 3 Student:

Enter name: XYZ

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

Enter your choice: 1

Enter rollno to be searched: 60

SEARCH SUCCESSFUL:

Details of student is:

NAME ROLLNO MARKS1 MARKS2 MARKS3:

PQR 60 22 23 24

Run 2:

****** STUDENTS RECORDS ******

Enter the number of students: 2

Details of 1 Student:

Enter name: abc

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 name: qwe

Enter rollno: 57

Enter marks1: 24

Enter marks2: 21

Enter marks3: 23

1-SEARCH

2-AVERAGE

3-HIGHEST

Enter your choice: 1

Enter rollno to be searched: 45


47
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

SEARCH FAILED!

Run 3:

****** STUDENTS RECORDS ******

Enter the number of students: 3

Details of 1 Student:

Enter name: ABC

Enter rollno: 45

Enter marks1: 20

Enter marks2: 21

Enter marks3: 22

Details of 2 Student:

Enter name: PQR

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 name: XYZ

Enter rollno: 80

Enter marks1: 24

Enter marks2: 25

Enter marks3: 20

1-SEARCH

2-AVERAGE

3-HIGHEST

Enter your choice: 2

Average of test1= 22.000000

Average of test2= 30.333333

Average of test3= 28.666667


49
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

Run 4:

****** STUDENTS RECORDS ******

Enter the number of students: 2

Details of 1 Student:

Enter name: RTS

Enter rollno: 25

Enter marks1: 25

Enter marks2: 22

Enter marks3: 21

Details of 2 Student:

Enter name: LMN

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

Enter your choice: 3

Highest marks in test1= 22

Highest marks in test2= 22

Highest marks in test3= 21

Run 5:

****** STUDENTS RECORDS ******

Enter the number of students: 2

Details of 1 Student:

51
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

Enter name: ABC

Enter rollno: 56

Enter marks1: 23

Enter marks2: 24

Enter marks3: 25

Details of 2 Student:

Enter name: PQR

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

Enter your choice: 5

Invalid choice!

                                                                                

------------------------------------------------------------------------------------------------------------

14. Perform following string opera ons

a) Write a C program to copy a string using pointers.


b) Write a C program to compare two string using pointers.
c) Write a C program to concatenate two string using pointers.

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 ("***** STRING OPERATIONS ******");

prin ("\n\n");

prin ("\n 1-STRING COPY \n");

prin ("\n 2-STRING COMPARE \n");

prin ("\n 3-STRING CONCATENATE \n");

prin ("\n Enter your choice: ");

scanf("%d",&ch);

prin ("\n");

switch(ch)

case 1: //Func on to perform string copy opera on

prin ("\n Enter the string: ");

scanf("%s",str1);

for(i=0;*(ptr1+i)!='\0';i++) //If not NULL character

*(ptr2+i)=*(ptr1+i); //Copy from source to des na on

*(ptr2+i)='\0'; /* Place NULL character at the

end of des na on string */

prin ("\n The des na on string= %s",str2);

break;

case 2: // Func on to perform string compare opera on

54
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

prin ("\n Enter the first string: ");

scanf("%s",str1);

prin ("\n Enter the second string: ");

scanf("%s",str2);

while(*ptr1 == *ptr2) //If equal

if(*ptr1=='\0') //NULL character specifies end of str1

break;

//Points to next character in each string when comparing

ptr1++;

ptr2++;

diff= *ptr1 - *ptr2; //Take difference to compare two strings

if(diff==0)

prin ("\n Two strings are equal! \n");

else if(diff>0)

prin ("\n First string is greater than the second! \n");

else

prin ("\n Second string is greater than the first! \n");

break;

case 3: //Func on to perform string concatenate opera on

prin ("\n Enter the first string: ");

scanf("%s",str1);

55
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

prin ("\n Enter the second string: ");

scanf("%s",str2);

/* Copy the first string to des na on string excluding

the null character */

while(*ptr1) // If not NULL character

*ptr3++ = *ptr1++;

/* Copy the second string to des na on string excluding

the null character */

while(*ptr2)

*ptr3++=*ptr2++;

*ptr3='\0'; //NULL terminates the des na on string

prin ("\n The concatenated string= %s", str3);

break;

default: //erroneous input

prin ("\n Invalid choice! ");

break;

getch();

------------------------------------------------------------------------------------------------------------

56
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

Output

Run 1:

***** STRING OPERATIONS ******

1-STRING COPY

2-STRING COMPARE

3-STRING CONCATENATE

Enter your choice: 1

Enter the string: computerscience

The des na on string= computerscience

Run 2:

***** STRING OPERATIONS ******

57
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

1-STRING COPY

2-STRING COMPARE

3-STRING CONCATENATE

Enter your choice: 2

Enter the first string: computer

Enter the second string: computer

Two strings are equal!

Run 3:

***** STRING OPERATIONS ******

1-STRING COPY

2-STRING COMPARE

3-STRING CONCATENATE

Enter your choice: 2

58
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

Enter the first string: science

Enter the second string: engineer

First string is greater than the second!

Run 4:

***** STRING OPERATIONS ******

1-STRING COPY

2-STRING COMPARE

3-STRING CONCATENATE

Enter your choice: 2

Enter the first string: informa on

Enter the second string: technology

Second string is greater than the first!

59
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

Run 5:

***** STRING OPERATIONS ******

1-STRING COPY

2-STRING COMPARE

3-STRING CONCATENATE

Enter your choice: 3

Enter the first string: so ware

Enter the second string: architecture

The concatenated string= so warearchitecture

Run 6:

***** STRING OPERATIONS ******

1-STRING COPY

60
FINDING ROOTS OF A QUADRATIC EQUATION                                                              PIC LAB MANUAL

2-STRING COMPARE

3-STRING CONCATENATE

Enter your choice: 9

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_count­n_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 b) Write a C program To remove duplicate characters from the


given string.

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[n­1])
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 == (MAX­1))
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 == (MAX­1))
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

You might also like