Sunday 10 June 2018

Lesson 6-Logic operations, And operation, Or operation, Not Operation - coding overdose

June 10, 2018 0
Logic operators in c++

&& - and operator

||- or operator
! - not operator


Program 1:- To find greatest among three numbers



#include <iostream>
using namespace std;
int main ()

{


    int a,b,c;


    cout<<"enter any three numbers a,b,c"<<endl;

    cin>>a>>b>>c;


    if(a>=b && a>=c)

    {
        cout<<"a is greatest number";
    }


      if(b>=a && b>=c)

    {
        cout<<"b is greatest number";
    }



      if(c>=a && c>=b)

    {
        cout<<"c is greatest number";
    }


    return 0;



}

















Program 2:- 
The candidate is selected if his age is >= 18 years and height is >=172cm respectively.


#include <iostream>
using namespace std;
int main ()

{

    float age,height;
    cout<<"enter age and height"<<endl;
    cin>>age>>height;



      if(age>=18 && height>=172)
    {
        cout<<"congrats!! candidate selected";
    }


else    {
        cout<<"sory.. candidate not selected";
    }

    return 0;


}



























Program 3:- 













Lesson 5-Mathematical functions in C++- Coding overdose

June 10, 2018 0

Mathematical functions


Required header:  #include <cmath>
          
sqrt(x) square root
 sin(x)sine of x (in radians)
asin(x)Inverse Sine of x 
cos(x)cosine of x (in radians)
acos() Inverse cosine of x
tan(x)tangent of x (in radians)
atan() Returns Inverse tangent a Number
abs()returns absolute value of an argument
log(x)natural logarithm of x (base e)
log10(x)logarithm of x to base 10
exp(x)  exponential function
pow(x,y)x raised to power y
cbrt()Computes Cube Root of a Number

1.Program to calculate roots of a quadratic equation

#include <iostream>
#include <cmath>
using namespace std;
int main ()

{
    float a,b,c,d,real,imag;


    cout<<"enter a,b,c"<<endl;
    cin>>a>>b>>c;


d=(b*b)-(4*a*c);

cout<<"The given equation is"<<a<<"x^2+"<<b <<"x+"<<c <<endl;
cout<<"The discriminant is" << d <<endl;

if(d>0)
{cout<<"The given equation has two real distinct roots"<<endl;
cout<<"first root is x=" <<(-b+sqrt(d))/(2*a);
cout<<"second root is x=" <<(-b-sqrt(d))/(2*a);
}


if(d==0)
{cout<<"The given equation has two same roots"<<endl;
cout<<"it has two same roots equal to x= " <<-b/(2*a);

}

if (d<0)
{cout<<"The given equation has two imaginary roots"<<endl;
real= -b/(2*a);
imag=(sqrt(-d))/(2*a);
cout<<"first root is x=" <<real <<"+"<< imag<<"i";
cout<<"second root is x=" <<real <<"+"<< imag<<"i";
}

    return 0 ;
}






























C++ program examples:- Mathematics

June 10, 2018 0
1.Program to calculate area of a triangle..

Concepts you should know to write this program
:-Float
Formula of area of triangle = 1/2 base x height of trainge

#include <iostream>
using namespace std;
int main ()

{



    float b,h,a;


    cout<<"enter base of triangle"<<endl;

    cin>> b;

cout <<"enter height of triangle"<<endl;

cin>>h;

a=(b*h)/2 ;


cout <<"The area of the triangle is " <<a <<" units";


return 0;

}





























































2.Program to calculate area of circle

Formula of area of triangle = pi *r* r



#include <iostream>
using namespace std;
int main ()

{


    float a,r;


    cout<<"enter radius"<<endl;

    cin>> r;

a=3.14*r*r;


cout <<"THe area of the circle is" <<a <<" units";


return 0;

}































3.Program to find discriminant of a quadratic equation


Concepts you should know to write this program:-


  • Float
  • If else statements
  • Formula:= d is discriminant and is equal to 
    (b^2) -(4*a*c)




#include <iostream>
using namespace std;
int main ()

{
    float a,b,c,d;

    cout<<"enter a";
    cin>>a;

    cout<<"enter b";
    cin>>b;

    cout<<"enter c";
    cin>>c;

d=b*b-4*a*c;

cout<< "discriminant is"<< d ;


    return 0 ;


}




















































4.Program to find sum of arithmetic progression



#include <iostream>
using namespace std;
int main ()

{
int a,d,n;
float sum;

cout<< "enter the first number of ap series";
cin>>a;


cout<< "enter the difference between the terms";
cin>>d;


cout<< "enter the total terms in the ap series";
cin>>n;

sum=(n*(2*a+(n-1)*d)) /2;

cout <<"The sum of ap series is"<<sum;

    return 0;

}




















































5.Program to find whether number iss even or odd


#include <iostream> using namespace std; int main()

{ int a; cout << "Enter the number: ";
cin >> a; if (a % 2 == 0) { cout << "The given number is EVEN" << endl; }


else { cout << "The given number is ODD" << endl; }
return 0; }

































Wednesday 6 June 2018

Lesson 4-C++ Loops :- While loop, For loop, Do-While loop - Coding overdose

June 06, 2018 0
in programming languages, loops are used to execute a set of statements repeatedly, until a particular condition is satisfied.

There are 3 kind of loops in c++

1. While loop
2.For loop
3.Do-While loop


1.While loop


Example1:-counting from 0 to 9 (using while loop)




#include <iostream>
using namespace std;

int main()
{
    int x=0;

while (x<=9) {
        cout << x<<endl;
        x=x+1;
    }

    return 0;
}









































The same code can be executed by for loop as well



2.For LOOP


syntax :-

   for ( intitiation point ; termination point ;  increment or decrement)

    {condition;
}


Example 1:-counting from 0 to 9 (using for loop)


#include <iostream>
using namespace std;
int main ()

{
    int x=0;

    for ( x=0; x<=9; x=x+1)

    {cout<<x <<endl;
}


    return 0;

}








































Example 2:-Multiplication table

#include <iostream>
using namespace std;
int main()

{

    int a;
    cout << "Enter a positive integer: ";
    cin >> a;

    for (int b=1; b<= 10; b=b+1)

    {
        cout << a << " * " << b << " = " << a * b << endl;
    }

    return 0;

}


















































3.Do while loop


The same program that counted 0-9,,,, can also be made using this loop..

The especiality of do while loop is that it tests the condition at last,, whether its true or not..


syntax :-

do{statements;

}
while ( conditions) ;

dont forget the semicolon after while statement

Example 2:-counting from 0 to 9 (using do-while loop)
    


#include <iostream>
using namespace std;
int main()

{

int x=0;

do {cout<<x<<endl;
x=x+1;}

while (x<=9) ;

    return 0;

}















































in next lesson you will learn about mathematical functions

Mathematical functions in c++