Monday, 10 October 2011

while loop

It is often the case in programming that we want to do something a fixed number of times. Perhaps we want to calculate gross salaries, or convert temperatures form centigrade to Fahrenheit for 20 different cities. The while loop is ideally suited f or such cases.

Syntax of using while loop:
initialise loop counter
while(valid condition)
{
  statement1;
  statement2;
  statement3;
  statement4;
  increment or decrement loop counter;
}

some basic rule of while loop:
  • The statements within the while loop would keep in getting executed till the condition being tested remains true. When the condition becomes false, the control passes to the first statement that follows the body of the while loop.
  • The condition being tested may use relational or logical operators as shown in the following examples:
    while(x<=20)
    while
    (x>=20 && x<=50)
    while
    (x>=10 || (i>=10 && i<=20)||y==15)
  • Almost always, the while must test a condition that will eventually become false, otherwise the loop would be executed forever,indefinitely.
  • It is not necessary that a loop counter must only be an int. It can even be a float.
  • Example of while loop:
#include<stdio.h>
#include<conio.h>
void main()
{
 int i=1;
 while(i<=5)
 {
  printf("%d\n",i);
  i=i+1;  //i++;
 }
 getch();
}
       Output of above program:
1
2
3
4
5

Nested while loop

One or more loop which require execute again and again then these loop and loops place in separate block is known as nested loop.
Example of nested while loop:

/*demonstration of nested while loop*/

#include<stdio.h>
#include<conio.h>
void main()
{
 int r,c,s;
 clrscr();
 r=1;
 while(r<=5)  /*outer loop*/
 {
  c=1;
  while(c<=2)  /*inner loop*/
  {
   s=r+c;
   printf("r=%d c=%d sum=%d\n",r,c,s);
   c++;
  }
  printf("\n");
  r++;
 }
 getch();
}
         Output of above program:
r=1 c=1 sum=2
r=1 c=2 sum=3
r=2 c=1 sum=3
r=2 c=2 sum=4
r=3 c=1 sum=4
r=3 c=2 sum=5
r=4 c=1 sum=5
r=4 c=2 sum=6
r=5 c=1 sum=6
r=5 c=2 sum=7

Saturday, 8 October 2011

Looping concept

The program that we have developed so far used either a sequential or a decision control instruction.
The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied. This repetitive operation is done through a loop control instruction.
Loops can be created to executed a block of code for a fixed number of times. Alternatively, loops can be created to repetitively execute a block of code until a Boolean condition changes state.
For instance, the loop may continue until a condition changes from false to true, or from true to false. In this case, the block o code being executed must update the condition being tested in order for the loop to terminate at some point. If the test condition is not modified somehow within the loop, the loop will never terminate. This created a programming bug known as an infinite loop.
There are three method by way to which we can repeat a part of a program. They are:
  1. Using a for statement
  2. using a while statement
  3. Using a do-while statement

Sunday, 2 October 2011

Check String Palindrome

Q. What is palindrome?
Ans:Palindrome is a Greek word which is divided in two or three word as (palin=Again + drome/deamein=Run). So A palindrome is word or phrase which reads the same in both direction i.e. left to right and right to left are character must be same. Example of string palindrome as follows : 
1. POP
2. MALAYYALAM
3. LEVEL
4. LIVE EVIL
5. MADAM, I'M MADAM

/*c program to compare two string and find out whether a string is palindrome or not*/
/*simple c program to check whether a string is palindrome or not*/
#include<stdio.h>
#include<conio.h>
int main()
{
 char str[20];
 int i,j;
 printf("Enter string : ");
 gets(str);
 for(i=0; str[i]!=NULL; i++);
 for(i--,j=0; j<=i; )
 {
   if(str[i]==str[j])
   {
      i--;
      j++;
   }
   else
      break;
 }
 if(j>i)
    printf("\nString is palindrome!!");
 else
    printf("\nString is not palindrome!!");
 getch();
 return 0;
}


/****************Output**************** 
Enter string :navana
String is not palindrome



Enter string : madam n madam
String is palindrome

***************************************/

You might also like:

  1. Check palindrome using pointer
  2. Check palindrome using function

Lucas number Program

Lucas numbers: It is similar to Fibonacci numbers, each Lucas number is defined to be the sum of its two immediate previous terms Where as the first two number are 2 and 1.
The Lucas numbers are as following :
2 1 3 4 7 11 18 29 47 76

/*c program to accept number from user and print less than Lucas numbers*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int x,y,z,num;
 printf("Enter the limit of Lucas number : ");
 scanf("%d",&num);
 x=2;
 y=1;
 while(num>=x)
 {
   printf(" %d",x);
   z=x+y;
   x=y;
   y=z;
 }
 getch();
 return 0;
}


/***************Output*************** 

Enter the limit of Lucas number : 125
 2 1 3 7 11 18 29 47 76 123


************************************/

Related Programs:

  1. Amicable number C program
  2. Perfect number C program
  3. Armstrong number C program
  4. Print Armstrong number range C program
  5. Fibonacci series C program
  6. Generate Fibonacci series using recursion
  7. Search number is Fibonacci or not

Saturday, 1 October 2011

Program binary numbers

Binary numbers :
0 1 2 4 8 16 32 64 128 256.......n


/*c program to print binary numbers series*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int x,y,z,num;
 printf("Enter last number where goes to end binary numbers : ");
 scanf("%d",&num);
 x=0;
 y=1;
 while(num>=x)
 {
   z=x+y;
   printf("%d\t",x);
   y=z;
   x=y;
 }
 getch();
 return 0;
}

/***************Output*****************
Enter last number where goes to end binary numbers : 50
0 1 2 4 8 16 32


*************************************/

Friday, 30 September 2011

create database mysql

<?php
 $host="localhost";
 $user="root";
 $password="";
 mysql_connect($host,$user,$password);
 if(!mysql_select_db("mynewdb"))
 {
  $createdb="create database mynewdb";
  mysql_query($createdb) or die("can't create database");
 }
 echo "database already exist";
?>

More if-else

Notice the following if statement :
if(expression)
  statement;
Here the expression can any valid expression including a relational expression. We can even use arithmetic expression in the if statement. For example all the following if statement are valid :
if(10 + 5 % 7)
  statement;
if(n=20)
  statement;
if(-15)
  statement;
if(-2.0)
  statement;

In C every expression is evaluated in terms of zero and non-zero values.
Note: that in C a non-zero value is considered to be true, whereas a 0 is considered to be false.
The default scope of the if statement is only the next statement. So, to execute more than one statement they must be written in a pair of braces