To Write A C Program To Find The Roots of A Quadratic Equation
To Write A C Program To Find The Roots of A Quadratic Equation
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);
return 0;
}
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");
}
}
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
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;
}
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;
}
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;
}
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;
}
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");
}
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;
}