SlideShare a Scribd company logo
1 of 3
Programming in C and Data Structures Lab(15CPL16)
Ayesha Shariff, CSE, SECAB IET-2016 Page 1
Lab Program1: Design and develop a flowchart or an algorithm that takes three coefficients (a, b, and c) of a Quadratic equation
(ax2+bx+c=0) as input and compute all possible roots. Implement a C program for the developed flowchart/algorithm and execute
the same to output the possible roots for a given set of coefficients with appropriate messages.
Problem Analysis:General form of Quadratic equation is:ax2+bx+c=0, where a, b and c are three coefficients with real values.Here is
the criteria thatis used to determine all possibleroots to this equation:
Discriminant(d) Nature of roots Equation for roots
b2- 4ac > 0 Real &
distinct(unequal)
These solutions are: root1 =
−𝑏+√ 𝑏2 −4𝑎𝑐
2𝑎
root2 =
−𝑏−√ 𝑏2−4𝑎𝑐
2𝑎
b2- 4ac = 0 Real & equal root1=root2=-b/(2a)
b2- 4ac < 0 Imaginary(complex) x1=(realpart)+i(imaginarypart)
x2= (realpart)-i(imaginarypart)
where (realpart)=-b/(2a) and (imaginarypart)=sqrt(-d)/(2a) where d=b2-4ac
ALGORITHM
Step1: [Beginning of algorithm]
Start
Step2: [Read co-efficients]
Read a,b,c
Step3: [Find DiscriminantValue]
d=b2 – 4ac
If d>0 go to step 4
ElseIf d==0 go to step 5
Elsego to step 6
Step4: [Compute real and distinctroots]
root1=(-b+sqrt(d))/2a
root2=(-b-sqrt(d))/2a
Printroots arereal and distinct
Printroot1 and root2
Go to step 7
Step5: [Compute equal roots]
root1=root2=-b/2a
Printroots arereal and equal
Printroot1 and root2
Go to step 7
Step6: [Compute complex roots]
realpart=-b/2a
imagpart=(sqrt(-disc))/2a
root1= realpart+i*imagpart
root2= realpart- i*imagpart
Printroot1 and root2
Go to step 7
Step 7: [End of algorithm]
Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
floata, b, c, discriminant,root1,root2, realPart, imagPart;
clrscr();
printf("Enter non-zero coefficients a,b and c: ");
scanf("%f %f %f",&a, &b, &c);
discriminant=b*b-4*a*c;
// real and unequal roots
if (discriminant>0)
{
// sqrt() function returns squareroot
root1 = (-b+sqrt(discriminant))/(2*a);
root2 = (-b-sqrt(discriminant))/(2*a);
printf("Roots arereal and distinctn root1 = %f
andn root2 = %fn",root1 , root2);
}
//real and equal roots
else if (discriminant==0)
{
root1 = root2 = -b/(2*a);
printf("Roots arereal and equaln root1 = root2 =
%fn", root1);
}
// imaginary roots
else
{
realPart= -b/(2*a);
imagPart= sqrt(-discriminant)/(2*a);
printf("Roots arecomplex and distinctn root1 =
%f+i%f andn root2 = %f-i%fn", realPart,imagPart,realPart,
imagPart);
}
getch();
}
/********************Output********************
1. [x2 + 2x + 1 = 0 ]
Enter non-zero coefficients a,b and c: 1 2 1
Roots are real and equal
root1 = root2 = -1.000000
2. [ 2x2 + 5x + 2 = 0 ]
Enter non-zero coefficients a,b and c: 2 5 2
Roots are real and distinct
root1 = -0.500000 and
root2 = -2.000000
3. [ 2x2 + 2x + 2 = 0 ]
Enter non-zero coefficients a,b and c: 2 2 2
Roots are complex and distinct
root1 = -0.500000+i0.866025 and
root2 = -0.500000-i0.866025
*************************************************/
Programming in C and Data Structures Lab(15CPL16)
Ayesha Shariff, CSE, SECAB IET-2016 Page 2
Lab Program 2: Design and develop an algorithm to find the reverse of an integer number NUM and check whether it is
PALINDROME or NOT. Implement a C program for the developed algorithm that takes an integer number as input and output the
reverse of the same with suitable messages. Ex: Num: 2014, Reverse: 4102, Not a Palindrome
Problem Analysis: For a given integer number num, its reverse number (say rev) is obtained by writingits digits in reverse order.
For Example, for number 1234,the reverse number is 4321.An integer number is a palindromeif the reverse of that number is the
same as the number itself.Thus, the numbers 5, 22, 141 and 2442 arepalindromenumbers
Num Rem =Num%10 Rev=rev*10+rem Num=num/10
0 1234
1234 1234%10= 4 0*10+4=4 1234/10
123 123%10=3 4*10+3=43 123/10
12 12%10=2 43*10+2=432 12/10
1 1%10=1 432*10+1=4321 1/10
0 Stop the whileloop (end condition)
ALGORITHM
Step1: [Beginning of algorithm]
Start
Step2: [Read the number to reverse]
Read num
Step3: [Initialize]
temp=num
Step4: [Looping]
Repeat whilenum!=0
rem=num%10
rev=rev*10+rem
num=num/10
Step5: [Printreversed number]
Printreverse of given number
Step6: [Compare original number and reversed number]
If temp=rev then
Print‘num is palindrome‘
Else
Print‘num is not palindrome‘
Step 7: [End of algorithm]
Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,num,rev=0,rem;
clrscr();
printf("Enter the numbern");
scanf("%d",&num);
n=num;
while(num != 0)
{
rem=num%10;
num = num/10;
rev=rev*10+rem;
}
printf("The reverse of given number %d is %dn",n,rev);
if(rev == n)
printf("The given number %d is a palindromen",n);
else
printf("The given number %d is not a
palindromen",n);
getch();
}
/****************OUTPUT**************
Enter the number
12321
The reverse of given number 12321 is 12321
The given number 12321 is a palindrome
Enter the number
1234
The reverse of given number 1231 is 4321
The given number 1234 is a not palindrome
*************************************/
************************************************************************************************************
Programming in C and Data Structures Lab(15CPL16)
Ayesha Shariff, CSE, SECAB IET-2016 Page 3
Lab Program 3a: Design and develop a flowchart to find the square root of given number n. Implement a C program for thesame
and execute for all possible inputs with appropriate messages. Note: Do not use library function sqrt(n)
ALGORITHM
Step1: [Beginning of algorithm]
Start
Step2: [Read a number]
Input num
Step3: [Check if positivenumber]
If num>=0
goto 4
else
goto 5
Step4: [Calculateand Printsquareroot]
sqroot=pow(num,0.5)
Printsqroot
Step5: [Printnot possibleto find square
root]
Print“cannot find squareroot”
Step 6: [End of algorithm]
Stop
FLOWCHART
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
floatnum,sqroot;
printf("Enter the numbern");
scanf("%f",&num);
if(num>=0)
{
sqroot=pow(num,0.5);
printf("nSquareroot of a number %f is
%f",num,sqroot);
}
else
printf("nNot possibleto find square
root");
getch();
}
/**************OUTPUT************
Enter the number
4
Square root of 4.000000 is 2.000000
Enter the number
-1
Not possibleto find squarethe root
Enter the number to find the squareroot
23
Square root of 23.000000 is 4.795832
Lab Program 3b: Design and develop a C program to read a year as an input and find whether it is leap year or not. Also consider
end of the centuries.

More Related Content

What's hot (19)

C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
week-16x
week-16xweek-16x
week-16x
 
Function basics
Function basicsFunction basics
Function basics
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 
C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
 
C program
C programC program
C program
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
 
week-6x
week-6xweek-6x
week-6x
 
6. function
6. function6. function
6. function
 
Introduction to c part 2
Introduction to c   part  2Introduction to c   part  2
Introduction to c part 2
 

Similar to QUADRATIC EQUATION SOLVER

Similar to QUADRATIC EQUATION SOLVER (20)

Simple C programs
Simple C programsSimple C programs
Simple C programs
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
C programming BY Mazedur
C programming BY MazedurC programming BY Mazedur
C programming BY Mazedur
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
2. operator
2. operator2. operator
2. operator
 
C Programming
C ProgrammingC Programming
C Programming
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,ppt
 
C programs
C programsC programs
C programs
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
C important questions
C important questionsC important questions
C important questions
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
901131 examples
901131 examples901131 examples
901131 examples
 
C - programming - Ankit Kumar Singh
C - programming - Ankit Kumar Singh C - programming - Ankit Kumar Singh
C - programming - Ankit Kumar Singh
 
C programming
C programmingC programming
C programming
 
sodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdfsodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdf
 
C code examples
C code examplesC code examples
C code examples
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 

QUADRATIC EQUATION SOLVER

  • 1. Programming in C and Data Structures Lab(15CPL16) Ayesha Shariff, CSE, SECAB IET-2016 Page 1 Lab Program1: Design and develop a flowchart or an algorithm that takes three coefficients (a, b, and c) of a Quadratic equation (ax2+bx+c=0) as input and compute all possible roots. Implement a C program for the developed flowchart/algorithm and execute the same to output the possible roots for a given set of coefficients with appropriate messages. Problem Analysis:General form of Quadratic equation is:ax2+bx+c=0, where a, b and c are three coefficients with real values.Here is the criteria thatis used to determine all possibleroots to this equation: Discriminant(d) Nature of roots Equation for roots b2- 4ac > 0 Real & distinct(unequal) These solutions are: root1 = −𝑏+√ 𝑏2 −4𝑎𝑐 2𝑎 root2 = −𝑏−√ 𝑏2−4𝑎𝑐 2𝑎 b2- 4ac = 0 Real & equal root1=root2=-b/(2a) b2- 4ac < 0 Imaginary(complex) x1=(realpart)+i(imaginarypart) x2= (realpart)-i(imaginarypart) where (realpart)=-b/(2a) and (imaginarypart)=sqrt(-d)/(2a) where d=b2-4ac ALGORITHM Step1: [Beginning of algorithm] Start Step2: [Read co-efficients] Read a,b,c Step3: [Find DiscriminantValue] d=b2 – 4ac If d>0 go to step 4 ElseIf d==0 go to step 5 Elsego to step 6 Step4: [Compute real and distinctroots] root1=(-b+sqrt(d))/2a root2=(-b-sqrt(d))/2a Printroots arereal and distinct Printroot1 and root2 Go to step 7 Step5: [Compute equal roots] root1=root2=-b/2a Printroots arereal and equal Printroot1 and root2 Go to step 7 Step6: [Compute complex roots] realpart=-b/2a imagpart=(sqrt(-disc))/2a root1= realpart+i*imagpart root2= realpart- i*imagpart Printroot1 and root2 Go to step 7 Step 7: [End of algorithm] Stop PROGRAM #include<stdio.h> #include<conio.h> #include<math.h> void main() { floata, b, c, discriminant,root1,root2, realPart, imagPart; clrscr(); printf("Enter non-zero coefficients a,b and c: "); scanf("%f %f %f",&a, &b, &c); discriminant=b*b-4*a*c; // real and unequal roots if (discriminant>0) { // sqrt() function returns squareroot root1 = (-b+sqrt(discriminant))/(2*a); root2 = (-b-sqrt(discriminant))/(2*a); printf("Roots arereal and distinctn root1 = %f andn root2 = %fn",root1 , root2); } //real and equal roots else if (discriminant==0) { root1 = root2 = -b/(2*a); printf("Roots arereal and equaln root1 = root2 = %fn", root1); } // imaginary roots else { realPart= -b/(2*a); imagPart= sqrt(-discriminant)/(2*a); printf("Roots arecomplex and distinctn root1 = %f+i%f andn root2 = %f-i%fn", realPart,imagPart,realPart, imagPart); } getch(); } /********************Output******************** 1. [x2 + 2x + 1 = 0 ] Enter non-zero coefficients a,b and c: 1 2 1 Roots are real and equal root1 = root2 = -1.000000 2. [ 2x2 + 5x + 2 = 0 ] Enter non-zero coefficients a,b and c: 2 5 2 Roots are real and distinct root1 = -0.500000 and root2 = -2.000000 3. [ 2x2 + 2x + 2 = 0 ] Enter non-zero coefficients a,b and c: 2 2 2 Roots are complex and distinct root1 = -0.500000+i0.866025 and root2 = -0.500000-i0.866025 *************************************************/
  • 2. Programming in C and Data Structures Lab(15CPL16) Ayesha Shariff, CSE, SECAB IET-2016 Page 2 Lab Program 2: Design and develop an algorithm to find the reverse of an integer number NUM and check whether it is PALINDROME or NOT. Implement a C program for the developed algorithm that takes an integer number as input and output the reverse of the same with suitable messages. Ex: Num: 2014, Reverse: 4102, Not a Palindrome Problem Analysis: For a given integer number num, its reverse number (say rev) is obtained by writingits digits in reverse order. For Example, for number 1234,the reverse number is 4321.An integer number is a palindromeif the reverse of that number is the same as the number itself.Thus, the numbers 5, 22, 141 and 2442 arepalindromenumbers Num Rem =Num%10 Rev=rev*10+rem Num=num/10 0 1234 1234 1234%10= 4 0*10+4=4 1234/10 123 123%10=3 4*10+3=43 123/10 12 12%10=2 43*10+2=432 12/10 1 1%10=1 432*10+1=4321 1/10 0 Stop the whileloop (end condition) ALGORITHM Step1: [Beginning of algorithm] Start Step2: [Read the number to reverse] Read num Step3: [Initialize] temp=num Step4: [Looping] Repeat whilenum!=0 rem=num%10 rev=rev*10+rem num=num/10 Step5: [Printreversed number] Printreverse of given number Step6: [Compare original number and reversed number] If temp=rev then Print‘num is palindrome‘ Else Print‘num is not palindrome‘ Step 7: [End of algorithm] Stop PROGRAM #include<stdio.h> #include<conio.h> void main() { int n,num,rev=0,rem; clrscr(); printf("Enter the numbern"); scanf("%d",&num); n=num; while(num != 0) { rem=num%10; num = num/10; rev=rev*10+rem; } printf("The reverse of given number %d is %dn",n,rev); if(rev == n) printf("The given number %d is a palindromen",n); else printf("The given number %d is not a palindromen",n); getch(); } /****************OUTPUT************** Enter the number 12321 The reverse of given number 12321 is 12321 The given number 12321 is a palindrome Enter the number 1234 The reverse of given number 1231 is 4321 The given number 1234 is a not palindrome *************************************/ ************************************************************************************************************
  • 3. Programming in C and Data Structures Lab(15CPL16) Ayesha Shariff, CSE, SECAB IET-2016 Page 3 Lab Program 3a: Design and develop a flowchart to find the square root of given number n. Implement a C program for thesame and execute for all possible inputs with appropriate messages. Note: Do not use library function sqrt(n) ALGORITHM Step1: [Beginning of algorithm] Start Step2: [Read a number] Input num Step3: [Check if positivenumber] If num>=0 goto 4 else goto 5 Step4: [Calculateand Printsquareroot] sqroot=pow(num,0.5) Printsqroot Step5: [Printnot possibleto find square root] Print“cannot find squareroot” Step 6: [End of algorithm] Stop FLOWCHART PROGRAM #include<stdio.h> #include<conio.h> #include<math.h> void main() { floatnum,sqroot; printf("Enter the numbern"); scanf("%f",&num); if(num>=0) { sqroot=pow(num,0.5); printf("nSquareroot of a number %f is %f",num,sqroot); } else printf("nNot possibleto find square root"); getch(); } /**************OUTPUT************ Enter the number 4 Square root of 4.000000 is 2.000000 Enter the number -1 Not possibleto find squarethe root Enter the number to find the squareroot 23 Square root of 23.000000 is 4.795832 Lab Program 3b: Design and develop a C program to read a year as an input and find whether it is leap year or not. Also consider end of the centuries.