Friday, September 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.

C Data type - void

Linguistic meaning of void is nothing. Size of void data type is meaningless question.

What will be output of following c code?
#include<stdio.h>
int main(){
    int size;
    size=sizeof(void);
    printf("%d",size);
    return 0;
}

Output: Compilation error

If we will try to find the size of void data type complier will show an error message �not type allowed�. We cannot use any storage class modifier with void data type so void data type doesn�t reserve any memory space. Hence we cannot declare any variable as of void type i.e.
void num;   //compilation error

Use of void data type

1. To declare generic pointer
2. As a function return type
3. As a function parameter.    

Write a C program to check prime number or not without using recursion

#include<stdio.h>

int isPrime(int);

int main(){

    int num,prime;

    printf("Enter a positive number: ");
    scanf("%d",&num);

    prime = isPrime(num);

   if(prime==1)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);

   return 0;
}

int isPrime(int num){

    int i=2;

    while(i<=num/2){
         if(num%i==0)
             return 0;
         else
             i++;
    }

    return 1;
}

Prime number program in c using recursion

#include<stdio.h>

int isPrime(int);

int main(){

    int num,prime;

    printf("Enter a positive number: ");
    scanf("%d",&num);

    prime = isPrime(num);

   if(prime==1)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);

   return 0;
}

int isPrime(int num){

    static int i=2;

    if(i<=num/2){
         if(num%i==0)
             return 0;
         else{
             i++;
             isPrime(num);
         }
    }

    return 1;
}

Thursday, September 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

Wednesday, September 28, 2011

C interview questions and answers

1. Write a c program without using any semicolon which output will : Hello word.


Solution: 1
void main(){
    if(printf("Hello world")){
    }
}
Solution: 2
void main(){
    while(!printf("Hello world")){
    }
}
Solution: 3
void main(){
    switch(printf("Hello world")){
    }
}

2. Write a C program to Swap two variables without using third variable.

int main()
{
    int a=5,b=10;
//process one
    a=b+a;
    b=a-b;
    a=a-b;
    printf("a= %d  b=  %d",a,b);

//process two
    a=5;
    b=10;
    a=a+b-(b=a);
    printf("\na= %d  b=  %d",a,b);
//process three
    a=5;
    b=10;
    a=a^b;
    b=a^b;
    a=b^a;
    printf("\na= %d  b=  %d",a,b);
  
//process four
    a=5;
    b=10;
    a=b-~a-1;
    b=a+~b+1;
    a=a+~b+1;
    printf("\na= %d  b=  %d",a,b);
  
//process five
    a=5,
    b=10;
    a=b+a,b=a-b,a=a-b;
    printf("\na= %d  b=  %d",a,b);
    getch();

}

3. What is dangling pointer in c?

If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.

Initially:

Later: 


4. What is wild pointer in c ?

A pointer in c which has not been initialized is known as wild pointer.

Example:

(q)What will be output of following c program?

void main(){
int *ptr;
printf("%u\n",ptr);
printf("%d",*ptr);

}

Output: Any address
Garbage value

Here ptr is wild pointer because it has not been initialized.
There is difference between the NULL pointer and wild pointer. Null pointer points the base address of segmentwhile wild pointer doesn�t point any specific memory location.

5. What is the meaning of prototype of a function ?

Prototype of a function
Answer: Declaration of function is known as prototype of a function. Prototype of a function means
(1) What is return type of function?
(2) What parameters are we passing?
(3) For example prototype of printf function is:
int printf(const char *, �);
I.e. its return type is int data type, its first parameter constant character pointer and second parameter is ellipsis i.e. variable number of arguments.


6. What are merits and demerits of array in c?

(a) We can easily access each element of array.
(b) Not necessity to declare two many variables.
(c) Array elements are stored in continuous memory location.

Demerit:
(a) Wastage of memory space. We cannot change size of array at the run time.
(b) It can store only similar type of data.



Tuesday, September 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

Sunday, September 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

Friday, September 23, 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

search Max & Min numbers

/*program for searching minimum and maximum number in ten numbers*/

#include<stdio.h>
#include<conio.h>
int main()
{
 int arr[10];
 int i,num,min,max;
 for(i=0; i<10; i++)
 {
   printf("Enter %d Number : ",i+1);
   scanf("%d",&arr[i]);
 }
 min=max=arr[0];
 for(i=0; i<10; i++)
 {
   if(arr[i] < min)
      min=arr[i];
   else if(arr[i] > max)
      max=arr[i];
 }
 printf("\nMaixmum number is %d",max);
 printf("\nMinimum number is %d",min);
 getch();
 return 0;
}

      Output of above program :

Enter 1 Number : 55
Enter 2 Number : 36
Enter 3 Number : 45
Enter 4 Number : 78
Enter 5 Number : 156
Enter 6 Number : 11
Enter 7 Number : 1256
Enter 8 Number : 2
Enter 9 Number : 596
Enter 10 Number : 367

