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

Week 5-C Lab Program

The document contains three C programs that determine the properties of numbers. The first program checks if a number is odd or even using both if-else and separate if statements. The second program identifies if a number is positive, negative, or zero using nested if statements, and the third program calculates the roots of a quadratic equation given non-zero coefficients, handling different cases for the discriminant.

Uploaded by

yashasgowdap82
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)
1 views

Week 5-C Lab Program

The document contains three C programs that determine the properties of numbers. The first program checks if a number is odd or even using both if-else and separate if statements. The second program identifies if a number is positive, negative, or zero using nested if statements, and the third program calculates the roots of a quadratic equation given non-zero coefficients, handling different cases for the discriminant.

Uploaded by

yashasgowdap82
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/ 4

/*

To determine whether a given number is "Odd" or "Even" and print the message
NUMBER IS EVEN or NUMBER IS ODD
with and without using "else" statement.
*/

//Write a program to determine whether a given number is ‘odd’ or ‘even’ and


print the message NUMBER IS EVEN or NUMBER IS ODD with and without using else
option.

#include <stdio.h>
#include <stdlib.h>

int main()
{
int number;

system("clear");
printf("Enter a Number: "); scanf("%d", &number);

printf("***Using IF...ELSE Statement\n");


if((number%2)==0)
printf("NUMBER IS EVEN\n");
else
printf("NUMBER IS ODD\n");

printf("***Without using IF...ELSE Statement\n");


if((number%2)==0)
printf("NUMBER IS EVEN\n");

if((number%2)!=0) //if(number%2) or if((number%2)==1)


printf("NUMBER IS ODD\n") ;

return 0;
}
/*
To determine whether a given number is "Positive", "Negative" or "ZERO" and
print the message "NUMBER IS POSITIVE”, “NUMBER IS NEGATIVE” or “NUMBER IS
ZERO” using nested "if" statement.
*/

// Write a program to determine whether a given number is Positive / Negative


/ Zero

#include <stdio.h>
#include <stdlib.h>

int main()
{
int number;

system("clear");

printf("Enter a Number: "); scanf("%d", &number);

if(number>=0)
if(number>0)
printf("NUMBER IS POSITIVE\n") ;
else
printf("NUMBER IS ZERO\n");
else
printf("NUMBER IS NEGATIVE\n") ;

return 0;
}
// To compute all the roots of a quadratic equation by accepting the non-zero
coefficients. Print appropriate messages.

/* Design, develop and execute a program to find and output all the roots of a
given quadratic equation,
for non-zero coefficients.*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
int a, b, c, disc;
float root1, root2;
system("clear");

NEXT:
printf("Enter the Non-Zero Co-efiicients: ");
scanf("%d %d %d", &a, &b, &c);

if(a==0||b==0||c==0) //if(a==0) alone enough/sufficient


{
printf("!!!Invalid Input/Co-efiicients...");
goto NEXT;
}

disc = b*b-4*a*c; //disc = pow(b,2) - 4 * a * c

if(disc<0)
{
printf("Roots are Imaginery....\n");
}
else if(disc>0)
{
root1 = (-b + sqrt(disc)) / (2*a) ;
root2 = (-b - sqrt(disc)) / (2*a) ;
printf("Roots are Real and Distinct\n");
printf("Root-1 = %.2f\n", root1);
printf("Root-2 = %.2f\n", root2);
}
else
{
root1 = root2 = -b / (2*a) ;
printf("Roots are Real and Equal/Identical\n");
printf("Root-1 = %.2f\n", root1);
printf("Root-2 = %.2f\n", root2);
}
return 0;
}

You might also like