The Best Programming Platform

Simple way to create amazing programs for your project.

Saturday, July 9, 2016

C++ program to find natural log of 2 by solving a term step by step guide (logic explained) 

C++ program to find natural log of 2 by solving a term

This is a C++ program to find natural log of 2 by solving following term :
1/1 - 1/2 + 1/3 - 1/4 + 1/5. .. 
Steps
User side:
1: User asked to enter the numbers of terms.
2: User displayed with the natural log.
Developer side
1: Declare variables that are going to be used in program.
2: Input the numbers of terms from user and store it in a variable 'n'.
3: Initialize the variable 'm' with -1.
4: Start a while loop from one to numbers of terms entered by user.
5: In the loop assign 'm' with m*-1.
6: Store log in the variable 'log' using following statement  log=log+(m/i); and increase the counter by 1. While loop ends here.
7: Display the log outside the loop .
8: Program ends here.

Code:
#include<iostream>
using namespace std;

int main()
{      //Variable declaration
       int i=1;
       float n,m,log=0;
       //heading
       cout<<"\t\"This program is to find natural log of 2\""<<endl;
       //input of number of terms
       cout<<"Enter the number of terms"<<endl;
       cin>>n;
       m=-1;
       //while loop to solve the series
       while(i<=n)
       { m=m*-1;
       log=log+(m/i);
       i++;
       }
       //output of result
       cout<<"The natural log of 2 is equal to  "<<endl<<log<<endl;

       system("pause");
       return 0;
}


Output:

Wednesday, July 6, 2016

C++ program to reverse an array within given indexes using recursion (logic explained)

C++ program to reverse an array within given indexes using recursion.

Here is the C++ program to reverse an array within given indexes using recursion. It takes inputs two indexes from user and convert the array between that indexes. You all should have in mind how it works exactly. Here is the algorithm.

  Steps
User side
1: User asked to enter size of array
2: User asked to enter array elements
3: User asked to enter two indexes (second should be greater than first)
Developer side
1: Declare variables that are going to be used in program 
2: Input size of array
3: Input elements of array using for loop 
4: Input start and stop indexes 
5: Pass array , start and stop indexes to a function reverse
6: Check if left index is less than right
7: if above condition holds swap elements using swap function and
8: and call reverse function and pass array , left index +1 and right index -1
9: Back in the main show the array 
10: Program ends here.
Code:


#include<iostream>
using namespace std;

void swap(char *array, int leftIndex, int rightIndex);
void reverse(char *array, int leftIndex, int rightIndex);

int main(){
       int  elementCount, counter,start,stop;
       char inputArray[500];
       cout<<"Enter number of elements in array: ";
       cin>>elementCount;

       cout << "Enter chars  ";

       for (counter = 0; counter < elementCount; counter++){
              cin>>inputArray[counter];
       }
       cout << "Enter start index";
       cin >> start;
       cout << "Enter stop index";
       cin >> stop;
       reverse(inputArray, start, stop );

       /* Print Reversed array */
       cout<<"Reversed Array\n";
       for (counter = 0; counter < elementCount; counter++){
              cout<<inputArray[counter];
       }
       system("pause");
      
       return 0;
}
/*
*  Function to swap two numbers in array
*/

void swap(char *array, int leftIndex, int rightIndex){
       int temp;
       temp = array[leftIndex];
       array[leftIndex] = array[rightIndex];
       array[rightIndex] = temp;
}
/*
* Function to reverse an Array using recursion
*/
void reverse(char *array, int leftIndex, int rightIndex){
       if (NULL == array){
              cout<<"Invalid Input";
              return;
       }
       /*
       * Swap left and right elements and recursively call reverse
       * function on subArray [leftIndex + 1, rightIndex - 1]
       */
       if (leftIndex < rightIndex){
              swap(array, leftIndex, rightIndex);
              reverse(array, leftIndex + 1, rightIndex - 1);
       }
}


 

Output:



Sunday, July 3, 2016

C++ code to calculate body fat of a person (Logic explained)

Here is the C++ code to calculate body fat of a person  . It works by inputting some necessary data of person. You all should have in mind how it works exactly. Here is the algorithm.

  Steps
