You are on page 1of 6

Manual # 3

Topic:
If, If-Else & Nested If-Else Statements

Subject:
Introduction to Computing

Author:
Engr. Ali Faisal Murtaza

1
-----------------------------------------------------------------------------------------------------------
-
Write a program that if the user entered value other than zero then the program should
display that value otherwise do nothing.
Note the use of not(!) operator.

#include <iostream.h>
#include<math.h>

int main()
{
int a;

cout<<"Enter the Value: ";


cin>>a;

cout<<"\n";

if(a!=0)
{
cout<<"Value is: "<<a<<endl;
}

cout<<"\n\n";

return 0;
}
-----------------------------------------------------------------------------------------------------------
-
Write a program which will take the integer value from the user and then the program
tells us that whether the integer entered is odd or even.

#include<iostream.h>

int main()
{
int a,b;

cout<<”Enter the Integer Value: “;


cin>>a;

b = a % 2;

if(b == 0) // if(a%2 == 0)
{
cout<<”Integer Entered is Even”<<”\n\n”;

2
}
else
{
cout<<”Integer Entered is Odd”<<”\n\n”;
}
return 0;
}
-----------------------------------------------------------------------------------------------------------
-
Write a program in which the user can enter the age of a person. If the age of a person is
from 1 to 15, the program should display, person is teenager, and if the age is between 16
to 35, the program should display that person is young, and if the age is between 36 to 50
the program should display that person is in mid-age, and if he is above 50, the program
should display that he is old.
In this program we have to use And operators in our conditions.
Note: Note the indentation in the Program below. Please make a habit to write a
code in a way below code is written.

Program:

#include <iostream.h>
#include<math.h>

int main()
{
int age;

cout<<"Enter the age of a Person: ";


cin>>age;

cout<<"\n";

if(age>=1 && age<=15)


{
cout<<"Person is a Teenager"<<endl;
}
else if(age>=16 && age<=35)
{
cout<<"Person is Young"<<endl;
}
else if(age>=36 && age<=50)
{
cout<<"Person is in Mid-Age"<<endl;
}
else
{

3
cout<<"Person is Old"<<endl;
}

cout<<"\n\n";

return 0;
}
-----------------------------------------------------------------------------------------------------------
-

Write a program which will take the value from the user between 1 to 10. And if the
value entered is divisible by 3 then the program should display that the value is divisible
by 3 otherwise display invalid.
Now between 1 to 10, three values are divisible by 3 i.e. 3,6 & 9. So, you may use
a OR operator in this case.

Program:

#include <iostream.h>
#include<math.h>

int main()
{
int a;

cout<<"Enter the age of a Person: ";


cin>>a;

cout<<"\n";

if(a==3 || a==6 || a==9)


{
cout<<"Value is divisible by 3"<<endl;
}
else
{
cout<<"Invalid";
}

cout<<"\n\n";

return 0;
}
-----------------------------------------------------------------------------------------------------------
-

4
-----------------------------------------------------------------------------------------------------------
-
Q1: Write a program which will determine the roots of the equation. The program also
tells us that whether the roots are real or imaginary.
-----------------------------------------------------------------------------------------------------------
-
-----------------------------------------------------------------------------------------------------------
-
Q2: A palindrome is a number or text phrase that reads the same backwards as forwards.
For each example, each of the following five digits are palindrome: 12231, 54425, 24441
and 11611. Write a program that reads in a five-digit integer and determines whether it is
a palindrome or not.
Hint: Use the division and modulus operators to separate the numbers into individual
digits.
-----------------------------------------------------------------------------------------------------------
-
-----------------------------------------------------------------------------------------------------------
-
Q3: A certain grade of steel is graded according to the following conditions:
(1) Hardness must be greater than 50.
(2) Carbon contents must be less than 0.7.
(3) Tensile strength must be greater than 500.

The grades are as follows:

Grade is 10 if all three conditions are met.


Grade is 9 if conditions (i) and (ii) are met.
Grade is 8 if condition (ii) and (iii) are met.
Grade is 7 if condition (i) and (iii) are met.
Grade is 6 if only one condition is met.
Grade is 5 if none of the conditions are met.

Write a program which will require the user to give values of harness, carbon
content and tensile strength of the steel under consideration and output the grade
of the steel.
-----------------------------------------------------------------------------------------------------------
-

-----------------------------------------------------------------------------------------------------------
-
Q4: Any character is entered through the keyboard; write a program to determine whether
the character entered is a capital letter, a small letter, a digit or a special symbol.

The following table shows the range of ASCII values for various characters:

Characters ASCII Values

5
A-Z 65-90
a-z 97-122
0-9 48-57
special symbols 0-47, 58-64, 91-96, 123-127

-----------------------------------------------------------------------------------------------------------
-
Case Study: Cryptography (Encryption -- Decryption)
A company wants to transmit data over the telephone, but is concerned that its phones
can be tapped. All the data are transmitted as four digit integers. The company has asked
you to write a program that encrypts that data so that it can be transmitted more securely.
Your program should read a four digit integer and encrypt it as follows: Replace each
digit by ( the sum of that digit plus 7) modulus 10. Then, swap the first digit with the
third, swap the second digit with fourth and print the encrypted integer.
Write a separate program that inputs an encrypted four digit integer and decrypts
it to form the original number.
-----------------------------------------------------------------------------------------------------------
-

You might also like