Sunday 26 May 2013

Write a program to create a linear linked list interactively and print out the list and the total number of items in the list

#include<stdio.h>
#include<stdlib.h>
#define NULL 0
struct linked_list
{
int number;
struct linked_list *next;
};
typedef struct linked_list node; /* node type defined */
main()
{
node *head;
void create(node *p);
int count(node *p);
void print(node *p);
head = (node *)malloc(sizeof(node));
create(head);
printf("\n");
printf(head);
printf("\n");
printf("\nNumber of items = %d \n", count(head));
}
void create(node *list)
{
printf("Input a number\n");
printf("(type -999 at end): ");
scanf("%d", &list -> number); /* create current node */
if(list->number == -999)
{
list->
next = NULL;
}
else /*create next node */
{
list->
next = (node *)malloc(sizeof(node));
create(list->next); */ Recursion occurs */
}
return;
}
void print(node *list)
{
if(list->next != NULL)
{
printf("%d-->",list ->number); /* print current item */
if(list->next->next == NULL)
printf("%d", list->next->number);
print(list->next); /* move to next item */
}
return;
}
int count(node *list)
{
if(list->next == NULL)
return (0);
else return(1+ count(list->next));
}

Output:

Input a number
(type -999 to end); 60
Input a number
(type -999 to end); 20
Input a number
(type -999 to end); 10
Input a number
(type -999 to end); 40
Input a number
(type -999 to end); 30
Input a number
(type -999 to end); 50
Input a number
(type -999 to end); -999
60 -->20 -->10 -->40 -->30 -->50 --> -999
Number of items = 6

Find 2’s complement of a binary number

#include<stdio.h>
#include<conio.h>
void complement (char *a);
void main()
{
char a[16];
int i;
clrscr();
printf("Enter the binary number");
gets(a);
for(i=0;a[i]!='\0'; i++)
{
if (a[i]!='0' && a[i]!='1')
{
printf("The number entered is not a binary number. Enter the correct number");
exit(0);
}
}
complement(a);
getch();
}
void complement (char *a)
{
int l, i, c=0;
char b[16];
l=strlen(a);
for (i=l-1; i>=0; i--)
{
if (a[i]=='0') b[i]='1';
else b[i]='0';
}
for(i=l-1; i>=0; i--)
{
if(i==l-1)
{
if (b[i]=='0') b[i]='1';
else
{
b[i]='0'; c=1;
}
}
else
{
if(c==1 && b[i]=='0')
{
b[i]='1'; c=0;
}
else if (c==1 && b[i]=='1')
{
b[i]='0';
c=1;
}
}
}
b[l]='\0';
printf("The 2's complement is %s", b);
}

Implement Trapezoidal Method

#include<stdio.h>
#include<conio.h>
#include<math.h>

char postfix[80];
float stack[80];
char stack1[80];
int top=-1,top1=-1;

float eval(char postfix[], float x1);
void infix_postfix(char infix[]);

main()
{
float x0, xn, h, s,e1,e2;
char exp[80], arr[80];
int i,n,l=0;
clrscr();
printf("\nEnter an expression: ");
gets(exp);
puts("Enter x0, xn and number of subintervals");
scanf("%f%f%d", &x0, &xn, &n);
h=(xn-x0)/n;
if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g')
{
l=strlen(exp);
for(i=0;i<l-3; i++)
arr[0]=exp[i+3];
arr[i]='\0';
infix_postfix(arr);
e1=eval(postfix,x0);
e2=eval(postfix,xn);
s=log(e1)+log(e2);
for (i=1;i<=n-1;i++)
s+=2*log(eval(postfix,x0+i*h));
}
else
{
infix_postfix(exp);
s=eval(postfix,x0)+eval(postfix,xn);
for (i=1;i<=n-1;i++)
s+=2*eval(postfix,x0+i*h);
}
printf("Value of the integral is %6.3f\n",(h/2)*s);
return(0);
}
/*Inserting the operands in a stack. */
void push(float item)
{
if(top==99)
{
printf("\n\tThe stack is full");
getch();
exit(0);
}
else
{
top++;
stack[top]=item;
}
return;
}
/*Removing the operands from a stack. */
float pop()
{
float item;
if(top==-1)
{
printf("\n\tThe stack is empty\n\t");
getch();
}
item=stack[top];
top--;
return (item);
}
void push1(char item)
{
if(top1==79)
{
printf("\n\tThe stack is full");
getch();
exit(0);
}
else
{
top1++;
stack1[top1]=item;
}
return;
}
/*Removing the operands from a stack. */
char pop1()
{
char item;
if(top1==-1)
{
printf("\n\tThe stack1 is empty\n\t");
getch();
}
item=stack1[top1];
top1--;
return (item);
}