Maximum number is : 1256                       
Minimum number is :                        

Monday, September 19, 2011

Addition 3X3 Matrix using function

/*program of addition of two given 3X3 matrix through function*/
#include<stdio.h>
#include<conio.h>
void show_matrix(int mat[3][3]);
void add_matrix(int matA[3][3], int matB[3][3], int matSum[3][3]);
int main()
{
 int x[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
 int y[3][3] = { {1,4,7}, {2,5,8}, {4,1,2} };
 int z[3][3];
 add_matrix(x,y,z);
 printf("\nFirst matrix is :\n");
 show_matrix(x);
 printf("\nSecond matrix is :\n");
 show_matrix(y);
 printf("\nNew addition matrix is :\n");
 show_matrix(z);
 getch();
 return 0;
}


void add_matrix(int matA[3][3], int matB[3][3], int matSum[3][3])
{
  int r,c;
  for(r=0; r<3; r++)
  {
    for(c=0; c<3; c++)
        matSum[r][c]=matA[r][c]+matB[r][c];
  }
}


void show_matrix(int mat[3][3])
{
  int r,c;
  for(r=0; r<3; r++)
  {
    for(c=0; c<3; c++)
        printf(" %d",mat[r][c]);
    printf("\n");
  }
}


             Output of above program : 

First matrix is :
1 2 3
4 5 6
7 8 9


Second matrix is :
1 4 7
2 5 8
4 1 2


New addition matrix is :
2  6  10
6  10 14
11 9  11

Related program:

  1. Difference of two matrix
  2. Sum of matrix
  3. Transpose of matrix
  4. Product of matrix
  5. Diagonal sum of matrix

malloc c program

/*Example of malloc function c program */

#include<stdio.h>
#include<conio.h>
struct st
{
  char nam[30];
  int clas;
  int marks;
};
int main()
{
 struct st *p;
 p=(struct st *)malloc(sizeof(struct st));
 printf("Enter your name : ");
 gets(p->nam);
 printf("Enter your class : ");
 scanf("%d",&p->clas);
 printf("Enter your obtained marks : ");
 scanf("%d",&p->marks);
 printf("\nYour name : %s",p->nam);
 printf("\nYour class : %d",p->clas);
 printf("\nYour obtained marks : %d",p->marks);
 getch();
 return 0;
}

      Output of above program :

Enter your name : Sid
Enter your class : 12
Enter your obtained marks : 483

Your name : Sid
Your class : 12
Your obtained marks : 483

Sunday, September 18, 2011

malloc swap function program

/*swapping two values through malloc function C program*/
#include<stdio.h>
#include<conio.h>
struct stud
{
 char nam[30];
 struct stud *next;
};
struct stud *p,*q;
int main()
{
 p=(struct stud *)malloc(sizeof(struct stud));
 q=(struct stud *)malloc(sizeof(struct stud));
 printf("Enter name p : ");
 gets(p->nam);
 //fflush(stdin);
 printf("Enter name q : ");
 gets(q->nam);
 p->next=q;
 q->next=p;
 printf("\nName of p : %s",p->next->nam);
 printf("\nName of q : %s",q->next->nam);
 getch();
 return 0;
}

       Output of above program : 

Enter name p : Peter
Enter name q : Jhon

Name of p : Jhon
Name of q : Peter

Leap year C program

/*Program to find a year is century leap year or not and a simple leap year or not*/

#include<stdio.h>
#include<conio.h>
int main()
{
 int year;
 printf("Enter Year : ");
 scanf("%d",&year);
 if(year%100==0)
 {
  if(year%400==0)
    printf("\n%d is century leap year",year);
  else
    printf("\n%d is not century leap year",year);
 }
 else
 {
  if(year%4==0)
    printf("\n%d is leap year",year);
  else
    printf("\n%d is not leap year",year);
 }
 getch();
 return 0;
}
    Output of above program :

Enter year : 2012
2012 is leap year

Friday, September 16, 2011

Fibonacci Series C program

Fibonacci numbers:Each Fibonacci numbers is defined to be the sum of its two immediate previous terms, where as the first two number are 0 and 1.
The Fibonacci numbers are the numbers in the following integer sequence :
0 1 1 2 3 5 8 13 21 34 55 89 144. . . . .

/*Programme to print Fibonacci numbers less than given user number*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int x,y,z,num;
 printf("Enter any last number of Fibonacci series : ");
 scanf("%d",&num);
 x=0;
 y=1;
 while(num>=x)
 {
    z=x+y;
    printf("%d\t",x);
    x=y;
    y=z;
 }
 getch();
 return 0;
}

        Output of above program :

Enter last number where goes to end Fibonacci series : 70

0  1  1  2  3  5  8  13  21  34  55



Related Programs:
  1. Generate Fibonacci series using recursion method
  2. Search number is Fibonacci or not C program
  3. Flowchart for Fibonacci series