Showing posts with label C Program. Show all posts
Showing posts with label C Program. Show all posts

Friday, 21 October 2011

continue statement

when we write program, if we want to take control to the beginning of the loop, bypassing the statements inside the loop, which have not yet been executed, in this situation we uses continue. continue is c reserved keyword in C. When continue is encountered inside any loop, control automatically passes to the beginning to the loop.
A continue is usually associated with an if.
Let's understand continue with example:

/*demonstration of continue statement*/
 #include<stdio.h>
 #include<conio.h>
 int main()
 {
  int i,j;
  for(i=1; i<=2; i++)
  {
   for(j=1; j<=2; j++)
   {
    if(i==j)
     continue;
    printf("\n%d %d",i,j);
   }
  }
  getch();
  return 0;
 }

Output:
 1 2
 2 1

Explanation: When the value of i equals that of j, the continue statement takes the control to the for loop(inner) bypassing the rest of the statements execution in the for loop(inner).

Let's understand continue statement with another example:

/*demonstration of continue statement*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int i;
 for(i=1; i<=10; i++)
 {
   if(i==6)
     continue;
   printf("%d ",i);
 }
 printf("\nNow we are out of for loop");
 getch();
 return 0;
}


Output:-

 1 2 3 4 5 7 8 9 10
 Now we are out of for loop

Explanation: As when the value of i will becomes 6 it will move for the next iteration by skipping the iteration i=6.

break statement

When we writing programming code, we often come across situations where we want to jump out of a loop instantly, without waiting to get back to the conditional test. The keyword break allows us to do this. When break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually associated with an if.
The keyword break, breaks the control only from the while in which it is placed.
Let's understand break with c program example:

/*demonstration of break statement through c program*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int i=5;
 clrscr();
 while(i>=1)
 {
  if(i==2)
  {
   printf("\nIt is equal to two!!");
   break;
  }
  printf("%d\n",i);
  i--;
 }
 getch();
}

Output:
5
4
3
It is equal to two!!

Thus, it is clear that a break statement takes the execution control out of the loop.
In above program, if we omitted break statement then what will be output? The answer is
5
4
3
It is equal to two!!
2
1


because when i's value is 2, condition will be satisfy and executed printf statement, after that compiler goes to next statement i.e. printf("%d",i); because loop do not terminate and it is run till the i value is not less than 1.
See more example of break statement

Thursday, 20 October 2011

Number seriess


/*program to print number series as
1 4 9 25....100 */
#include<stdio.h>
#include<conio.h>
int main()
{
 int x=1,y=2,z,s;
 for(; x<=8; )
 {
   s=x*x;
   printf(" %d",s);
   z=x+y;
   x=y;
   y=z;
 }
 getch();
 return 0;
}

Output:1 4 9 25 64

/*program of print following number series
-10 -8 -6 -4 -2 0 2 4 6 8 10 */
#include<stdio.h>
#include<conio.h>
int main()
{
 int x;
 for(x=-10; x<=10;x=x+2)
    printf(" %d",x);
 getch();
 return 0;
}
Output:  -10 -8 -6 -4 -2 0 2 4 6 8 10

Wednesday, 19 October 2011

do-while loop

Now we know about for, while loop that executed the statement within them finite number of times. However, in real life programming, one comes across a situation when it is not known beforehand how many times the statement in the loop are to be executed. In this situation we used do-while loop.
do-while tests the condition after having executed the statement within the loop i.e. do-while would executed its statement at least once, even if the condition fails for the first time. For example notice in following program:

/*demonstration of do-while*/
#include<stdio.h>
#include<conio.h>
void main()
{
 clrscr();
 do
 {
  printf("Its work!!");
 }while(10<1);
 getch();
}
Output of above program:
Its work!!

In above program, the printf() would be executed once, since first the body of loop is executed and then the condition tested, 10<1 condition false so loop terminate and go to next statement.

/*program to find factorial value of any number, and it is execute unknown number of times when user enter no, then program should be terminate.*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int n,f=1;
 char choice;
 clrscr();
 do
 {
  printf("Enter number : ");
  scanf("%d",&n);
  while(n>=1)
  {
   f=f*n;
   n--;
  }
  printf("Factroial value of %d is %d",n,f);
  printf("\nCalculate another value y/n :");
  scanf("%c",&choice);
 }while(choice=='y');
}

output of above program:
Enter number :5
Factorial value of 5 is 120
Calculate another value y/n :y
Enter number :4
Factorial value of 4 is24
Calculate another value y/n :n

In above program, the do-while loop would keep getting executed till the user continues to answer y. When user enter answer n,the loop terminate, since the condition fails.

Sunday, 25 September 2011

Even/Odd sum,average

/*
program for read 10 numbers and calculate even sum,odd sum,even average and odd average.
variable's name stands for as : 
es=even sum, ea=even average, ec=even count
os=odd sum, oa=odd average, oc=odd count
*/

#include<stdio.h>
#include<conio.h>
int main()
{
 int arr[15];  
 int i;
 int es=0,ea,ec=0;
 int os=0,oa,oc=0;
 for(i=1; i<=10; i++)
 {
  printf("Enter %d number : ",i);
  scanf("%d",&arr[i]);
 }
 for(i=1; i<=10; i++)
 {
   if(arr[i]%2==0)
   {
      ec++;
      es=es+arr[i];
   }
   else
   {
      oc++;
      os=os+arr[i];
   }
 }
 ea=es/ec;
 oa=os/oc;
 printf("\nEven sum = %d",es);
 printf("\nEven average = %d",ea);
 printf("\nOdd sum = %d",os);
 printf("\nOdd Average = %d",oa);
 getch();
 return 0;
}

       Output of above program :

Enter 1 number : 1
Enter 2 number : 3
Enter 3 number : 5
Enter 4 number : 7
Enter 5 number : 9
Enter 6 number : 2
Enter 7 number : 4
Enter 8 number : 6
Enter 9 number : 8
Enter 10 number : 10

Even sum = 30
Even average = 6
Odd sum = 25
Odd Average = 5

Friday, 23 September 2011

Floyd's triangle

Q. Print the following Floyd's triangle :
          1
          2 3
          4 5 6
          7 8 9 10
Ans.
/*c Program of Floyd's triangle*/

#include<stdio.h>
#include<conio.h>
int main()
{
 int r,c,n,x=1;
 printf("Enter No. of rows of Floyd's triangle : ");
 scanf("%d",&n);
 for(r=1; n>=r; r++)
 {
   for(c=1; c<=r; c++,x++)
       printf(" %d",x);
   printf("\n");
 }
 getch();
 return 0;
}
      Output of the above program :
 
Enter No. of rows of Floyd's triangle : 4
1
2 3
4 5 6
7 8 9 10