/*Converting an infix expression to a postfix expression. */
void infix_postfix(char infix[])
{
int i=0,j=0,k;
char ch;
char token;
for(i=0;i<79;i++)
postfix[i]=' ';
push1('?');
i=0;
token=infix[i];
while(token!='\0')
{
if(isalnum(token))
{
postfix[j]=token;
j++;
}
else if(token=='(')
{
push1('(');
}
else if(token==')')
{
while(stack1[top1]!='(')
{
ch=pop1();
postfix[j]=ch;
j++;
}
ch=pop1();
}
else
{

while(ISPriority(stack1[top1])>=ICP(token))
{
ch=pop1();
/*Assigning the popped element into the postfix array. */
postfix[j]=ch;
j++;
}
push1(token);
}
i++;
token=infix[i];
}
while(top1!=0)
{
ch=pop1();
postfix[j]=ch;
j++;
}
postfix[j]='\0';
}

int ISPriority(char token)
{
switch(token)
{
case '(':return (0);
case ')':return (9);
case '+':return (7);
case '-':return (7);
case '*':return (8);
case '/':return (8);
case '?':return (0);
default: printf("Invalid expression");
break;
}
return 0;
}
/*Determining the priority of elements that are approaching towards the stack. */
int ICP(char token)
{
switch(token)
{
case '(':return (10);
case ')':return (9);
case '+':return (7);
case '-':return (7);
case '*':return (8);
case '/':return (8);
case '\0':return (0);
default: printf("Invalid expression");
break;
}
return 0;
}
/*Calculating the result of expression, which is converted in postfix notation. */
float eval(char p[], float x1)
{
float t1,t2,k,r;
int i=0,l;
l=strlen(p);
while(i<l)
{
if(p[i]=='x')
push(x1);
else
if(isdigit(p[i]))
{
k=p[i]-'0';
push(k);
}
else
{
t1=pop();
t2=pop();
switch(p[i])
{
case '+':k=t2+t1;
break;
case '-':k=t2-t1;
break;
case '*':k=t2*t1;
break;
case '/':k=t2/t1;
break;
default: printf("\n\tInvalid expression");
break;
}
push(k);
}
i++;
}
if(top>0)
{
printf("You have entered the operands more than the operators");
exit(0);
}
else
{
r=pop();
return (r);
}
return 0;
}

Calculate the cost of publishing conference

#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
const int std_size=15;
const float std_cost=5;
const float penalty=0.5;
class paper;
float totalcost(paper*,int);
class paper
{
char author[25];
int page;
   public:
paper()
{
author[0]='\0';
page=0;
}
paper(char a[],int p)
{
strcpy(author,a);
page=p;
}
void input();
float papercost();
};
void paper::input()
{
cout<<"\nEnter name of the author : ";
gets(author);
cout<<"\nEnter number of pages in his research paper : ";
cin>>page;
}
float paper::papercost()
{
int extra;
float cost;
if(page<=std_size)
return page*std_cost;
else
{
extra = page - std_size;
cost  = (std_size*std_cost) + extra*(std_cost + penalty);
return cost;
}
}
int main()
{
int count;
paper *conf;
cout<<"Enter total number of papers : ";
cin>>count;
cout<<"\nEnter details of the papers\n";
cout<<"---------------------------\n";
conf = new paper[count];
for(int i=0;i<count;i++)
conf[i].input();
cout<<"\n\nThe total cost of publishing is : Rs."<<totalcost(conf,count);
delete conf;
return 0;
}
float totalcost(paper *p,int n)
{
float tcost=0;
for(int i=0;i<n;i++)
{
tcost +=  p[i].papercost();
}
return tcost;
}


