# if else in c, some important programming exercises

If and If else in C is a selection statement that used to select statements depending on the value of a controlling expression.

#### Syntax:

if ( expression )
statement;

In the above scenario, the statement will only be executed if the expression is non-zero.

if ( expression )
statement1 ;
else
statement2;

In the above scenario, the statement1 will only be executed if the expression is non-zero. if the expression is zero, then statement2 will be executed.

### C program to find out whether a given year is a leap year or not:

##### Example code,

Output:

Enter the year:2016

Leap Year.

### C program to convert temperature from Fahrenheit to Celsius and Celsius to Fahrenheit:

##### The basic formula to convert Fahrenheit and Celsius to each other.

Celsius to Fahrenheit: (°C × 9/5) + 32 = °F
Fahrenheit to Celsius: (°F − 32) x 5/9 = °C

### C program to check sign of a given number:

The MSB bit of a number defines their sign. If the MSB bit is set, the number will be negative.

### Check whether a given number is even or odd:

If a number is divided by 2, it means it is an even number. We can also check number is even or odd to check LSB bit. If LSB bit of a number is set that means it is an odd number otherwise even.

### C program to check whether triangle is equilateral, scalene or isosceles:

Before writing the program, we should know the properties of isosceles, equilateral and scalene triangles.

Isosceles triangle: In geometry, an isosceles triangle is a triangle that has two sides of equal length.

Equilateral triangle: In geometry, an equilateral triangle is a triangle in which all three sides are equal.

Scalene triangle: A scalene triangle is a triangle that has three unequal sides.

### C program to check whether a character is an alphabet or not:

An input character is an alphabet if it lies in between a-z or A-Z.

Using the ASCII value, we can also check the alphabet character.

### C program to check vowel or consonant:

There are 5 vowels in English alphabets, these are a, e, i, o, u. These vowels can be in uppercase or lowercase.

### C program to check whether the triangle is valid or not if angles are given:

The angle property of the triangle says that the sum of all three angles should be equal to 180.

### C program to check whether the triangle is valid or not if sides are given:

A triangle is valid if the sum of its two sides is greater than the third side.

Let’s say that a, b, c is the sides of the triangle. So it must satisfy the below criteria :

1. a + b > c
2. a + c > b
3. b + c > a

### C program to check whether a number is a prime number or not:

A prime number is a positive natural number, whose value greater than 1 and has only two factors 1 and the number itself. In another word prime number only divided by itself and 1. Any positive natural number who is not a prime number is called a composite number.

For example,
2,3,5,7,11..
In above example 2 is the (smallest) prime number because it has only two factor 1 and 2.

Note: 1 is not a prime or composite number and 2 is the only even prime number.

#### An algorithm to check prime number using the division method

START

1 → Take the number n

2 → Divide the number n with (2, n-1) or (2, n/2) or (2, sqrt(n)).

3 → if the number n is divisible by any number between (2, n-1) or (2, n/2) or (2, sqrt(n)) then it is not prime.

4 → If it is not divisible by any number between (2, n-1) or (2, n/2) or (2, sqrt(n)) then it is a prime number

STOP

### C program to enter student marks and find percentage and grade:

A college has the following rules for grading system:

1. Below 25 – F
2. 25 to 45 – E
3. 45 to 50 – D
4. 50 to 60 – C
5. 60 to 80 – B
6. Above 80 – A

Ask the user to enter mark of 5 subjects and print the corresponding grade.