0% found this document useful (0 votes)
483 views12 pages

To Write A C Program To Find The Roots of A Quadratic Equation

The document describes algorithms and programs to solve several problems related to quadratic equations, triangles, number manipulation, and counting positives/negatives/zeros. The quadratic equation program takes inputs a, b, c and uses the discriminant to determine if the roots are real/imaginary and prints the appropriate roots. The triangle program takes side inputs a, b, c, checks if a valid triangle, and prints the type of triangle and its area. The number programs find the sum of digits, reverse of a number, and factorial of a number by iterating through the digits. The leap year program checks for divisibility by 400, 100, and 4 to determine if a year is a leap year. The last program counts the positives, negatives

Uploaded by

roshni varghese
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
483 views12 pages

To Write A C Program To Find The Roots of A Quadratic Equation

The document describes algorithms and programs to solve several problems related to quadratic equations, triangles, number manipulation, and counting positives/negatives/zeros. The quadratic equation program takes inputs a, b, c and uses the discriminant to determine if the roots are real/imaginary and prints the appropriate roots. The triangle program takes side inputs a, b, c, checks if a valid triangle, and prints the type of triangle and its area. The number programs find the sum of digits, reverse of a number, and factorial of a number by iterating through the digits. The leap year program checks for divisibility by 400, 100, and 4 to determine if a year is a leap year. The last program counts the positives, negatives

Uploaded by

roshni varghese
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

FINDING THE ROOTS OF QUADRATIC EQUATION

AIM: To write a C Program to find the roots of a Quadratic equation.


ALGORITHM:
Step 1: Start
Step 2: Read a,b,c
Step 3: Initialize d= 𝑏 2 − 4𝑎𝑐
Step 4: If d>0 go to step 5
else go to step 7
Step 5: Calculate r1=(-b+(√(d))/2a) and r2 =(-b-(√(d))/2a)
Step 6: print the roots are real and distinct, root1= r1, root2= r2
Step 7: If d=0 go to step 8
else go to step 10
Step 8: Calculate root1=-b/2a
Step 9: Print the roots are real and equal, root1=root2= -b/2a
Step 10: Calculate real=-b/2a
Step 11: Calculate imaginary=√(d)/2a
Step 12: print roots are imaginary, root1= real+ i imaginary, root2= real- i imaginary
Step 13: Stop

PROGRAM:

/******************************************
C program to find all roots of a quadratic equation
*****************************************/
#include <stdio.h>
#include <math.h> /* Used for sqrt() */

int main()
{
float a, b, c;
float root1, root2, real, imaginary;
float discriminant;
printf("Enter values of a, b, c of quadratic equation (aX^2 + bX + c): ");
scanf("%f%f%f", &a, &b, &c);
/* Find discriminant of the equation */
discriminant = (b * b) - (4 * a * c);
/* Find the nature of discriminant */
if(discriminant > 0)
{
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);

printf("Two distinct and real roots exists: root1 =%.2f ,


root2=%.2f", root1, root2);
}
else if(discriminant == 0)
{
root1 = root2 = -b / (2 * a);
printf("Two equal and real roots exists: root1=%.2f,
root2 =%.2f ", root1, root2);
}
else if(discriminant < 0)
{
real = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);
printf("Two distinct complex roots exists: root1 =%.2f + i%.2f,
root2= %.2f - i%.2f", real, imaginary, real, imaginary);
}

return 0;
}
THE AREA AND NATURE OF A TRIANGLE

AIM: To write a C program to find the area and nature of a triangle.

ALGORITHM:

Step 1: Start
Step 2: Read a,b,c
Step 3: if (a + b > c) and (b + c > a) and (c + a > b) go to step 4
Else go to step 12
Step 4: If (a =b) and (b=c) go to step 5
else go to step 6
Step 5: print the triangle is equilateral
Step 6: If (a =b) or (b = c) or (a = c) go to step 7
Else go to step 8
Step 7: Print the triangle is isosceles
Step 8: Print the triangle is scalene.
Step 9: Calculate s=(a+b+c)/2
Step 10: Calculate Area=√𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐)
Step 11: print the area of the triangle is Area
Step 12: print the sides don’t make a triangle
Step 13: Stop
PROGRAM:
/******************************************
C program to find the area and nature of a triangle.
******************************************/
#include<stdio.h>
#include<math.h>

