9.30.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

forms of if-else

There are many way to write if-else statement in program,it can be single if statement,both if-else statement,if-else if statement and etc. There are no limit on how deeply the if's and the else's can be nested. Now let's start to form of if-else  :

1. if(condition)
     do this;
2. if(condition)
   {
     do this;
     and this;
   }
3. if(condition)
     do this;
   else
     do this;
4. if(condition)
   {
     do this;
     and this;
   }
   else
     do this;
5. if(condition)
   {
     do this;
     and this;
   }
   else
   {
     do this;
     and this;
   }
6. if(condition)
     do this;
   else
   {
     if(condition)
       do this;
     else
     {
       do this;
       and this;
     }
   }
7.  if(condition)
    {
      if(condition)
       do this;
     else
     {
       do this;
       and this;
     }
   }
   else
     do this;
8.  if(condition)
      do this;
    else if(condition)
      do this;
    else if(condition)
      do this;
    else
      do this;


And now you thinking, what the hell condition? Don't worry, its quite simple and easy to use in programming. In condition part we put data,numeric values with with appropriate operator like as Logical operator, Arithmetic operator etc.

9.29.2011

if-else program

Q. Write a C program to read quantity and price for each item. Calculate the total expenses, and if the quantity is more than 1000 than giving discount is 20%.

Ans.
/*Calculate the total expenses*/

#include<stdio.h>
#include<conio.h>
void main()
{
 int quant,dis=0;
 float tot,rate;
 clrscr();
 printf("Enter quantity : ");
 scanf("%d",&quant);
 pirntf("\nEnter rate : ");
 scanf("%f",&rate);
 if(quant>1000)
  dis=20;
 tot=(quant*rate)-(quent*rate*dis/100);
 pirntf("\nTotal expenses = %f",tot);
 getch();
}

Output :
Enter quantity :750
Enter rate : 35.54
Total expenses = 25905.000000

Enter quantity : 1450
Enter rate : 9.50
Total expenses = 12397.500000

Intro if-else

In C language there are three major decision making instruction as if statement, the if-else statement, and the switch statement. And one also minor decision making instruction is conditional operator.

The keyword if tells the compiler that what follows is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is, is true, then the statement is executed, and If the condition is not true, then statement is not executed.

Let's demonstrates the use of if-else with c program:

/*demonstrates of if-else statement*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int n=10;
 if(n>=15)
  printf("n is greater!!");
 else
  printf("n is less than!!");
 getch();
}
Output :
n is less than!!

On execution of this program, when compiler check condition 10>=15, the condition is false so its execute else part statement i.e. n is less than!!.

Now try to write below program for more familiar with if-else:
Q. Write a C program to read quantity and price for each item. Calculate the total expenses, and if the quantity is more than 1000 than giving discount is 20%.

Ans: To see solved program click here

9.27.2011

search prime number

Q. Write a C program to find whether a number is prime or not.
Definition of prime number:A prime number is one,which is divisible only by one or itself. And its must greater than 1.
And if number is not prime, it is called composite number and vice versa.

Ans.

#include<stdio.h>
#include<stdio.h>
int main()
{
 int x,num;
 printf("Enter number : ");
 scanf("%d",&num);
 x=2;
 while(x<=num-1)
 {
   if(num%x==0)
   {
      printf("Number is not prime!!");
      break;
   }
   x++;
 }
 if(x==num)
    printf("Number is prime!!");
 getch();
 return 0;
}

    output of above program :

Enter number : 7
Number is prime!!


Related Programs:

  1. Generate first n Prime number C program
  2. Print Prime Number 1 to 100 C program
  3. Flowchart for search prime number

9.25.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