Showing posts with label basic tutorial. Show all posts
Showing posts with label basic tutorial. Show all posts

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 ;
}






























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++








Monday, 4 June 2018

Lesson 3-Conditional statements..if statement, If else statement, nested if statement, nested if-else statement -Coding overdose

June 04, 2018 0
1.If Statement

it is a conditional statement.. The if statement is used to execute the code, only when a condition is true.

if (condition) 



statements

}



Example 1. checking if a number is greater than 100


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

{
 int a;

   cout << "enter the number \n" ;
        cin >> a;


    if (a>100)
    {
        cout<<"yes the number is greater than 100" ;
    }


    return 0;


}














































similarly you can use

>=       greater than or equal to 
<=       less than or equal to
equal to==
!=        not equal to




Example 2. checking the greater number out of two

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

{
    int a;
    int b;

    cout<< "enter a number a\n" ;
    cin>> a;

    cout << "enter another number b\n" ;
    cin>> b;

    if (a>b)
    {
        cout<< "a is greater than b";
    }


    if (b>a)
    {
        cout<< "b is greater than a" ;
    }


    if (b==a)
    {
        cout<< "both are equal" ;
    }

    return 0;

}

























































2.If-Else Statement

if (condition)

{ statements}
else
{statements}



Example 1. fail/pass in an exam

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

{

    int marks;


    cout << "enter your marks\n" ;
    cin>> marks;


    if (marks<33)

    {
        cout<< "failed";
    }

    else
    {
        cout<<"passed\n" << "congratulations";


    }

    return 0;
}




















































3.Nested If Statements


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


{
    int age;

    cout <<"enter ur age" ;
    cin>> age;

    if( age>14)

        {

                if (age>=18)
                 {
                cout<< "adult" ;
                  }


                else
                    {
                    cout <<"teenager";
                    }


          }

return0;

}























































the above program wont give any output for age less than 14...
for that, you will need another else statement.


4.Nested If-Else Statements

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

{
    int age;
    cout <<"enter ur age \n" ;
    cin>> age;

    if( age>=13)

        {
                    if (age>=18)
                     {
                    cout<< "adult" ;
                      }

                         else
                        {
                         cout <<"teenager";
                         }
          }


          else { cout<<"you are a kid";}

return 0;

}




















































In the next blog,you will get to learn about loops.