Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  


Shop
CPP All : Simple If and If Else
Concept Learning Code Sheets

Simple If – Senior Citizen Test #1854

Taking age as input and finding if it is greater or equal to 60.

#include <iostream>
using namespace std;
int main( )
{
int age;
cout<<"Give your age :";
cin>>age;
if(age >=60)
cout<<"You are a senior citizen"<<endl;
return 0 ;
}

Check for non zero input value #1859

Take user input as a positive, negative or zero integer value and check if it is a non zero value.

#include <iostream>
using namespace std;
int main( )
{
int n;
cout<<"Enter a negative or positive integer:";
cin>>n;
if(n)
cout<<"Given number is a non zero value"<<endl;
return 0 ;
}

Making a number absolute using If #1861

A number is made absolute by removing its negative sign if present.

#include <iostream>
using namespace std;
int main( )
{
int n;
cout<<"Enter a negative or positive integer:";
cin>>n;
int on=n;
if(n < 0) n=- n;
cout<<"The absolute value of "
<<on<<" is "<<n;
return 0 ;
}

Weather Check – Multiple Instruction in If Scope #1863

We collect weather temperature from user and do a range check, then if true will do multiple instructions in the if block scope.

#include <iostream>
using namespace std;
int main( )
{
int good_low_limit=15;
int good_high_limit=28;
int t,good_weather=0;
cout<<"Input current weather temperature:";
cin>>t;
if(t>good_low_limit && t<good_high_limit)
{
good_weather=1;
cout<<"The weather is good."<<endl;
}
if(good_weather)
{
cout<<"Let's call Friends."<<endl;
cout<<"We all will have a great outing."<<endl;
}
return 0 ;
}

Checking for Digit using if else #1865

Collecting a char input and checking the input as a digit or a non-digit.

#include <iostream>
using namespace std;
int main( )
{
char d;
cout<<"Enter a character : ";
cin>>d;
if(d >= '0' && d<='9')
cout<<"You entered a digit"<<endl;
else
cout<<"Character you entered was not a digit"<<endl;
return 0 ;
}

Stock Finished Check – If Else #1872

Check if stock is finished using if-else selection condition

#include <iostream>
using namespace std;
int main( )
{
unsigned int stock;
cout<<"Enter items in stock : ";cin>>stock;
if(!stock)
cout<<"Items in stock are finished."<<endl;
else
cout<<"You have "<<stock<<" items in stock"<<endl;
return 0 ;
}

Check for alphabet – “Not an alphabet” as True Condition #1874

Checking for user’s entry being not an alphabet first, else tell that it is an alphabet

#include <iostream>
using namespace std;
int main( )
{
char c;
cout<<"Enter an alphabet : ";
cin>>c;
if((c<'A' || c>'Z') && (c<'a' || c>'z'))
{
cout<<"You did not enter an alphabet."<<endl;
}
else
{
cout<<"You did enter an alphabet."<<endl;
}
return 0 ;
}

Check for alphabet – “Is an alphabet” as True Condition #1876

Checking for user’s entry being is an alphabet first, else tell that it is not an alphabet

#include <iostream>
using namespace std;
int main( )
{
char c;
cout<<"Enter an alphabet : ";
cin>>c;
if((c>='A' && c<='Z') || (c>='a' && c<='z'))
{
cout<<"You entered and alphabet."<<endl;
}
else
{
cout<<"You did not enter an alphabet."<<endl;
}
return 0 ;
}

Applying Discounts – Only 1 Volume Slab #1878

Applying discounts on price based on only 1 quantity or volume of purchase based slab.

#include <iostream>
using namespace std;
int main()
{
float price,amount2pay;
int items,discount;
cout<<"Enter item price : ";cin>>price;
cout<<"Enter number of items purchased : ";
cin>>items;
if(items>=10)
{
discount=20;
cout<<"Based on your purchase volume "
"you get a special discount"<<endl;
}
else
{
discount=10;
cout<<"You get a regular discount"<<endl;
}
cout<<"Your discount is "<<discount<<"%"<<endl;
amount2pay=items*(price-price*discount/100);
cout<<"Amount to pay = "<<amount2pay<<endl;
return 0 ;
}

Comparing age of two friends of different age #1880

With this program we can find who is elder amongst the friends of different age.

#include <iostream>
using namespace std;
int main( )
{
int sa,fa;
cout<<"Enter your age : ";cin>>sa;
cout<<"Enter your friend's age : ";cin>>fa;
if(sa>fa)
cout<<"You are older than your friend."<<endl;
else
cout<<"You friend is older than you."<<endl;
return 0 ;
}

Teens party – empty if statement #1920

Understanding the behaviour of if when nothing is done when if condition is true.

#include <iostream>
using namespace std;
int main( )
{
int age;
cout<<"Tell your age : ";
cin>>age;
if(age<=19 && age >=13);
else
cout<<"Sorry! only teens allowed."<<endl;
return 0 ;
}

Userid-password match with common mismatch message #1956

Matching of userid and password in the same condition and if not matched giving a common mismatch message.

#include <iostream>
using namespace std;
int main( )
{
int id1=123,pw1=321;
int id,pw;
cout<<"Enter userid : ";cin>>id;
cout<<"Enter password : ";cin>>pw;
if(id==id1 && pw==pw1)
cout<<"Access Granted"<<endl;
else
cout<<"userid or password incorrect"<<endl;
return 0 ;
}

Checking for buzz number #3176

Test if a number is divisible by 7 or the last digit is 7, means whether the number is a buzz number.

#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Please enter a number: ";
cin>>n;
if(n%7 == 0 || n % 10 == 7)
cout << n << " is a buzz number "<<endl;
else
cout << n<< " is not a buzz number "<<endl;
return 0;
}
Solved Problems

Even odd test #2012

Write a c++ program to test if the user given integer value is an even number

Output writing – If-else with post/pre increment #2014

Write the output of the following c++ program when the user input value are -

Output writing – If else – leap check incorrect way. #2284

Write the output of the following problem if the user given input is 1900, 20

Square or cube based on even check selection #3122

Write a program which collects an integer number from the user and if the nu


Code Sheets:13  Solved Problems:4 
Back