for Statement (Sample Program)

Sunday, July 29, 2012
Let’s write a program using for loop to find the sum of the squares of the integers from 1 to n. Where n is a positive value entered by the user (i.e. Sum = 12 + 22 + 32 + ……+ n2) The code of the program is given below: In the program we declared three variables i, n and sum. We prompted the user...

Increment Decrement Operators

We have seen that in while, do-while and for loop we write a statement to increase the value of a variable. For example, we used the statements like counter = counter + 1; which adds 1 to the variable counter. This increment statement is so common that it is used almost in every repetition structure...

for Statement (Sample Program)

Saturday, July 28, 2012
Let’s take an example to explain for loop. We want to write a program that prints the table of 2 on the screen. In this program, we declare a variable counter of type int. We use this variable to multiply it by 2 with values 1 to 10. For writing the table of 2, we multiply 2 by 1, 2, 3 .. upto 10 respectively...

for Statement

Let’s see what we do in a loop. In a loop, we initialize variable(s) at first. Then we set a condition for the continuation/termination of the loop. To meet the condition to terminate the loop, we affect the condition in the body of the loop. If there is a variable in the condition, the value of that...

Use of Operators

Sunday, July 1, 2012
Here are sample programs which will further explain the use of operators in programming. Problem Statement: Write a program that takes a four digits integer from user and shows the digits on the screen separately i.e. if user enters 7531, it displays 1,3,5,7 separately. Solution: Let’s first analyze...

do-while Statement

Saturday, June 16, 2012
We have seen that there may be certain situations when the body of while loop does not execute even a single time. This occurs when the condition in while is false. In while loop, the condition is tested first and the statements in the body are executed only when this condition is true. If the condition...

while exercises

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...

While Sample Program

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...

While Flow Chart

The basic structure of while loop in structured flow chart is: At first, we will draw a rectangle and write while in it. Then draw a line to its right and use the decision symbol i.e. diamond diagram. Write the loop condition in the diamond and draw a line down to diamond which represents the flow...

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...

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...

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...

Repetition Structure (Loop)

In our day to day life, most of the things are repeated. Days and nights repeat themselves 30 times a month. Four seasons replace each other every year. We can see similar phenomenon in the practical life. For example, in the payroll system, some procedures are same for all the employees. These are...
 

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