Coding for DUMMIES 06 : Repetition and Loops


Today lets see how to use repetition inside of a c program. first lets see some example of loops .



In this program we are taking the advantage of while statements. That allows us to prevent same code again and again. By using while statements we are able to run the program while the condition is true. In this case the program runs while (x <= 10)  



This is same as the previous program.

This program computes the greatest common divisor of two integer values.


Now lets move into another type of loops, Which are do while loops.


This is an example for a simple do while loop. Do while loop is a until loop. It allows looping until the condition is true. In here the loop body will be executed once before it checks the condition.


This program shows the same while loop program that we did earlier, but as a do while loop.


In this program you can see a for loop. By using for loops it makes us easy to write programs easily and it enhances readability. In a for loop we represent initialization, loop condition as well as the increment of the loop control variable. In here also braces are not mandatory when there is only one statement that executes within the for loop.

Lets edit some for loop programs for more practice. The following programs show some triangular number generating C programs.






In this program we can see a inner for loops. These loops are also behave as for loops. But they are executes within another for loop.
   

For loops allow us to use multiple expressions within the loop declaration.                                               


And it also allow us to omit fields. But in here we cannot omit the condition. But we can omit initialization and the increment.

This program results in 1 to 12 multiplication tables.


In here we can see a break statements. This break statement allow us to come our of the inner for loop when the condition is true.



In here we can see a continue statement . When the continue statement is executed, any statements in the loop that appear after the continue
statement are skipped.


In here you can see a goto statement. This allows a unconditional jump. 

now lets look into some questions : 




#include <stdio.h>
int main()
{
const char STAR='*';
int i,j;
/*shape 1*/
for(i=1;i<7;i++)
{
for(j=1;j<=i;j++)
{
printf("%c ",STAR);
}
printf("\n");
}
printf("\n");
/*shape 2*/
for (i = 1; i < 7; i++) 
{
        printf("* * * * * * *\n");
    }
printf("\n");
/*shape 3*/
for(i=6;i>0;i--)
{
for(j=1;j<=i;j++)
{
printf("%c ",STAR);
}
printf("\n");
}
printf("\n");
/*shape 4*/
for(i=7;i>0;i--)
{
for(j=1;j<7;j++)
{
if(i<=j)
printf("%c ",STAR);
else
printf("  ");
}
printf("\n");
}
printf("\n");
/*shape 5*/
for(i=6;i>0;i--)
{
for(j=1;j<7;j++)
{
if(i>=j)
printf("%c ",STAR);
else
printf("  ");
}
printf("\n");
}
printf("\n");

/*shape 5*/
for(i=1;i<6;i++)
{
for(j=1;j<=i;j++)
{
printf("%i ",j);
}
printf("\n");
}
printf("\n");           
return 0;
}

3. Find the minimum and maximum of sequence of 10 numbers.


4. Find the total and average of a sequence of 10 numbers.




5. Write a program to generate and display a table of n and n2, for integer values of n
ranging from 1 to 10. Be certain to print appropriate column headings.



6. A triangular number can also be generated by the formula
triangularNumber = n (n + 1) / 2

for any integer value of n. For example, the 10th triangular number, 55, can be generated
by substituting 10 as the value for n in the preceding formula. Write a program that
generates a table of triangular numbers using the preceding formula.



7. The factorial of an integer n, written n!, is the product of the consecutive integers 1
through n. For example, 5 factorial is calculated as
5! = 5 x 4 x 3 x 2 x 1 = 120



8. Write a program to generate and print a table of the first 10 factorials.



9. Display the n terms of harmonic series and their sum.
1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms
Test Data :
Input the number of terms : 5
Expected Output :
1/1 + 1/2 + 1/3 + 1/4 + 1/5 +
Sum of Series upto 5 terms : 2.283334




11. Assume a village has 20 houses. Input electricity unit charges and calculate total
electricity bill according to the following criteria:
 For first 50 units Rs. 0.50/unit
 For next 100 units Rs. 0.75/unit
 For next 100 units Rs. 1.20/unit
 For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill
The output should be as follows:

Serial Number    House Address     Units     Surcharge     Amount to be paid
                                                                                                        


Now we hope you have learned about C operators and how to use them in programming well . so until next make sure to practice programming everyday. practice what makes you a good programmer.

contribution : - Sandakelum Kumarasiri @SK2208    5737
                        Ranishka Gunathilake    @RG761     5641
                        Mahesh Wijenayaka    @MW1513    5734