Sunday, February 12, 2017

The For Loop # 1

This blog is dedicated to all those who find the "FOR Loop" to be very difficult. This blog will contain a detailed explanation about for loop with ample of examples for your better understanding.
Here we begin.


The word loop all about something that repeats itself so is the FOR loop. If you wish to print your name thousand times you have to write printf("SRK"); a thousand times or you need to print number from 1 to 100, you gonna need to write printf("1"); printf("2"),....printf("100");. Isn't this method nasty and long.

But here FOR loop comes to your rescue. But before we begin I must tell you the syntax("The method to write the FOR loop")

Here's the syntax:

for( initialization ; testing ; updation )  
{
Body of loop/statements
}

here Initialization means to initialize a variable e.g. int num = 1; Or char x = 'a' etc.
In case of testing, you test the expression. how many times you want to execute the loop.
e.g. num<10
the for loop will execute until the number reaches 9 and when the num reaches 10, The for loop stops because num is not less than 10.
But if I say num<=10
the for loop will execute until the num becomes 10.

In the Updation part we may update the variable by one or by two even by three or whatever.
ex:
++num;
Or
num++;
Or
num = num+1;
Or 
num = num*2;
etc.


Now since we know the syntax. We may dive in the For loop.

ex #1

Print numbers from 1 to 100.


for(int num = 1; num<=100;num=num+1)
printf("%d ",num);

Or
for(int num = 1;num<101;num=num+1)    
printf("%d ",num);

Note the difference in the above two code. In the first expression the test expression is n<=100. It means we will continue to run the for loop until the num reaches 100. But in the second expression the test expression is num<101. Here the for loop will execute until the num reaches 100. when the num reaches 101 the execution of for loop will stop because the num 101 is not less than 101.

ex #2

Print the table of any number


int num = 7;
for(int i = 1;i<11;i++)
printf("%d ",num*i);

Here we multiply the number by i. when i is 1 we get 7 x 1, when i is 2 we get 7 x 2, so on and so forth. 

ex #3

Print characters from a to z.


for(char c = 'a'; c <= 'z' ; c++)
printf("%c ", c);

Here we begin by initialing the character 'a' in the CHARACTER VARIABLE c and we go on increasing the c by one.


ex # 4

Print factorial of a number.


Note : 5 ! = 5*4*3*2*1 = 120.

int num = 5; /* Say we are going to find factorial of 5   */
int fact = 1;  /* The variable fact will store the factorial */
for(int x = 1;x<=num; x=x+1) 
fact = fact * x;

Note: If I would have intialized fact by 0. Then the answer would be zero. Take care to initialize fact by 1.

ex # 5

Find the sum of numbers from 1 to 50.


int sum = 0;
for(int j = 1;j<51;j++)
sum = sum + j;
printf("sum = %d ",sum);

Here we drive the for loop from 1 to 50 and add the number to sum and than update the sum expression.

ex # 6

Find the sum of squares of number from 1 to 10.
i.e. 1*1 + 2*2 + 3*3 + 4*4 .....10*10.


int sum = 0;
for(int k = 1;k<=10;k++)
sum = sum + ( k * k);    
printf("sum = %d ",sum);

Here we drive the for loop from 1 to 50 and  add the number square  to sum and than update the sum expression.

ex # 7

Find the sum of the given expression
1*1 + 3*3 + 5*5 +7*7 .....51*51.


int sum = 0;
for(int i = 1;i<=51;i = i +2 )
sum = sum + (i*i);
printf(" sum = %d ",sum);

Here you must note that we update i by 2 and not by 1 as usual.

ex # 8

Print only even numbers between 1 and 25.


for(int i = 1;i<=25;i++)
{
if(i%2==0)         /* check if i is divisible by two. */
printf("%d ", i);
}

Or

for(int i = 2;i<25;i=i+2)   /* Here i update i by two */
printf("%d ", i);

ex # 9

Check if the given number is prime or not.


/* Lets say the given number is 13 */
/* Logic here is we will test by dividing the number 13 from 2 to 12 i.e. (13-1) If the number is divisible we will say the number is not prime */

int number = 13;
int flag = 0; /* Say we will show flag is equal to zero if the number 13 is not divisible by any number between 2 to 12 */ 
/* If the number is divisible we will turn flag equal to 1 */

for(int i = 2;i<number;i++)
{
if(number % i == 0)    /* If the number 13 is divisible by any number between 1 to 12  */
{
flag = 1;                       /* WE will turn flag is equal to 0 and break the execution of the for loop */
break;
}
}
if(flag ==  0)
printf("the number is prime ");
else
printf("the number is not prime ");



Questions you may try:
#1 print all prime numbers between 1 to 100
#2 print the Fibonacci series i.e. 0 1 1 2 3 5 8 .... (sum of next num is equal to sum of previous 2 num)
#3 print the Tribonacci series i.e. 0 1 1 2 4 7 13 .... (sum of next num is equal to sum of previous three numbers )
#4 print the factorials of all numbers between 1 to 10.

Next blog will contain solutions to these questions. Hope that this blog helped you understand the basics of the for loop. In the upcoming blog I will cover for loop inside the for loop.


No comments:

Post a Comment