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

9.24.2011

Floyd's triangle

Q. Write a C program to floyd algorithm number triangle in C as:

  1
  2 3
  4 5 6
  7 8 9 10

Ans. 

/* c program for floyd algorithm number triangle */
#include<stdio.h>
int main()
{
 int r,c,n,x=1;
 printf("Enter No. Of rows : ");
 scanf("%d", &n);
 for(r=1; r<=n; r++)
 {
   for(c=1; c<=r; c++,x++)
       printf(" %d",x);
   printf("\n");
 }
 getch();
 return 0;
}

/*************************************************************
The output of above program would be:
**************************************************************/

Output of Floyd algorithm number triangle C program
Figure: Screen shot for Floyd algorithm number triangle C program

You might also like to read:


      A
      B C
      D  E  F
      G  H  I J


      9
      8  7
      6  5  4
      3  2  1  0 
         

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

9.23.2011

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 :                        

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

9.19.2011

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

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

9.18.2011

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

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

Find students grades through structure

Q. Write a C program using structure to find student grades in a class. Make necessary assumption.

Ans.
Before writing program we make some assumption as following :

Maximum total marks is 500.     
  Student_percentage         Grades
     >=80                        A
     >=60                        B
     >=50                        C
     >=40                        D
     <40                         F
  
/*C program to find students grades in a class through structure */
#include<stdio.h>
#include<conio.h>
struct stud
{
  char nam[20];
  int obtain_mark;
  int per;
  char grad[5];
};
struct stud s[5];
int i;
int main()
{
 for(i=1; i<=5; i++)
 {
  printf("Enter %d student name : ",i);
  scanf("%s",&s[i].nam);
  printf("Enter %d student obtained marks = ",i);
  scanf("%d",&s[i].obtain_mark);
  fflush(stdin);
 }
 for(i=1; i<=5; i++)
   s[i].per=s[i].obtain_mark/5;
 for(i=1; i<=5; i++)
 {
  if(s[i].per>=80)
    strcpy(s[i].grad,"A");
  else if(s[i].per>=60)
    strcpy(s[i].grad,"B");
  else if(s[i].per>=50)
    strcpy(s[i].grad,"C");
  else if(s[i].per>=40)
    strcpy(s[i].grad,"D");
  else
    strcpy(s[i].grad,"F");
 }
 for(i=1; i<=5; i++)
  printf("\n%d student %s has obtained grade %s ",i,s[i].nam,s[i].grad);
 getch();
 return 0;
}
  Output of above programe :

Enter 1 student name : John
Enter 1 student obtained marks : 250
Enter 2 student name : Robert
Enter 2 student obtained marks : 410
Enter 3 student name : Isabell
Enter 3 student obtained marks : 386
Enter 4 student name : Adem
Enter 4 student obtained marks :197
Enter 5 student name :Harry
Enter 5 student obtained marks : 490 

1 student Jhon has obtained grade C
2 student Robert has obtained grade A
3 student Isabell has obtained grade B
4 student Adem has obtained grade F
5 student Harry has obtained grade A
 


C program for find student grade through structure
Figure: Screen-shot of find student grades 
through structure C program

Related programs:

  1. Get student 5 subject marks and calculate percentage and division