Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Manual # 4

Topic:
IF-ELSE (Ladder) Continue…

Subject:
Introduction to Computing

Author:
Engr. Ali Faisal Murtaza

1
-----------------------------------------------------------------------------------------------------------
-
Write a program as written below, and enter characters from keyboard like: a, b, c, A, ; , !
and ^, note the output, you`ll always get output 0.
Remember: C++ Compiler characterized the special characters ;,>,<,etc under
char category.

#include <iostream.h>

int main()
{
int a;

cout<<"Enter the Character: ";


cin>>a;

cout<<a;

cout<<"\n\n";

return 0;
}
-----------------------------------------------------------------------------------------------------------
-
Write a program as written below, and enter characters from keyboard like: a, b, c, A and
note the output, if you’ll enter single character then you’ll get the desired result but if
you’ll enter the word like cat, house then you’ll get only the first character of that word.
Now, how can we get the full word, later we will cover String Topic, which will remove
this restriction. But for the time being, you’ll play with a single character.
You can also check the output by entering the special characters: ; ,) ,! ,@ etc.
And also you can see the output by giving the integer like 4556, 2323 even in this case
the output shows only the first character.

#include <iostream.h>

int main()
{
char a;

cout<<"Enter the Character: ";


cin>>a;

cout<<a;

cout<<"\n\n";

2
return 0;
}
-----------------------------------------------------------------------------------------------------------
-
Write a program as below and note the output of the following code:
#include <iostream.h>

int main()
{
char a;

cout<<"Enter the Character: ";


cin>>a;

if(a==c)
{
cout<<a;
}

cout<<"\n\n";

return 0;
}

You`ll get the output as an error i.e. undeclared identifier c; or something like that. You`ll
already know that you cant play with characters in case int. So, in order to compare the
characters you have to made the following change in the if line.

if(a==’c’)

Now, your code will not give you any errors. And also see the output.

Now, if you want to compare the value without giving and commas (like we did above
a==’c’), then you have to give its ASCII (ASCII (American Standard Code for
Information Interchange) value, like ASCII value of c is 99.
So, replace the if line i.e. if(a==’c’) with if(a==99), and note the output again.

#include <iostream.h>

int main()
{
char a;

cout<<"Enter the Character: ";


cin>>a;

3
if(a==’c’) // Same as if(a==99)
{
cout<<a;
}

cout<<"\n\n";

return 0;
}
-----------------------------------------------------------------------------------------------------------
-
Now, by using the following code, you can also detect that the following ASCII value
belongs to which character.
#include <iostream.h>

int main()
{
char a;

a=45;

cout<<a;

cout<<"\n\n";

return 0;
}
-----------------------------------------------------------------------------------------------------------
-
Q1: Write a program in which we will consider the ages of four persons, name of the
following persons with respective ages are as follow:
ikram is 60 years old,
moin is 45 years old,
shah is 33 years old &
khan is 22 years old,
the program should take the name from the user and display his respective age.
Remember: When you enter the name, it will take only the first word, so in comparison
you can only consider the first character.
-----------------------------------------------------------------------------------------------------------
-
Q2: An insurance company follows following rules to calculate premium.

(a) If a person’s health is excellent and the person is between 25 and 35 years of age
and lives in a city and is a male then the premium is Rs. 4 per thousand and his
policy amount cannot exceed Rs. 2 lakhs.

4
(b) If a person satisfies all the above conditions except that the sex is female then the
premium is Rs. 3 per thousand and her policy amount cannot exceed Rs. 1 lakh.
(c) If a person’s health is poor and the person is between 25 and 35 years of age and
lives in a village and is a male then the premium is Rs. 6 per thousand and his
policy cannot exceed Rs. 10,000.
(d) In all other case the person is not ensured.

Write a program which will ask you to enter the person’s health, sex, age and policy
amount. If the policy amount do not exceed the limit then it will calculate the
premium of the person according to above given criteria otherwise says that policy
amount exceeds the limit.

Note: Now here you cannot enter the complete word, like ‘Excellent’ you have to just
enter the first word of Excellent i.e. E or e whatever you like.
You can see the behavior of a program by entering the full word like
‘Excellent’, then it will not ask you the remaining inputs because it will take the input
from the remaining word of Excellent word.
The above program works with a full word because they have only the single output
while this program takes the multiple outputs.
-----------------------------------------------------------------------------------------------------------
-
Q3: Write a program which will take the year as input from the user. Write a program to
find whether the year is leap or not.
-----------------------------------------------------------------------------------------------------------
-
Q4: Manual # 3 – Q4
-----------------------------------------------------------------------------------------------------------
-

You might also like