int main(void)
{
float a, b, c, s, area;
printf("Enter sides of a triangle: \n");
printf("a: ");
scanf("%f", &a);
printf("b: ");
scanf("%f", &b);
printf("c: ");
scanf("%f", &c);

// sum of any two sides must be greater than the third side
if( (a + b > c) && (b + c > a) && (c + a > b) )
{
// three sides are equal
if( (a == b )&& (b== c) )
{
printf("Triangle is equilateral.\n");
}

// two sides are equal


else if( (a == b) || (b == c) || (a == c) )
{
printf("Triangle is isosceles.\n");
}

// no sides are equal


else
{
printf("Triangle is scalene.\n");
}
s = (a + b + c) / 2; //semi perimeter
area = sqrt( s * (s - a) * (s - b) * (s - c) ); // area
printf("Area of triangle %.2f.", area);

}
else
{
printf("Sides don't make a triangle.");
}
return 0;
}
SUM OF DIGITS AND REVERSE OF A NUMBER

AIM: To write a C program to find the sum of digits and reverse of a number
ALGORITHM:
Step 1: Start
Step 2: Read num
Step 3: Set sum=0, rev=0
Step 4: Repeat step 5 to 8 until num=0
Step 5: Set d = num mod 10
Step 6: Set num=num/10
Step 7: Set sum=sum + d
Step 8: Set rev=rev*10+d
Step 9: Print sum of the digits of the number num is sum
Step 10: Print reverse of the digits of the number num is rev
Step 11: Stop.
PROGRAM:
/******************************************
C program to find the sum and reverse of a number
******************************************/
#include<stdio.h>
#include<conio.h>
int main( )
{
clrscr( )
int num,sum=0,rev=0,d;
printf("Enter the number: ");
scanf("%d",&n);
while(num>0)
{
d=num%10;
num=num/10;
sum=sum+d;
rev=rev*10+d;
}
printf("Sum of digits = %d", sum);
printf("\n Reverse of the number = %d", rev);
return 0;
}
FACTORIAL OF A NUMBER

AIM: To write a C program to find the factorial of a number


ALGORITHM:
Step 1: Start
Step 2: Read the number n
Step 3: Initialize i=1, fact=1
Step 4: Repeat step 5 to 6 until i>n
Step 5: Calculate fact = fact × i
Step 6: Set i=i+1
Step 7: Print factorial of the number n is fact
Step 8: Stop

PROGRAM:

/******************************************
C program to find the factorial of a number
******************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,fact;
fact=i=1;
clrscr();
printf("Enter a Number to Find Factorial: ");
scanf("%d",&n);
while(i<=n)
{
fact=fact*i;
i++;
}
printf("The Factorial of %d is : %d",n,fact);
return 0;
}

CHECK FOR A LEAP YEAR

AIM: To write a C program to check a leap year or not.


ALGORITHM:
Step 1: Start
Step 2: Read the value of the year into variable year
Step 3: If year mod 400 =0 then go to step 4
Else go to step 5
Step 4: Print the year n is a leap year
Step 5: If year mod 100 =0 then go to step 6
Else go to step 7
Step 6: Print the year n is not a leap year
Step 7: If year mod 4 =0 then go to step 8
Else go to step 9
Step 8: Print the year n is a leap year.
Step 9: Print the year n is not a leap year.
Step10: Stop

PROGRAM:

/******************************************
C program to check a leap year or not
******************************************/
#include <stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if (year%400 == 0) // Exactly divisible by 400
printf("%d is a leap year.\n", year);
else if (year%100 == 0) // Exactly divisible by 100 and not by 400
printf("%d isn't a leap year.\n", year);
else if (year%4 == 0) // Exactly divisible by 4 and neither by 100 nor 400
printf("%d is a leap year.\n", year);
else // Not divisible by 4 or 100 or 400
printf("%d isn't a leap year.\n", year);

return 0;
}

TO DISPLAY THE COUNT OF POSITIVES, NEGATIVES AND ZEROS

