The document describes several programs to solve problems related to quadratic equations, reversing integers, finding square roots, and determining if a year is a leap year. It includes algorithms and C code to:
1) Take coefficients of a quadratic equation as input and compute all possible roots, handling real, equal, and complex cases.
2) Reverse an integer number and check if it is a palindrome.
3) Find the square root of a number without using library functions.
4) Read a year and determine if it is a leap year, considering end of century rules.
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.