If If....else statement in C++

 If  in CPP


if-block are executed only if -expression contain TRUE value. if statement consists of a boolean expression followed by one or more statements.

Syntax:   

if (expression is true)
{
    action1;
}
action2;
action3;


Program: Write a program in C++ to find the greatest number in two numbers with the help of if

Code:
#include<iostream>
using namespace std;
int main()
{
int num1, num2;
 cout<<"Enter the two number ";
 cin>>num1;
 cin>>num2;
 if(num1>num2)
 cout<<"Num1 is greatest number";
 if(num2>num1)
 cout<<"num2 is greatest number";
if(num1==num2)
  cout<<"number are equal";
return 0;
}
 
Output:
Enter the two number 25
20
Num1 is greatest number
--------------------------------
Enter the two number 10
32
num2 is greatest number
--------------------------------
Enter the two number 10
10
number are equal
--------------------------------



Q.n: What will be the Output following.

#include<iostream>
using namespace std;
int main()
{
int i ;
if(cout<<0)
{
i=3;
}
if(0)
i=5;
printf("%d",i);
return 0;
}

a)3           b) 03            c)5                d)error

Ans:b)03  


If.. else

when more than one condition in same program then we used if else statement. If the condition is true then if block is executed otherwise else block is executed.


Syntax:

if(expression is true) 
{
       action1;
}
else 
{
        action2;
}
action3;
action4;


Program: Write a program to find the largest of three number using if else  statement 
Code:
#include<iostream>
using namespace std;
int main()
{
int num1,num2,num3;
cout<<"Enter three number :";
cin>>num1;
cin>>num2;
cin>>num3;
if(num1>num2 & num1>num3)
{
cout<<"Number1 is largest";
}
else if(num2>num3)
{
cout<<"Number2 is largest";
}
else
cout<<"Number3 is largest";
}

OUTPUT:
Enter three number :18
16
12
Number1 is largest
--------------------------------























Comments

Popular posts from this blog

CYBER SECURITY & CRACKING PASSWORDS

"The Dark Side of Social Media: How It Fuels Digital Addiction and Impacts Mental Health"

Online Platforms to Learn Coding