Output:
Enter total number of papers : 2

Enter details of the papers
---------------------------
Enter name of the author :
Enter number of pages in his research paper : 3
Enter name of the author :
Enter number of pages in his research paper : 3
The total cost of publishing is : Rs.30

Area of two triangles - CPP Program

#include<iostream.h>
#include<conio.h>
#include<math.h>
class right_triangle
{
double base;
double height;
   public:
void initialize(double,double);
double area();
double peri();
};
void right_triangle::initialize(double b, double h)
{
base=b;
height=h;
}
double right_triangle::area()
{
return (0.5*base*height);
}
double right_triangle::peri()
{
double hypt;
hypt = sqrt(base*base + height*height);
return base+height+hypt;
}
int main()
{
right_triangle r1,r2;
double bs,ht;
//Initializing triangles
cout<<"\nINPUT\n";
cout<<"\nEnter base of first triangle : ";
cin>>bs;
cout<<"Enter height of first triangle : ";
cin>>ht;
r1.initialize(bs,ht);
cout<<"\nEnter base of second triangle : ";
cin>>bs;
cout<<"Enter height of second triangle : ";
cin>>ht;
r2.initialize(bs,ht);
//Calculating area and perimeter
cout<<"\nArea of first triangle       : "<<r1.area();
cout<<"\nPerimeter of first triangle  : "<<r1.peri();
cout<<"\n\nArea of second triangle      : "<<r2.area();
cout<<"\nPerimeter of second triangle : "<<r2.peri();
return 0;
}


Output:
INPUT
Enter base of first triangle : 7
Enter height of first triangle : 4
Enter base of second triangle : 6
Enter height of second triangle : 2
Area of first triangle       : 14
Perimeter of first triangle  : 19.0623
Area of second triangle      : 6
Perimeter of second triangle : 14.3246
Process returned 0 (0x0)   execution time : 26.804 s
Press any key to continue.

Income Class in C++

#include<iostream.h>
#include<conio.h>
class income
{
double BS;
double DA;
   public:
void initialize(double,double);
double pay_sal();
double deduction();
double calc_tax();
void income_detail();
};
void income::initialize(double b,double d)
{
BS=b;
DA=d;
}
double income::pay_sal()
{
double HRA;
HRA = 0.15 * BS;
return (BS+DA+HRA);
}
double income::deduction()
{
double SC,PF;
SC = PF = 0.08 * BS;
return (SC+PF);
}
double income::calc_tax()
{
double sal,tax,sc;
sal = pay_sal();
sal*=12;
if(sal<100000)
{
tax = 0.2 * sal;
}
else
{
tax = 0.3 * sal;
sc  = 0.1 * tax;
tax+=sc;
}
return tax;
}
void income::income_detail()
{
cout<<"\nBasic Salary is         : "<<BS;
cout<<"\nDearness Allowance is   : "<<DA;
cout<<"\nMonthly Deduction is    : "<<deduction();
cout<<"\nTotal Monthly Salary is : "<<pay_sal();
cout<<"\nTotal Annual Salary is  : "<<12*pay_sal();
cout<<"\nAnnual Payable Tax is   : "<<calc_tax();
}
int main()
{


double bs,da;
income s;
//Initializing Income
cout<<"\nEnter Basic Pay : ";
cin>>bs;
cout<<"\nEnter Dearness Allowance : ";
cin>>da;
s.initialize(bs,da);
s.income_detail();
return 0;
}


