Aikman Series C
Aikman Series C
Page |1
#include<iostream>
using namespace std;
int main( )
{
int num;
cout<<"\nEnter any number from 0 to 9 : ";
cin>>num;
switch(num){
case 0:
cout<<"\nzero";
break;
case 1:
cout<<"\nOne";
break;
case 2:
cout<<"\nTwo";
break;
case 3:
cout<<"\nThree";
break;
case 4:
cout<<"\nFour";
break;
case 5:
cout<<"\nFive";
break;
case 6:
cout<<"\nSix";
break;
case 7:
cout<<"\nSeven";
break;
case 8:
cout<<"\nEight";
break;
case 9:
Page |2
cout<<"\nNine";
break;
default:
cout<<"\nOnly the numbers between 0 and 9 are allowed";
}
return 0;
Page |3
#include<iostream>
using namespace std;
int main( )
{
char alphabet;
cout<<"\nEnter any alphabet : ";
cin>>alphabet;
switch(alphabet){
case 'a':
cout<<"\nits a vowel";
break;
case 'e':
cout<<"\nits a vowel";
break;
case 'i':
cout<<"\nits a vowel";
break;
case 'o':
cout<<"\nits a vowel";
break;
case 'u':
cout<<"\nits a vowel";
break;
//to handle the capital letters use the following code
case 'A':
cout<<"\nits a vowel";
break;
case 'E':
cout<<"\nits a vowel";
break;
case 'I':
cout<<"\nits a vowel";
break;
case 'O':
cout<<"\nits a vowel";
break;
case 'U':
cout<<"\nits a vowel";
break;
default:
cout<<"\nits a consonant";
}
return 0;
Page |4
#include<iostream>
using namespace std;
int main( )
{
char alphabet;
cout<<"\nEnter any alphabet : ";
cin>>alphabet;
return 0;
Page |5
int main( )
{
//marks obtained by a student in a subject
int marksObtained;
cout<<"Enter Marks Obtained \n";
cin>>marksObtained;
if(marksObtained>=90&&marksObtained<=100)
cout<<"\n\nGrade is A+"<<endl;
if(marksObtained<90&&marksObtained>=70)
cout<<"\n\nGrade is A"<<endl;
if(marksObtained>=50&&marksObtained<70)
cout<<"\n\nGrade is B"<<endl;
if(marksObtained<50)
cout<<"\n\nGrade is F"<<endl;
if(marksObtained>100)
cout<<"\n invalid input" ;
return 0;
}
#include<iostream>
using namespace std;
int main( )
{
//marks obtained by a student in a subject
int marksObtained;
cout<<"Enter Marks Obtained \n";
cin>>marksObtained;
Page |6
if (marksObtained<50)
cout<<"\n\n Grade is F" ;
else if(marksObtained>=50&&marksObtained<70)
cout<<"\n\nGrade is B";
else if(marksObtained>=70&&marksObtained<90)
cout<<"\n\nGrade is A";
else if (marksObtained>=90&&marksObtained<101)
cout<<"\n\nGrade is A+";
else if (marksObtained>100)
cout<<"\n\ninvalid input";
return 0;
#include<iostream>
using namespace std;
int main( )
{
int a,b,c,d;
cout<<"enter 4 different variables\n";
cin>>a>>b>>c>>d;
if(a>b&&a>c&&a>d)
cout<<"\nthe largest value is :"<<a;
else if (b>a&&b>c&&b>d)
cout<<"\nthe largest value is :"<<b;
else if(c>a&&c>b&&c>d)
cout<<"\nthe largest value is :"<<c;
else
cout<<"\nthe largest value is :"<<d;
}
return 0;
}
Page |7
#include<iostream>
using namespace std;
int main()
{
char ch;
return 0;
}
Page |8
i) if statement
if condition is used to put some control over your code. the statements under if condition will run
when the given condition in if statement is true. if the given condition is false it not execute the
instruction followed by the if statement.
if(condition)
{
statement 1;
statement 2;
...
}
Example
#include<iostream>
using namespace std;
int main()
{
int a=15,b=20;
if(b>a)
{
cout << "b is greater"<<endl;
}
system("PAUSE");
Example
#include<iostream>
using namespace std;
int main()
{
Page |9
int number;
syntax is as follows
switch(variable)
{
case 1:
//execute your code
break;
case n:
//execute your code
break;
default:
//execute your code
break;
Example
main() { int a; cout << "Please enter a no between 1 and 5: " << endl; cin >> a; switch(a) { case 1:
cout << "You chose One" << endl; break; case 2: cout << "You chose Two" << endl; break; case 3:
cout << "You chose Three" << endl; break; case 4: cout << "You chose Four" << endl; break; case 5:
cout << "You chose Five" << endl; break; default : cout << "Invalid Choice. Enter a no between 1 and
5" << endl; break; } system("PAUSE"); }
P a g e | 10
It is always legal to nest if-else statements, which means you can use one if or else if statement
inside another if or else if statement(s).
Syntax:
The syntax for a nested if statement is as follows:
if( boolean_expression 1)
{
// Executes when the boolean expression 1 is true
if(boolean_expression 2)
{
// Executes when the boolean expression 2 is true
}
}
You can nest else if...else in the similar way as you have nested if statement.
Example:
#include <iostream>
using namespace std;
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
// check the boolean condition
if( a == 100 )
{
// if condition is true then check the following
if( b == 200 )
{
// if condition is true then print the following
cout << "Value of a is 100 and b is 200" << endl;
}
}
cout << "Exact value of a is : " << a << endl;
cout << "Exact value of b is : " << b << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
P a g e | 11
a)
#include<iostream.h>
main()
{
int a,b,c;
a = b = c;
c = 13;
if ( a == b && a < b )
cout<<"Condition is True"<<endl;
else
{
cout<<"a is equal to b<<endl;
cout<<"a is not greater than b"<<endl;
}
cout<<"OK";
Output
a is equal to b
a is not greater than b
OK
------------------------------------------------
b)
#include<iostream.h>
main()
{
int ac;
ac=15;
if ( ac % 2 == 0)
{
cout<<"Number is Even"<<endl;
}
else
cout<<"Number is odd"<<endl;
cout<<"OK";
P a g e | 12
Output
------------------------------------------------
c)
#include<iostream.h>
main()
{
Output
Number is odd
OK
P a g e | 13
a)
#include <iostream.h>
main()
{
int a;
cout<<"Enter any number";
cin>>a;
if(a>10); // this is logically invalid
cout<<"Value is greater than 10";
else
cout<<"value is less than 10";
}
b)
#include<iostream.h>
main()
{
int a;
cout<<"Enter any number";
cin>>a;
switch (a>10);// semicolon terminates
case 1:
cout<<"One"; break;
case 2:
cout<<""; break;
c)
#include<iostream.h>
main()
{
int a;
cout<<"Enter any number";
cin>>a;
if(a>100)
{ // brackets are missing in this case
cout<<"Value is greater than 100";
cout<<"ok";
}
else
cout<<"Value is less than 100";
-------------------------------------------------------------------------------------------------------------------------------------
P a g e | 14
ii. In C++ == sign is used to compare if the two value are equal.
iv. The if-else structure is used to execute one task when the given condition is true
and other task when the given condition is false.
vii. In the switch statement, if no case is matched then the statements under the
keyword “Default” will be executed.
viii. The break statement is used to exit from the body of switch structure.
----------------------------------------------------------------------------------
P a g e | 15
Question No.2
Mark True or False
ii. The != operator is used to test if two expressions are equal. FALSE
iii. The alternative of nested if-else structure is the switch statement. TRUE
iv. More than one values can be given in the case of switch structure, FALSE (Default
is the right answer)
v. In switch statement only one single expression is used for all cases. TRUE
ix. The use of keyword default is optional in the switch statement. FALSE
xi. The logical operator && is used to combine two expressions if both the expressions
are true then the result will be true. TRUE
P a g e | 16
Question Number.1
Select a suitable choice.
iv) The quantity whose value may be changed during the execution of the program is
called variable.
vi) The define directives is used to add definition of a function used in the program .
viii) The cout object is an output stream used to print the output on the screen.
x) if x=2, then after executing the statement "x = ++x;" the value in x will be : 3 .
xi) if x=2, then after executing the statement "x = x++;" the value in x will be : 2
xii) if x=2, the after executing the statement "x+=10;", the value of x will be 12
xiii) if x=10;, then after executing the statement "x*=++x;", the value of x will
be 121 ( Answer: none of these ).
xiv) if x=2;, the after executing the statement "x=x+(++x);" the value of x will be 6
xv) if x=2;, then after executing the statement "x= x+x++;" the value of x will be 5
------------------------------------------------------------------------------------------------
P a g e | 17
Question No.2
i) A variable is a quantity whose value can be changed during the execution of the
program: TRUE
ii) The preprocessor directive "include" is used to define a constant quantity. FALSE
iii) The "define" is a keyword and cannot be used as variable name. TRUE
iv) The increment operator is indicated as double minus sign (--). it is used to increase the
previous value of the variable by one. FALSE
vi) The "cin" object is used to get input into variable from the keyboard during program
execution. TRUE
vii) if a=10 then after executing the statement “a = ++a; " the value in "a" will be
12. FALSE correct answer is 11.
ix) The % operator returns the remainder of two integer values. TRUE
x) The main () function indicates the start of the actual program. TRUE
xi) Each statement in C++ program is terminated with a semicolon (;). TRUE
-----------------------------------------------------------------------------------------------
P a g e | 18
Question No.3
Point out valid and invalid variable names. Give reasons if a variable is invalid.
a) name VALID
b) AAA VALID
d) DEFINE VALID
e) token VALID
h) cpp VALID
-----------------------------------------------------------------------------------------
P a g e | 19
Question No.4
iv) The increment operator that is used before the variable is called prefix increment
operator
vi) if x=3 the after executing the statement "x=++x+(++x);" the new value of x will be
10
vii)if x=5 the after executing the statement "x*=10;" the value of x will be 50
viii) The setw(n) manipulator is used to set the width of the output on the device.
ix) The << operator is used with "cout" object to direct the output to the output device.
This operator is called insertion operator.
x) The include directive is used to include the header files in the program.
xvi) The int type variable for MSDOS takes 2 bytes in memory
--------------------------------------------------------------------------
P a g e | 20
Question No.5
(radius= π r π = 3.14)
2,
Solution:
#include<iostream>
int main(){
float r;
float radius;
float pi = 3.14;
cin>>r;
radius = pi*r*r;
return 0;
Otherwise the r is radius itself and the formula given is the formula of Area of the circle )
-------------------------------------------------------------------------------------------------------
P a g e | 21
Question No.6
Write a program in C++ to input your name, address and age during the execution o the
and then print it on the screen.
#include<iostream>
int main(){
string name;
string address;
int age;
cin>>name;
cin>>address;
cin>>age;
/* Note:
-------------------------------------------------------------------------------------------------------------------------------------
P a g e | 22
Question No.7
Write a program in C++ to input an amount in rupees and then to convert it in dollars and
print the result on screen. (1 Dollar = Rs.99 )
#include<iostream>
int main(){
float pkr;
float dollars;
cin>>pkr;
dollars = pkr/99;
cout<<"Rs."<<pkr<<" = "<<dollars<<"$";
return 0;
-------------------------------------------------------------------------------------------------------------------------------------
P a g e | 23
Question No.8
Write a program in C++ to input your age in years . Covert the age in months, days ,
hours, minutes and seconds then print these values in the screen
#include<iostream>
int main(){
int age;
cin>>age;
-------------------------------------------------------------------------------------------------------------------------------------
P a g e | 24
Question No.9
Answer:
The preprocessors are the directives, which give instruction to the compiler to preprocess the
information before actual compilation starts.
All preprocessor directives begin with #, and only white-space characters may appear before a
preprocessor directive on a line. Preprocessor directives are not C++ statements, so they do not end
in a semicolon (;).
There are number of preprocessor directives supported by C++ like #include, #define, #if, #else,
#line, etc.
#include <iostream>
using namespace std;
#define PI 3.14
cout << endl << "Enter Radius of the Circle:"; cin >> radius;
cout << endl << "Area of the Circle is: " << PI * SQR(radius);
#undef PI
//It is now invalid to use PI, thus we cannot use the next line.
//cout << endl << "Perimeter of Circle is : " << 2 * PI * radius;
cout << endl << "Area of the Sphere of Radius " << \
radius << " is: " << 4/3.0 * 3.14 * CUBE(radius);
}
P a g e | 25
Question No.10
What is variable? Write down the rules to define a variable in C++
Answer:
A variable provides us with named storage that our programs can manipulate. Each variable in C++
has a specific type, which determines the size and layout of the variable's memory; the range of
values that can be stored within that memory; and the set of operations that can be applied to the
variable.
The name of a variable can be composed of letters, digits, and the underscore character. It must
begin with either a letter or an underscore. Upper and lowercase letters are distinct because C++ is
case-sensitive:
There are following basic types of variable in C++ as explained in last chapter:
Type Description
P a g e | 26
-------------------------------------------------------------------------------------------------------------------------------------
Question No.10
What is variable ? write down the rules to define a variable name in C++
Answer:
-------------------------------------------------------------------------------------------------------------------------------------
Question No.11
Differentiate between "cin" & "cout" objects. Give suitable examples.
Answer.
cout and cin are synonymous with stdout and stdin, the global standard output.and input streams,
respectively.
The standard output stream (cout): The predefined object cout is an instance of ostream class. The
cout object is said to be "connected to" the standard output device, which usually is the display
screen.
As part of the C++ standard library, these objects are a part of the std namespace. cout and cin are
not key words in the C++ language. They are variables, instances of classes, that have been
declared in <iostream>. cout is a variable of type ostream.
P a g e | 27
________________________________________________________________________________