Monday, 26 October 2015

oopm programs

oopm programs
1. https://drive.google.com/file/d/0BwgAqa0RXNFUYXQtVkZsVWRtVkk/view?usp=sharing
2. https://drive.google.com/file/d/0BwgAqa0RXNFUOUNhSXNBRC1uNlk/view?usp=sharing

Friday, 10 April 2015

Difference between switch case and else if ladder:

Difference between switch case and else if ladder:

So, after going through the two control statements in brief, here are the main difference between switch case and else if ladder:
  1. In else if ladder, the control goes through the every else if statement until it finds true value of the statement or it comes to the end of the else if ladder. In case of switch case, as per the value of the switch, the control jumps to the corresponding case.
  2. The switch case is more compact than lot of nested else if. So, switch is considered to be more readable.
  3. The use of break statement in switch is essential but there is no need of use of break in else if ladder.
  4. The variable data type that can be used in expression of switch is integer only where as in else if ladder accepts integer type as well as character.
  5. Another difference between switch case and else if ladder is that the switch statement is considered to be less flexible than the else if ladder, because it allows only testing of a single expression against a list of discrete values.
  6. Since the compiler is capable of optimizing the switch statement, they are generally considered to be more efficient. Each case in switch statement is independent of the previous one. In case of else if ladder, the code needs to be processed in the order determined by the programmer.
  7. Switch case statement work on the basis of equality operator whereas else if ladder works on the basis of true false( zero/non-zero) basis.

Monday, 16 March 2015

Break Statement in C

1. For Loop

#include<stdio.h>
int main()
{
    int i;
    for(i=0;i<10;i++)
    {
        if(i%2!=0)
            break;
        printf("%d ",i);
    }
    return 0;
}
Output:
0

2.While Loop
#include<stdio.h>
int main()
{
    int i;
    i=0;
    while(i++<10)
    {
        if(i%2==0)
            break;
        printf("%d ",i);
    }
    return 0;

}
Output:
1

3. Do-While Loop
#include<stdio.h>
int main()
{
    int i;
    i=0;
    do
    {
        if(i%2!=0)
            break;
        printf("%d ",i);
    }while(i++<10);
    return 0;

}
Output:
0

4. Nested Loop
#include<stdio.h>
int main()
#include<stdio.h>
int main()
{
    int i,j;
    for(j=0;j<3;j++)   // outer loop
    {
        for(i=0;i<10;i++)  //inner loop
        {
            if(i%2!=0)
                break;
            printf("%d ",i);
        }
    }  
    return 0;

}
Output:
0 0 0 

5. Continue the outer loop:
#include<stdio.h>
int main()
{
    int i,j;
    Outer: for(j=0;j<3;j++)
    {
        for(i=0;i<10;i++)
        {
            if(i%2!=0)
                break Outer;
            printf("%d ",i);
        }
    }    
    return 0;
}

Use of Continue in C

1. For Loop
#include<stdio.h>
int main()
{
    int i;
    for(i=0;i<10;i++)
    {
        if(i%2==0)
            continue;
        printf("%d ",i);
    }
    return 0;

}
Output:
1 3 5 7 9

2.While Loop
#include<stdio.h>
int main()
{
    int i;
    i=0;
    while(i++<10)
    {
        if(i%2==0)
            continue;
        printf("%d ",i);
    }
    return 0;

}
Output:
1 3 5 7 9

3. Do-While Loop
#include<stdio.h>
int main()
{
    int i;
    i=0;
    do
    {
        if(i%2==0)
            continue;
        printf("%d ",i);
    }while(i++<10);
    return 0;

}
Output:
1 3 5 7 9

4. Nested Loop
#include<stdio.h>
int main()
{
    int i,j;
    for(j=0;j<3;j++)   // outer loop
    {
        for(i=0;i<10;i++)  //inner loop
        {
            if(i%2==0)
                continue;
            printf("%d ",i);
        }
    }    
    return 0;

}

Output:
1 3 5 7 9 1 3 5 7 9 1 3 5 7 9

5. Continue the outer loop:
#include<stdio.h>
int main()
{
    int i,j;
    Outer: for(j=0;j<3;j++)
    {
        for(i=0;i<10;i++)
        {
            if(i%2==0)
                continue Outer;
            printf("%d ",i);
        }
    }    
    return 0;
}


Note :
If increment is written at last that line never gets executed 
Sample: 
#include<stdio.h>
int main()
{
    int i;
    i=0;
    do
    {
        if(i%2==0)
            continue;
        printf("%d ",i);
        i++;
    }while(i<10);
    return 0;

}

It will never terminate the loop as increment will never happen.

Program for prime numbers in C

 #include<stdio.h>
 void main()
 {
     int val,i,n1,n2;
     clrscr();
     printf("Enter the range:");
     scanf("%d%d",&n1,&n2);
     printf("\nPrime Numbers between %d and %d are:",n1,n2);
     for(val=n1;val<=n2;val++)
     {
for(i=2;i<val;i++)
{
if(val%i==0)
{
      break;
}
}
if(i==val)
printf("\n%d",val);
     }
     getch();
 }

 /*
 Output:
Enter the range:99
150

Prime Numbers between 99 and 150 are:
101
103
107
109
113
127
131
137
139
149
*/
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               

Program to find LCM and GCD of two numbers in C

 #include<stdido.h>
 void main()
 {
     int i,a,b,min,max,lcm,hcf;
     clrscr();
     printf("Enter the two numbers:");
     scanf("%d%d",&a,&b);
     //find out smallest and largest and assign them to min and max variable respectively
     a<b?(min=a,max=b):(min=b,max=a);
     printf("%d%d",min,max);
     for(i=min;i>=1;i--)
     {
if(max%i==0&&min%i==0)
{
hcf=i;
break;
}
     }
     lcm=(a*b)/hcf;
     printf("\nThe LCM=%d\nThe GCD=%d",lcm,hcf);
     getch();
 }

/*
Output:
Enter the two numbers:90
75                                                                            
7590                                                                          
The LCM=450                                                                  
The GCD=15                                                                    

*/
                                                                             
      

Thursday, 12 March 2015

Difference between while, do-while and for with example

1. while and for are entry control loop whereas do-while is an exit control loop

2. the program illustrates the working. The program is to calculate the sum of n nos. using while, do-while and for.

#include<stdio.h>
 int main()
 {
     int i,j=0,n=3;
     for (i=1;i<=n;i++)
     {
        j+=i;
     }
     printf("\nFor sum= %d",j);
     i=1;
     j=0;
     while(i<=n)
     {
        j+=i++;
     }
     printf("\nWhile sum= %d",j);
     i=1;
     j=0;
     do
     {
        j+=i++;
     }while(i<=n);
     printf("\nDo-While sum= %d",j);
     return(0);
 }


3.
a)Output of the above program when n=3:
For sum= 6
While sum= 6
Do-While sum= 6

b)Output of the above program when n=0:
For sum= 0
While sum= 0
Do-While sum= 1

observing the output we can find that for and while do not run n=0 but do-while did run.