Output:
Enter Basic Pay : 5000
Enter Dearness Allowance : 5
Basic Salary is  : 5000
Dearness Allowance is   : 5
Monthly Deduction is    : 800
Total Monthly Salary is : 5755
Total Annual Salary is  : 69060
Annual Payable Tax is   : 13812
Process returned 0 (0x0)   execution time : 36.459 s
Press any key to continue.

Example of Stack in C++

#include<iostream.h>
#include<conio.h>
const int size=10;
class stack
{
int arr[size];
int top;
   public:
void initialize();
void push(int);
int pop();
int stack_top();
void show();
};
void stack::initialize()
{
top=-1;
}
void stack::push(int n)
{
if(top!=size-1)
arr[++top]=n;
else
cout<<"\nOverflow!!!\n";
}
int stack::pop()
{
if(top!=-1)
return arr[top--];
else
{
cout<<"\nUnderflow!!!\n";
return NULL;
}
}
int stack::stack_top()
{
if(top==-1)
{
cout<<"\nStack is Empty!\n";
return NULL;
}
else
return arr[top];
}
void stack::show()
{
if(top==-1)
cout<<"\nEmpty Stack!!!\n";
else
{
for(int i=0;i<=top;i++)
cout<<arr[i]<<" ";
cout<<"<--TOP\n";
}
}
int main()
{
int p;
stack s1;
s1.initialize();
//Pushing Values
cout<<"\nPUSHING 3,5,7 onto stack\n";
s1.push(3);
s1.push(5);
s1.push(7);
cout<<"\nStack is : ";
s1.show();
//Show Top Value
cout<<"\nTop of Stack is :"<<s1.stack_top()<<endl;
//Popping Values
cout<<"\nPOPPING\n";
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
//Show Top of Stack
p=s1.stack_top();
if(p!=NULL)
cout<<"Top of Stack is : "<<p<<endl;
return 0;
}

Output:
PUSHING 3,5,7 onto stack
Stack is : 3 5 7 <--TOP
Top of Stack is :7
POPPING
Popped out value is : 7
Popped out value is : 5
Popped out value is : 3
Stack is Empty!


Complex number

