C Program to Find Total of Even Integers

C-If Statement with Example

We can see like most other languages; C uses the keyword if to implement the decision control instruction. The general form of if statement looks like below example.

if (this condition is true)
execute this statement;

The keyword C if statement tells the compiler that what follows is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is, is true, then the statement is executed. If the condition is not true then the statement is not executed; instead the program skips past it.

C If Statement

As a general rule, we express condition using C’s ‘relational’ operators. The relational operators allow us to compare two values to see whether they are equal to each other, unequal, or whether one is greater than the other. Here’s how they look and how they are evaluated in C.

Here is simple program, which demonstrates the use of if and the relational operators.


/* Demonstration of if statement */
main( )
{
int num ;
printf ( "Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( "What an person you are !" ) ;
}

On execution of this program, if you type a number less than or equal to 10, you get a message on the screen through printf( ). If you type some other number, the program doesn’t do anything. The following would help you understand the flow of control in the program.

Example, 

/* Calculation of total expenses */

main( )
{
int qty, dis = 0 ;
float rate, tot ;
printf ( "Enter quantity and rate " ) ;
scanf ( "%d %f", &qty, &rate) ;
if ( qty > 1000 )
dis = 10 ;
tot = ( qty * rate ) - ( qty * rate * dis / 100 ) ;
printf ( "Total expenses = Rs. %f", tot ) ;
}

Here is some sample interaction with the program.

Enter quantity and rate 1200 15.50
Total expenses = Rs. 16740.000000
Enter quantity and rate 200 15.50
Total expenses = Rs. 3100.000000

In the first run of the program, the condition evaluates to true, as 1200 (value of qty) is greater than 1000. Therefore, the variable dis, which was earlier set to 0, now gets new value 10. Using this new value total expenses are calculated and printed.

In the second run the condition evaluates to false, as 200 (the value of qty) isn’t greater than 1000. Thus, dis, which is earlier set to 0, remains 0, and hence the expression after the minus sign evaluates to zero, thereby offering no discount.

Online Training Tutorials

  • c++ interview questionsC++ Interview Questions and AnswersWe have listed of C++ Interview Questions and Answers that have been designed for C++ professionals who are preparing interviews on C++ Programming. C++ is not a purely object-oriented […]
  • sap interview questionsSAP Interview Questions and Answers – Technosapwe have listed some of most common SAP Interview questions and Answers are listed below, along with insights from several recruitment professionals about how to answer. As part of your SAP […]
  • SAP SD Pricing Procedure – OverviewSAP SD Pricing Procedure is determined (T.Code: OVKK) based on Sales Area (Sales Organization + Distribution Centre + Division) + Customer Pricing Procedure + Document Pricing Procedure. […]
  • SAP GUI 7.10SAP GUI 7.10 New Installation InstructionsPreviously I’m using SAP GUI 6.40 on my vista operating system. At the beginning, SAP GUI is running fine, all the t-codes I access are displayed correctly. But in time, maybe after 2 days […]
  • Data ArchivingIntroduction to Data ArchivingSAP Data Archiving : As the usage of SAP database increases it’ll results in huge amounts of enterprise data, which is stored in SAP R/3. The new updated data is entered into the system […]
  • Returnable Transport Packaging (RTP) in SAP MMWhat is Returnable Transport Packaging (RTP) in SAP MM?Returnable transport packaging (RTP) is a multi-trip packaging medium (for example, pallets or containers) in which goods can be transported more than once between vendors and […]
  • SAP Password for DatabaseChanging SAP Password for Database with BRCONNECTChanging SAP Password for Database To Changing SAP Password for Database Users Using BRCONNECT As of Release 6.10, you can use BRCONNECT to change the passwords for the database […]
  • Pricing ConditionsHow to Create Control Pricing Conditions Based on Order Type?You create an order type ZP00 for QT - Quotation and does not wish it to be used in OR - Standard Order. Pricing Conditions Follows this steps :- IMG - Sales and Distribution -> […]