About  Contact


Programing with if-else in C++

Program-14
#include <iostream.h>
void main(){
    float price, tot, dis, net;
    int qty;
    cout<<"Enter unit price:";
    cin>>price;
    cout<<"Enter quantity purchased:";
    cin>>qty;
    tot=price*qty;
    if(tot>=5000){
    	dis=tot*10/100;
    }
    else if(tot>=4000){
    	dis=tot*5/100;
    }
    else if(tot>=3000){
    	dis=tot*3/100;
    }
    else{
    	dis=0;
    }
    net=tot-dis;
    cout<<"Total Amount="<<tot<<"\n";
    cout<<"Discount Amount="<<dis<<"\n";
    cout<<"Net Amount payable="<<net;
}
Program-15
#include <iostream.h>
#include <math.h>
void main(){
    float a, b, c, d, r1, r2;
    cout<<"Enter coefficient of square term:";
    cin>>a;
    cout<<"Enter coefficient of x";
    cin>>b;
    cout<<"Enter the constant-c";
    cin>>c;
    d=b*b-4*a*c;
    if(d<0){
    	cout<<"Roots are Imaginary"
    }
    else{
        r1=(-b+sqrt(d))/(2*a);
        r2=(-b-sqrt(d))/(2*a);
        cout<<"Root-1="<<r1<<"\n";
        cout<<"Root-2="<<r2;
   }
}
Output of Program-15
expression


Logical operator : While programing sometimes we have to check two or more conditions together. In this situation we use logocal operators to join the conditions ( a condition is a relational expression ). In C++ we have the following logical operators.
Operator Symbol used in C++
And &&
Or ||
Not !

Note: The "&&" operation needs two operands (it is binary operation), means we can write a>10 && b<20, two conditions are required for "and" operation. The "and" operation given in the example will evaluates "true" if the two individual conditions evaluate "true" individually, otherwise the "and" operation evaluates "false". This can be understand using simple english statement as - when we say "Mr Sharma AND Mr. Sarkar will go"- this statement will be true when both the persons go, otherwise it is false. Same the case of logical AND operation.

The "||" operation also needs two operands, and it evaluates "true" if any one of the two conditions evaluates true. Means it evaluates false only if both the conditions are false.

The "!" operator needs only one operand ( it is unary operation ), so we can write as !(a>10), this operation will negate the evaluated value of the condition. If "a>10" evaluates true then "!(a>10)" evaluates "false" and vice-versa.
Q16.Write a C++ program to accept three numbers then display the greatest one.
Q17.Write a program to accept a number then check if it is within the range 1 to 30 ( both the numbers inclusive) or not.
Program-16
#include <iostream.h>
void main(){
    float a, b, c;
    cout<<"Enter First No:";
    cin>>a;
    cout<<"Enter Second No";
    cin>>b;
    cout<<"Enter Third No";
    cin>>c;
    if(a>b && a>c){
    	cout<<"The greatest no is="<<a;
    }
    else if(b>a && b>c){
		cout<<"The greatest no is="<<c;
    }
    else{
    	cout<<"The greatest no is="<<c;
    }
}
Program-17
#include <iostream.h>
void main(){
    float a, b, c;
    cout<<"Enter the No:";
    cin>>a;
    if(a>=1 && a<=30){
    	cout<<"The no. is within the range";
    }
    else{
    	cout<<"The no. is not within the range";
    }
}



Disclaimer: We have taken otmost care to keep the information error free. The site is not responsible for any kind of damage of whatsoever nature.
©Copy right reserved