Thursday 18 May 2017

Elements of programming-8




Do while loop:

 In while looping condition is checked at the entry of the loop.if the condition is false the loop will terminate even before the first iteration of loop. In while loop the condition is checked at the end of the loop. So there is a guarantee the loop will run at least one time Even if the condition is false.

Difference Between while and do-while loop.


While loop


Do-while loop

The condition checked at the entry of the loop

The condition is checked at the exit of the loop


Zero time execution is possible

The loop will run atleast ontime on any condition


 Example 

#include <stdio.h>
int main()
{
    int i=1;
 do
    {
        printf("%d\n",i);
        i++;
    }  while(i<=10);

    return 0;
}

Output:

Thankyou
-----Muthu karthikeyan,Madurai.

Thursday 4 May 2017

Elements of programming-7




Loops :
A block of statements executed repeatedly until a condition is true.this is called loop.
There are three types of loop.
  1. 1.       While loop
  2. 2.       Do—while loop
  3. 3.       For loop
While loop
A  variable is initialized first then it is  compared with a value. Until that condition is true the statements inside the loop  will be executed repeatedly. The counter variable will be incremented or decremented  inside the loop.
Example-1
#include <stdio.h>
int main()
{
    int i=1;
    while(i<=10)
    {
        printf("%d\n",i);
        i++;
    }
    return 0;
}
Output:

Example-2:

#include <stdio.h>


int main()
{
    int i=1,sum=0;
    while(i<=10)
    {
        sum=sum+i;
        i++;
    }
    printf("sum from 1+2--+3=%d",sum);
    return 0;
}
Output:


--------------will continue,
Thanking you
------Muthu karthikeyan,Madurai

Thursday 2 March 2017

Elements of programming-6




If-else if ladder.

Checking more than one else if condition is known as if-else if ladder. First a condition is checked if it is true then a block of statements are executed. If it is false an another condition is checked with the help else if construct. If it also false then another condition is checked. Checking more than one else if construct is known as else if ladder.


Example:

#include <stdio.h>

int main()
{
    int dayno;
    printf("enter day no:");
    scanf("%d",&dayno);
    if(dayno==1)
    {
        printf("sunday");
    }
   else  if(dayno==2)
    {
        printf("monday");
    }
   else if(dayno==3)
    {
        printf("tuesday");
    }
   else if(dayno==4)
    {
        printf("wednesday");
    }
    else if(dayno==5)
    {
        printf("thursday");
    }
   else if(dayno==6)
    {
        printf("friday");
    }
   else if(dayno==7)
    {
        printf("saturday");
    }
    else
    {
        printf("invalid day number");
    }


    return 0;
}

                                       In the above program day number is get as input . the block statements which will be executed is depending upon the variable dayno we entered. If all the condition is false then  else part will be executed.

                                       Thanking you,
                                                                    Muthu karthikeyan,Madurai.
contact:919629329142
email:muthu.vaelai@gmail.com

Wednesday 1 March 2017

Elements of programming -5.




Nested if statement.

First a condition is checked if it is true then another condition checked. If it is true another condition is checked. If it also true then a block of statement is executed. If the first condition is true, then else part will be executed. we can also check another condition in else part.

Example.

We are going to find big number among three numbers. The program for it.
#include <stdio.h> 
int main()
{
     int a,b,c;
     printf("enter a:");
     scanf("%d",&a);
     printf("enter b");
     scanf("%d",&b);
     printf("enter c");
     scanf("%d",&c);
     if(a>b)
     {
         if(a>c)
         {
             printf("a is big");

         }
         else
         {
             printf("c is big");

         }
     }
     else
     {
         if(b>c)
         {
             printf("b is big");
         }
         else
         {
             printf("c is big");
         }
     }
    return 0;
}
In the above program has been written in c language. It uses the method of nested if statement.  First a is greater than b is executed if it is true then it check for the second condition whether the second condition is true or not false. If it also true a is declared as big number. If the second condition is false then c is declared as big number .if the first condition is false then it comes to else part.  It also checks another condition. If it is true then b is declared as big number. If it is false then c is declared as big number.
                    Will continue
                                                                                Muthu karthikeyan,Madurai.
                                                     contact:919629329142
                                                          Email:muthu.vaelai@gmail.com.