AIM: To display the count of positives, negatives and zeros in a set of N numbers.
ALGORITHM:
Step 1: Start
Step 2: Read the number of elements in the set into num
Step 3: Repeat the steps 4 to 11 until i>num
Step 4: Read the number into n
Step 5: Set i=i+1
Step 6: If n >0 then go to step 7
Else go to step 8
Step 7: Set nop=nop+1
Step 8: If n<0 then go to step 9
Else go to step 10
Step 9: Set non=non+1
Step 10: If n= 0 then go to step 11
Step11: Set noz=noz+1
Step12: Display the number positives nop, number of negatives non, number of zeros noz.
Step 12: Stop.

PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,num,nop=0,non=0,noz=0;
printf(“enter the number of elements in the set\n”);
scanf(“%d”,&num);
for(i=1;i<=num;i++)
{
printf(“enter the number\n”);
scanf(“%d”,&n);
if(n>0)
nop=nop+1;
else if(n>0)
non=non+1;
else if(n==0)
noz=noz+1;
}
printf("The number of positives=%d, number of negatives=%d and numbeer of zeros=%d", nop,non,noz);
return 0;
}

TO FIND LCM AND HCF OF 2 NUMBERS

AIM: To find the LCM and HCF of 2 numbers.


ALGORITHM:
Step 1: Start
Step 2: Read two numbers into x and y
Step 3: Set a=x and b=y.
Step 4: Repeat steps 5 – 7 until b=0
Step 5: Set t=b
Step 6: Set b=a mod b
Step 7: Set a=t
Step 8: Set gcd=a
Step 9: Set lcm=(xy)/gcd
Step 10: Print greatest common divisor of x and y is gcd
Step11: Print least common multiple of x and y is lcm
Step12: Stop.

PROGRAM:
#include<stdio.h>
#include<conio.h>
int main() {
int a, b, x, y, t, gcd, lcm;
printf("Enter two integers\n");
scanf("%d%d", &x, &y);
a = x;
b = y;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x*y)/gcd;
printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
return 0;
}

AMSTRONG NUMBERS WITHIN A RANGE

AIM: To find the Armstrong numbers within a range.


ALGORITHM:
Step 1: Start
Step 2: Read the lower range into t1 and upper range to t2
Step 3: Set num=t1
Step 4: Repeat steps 5 – 12 until num>t2
Step 5: Set temp=num
Step 6: Set sum=0
Step 7: Repeat steps 8-10 until temp=0
Step 8: Calculate r=temp mod 10
Step 9: Set temp=temp/10
Step 10: Calculate sum=sum+r3
Step11: If sum=num then go to step 12
Else go to step 13
Step12: Print num
Step13: num=num+1
Step14: Stop

PROGRAM:
#include<stdio.h>
void main()
{
int num,r,sum,temp;
int t1,t2;
printf("Input starting number of range: ");
scanf("%d",&t1);
printf("Input ending number of range : ");
scanf("%d",&t2);
printf("Armstrong numbers in given range are: ");
for(num=t1;num<=t2;num++)
{
temp=num;
sum = 0;

while(temp!=0)
{
r=temp % 10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);
}
printf("\n");
}

TO CALCULATE THE STANDARD DEVIATION OF N NUMBERS

AIM: To find the standard deviation of n numbers


ALGORITHM:
Step 1: Start
Step 2: Read the count of numbers into n
Step 3: Set i=0, sum=0
Step 4: Repeat step 5-7 while i<=n
Step 5: Read the element into array a[i]
Step 6: Set i=i+1
Step 7: Calculate sum=sum + a[i]
Step 8: Calculate mean=sum/n
Step 9:Set i=0
Step 10: Repeat step 11-12 while i<=n
Step 11: Calculate sum_deviation=sum_deviation+(a[i]-mean)*(a[i]-mean)
Step12: Set i=i+1
Step13: Calculate stddev=sqrt(sum_deviation/n)
Step14: Print stddev
Step 15:Stop

PROGRAM
#include <stdio.h>
#include <math.h>
int main()
{
int n, i;
float a[10], mean=0.0, sum_deviation=0.0,stddev=0.0;
printf("Enter count n of number to find the standard deviation ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0; i<n; ++i)
scanf("%f",&a[i]);
printf("\n");
for(i=0; i<n;++i)
{
mean+=a[i];
}
mean=mean/n;
for(i=0; i<n;++i)
sum_deviation+=(a[i]-mean)*(a[i]-mean);
stddev= sqrt(sum_deviation/n);
printf("The std deviation is %f\n",stddev);
return 0;
}

You might also like