Thursday, July 05, 2012

The else if statement

Previously we have learnt about nested if else statement, the else if statement works some what like the nested if else statement. But it avoids the complication of the nested if else statement. In case of nested conditional operator, the programmer may indulge in the complication of the program. But a good programmer should always look forward to write a program a very easy and short way as possible. Thus else if statement is one such way to make a program simpler and easier for a developer to develop a program.
Let us understand, the utility of else if statement with an example.

Thursday, January 26, 2012

Program with Logical Operator

Exercise 16

The marks of five subjects of a particular student is inputted through the keyboard by the user. The division, which the student received will be prompted by the system as per following rules:

Percentage greater than or equal to 60 – First Division

Percentage less than 60 and greater than equal to 50 – Second Division

Percentage less than 50 and greater than equal to 40 – Third Division

Percentage less than 40 – Fail

Solution:
/* Program to find the Division of Marks obtained */
#include<stdio.h>
#include<conio.h>

Wednesday, January 25, 2012

Logical Operators

In C language, there are 3 types of logical operators which are used.

  1. && – Used for AND operation.
  2. || – Used for OR operation.
  3. ! – Used for NOT operation.
When we want to apply two conditions to occur simultaneously in any condition based structure, we need to use AND operator. In  C language, AND operator is represented by && sign.

Sunday, January 15, 2012

Program with Nested if-else statement

Exercise 15

Program to create report card of a student. The user need to enter the marks of English, Science, Maths, Social Studies of an examination out of 100. The program will find the average marks, if the marks is below 35, the system will prompt “Sorry, the student has failed”. If the marks is more than 20 and less than 35, the system will prompt “The student needs little improvement” and if the marks is below 20, then the system will prompt “The student needs to work hard”. If the marks is more than and equal to 35, the system will prompt “The student has passed”. If the marks is greater than or equal to 35 and less than 60 then the system will prompt “Well Done” and if the marks is greater than or equal to 60, then the system will prompt “Excellent”.

Solution:
/* Program to create Report Card of a student */
#include<stdio.h>
#include<conio.h>

Friday, January 06, 2012

Nested if-else statement

The word nested itself signifies one within another. Nested if-else statement also signifies the same, an entire if-else structure can be written the body of the if statement or the else statement or within both individually.

Syntax of nested if-else statement is as follows:

Thursday, January 05, 2012

Program with if-else Statement

Exercise 14

Write a program to find the gross salary of individual employee. The conditions to find the gross salary are as follows:

If the basic salary is less than $1500, then HRA is 10% of basic and DA is 90% of basic.

And if the basic salary is more than or equal to $1500, then HRA is $500 and DA is 98% of basic.

The basic salary is inputted through the keyboard by the user.

Solution:
/* Program to find out the gross salary */
#include<stdio.h>
#include<conio.h>

Wednesday, January 04, 2012

if-else Statement

Till now we have learnt, the statement(s) is/are executed when the condition with in if statement is true, but we can also execute statement(s) when the condition is false. To execute the statement(s), when the condition becomes false, we need to use else statement along with if statement.

Syntax for if-else statement:

Monday, January 02, 2012

Program with multiple statements within if

Exercise 13
Program to find out the bonus amount of the employees. If the service duration of a particular employee is more than or equal to 3 years then the bonus amount is $2500.00 else the bonus amount is $1000.00.

Solution:

/* Program to calculate the bonus amount of the employee */
#include<stdio.h>
#include<conio.h>

Sunday, January 01, 2012

Multiple statements within if

In some cases, we need to execute multiple instructions or statements on satisfying any condition. In such a condition, we need to place the statements within a pair of braces within if statement.

Syntax:
If (Condition)
{
Statement 1;
Statement 2;

Thursday, April 30, 2009

Program with if statement

Exercise 12
A person purchase certain product from a shop, the shop offers a discount of 10%, if the customer purchases the product of more than $25. If the quantities and price per items are input through the keyboard, write a program to calculate the total expenses.


Solution:



/* Program to calculate the total expense */
#include<stdio.h>
#include<conio.h>