Code for Logarithm using C++
This is a simple C++ program which calculte the Logarithem without any builtin function of the compiler header files. It will perform three calculations.
- Natural Log (Ln)
- Log with Base 10(Log10)
- Log of your desired Base (LogA^x)
#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
//////////////////////// Class For Logar
// ithemic Functions \\\\\\\\\\\\\\\\\\\
class logarithem
{
protected:
long ans;
public:
logarithem():ans(0)
{
/* cout<<"Enter the value of 'X':";
cin>>ans; */
}
double loga(double);
double ln(double x);
double checking(double);
};
//////////////// For Log10 \\\\\\\\\\\\\
// \\\\\\\\\\\
double logarithem::loga(double b)
{
double LOG10=0,c;
c=ln(b);
LOG10=c/2.30258509299404;
return LOG10;
}
/////////////////// For Natural Log \\\\
// \\\\\\\\\\\\\\\\\\\\
double logarithem:: ln(double x)
{
double lnX=0.000001,check;
do{
lnX=lnX-(exp(lnX)-x)/exp(lnX);
check=checking(lnX);
}while(check!=0);
return lnX;
}
////////////// Check Function \\\\\\\\\\
// \\\\\
double logarithem::checking(double x)
{
static double x1=0;
double x2;
if(x1==0)
{
x1=x;
return 1;
}
else
{
x2=x1-x;
if(x2<0)
x2*=-1;
x1=x;
return x2;
}
}
////////////////////////////////////////
////////////////////////////////
void main()
{
clrscr();
logarithem a;
double b=0,k=0;
int opt1;
char ch;
clrscr();
highvideo();
textcolor(BLACK);
textbackground(3);
one:
cout<<" ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß"<<endl;
cout<<"\t\t\tÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ";
cout<<"\n\t\t\tÛ Student Logarithem Panel Û "<<endl;
cout<<"\t\t\tÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ"<<endl;
cout<<"\nÉÍÍÍÍÍÍÍÍÍËÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\nº Option #º Logarithemic Function\t\t";
cout<<"\nÌÍÍÍÍÍÍÍÍÍÎÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\nº1.º for Natural Log (ln) ";
cout<<"\nº2.º for Log of Base 10";
cout<<"\nº3.º for Log of x with Base a, Log a^x = Logx/Loga";
cout<<"\nº5.º for Exit";
cout<<"\nº º";
cout<<"\n\n\n\tEnter Your Option:- ";
cin>>opt1;
switch(opt1)
{
case 2:
cout<<"\n\nEnter the value of X : ";
cin>>b;
cout<<"\n\n\t( Log_10^"<<b<< " is "<<a.loga(b)<<" )";
cout<<endl<<"\n\tPress any key to go back....";
ch=getch();
if(ch=='\r')
clrscr();
goto one;
case 1:
cout<<"\n\nEnter the value of X"<<"\t";
cin>>b;
cout<<"\n\n\t( Ln "<<b<<" : "<<a.ln(b)<<" )";
cout<<endl<<"\n\tPress any key to go back....";
ch=getch();
if(ch=='\r')
clrscr();
goto one;
case 3:
cout<<"\n\nEnter the value of a :"<<"\t";
cin>>b;
cout<<"\nEnter the value of x :"<<"\t";
cin>>k;
cout<<"\n\n\t( Log a^x = Logx/Loga "<<b<<" : "<<a.ln(k)/a.ln(b)<<" )";
cout<<endl<<"\n\tPress any key to go back....";
ch=getch();
if(ch=='\r')
clrscr();
goto one;
case 5:
delay(100);
exit(0);
default:
cout<<"\n\n\tPlease! Enter the options shown above ...";
cout<<endl<<"\n\tPress any key to go back....";
ch=getch();
if(ch=='\r')
clrscr();
goto one;
}
}