User side
1: User asked for his/her gender
2: User asked to enter body-weight 
3: User asked to enter wrist measurement 
4: User asked to enter waist measurement
5: User asked to enter hip measurement
6: User asked to enter forearm measurement
7: User displayed body fat
8: User displayed body fat percentage
9: Program ends here
Developer side
1: Declare variables that are going to be used in program .
2: Input gender from user (because there are different formulas for different genders)
3: If User is woman follow this
 Input enter body-weight (Calculate  A1=(bodyweight*0.732)+8.987;)
 Input wrist measurement (Calculate  A2=(wrist)/3.140;
 Input waist measurement (Calculate   A3=(waist)*0.157;)
 Input  hip measurement (Calculate   A4=(hip)*0.249;)
 Input  forearm measurement (Calculate   A5=(forearm)*0.434;)
  Know calculate following :  B=A1+A2-A3-A4+A5;
                         bodyfat=bodyweight-B;
                     
4:  If user is man then follow this
    Input body-weight (Calculate  A1=(bodyweight*1.082)+94.42;)
    Input wrist measurement (Calculate A2=(wrist)*4.15;)
    Know calculate following 
                            B=A1-A2;
                           bodyfat=bodyweight-B;
5: Calculate percentage body fat (To calculate percentage body fat use this formula  bodyfatparcentage=(bodyfat*100)/bodyweight;)

5: Output results 
6: Program ends here.
Here is the C++ code.

Code:

#include<iostream>

using namespace std;
void main()
{      char gender;
       float bodyweight,wrist,waist,hip,forearm;
       float A1,A2,A3,A4,A5,B,bodyfat,bodyfatparcentage;
       cout<<"\t\tThis program is to calculate the body fat of a person"<<endl;
       cout<<"If you are woman than press \'w\' . If you are man than press \'m\'  "<<endl;
       cin>>gender;
       switch(gender)
       {
                     case 'w':
             
                     cout<<"Enter your body weight ";
                     cin>>bodyweight;
                     A1=(bodyweight*0.732)+8.987;
                     cout<<"Enter your wrist measurement ";
                     cin>>wrist;
                     A2=(wrist)/3.140;
                     cout<<"Enter your waist measurement ";
                     cin>>waist;
                     A3=(waist)*0.157;
                     cout<<"Enter your hip measurement ";
                     cin>>hip;
                     A4=(hip)*0.249;
                     cout<<"Enter your forearm measurement ";
                     cin>>forearm;
                     A5=(forearm)*0.434;
                     //calculting B
                     B=A1+A2-A3-A4+A5;
                     bodyfat=bodyweight-B;
                     cout<<"Body Fat is equal to = "<<bodyfat<<endl;
                     bodyfatparcentage=(bodyfat*100)/bodyweight;
                     cout<<"Body Fat percentage is equal to = "<<bodyfatparcentage<<"%"<<endl;
             
              break;
       case 'm':
                    
                           cout<<"Enter your body weight ";
                           cin>>bodyweight;
                           A1=(bodyweight*1.082)+94.42;
                           cout<<"Enter your wrist measurement ";
                           cin>>wrist;
                           A2=(wrist)*4.15;
                           B=A1-A2;
                           bodyfat=bodyweight-B;
                           cout<<"Body Fat is equal to = "<<bodyfat<<endl;
                           bodyfatparcentage=(bodyfat*100)/bodyweight;
                            cout<<"Body Fat percentage is equal to = "<<bodyfatparcentage<<"%"<<endl;
                     break;
       default:
        cout<<"wrong input"<<endl;
       }
       system("pause");
}



Output:



Friday, July 1, 2016

C++ program to find roots of quadratic equation ax2 + bx + c=0 (with algorithm)

Here is the C++ code to find roots of quadratic equation ax2  + bx + c=0 . It takes input for a, b and c and find the roots. You all should have in mind how it works exactly. Here is the algorithm.

  Steps
User side
1: User asked to enter values for a, b and c.
2: Screen displays disc.
3: If discernment is greater than zero Roots are real and unequal
4: If discernment is less than zero Roots are imaginary 
5: If discernment is equal to zero Roots are equal
Developer side
1: Declare variables that are going to be used in program .
2: Input variables a,b and c
3: Place an if statement to check if all are non-zero
4:  Place an if statement to check if all are positive
5: If above two conditions are true then calculate discernment using following statement disc=(b*b)-4*a*c;
6: If discernment is greater than zero Roots are real and unequal and calculated using following formulas root1=(-b+sqrtf(disc) /(2*a));
         root2=(-b-sqrtf(disc) /(2*a)); 
7: If discernment is less than zero Roots are imaginary and calculated using following formulas 
root1=sqrt(disc)<<" /"<<2*a;
root2=sqrt(disc)<<" /"<<2*a
8: If discernment is equal to zero Roots are equal and calculated using following formulas 
root1=-b/(2*a);
root2=root1;
9: Program ends here.
Here is the C++ code.

Code:

#include<iostream>
#include<math.h>
using namespace std;
void main()
{      float a,b,c;
       float disc,root1,root2;
       int i=-1;
       cout<<"Find roots of quadratic equation ax2  + bx + c=0 "<<endl<<"where a b and c are scalers"<<endl;
       cout<<"Enter value of a"<<endl;
       cin>>a;
       cout<<"Enter value of b"<<endl;
       cin>>b;
       cout<<"Enter value of c"<<endl;
       cin>>c;
       if(a==0||b==0||c==0)
              {
                     cout<<"The value cannot be zero"<<endl;
              }
       else if(a<0||b<0||c<0)
                     {
                           cout<<"Values must be greater than Zero"<<endl;
                     }
              else { disc=(b*b)-4*a*c;
                           //disc>0
                           if(disc>0)
                                  {      cout<<"disc>0 so"<<endl;
                                         cout<<"Roots are real and unequal"<<endl;
                                         root1=(-b+sqrtf(disc) /(2*a));
                                         root2=(-b-sqrtf(disc) /(2*a));
                                         cout<<"First root is = "<<root1<<endl;
                                         cout<<"Second root is = "<<root2<<endl;
                                  }
                           else //disc<0
                                  if(disc<0)
                                         {      disc=(-1)*disc;
                                                cout<<"disc<0 so"<<endl;
                                                cout<<"Roots are imaginary "<<endl;
                                                cout<<"First root=(-"<<b<<"+i*"<<sqrt(disc)<<" /"<<2*a<<")"<<endl;
                                                cout<<"Second root=(-"<<b<<"-i*"<<sqrt(disc)<<" /"<<2*a<<")"<<endl;
                                               
                                           
                                               

                                         }
                                  else { //disc=0
                                                cout<<"disc=0 so"<<endl;
                                                cout<<"Roots are equal"<<endl;
                                                root1=-b/(2*a);
                                                root2=root1;
                                                cout<<"First root is = "<<root1<<endl;
                                                cout<<"Second root is = "<<root2<<endl;
                                          }
                          
                      }
       system("pause");
}

Output:


Featured Documents

C++ Programs

View more

Tips and Tricks

View more

Softwares

View more

Hacking Tutorials

View more

Java Programs

View more

Projects

View more