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

C Programs

The document contains three C programs: one for solving quadratic equations, another for generating the Fibonacci series, and a third for calculating the square root of a number using a goto statement. Each program includes input prompts and outputs results based on user-provided values. The examples demonstrate the functionality of each program with sample inputs and corresponding outputs.

Uploaded by

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

C Programs

The document contains three C programs: one for solving quadratic equations, another for generating the Fibonacci series, and a third for calculating the square root of a number using a goto statement. Each program includes input prompts and outputs results based on user-provided values. The examples demonstrate the functionality of each program with sample inputs and corresponding outputs.

Uploaded by

Sulochana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Quadratic Equation

#include <math.h>
#include <stdio.h>
void main()
{

double a, b, c, discriminant, root1, root2, realPart, imagPart;


printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;

if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}

else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}

else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart,
imagPart);
}

getch();
}

Input

Enter coefficients a, b and c: 2.3

5.6

Output:

root1 = -0.87+1.30i and root2 = -0.87-1.30i


Fibonacci series

#include<stdio.h>
#include<conio.h>
void main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);
for(i=2;i<number;++i)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
getch();
}

Input:
Enter the number of elements:6
Output:
0
1
1
2
3
5

Square root of a number using goto statement

#include <stdio.h>
#include <math.h>
void main() {
double number, squareRoot;
input:
printf("Enter a positive number: ");
scanf("%lf", &number);
if (number < 0) {
printf("Invalid input. Please enter a positive number.\n");
goto input; }
squareRoot = sqrt(number);
printf("Square root of %.2f is %.2f\n", number, squareRoot);
getch();
}

Input

Enter a positive number

Output

Square root of 6 is 2.45

You might also like