if else statement, sample programs, program to try
If–Else Statement
It is a decision-making statement in the C programming language which allows the program to test for one or more conditions (Boolean value) and execute a set of statements depending on the condition.
-
The statements in the if block are executed if the condition evaluates to true.
-
If the condition is false, the statements in the else block are executed.
Syntax:
Note
-
In C, zero and null values are treated as false.
-
Any non-zero or non-null value is treated as true.
-
The else part is optional.
Example
If…Else If Statement
An if statement can be followed by an optional else if…else statement, which is very useful for testing multiple conditions using a single decision structure.
Important Points
-
An if can have zero or one else, and it must come after any else if blocks.
-
An if can have zero to many else if blocks, and they must come before the else.
-
Once an else if condition succeeds, none of the remaining else if or else blocks will be tested.
Syntax
Note
Multiple conditions can also be tested using logical operators:
-
AND (&&)
-
OR (||)
-
NOT (!)
Example
Nested If–Else Statement
It is always legal in C programming to nest if–else statements, which means you can use one if or else if statement inside another if or else if statement.
Syntax
The syntax for a nested if statement is as follows:
Example
Summary
-
Decision-making or branching statements are used to select one path based on the result of an evaluated expression.
-
They are also called control statements because they control the flow of execution of a program.
-
C provides if and if-else constructs for decision-making statements.
-
We can nest if-else within one another when multiple paths must be tested.
-
The else-if ladder is used when we need to check several conditions based on the result of an expression.
Sample Programs
Check whether the given number is even or odd.
#include <stdio.h>
main()
{
long int n;
system("clear");
printf("Enter the number\n");
scanf("%ld",&n);
if(n%2==0)
{
printf("the number is even\n");
}
else
{
printf("the number is odd\n");
}
}
Check whether the given character is vowel or not
#include <stdio.h>
main()
{
char c;
system("clear");
printf("Enter a character\n");
scanf("%c",&c);
if(c=='a' || c=='e' || c=='i' || c=='o'||c=='u')
{
printf("vowel character\n");
}
else
{
printf("not vowel\n");
}
}
Read length of 3 sides and check whether it forms a triangle.
#include <stdio.h>
main()
{
int a,b,c;
system("clear");
printf("Enter three sides \n");
scanf("%d%d%d",&a,&b,&c);
if(a+b>c && b+c >a && a+c >b)
{
printf("the sides will form a triangle\n");
}
else
{
printf("cannot form a triangle\n");
}
}
check the quadrant of given point
#include <stdio.h>
main()
{
int x,y;
system("clear");
printf("Enter the point\n");
scanf("%d%d",&x,&y);
if(x>0 && y>0)
{
printf("first qudrant\n");
}
else if ( x<0 && y>0 )
{
printf("second qudrant\n");
}
else if (x<0 && y<0 )
{
printf("third qudrant\n");
}
else if ( x>0 && y <0 )
{
printf("fourth quadrant\n");
}
else
{
printf("the point is at origin\n");
}
}
biggest of 3 numbers
#include <stdio.h>
main()
{
int a,b,c;
system("clear");
printf("Enter three numbers\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
{ printf("biggest=%d\n",a);}
else if(b >a && b >c)
{printf("biggest=%d\n",b);}
else
{printf("biggest=%d\n",c);}
}
ax^2 + bx + c = 0, where
a, b and c are real numbers and
a != 0
The term b^2-4ac is known as the discriminant of a quadratic equation. It tells the nature of the roots.
If the discriminant is greater than 0, the roots are real and different.
root1=(-b+sqrt(discriminant))/(2*a) root2=(-b-sqrt(discriminant))/(2*a)
If the discriminant is equal to 0, the roots are real and equal.
root1=-b/(2*a) root2=-b/(2*a)
If the discriminant is less than 0, the roots are complex and different.
realpart=-b/(2*a) imagpart=sqrt(-discriment)/(2*a)
root1=realpart+imagpart i root2= realpart+ imagpart i
*/
#include <math.h>
#include <stdio.h>
int 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;
// condition for real and different roots
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf\n", root1, root2);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;\n", root1);
}
// if roots are not real
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi\n", realPart,
}
return 0;
}
1)Compare two numbers( a==b , a>b or b>a)
2)Check whether the given number is even or odd.
3)Find the biggest of 3 numbers(use if else if)
4)Read internal marks( out of 50) and external mark( out of 100) and print passed /failed in internal or external or failed in both.( use KTU criteria..45% for pass)
5)Check whether the given number is zero, positive or negative.
6)Find the roots of a quadratic equation ( ax^2+bx+c)Test the program using the following sets of data:
8)Check the type of a triangle after reading the three sides ( scalene, isosceles, equilateral)
9) Check the type of a triangle after reading the three vertices ( scalene, isosceles, equilateral)
Note:
From the vertices length of the sides can be computed using the distance formulae.
A scalene triangle is a triangle that has three unequal sides.
An isosceles has two equal sides and two equal angles.
An equilateral triangle is a triangle with all three sides of equal length and also has three equal 60 degree angles.
10) Read a mark ( out of 100) and print the corresponding grade.
Percentage Range Grade
>=90 O
>=85 and <90 A+
>= 80 and <85 A
> =70 and <80 B+
> =60 and <70 B
> =50 and <60 C
>=45 and <50 P
<45 F
1) Year is multiple of 400
2) Year is multiple of 4 and not multiple of 100.
13)Write a program to check the quadrant of a given point(x,y).
14)Write a program to get the absolute value of number without using the abs() function
15)Write a program that accepts the length of three sides of a triangle as input and determine whether or not the triangle is a right triangle
16)Given three points (x1,y1 ) ,(x2,y2) and (x3,y3), check whether they form a triangle.

Comments
Post a Comment