#include<iostream.h>
#include<conio.h>
class complex
{
                float real;
                float imag;
   public:
                void getnum();
                void putnum();
                void sum(complex,complex);
                void dif(complex,complex);
};
void complex::getnum()
{
                cout<<"\nEnter the real part : ";
                cin>>real;
                cout<<"\nEnter the imaginary part : ";
                cin>>imag;
}
void complex::putnum()
{
                cout<<real;
                if(imag<0)
                                cout<<imag<<"i\n";
                else
                                cout<<"+"<<imag<<"i\n";
}
void complex::sum(complex a,complex b)
{
                real=a.real+b.real;
                imag=a.imag+b.imag;
}
void complex::dif(complex a,complex b)
{
                real=a.real-b.real;
                imag=a.imag-b.imag;
}
int main()
{
                complex c1,c2,c3,c4;
                cout<<"\nEnter first complex number\n";
                c1.getnum();
                cout<<"\nEnter second complex number\n";
                c2.getnum();
                //Sum of two inputted numbers
                c3.sum(c1,c2);
                cout<<"\nThe Sum is : ";
                c3.putnum();
                //Difference of two inputted numbers
                c4.dif(c1,c2);
                cout<<"\nThe Difference is : ";
                c4.putnum();


Output:
Enter first complex number
Enter the real part : 4
Enter the imaginary part : 5
Enter second complex number
Enter the real part : 3
Enter the imaginary part : 6
The Sum is : 7+11i
The Difference is : 1-1i

Friday 24 May 2013

What can PHP do?

PHP is server side web scripting language. So it is mainly work on server such as collect form data, generate dynamic page content, or send and receive cookies. 
In summarize we can say that PHP can do the following things:
  • Creating dynamic web pages
  • Collecting form data
  • Send or receive cookie
  • Insert/update records of database
  • Prevent unauthorized access
  • Decode the data(i.e. encrypt)
If we talking the about the filed/area of php working, then php also used in below category:
  1. Server Side Script: This is tradition and mostly used php filed. Here we need code-editor, server and browser(showing result). If these three thing are available in your system, then your php program run successfully.
  2. Command line script: PHP code run through command without server and browser like as DOS command. To doing this we need only PHP parser. Example: This is executed using cron(on *nix or Linux) or task scheduler(Windows).
  3. Desktop application: PHP can desktop application. But this filed is rarely used in modern era because the php is not good with desktop application.

What is PHP

The PHP stands for PHP Hypertext Preprocessor.
But in past PHP originally stands for Personal Home Page.

PHP is server side scripting language, usually used to create web applications.

PHP is invisible to the end user. So we can say that php has nothing to do with  page design(i.e. layout), events.

In summarize, we can say that "php is an open source source, server-side, HTML-embedded web-scripting language that is compatible with all the major web servers."

To run the php code, we need a server. There are two options to setup the server as:


1. Online server - It is not good for bignners and it is not free, To setup this type server, we need to purchase a hosting space where we write our code and test them online.

2. Offline server - It is good for beginners and it is 100% free to download & install and used. There are three types offline server for php as:
  1. wamp
  2. lamp
  3. xampp 
The current php version: php 5.4.15 (current stable)


Friday 17 May 2013

Count digit repetition in number range

Q. Write a C program to count the no. of occurences of n's in a given number range.

For example:
random numbers : 45,78,2,65,489,6,2,6,55,9
find out how many times the digit 2 repeat?
2's digit repeat 2 times.

Ans.

/*c program to count the no. of occurences of n's in numbers range*/
#include<stdio.h>
int main()
{
 int arr[10],i,num,r,s,c=0;
 for(i=1; i<=10; i++)
 {
  printf("Enter %d number : ",i);
  scanf("%d"&arr[i]);
 }
 printf("\nEnter search number : ");
 scanf("%d", &s);

 for(i=1; i<=10; i++)
 {
  num = arr[i];
  while(num>=1)
  {
   r=num%10;
   if(r==s)
     c++;
   num=num/10;
  }
 }
 printf("\nThe digit %d is repeat in entered number is %d times",s,c);
 getch();
 return 0;
}

The output of above program would be:


Output of count repeate digit  in number range C program
Figure: Screen shot for count repeate digit
 in number range C program

Wednesday 15 May 2013

Nested Pyramid

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

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

Ans.

/*c program for nested pyramid*/
#include<stdio.h>
int main()
{
 int r,c,num=3,n;
 n=num;
 for(r=1; r<=num; r++,n--)
 {
   for(c=1; c<=r; c++)
       printf("*");
   printf(" ");

   for(c=n; c>=1; c--)
       printf("*");
   printf(" ");
   for(c=n; c>=1; c--)
       printf("*");
   printf(" ");
   for(c=1; c<=r; c++)
       printf("*");
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


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

Dignal Character Pyramid

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

 a b b b
 b a b b
 b b a b
 b b b a

Ans.

/*c program for alternate character pyramid*/
#include<stdio.h>
int main()
{
 char r,c,ch='d';

 for(r='a'; r<=ch; r++)
 {
  for(c='a'; c<=ch; c++)
  {
    if(r==c)
       printf(" a");
    else
       printf(" b");
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of dignal equal pyramid C program
Figure: Screen shot for dignal equal pyramid C program

Related program:

1. Print the following dignal number pyramid:

  1 2 2 2
  2 1 2 2
  2 2 1 2
  2 2 2 1

Alternate Number Pyramid

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

1222
2122
2212
2221

Ans.

/*c program for alternate number pyramid*/
#include<stdio.h>
int main()
{
 int r,c,num=4;
 for(r=1; r<=num; r++)
 {
  for(c=1; c<=num; c++)

  {
    if(c==r)
      printf("1");
    else
      printf("2");
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


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

Related program:

1. Print the dignal character pyramid as:

 a b b b
 b a b b
 b b a b
 b b b a