Stats

Thursday, 2 August 2012

C program for electricity bill


#include<iostream>
using namespace std;
#define cost1 0.60
#define cost2 0.80
#define cost3 0.90
#define min 50
#include<conio.h>
void main()
{
char name[10][20];
float amount[10];
int units[10],n;

cout<<"Enter Number of Customers: ";
cin>>n;

cout<<"Enter Names and Number of Units Consumed\n";
for(int i=0;i<n;i++)
{
cout<<"-------------------------\n";
cout<<"Enter Customer Name: ";
cin>>name[i];
cout<<"Enter Number of units= ";
cin>>units[i];
cout<<"-------------------------\n";

if(units[i]<=100)
{
amount[i]=(units[i]*cost1)+min;
}
else if(units[i]>100&&units[i]<=300)
{
amount[i]=(((units[i]-100)*(cost2))+(100*cost1))+min;

}
else if(units[i]>300)
{
amount[i]=(((units[i]-300)*(cost3))+(200*cost2)+(100*cost1))+min;
amount[i]=amount[i]+(amount[i]*(15.0/100.0));
}
}

cout<<"**************BILL**************\n";
cout<<"Name\t\t"<<"Units\t\t"<<"Amount\n";
for(int i=0;i<n;i++)
{
cout<<name[i]<<"\t\t"<<units[i]<<"\t\t"<<"Rs."<<amount[i];
cout<<"\n";
}
getch();
}









C program to implement Newton Raphson method


1)F(x)=x^2-3x+2

#include<stdio.h>
#include<math.h>
#include<conio.h>
#define e 0.001
#define F(x) (2*x)-3
float frac(float a)
{
       float f1;
       f1=a*a-3*a+2;
       return f1;
}
int main()
{
       float x1,x2,f1=0,f2,er,d;
       printf("F(x) = x^2-3x+2\n\n");
       printf("Enter the value of x1: ");
       scanf("%f",&x1);
       printf("\nx1 = %f",x1);
       printf("\n________________________________________________________________________________\n");
        
       printf("     x1      |       x2      |      f1       |       f'1      |  |(x2-x1)/x2|  |  \n");
       printf("--------------------------------------------------------------------------------\n");
       do
       {
              f1=frac(x1);
              d=F(x1);
              x2=x1-(f1/d);
              er=fabs((x2-x1)/x2);
           printf("  %f   |    %f   |     %f  |     %f  |      %f  |   \n",x1,x2,f1,d,er);
              x1=x2;
       }
       while(er>e);
       printf("--------------------------------------------------------------------------------\n\n");
       printf("\n  Root of the equation is: %f",x2);
       getch();
}