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

Sunday, November 18, 2012

Star Pyramid

Q. Write a C program to accept the number of rows of pyramid from user and print the correspondence star triangle.
For example,

Enter number by user : 7

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

Ans.

/*c program for star pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c;
 printf("Enter number of rows : ");
 scanf("%d", &num);
 for(r=1; r<=(num/2)+1; r++)
 {
  for(c=1; c<=r; c++)
    printf("*");
  printf("\n");
 }
 for(r=1; r<=(num/2); r++)
 {
  for(c=r; c<=num/2; c++)
    printf("*");
  printf("\n");
 }
 return 0;
}

The output of above program would be:

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

Character Pyramid

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

A
BA
ABA
BABA
ABABA

Ans.

/*c program for character pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c;
 char ch='A',st='B';
 printf("Enter number of rows: ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(c=r; c>=1; c--)
  {
   if(c%2==0)
     printf("%c", st);
   else
     printf("%c", ch);
  }
  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


Saturday, November 10, 2012

User Define Function- Power

Q. Write a C program to create function power and calculate the power of any number.
For example:
Assume number = 5
Power = 3
Result = 5*5*5 = 125

Ans.

/*c program for creating user define function power and calculating power of number*/
#include<stdio.h>
int power(int , int );
int main()
{
 int num,pow,res;   //res = result
 printf("Enter any number : ");
 scanf("%d", &num);
 printf("Enter power of number : ");
 scanf("%d", &pow);
 res = power(num,pow);
 printf("%d's power %d = %d",num,pow,res);
 return 0;
}

int power(int n, int p)
{
 int r=1;
 for(; p>=1; p--)
    r = r*n;
 return r;
}

The output of above program would be:


Output of user define function "power" C program
Figure: Screen shot for calculating power of any number
using user define function power C program

User Define Function- Reverse Number

Q. Write a C program to create a function that would reverse the any number.
For example:
Assume enter number = 57429
Result = 92475

Ans.

/*c program for reverse number using user define function- rev */
#include<stdio.h>
int rev(int );
int main()
{
 int num,res;  // res = result
 printf("Enter any number : ");
 scanf("%d", &num);
 res = rev(num);
 printf("Reverse order number = %d",res);
 return 0;
}

int rev(int n)
{
 int r=0;
 for(; n>=1; n=n/10)
   r = r*10 + n%10;
 return r;
}

The output of above program would be:


Output of reverse number using user define function C program
Figure: Screen shot for reverse digit using
user define function C program


User Define Function- LeapYear

Q. Write a C program to find entered year is leap year or not using own created function say "leap".

Ans.

/*c program for find year is leap year or not using user define function leap*/
#include<stdio.h>
int leap(int );
int main()
{

 int year;
 printf("Enter any year : ");
 scanf("%d", &year);
 if(leap(year))
   printf("\n%d is leap year",year);
 else
   printf("\n%d is not leap year",year);
 return 0;
}

int leap(int y)
{
 if((y%400==0 && y%100==0)||(y%4==0))
    return 1;
 else
    return 0;
}

The output of above program would be:


Output of check year is leap year or not using user define function C program
Figure: Screenshot for user define function to
check year is leap year C program



Output of check year is leap year or not using user define function C program
Figure: Screenshot for user define function to 
check year is not leap year C program