Showing posts with label While Sample Program. Show all posts
Showing posts with label While Sample Program. Show all posts

While Sample Program

Saturday, June 16, 2012
Problem statement:
Calculate the factorial of a given number.

Solution:
The factorial of a number N is defined as:


By looking at the problem, we can see that there is a repetition of multiplication of numbers. A loop is needed to write a program to solve a factorial of a number. Let's think in terms of writing a generic program to calculate the factorial so that we can get the factorial of any number. We have to multiply the number with the next decremented number until the number becomes 1. So the value of number will decrease by 1 in each repetition.
Here is the flow chart for the factorial.



Here is the code of the program.



The output of the program is as follows:


Exercise
1) Calculate the sum of odd integers for a given upper limit. Also draw flow chart of the program.
2) Calculate the sum of even and odd integers separately for a given upper limit using only one loop structure. Also draw flow chart of the program.

tips:
  • Always use the self explanatory variable names
  • Practice a lot. Practice makes a man perfect
  • While loop may execute zero or more time
  • Make sure that loop test (condition) has an adequate exit.

For previous lesson click here: While Flow Chart
For next lesson click here: while exercises


the easiest way to learn programming
introduction to programming
While Sample Program

sum of even numbers with while

Problem statement:
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

While Sample Program

To calculate the sum of 2000 integers, we will change the program (i.e. the while condition) in the editor and compile it and run it again. If we need to calculate the sum of first 5000 integers, we will change the program again in the editor and compile and run it again. We are doing this work again in a loop. Change the program in the editor, compile, execute it, again change the program, compile and execute it and so on. Are we doing this in a loop? We can make our program more intelligent so that we don’t need to change the condition every time. We can modify the condition as:


where upperLimit is a variable of data type int. When the value of upperLimit is 1000, the program will calculate the sum of first 1000 integers. When the value of upperLimit is 5000, the program will calculate the sum of first 5000 integers. Now we can make it re-usable and more effective by requesting the user to enter the value for upper limit:


We don’t have to change our program every time when the limit changes. For the sum of integers, this program has become generic. We can calculate the sum of any number of integers without changing the program. To make the display statement more understandable, we can change our cout statement as:



Try to write the program.


For previous lesson click here: Repetition Structure (Loop)
For next lesson click here: sum of even numbers with while


the easiest way to learn programming
introduction to programming
While Sample Program

 

introduction to programming Copyright © 2011-2012 | Powered by Blogger