Calculate the sum of even numbers for a given upper limit of integers.
Solution:
We analyze the problem and know that while statement will be used. We need to sum even numbers only. How can we decide that a number is even or not? We know that the number that is divisible by 2 is an even number. How can we do this in C language? We can say that if a number is divisible by 2, it means its remainder is zero, when divided by 2. To get a remainder we can use C’s modulus operator i.e. %. We can say that for a number if the expression (number % 2) results in zero, the number is even. Putting this in a conditional statement:
The above conditional statement becomes true, when the number is even and false when the number is odd (A number is either even or odd).
The complete code of the program is as follows:
The output of the program is:
Suppose if we don’t have modulus operator in the C language. Is there any other way to find out the even numbers? We know that in C integer division gives the integer result and the decimal portion is truncated. So the expression (2 * (number / 2)) gives the number as a result, if the number is even only. So we can change our condition in if statement as:
So far, we have been drawing flow charts after coding the program but actually we have to draw the flow chart first and then start coding.
Properties of while loop:
In the above example, if the user enters 0, as the value for upper limit. In the while condition we test (number <= upperLimit) i.e. number is less than or equal to upperLimit ( 0 ), this test return false. The control of the program will go to the next statement after the while block. The statements in while structure will not be executed even for a single time. So the property of while loop is that it may execute zero or more time. The while loop is terminated, when the condition is tested as false. Make sure that the loop test has an adequate exit. Always use braces for the loop structure. If you forget to put the braces, only one statement after the while statement is considered in the while block. Infinite Loop: Consider the condition in the while structure that is (number <= upperLimit) and in the while block the value of number is changing (number = number + 1) to ensure that the condition is tested again next time. If it is true, the while block is executed and so on. So in the while block statements, the variable used in condition must change its value so that we have some definite number of repetitions. What will happen if we do not write the statement number = number + 1; in our program? The value of number will not change, so the condition in the while loop will be true always and the loop will be executed forever. Such loops in which the condition is always true are known as infinite loops as there are infinite repetitions in it. For previous lesson click here: While Sample Program
For next lesson click here: Properties of while loop
the easiest way to learn programming
introduction to programming
sum of even numbers with while
1 comments:
please tell me about multidemanstion array
Post a Comment