Saturday, December 29, 2012

Connect MySql Database to Server

The connection of mysql database to server using object oriented concept, I uses these files:

  1. connection.php
  2. mysql.php
  3. index.php
So, Now let's start:

First of all create a root_folder that will contains all of files says: eshop
Now open your favorite text-editor like as Adobe Dreamweaver and create a php file names connection.php and save it in our root folder as eshop.

connection.php

<?php                                                   
define'DB_SERVER' , 'localhost' );
define'DB_USER' , 'root' );       
define'DB_PASSWORD' , '' );       
define'DB_NAME' , 'eshop_db' );   
?>                                  


Replace/change server, username, password and database_name as your default/desired setting.
And save it. That's over.

Now you think, What is benefit of make such a different file, we will make it separate with other php files?
Yes, you can do that but if we built a large php application like as eCommerce website then if these need to any change as change the database name etc. in future then it is quite easily to open connection.php file and replaced desired name rather then effect on rest of files.

Friday, December 28, 2012

Number Rectangle

Q. Write a C program to print the following number rectangle:

1333
2222
3331

Ans.

/*c program for print number rectangle pyramid*/
#include<stdio.h>
int main()
{

 int num,n,r,c,k;
 printf("Enter no. of rows: ");
 scanf("%d", &num);
 n=num;
 for(r=1; r<=num; r++,n--)
 {
  for(c=1; c<=r; c++)
     printf("%d",r);
  for(k=1; k<=n; k++)
     printf("%d",n);
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of number rectangle pyramid C program
Figure: Screen shot for number rectangle pyramid C program

Monday, December 17, 2012

Number Pyramid

Q. Write a C program to print the following number pyramid:

1
123
12345
1234567

Ans.

/*c program for number pyramid*/
#include<stdio.h>
int main()
{

 int num,r,c,p=1;
 printf("Enter number of rows: ");
 scanf("%d", &num);
 for(r=1; r<=num; r++,p=p+2)
 {
  for(c=1; c<=p; c++)
    printf("%d",c);
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of number pyramid C program
Figure: Screen shot for number pyramid C program

Sunday, December 9, 2012

Number Pryamid By Function

Q. Write a C program to print the following triangle using function.

 1
 2 3
 4 5 6
 7 8 9 1
 2 3 4 5 6

Ans.

/*C program for number pyramid using function*/
#include<stdio.h>
void pyramid(int );
int main()
{
 int num;
 printf("Enter the number of rows : ");
 scanf("%d", &num);
 pyramid(num);
 return 0;
}

void pyramid(int n)
{
 int r,c,x=1,y=1;
 for(r=1; r<=n; r++)
 {
  for(c=1; c<=r; c++,x++)
  {
    if(x<=9)
       printf(" %d",x);
    else
    {
       printf(" %d",y);
       y++;
    }
  }
  printf("\n");
 }
}

The output of above program would be:

Output of create number pyramid using function C program
Figure: Screen shot for number pyramid
using function C program

Monday, December 3, 2012

Character Rectangle Program


Q. Write a C program to print the following character pyramid as:

ABCD
BCDA
CDAB
DABC

Ans.

Cross-reference: Before you read answer of above program, i recommend to read Number Rectangle Program for easily understand this program.

/*c program for character pyramid*/
#include<stdio.h>
int main()

{
 char ch,r,c,t;
 printf("Enter any character : ");
 scanf("%c", &ch);
 if(ch>='a' && ch<='z')
    ch = ch - 32;
 for(r='A'; r<=ch; r++)
 {
  for(c=r; c<=ch; c++)
     printf("%c",c);
  for(t='A'; t<r; t++)
     printf("%c",t);
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of character rectangle C program
Figure: Screen shot for character rectangle pyramid C program

Number Rectangle Program

Q. Write a C program to print the following number pyramid as:

1234
2341
3412
4123

Ans.

/*c program for number pyramid*/
#include<stdio.h>
int main()
{

 int num,n,r,c,t;
 printf("Enter any number : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(c=r; c<=num; c++)
     printf("%d",c);
  for(t=1; t<r; t++)
     printf("%d",t);
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of number rectangle C program
Figure: Screen shot for number rectangle C program

Character Pyramid Program

Q. Write a C program to print the following character pyramid C program:

ABCD

CDA
AB
C

Ans.

Cross-reference: Before you read answer of above program i recommend to read Number Pyramid Program for easily understand this program. 

/*c program for character pyramid*/
#include<stdio.h>
int main()
{

 char ch='D',n,c,r,t;
 n=ch;
 for(r='A'; r<=ch; r++,n--)
 {
  if(r=='B' || r=='D')
  {
    for(c='A',t='C'; c<=n; c++,t++)
    {
      if(c=='C' && r=='B')
         printf("A");
      else
         printf("%c",t);
    }
  }
  else
  {
    for(c='A'; c<=n; c++)
        printf("%c", c);
  }
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of character pyramid C program
Figure: Screen shot for character pyramid C program

Number Pyramid

Q. Write a C program to print the following number pyramid C program:

1234
341
12
3

Ans.

/*c program for number pyramid*/
#include<stdio.h>
int main()
{

 int num=4,n,c,r,t;
 n=num;
 for(r=1; r<=num; r++,n--)
 {
  if(r==2 || r==4)
  {
    for(c=1,t=3; c<=n; c++,t++)
    {
      if(c==3 && r==2)
         printf("1");
      else
         printf("%d",t);
    }
  }
  else
  {
    for(c=1; c<=n; c++)
        printf("%d", c);
  }
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of number pyramid C program
Figure: Screen shot for number pyramid C program

Sunday, December 2, 2012

Character Rectangle Design

Q. Write a C program to print the following character rectangle structure:

ABCBA
AB BA
A   A
AB BA
ABCBA

Ans.

Cross-Reference: Before you reading answer of above program, i recommend to read Number Rectangle Design for easily understand this program.

/*c program for character rectangle pyramid*/
#include<stdio.h>
int main()
{

 char ch='C',r,c,q,t,sp,st;
 st=ch;
 for(r='A'; r<=ch; r++,st--)
 {
  for(c='A';c<='B'; c++)
  {
    if(r=='C')
    {
      printf("A ");
      break;
    }
    else
      printf("%c",c);
  }
  for(sp=r; sp>'A'; sp--)
    printf(" ");
  for(t=st; t>='A'; t--)
    printf("%c",t);
  printf("\n");
 }
 for(r='A'; r<=ch-1; r++)
 {
  for(c='A'; c<='B'; c++)
     printf("%c", c);
  for(sp=r; sp<='A'; sp++)
     printf(" ");
  for(t=r+1; t>='A'; t--)
     printf("%c", t);
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of character rectangle C program
Figure: Screen shot for character rectangle structure C program

Star Triangle Pyramid

Q. Write a C program to print the following star pyramid structure:

*********
*******
*****
***
*

Ans.

/*c program for star pyramid design*/
#include<stdio.h>
int main()
{

 int n=9,r,c;
 for(r=5; r>=1; r--,n=n-2)
 {
   for(c=1; c<=n; c++)
      printf("*");
   printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of star triangle C program
Figure: Screen shot for star triangle C program


Number Rectangle Design

Q. Write a C program to print the following number rectangle design :

12321
12 21
1   1
12 21
12321

Ans.

/*c program for number rectangle design*/
#include<stdio.h>
int main()
{

 int num,n,r,c,sp,q,t;
 printf("Enter any number : ");
 scanf("%d", &num);
 n=num;
 for(r=1; r<=num; r++,n--)
 {
  for(c=1; c<=2; c++)
  {
   if(r==3)
   {
     printf("1 ");
     break;
   }
   else
     printf("%d",c);
  }
  for(sp=r; sp>1; sp--)
     printf(" ");
  for(t=n; t>=1; t--)
     printf("%d",t);
  printf("\n");
 }
 for(r=1; r<=num-1; r++)
 {
  for(c=1; c<=2; c++)
     printf("%d",c);
  for(sp=r; sp<=1; sp++)
     printf(" ");
  for(t=r+1; t>=1; t--)
     printf("%d",t);
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of number rectangle C program
Figure: Screen shot for number rectangle C program


Number Triangle Program

Q. Write a C program to print the following number triangle design:

1
232
34543
4567654

Ans.

/*c program for number triangle design*/
#include<stdio.h>
int main()
{

 int num,r,c,s,t,q;
 printf("Enter any number : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(c=r, s=r; c>=1; c--,s++)
     printf("%d", s);
  for(q=r-1,t=s-2; q>=1; q--,t--)
     printf("%d", t);
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of number pyramid C program
Figure: Screen shot for number pyramid C program