0% found this document useful (0 votes)
118 views28 pages

Aikman Series C

The document contains a series of C++ programming exercises focused on conditional statements, including switch and if-else structures. It provides code examples for various tasks such as printing numbers in words, checking for vowels, grading based on marks, and finding the largest number among inputs. Additionally, it includes explanations of control statements and common programming errors.

Uploaded by

moltrogaming684
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views28 pages

Aikman Series C

The document contains a series of C++ programming exercises focused on conditional statements, including switch and if-else structures. It provides code examples for various tasks such as printing numbers in words, checking for vowels, grading based on marks, and finding the largest number among inputs. Additionally, it includes explanations of control statements and common programming errors.

Uploaded by

moltrogaming684
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

lOMoARcPSD|52522856

Aikman Series C++

Visual Programming in C# (Islamia College University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Dr Doom (moltrogaming684@gmail.com)
lOMoARcPSD|52522856

Page |1

Aikman Series Book c++


AIKMAN Series C++ Solutions Chapter No.2 (Conditional Statements)
Question 11
Question. 11
Write a program to input a single number from 0 to 9 and print the word
in English representing this number, using switch statement

#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:

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

Page |2

cout<<"\nNine";
break;

default:
cout<<"\nOnly the numbers between 0 and 9 are allowed";
}

return 0;

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

Page |3

AIKMAN Series C++ Solutions Chapter No.2 (Conditional Statements)


Question 10
Question. 10
Write a program to input a single character and print a message "its a vowel"
if it is a vowel otherwise print "its a consonant".
Use switch statement

#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;

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

Page |4

AIKMAN Series C++ Solutions Chapter No.2 (Conditional Statements)


Question 9
Question. 9
Write a program to input a single character and print a message "its a vowel"
if it is a vowel otherwise print "its a consonant".
Use only if-else structure and OR (||) operators only.

#include<iostream>
using namespace std;

int main( )
{
char alphabet;
cout<<"\nEnter any alphabet : ";
cin>>alphabet;

if(alphabet == 'a'||alphabet == 'e'||alphabet == 'i'||alphabet ==


'o'||alphabet == 'u')
cout<<"\n\nits a Vowel";
else
cout<<"\n\nits a consonent";

cout<<"\n\nEnd of the program";

return 0;

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

Page |5

AIKMAN Series C++ Solutions Chapter No.2 (Conditional Statements)


Question 8
Question Number.8
Write a program to input marks obtained by a student in a subject. The total marks are 100.
Find out the grades of the student by using the if else neseted structure. The grades are:

if marks are equal to or greater than 90, grade is A+


if marks are equal to or greater than 70 and less than 90, grade is A
if marks are equal to or greater than 50 and less than 70, grade is B
if marks are less than 50, grade is F

//Following is the simple if method


#include<iostream>
using namespace std;

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;
}

//Following is the nested if else method

#include<iostream>
using namespace std;

int main( )
{
//marks obtained by a student in a subject
int marksObtained;
cout<<"Enter Marks Obtained \n";
cin>>marksObtained;

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

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;

AIKMAN Series C++ Solutions Chapter No.2 (Conditional Statements)


Question 7
Question Number.7
Write a program to input four integers. Find out the largest value and print it on the screen
using nested if else.

#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;
}

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

Page |7

AIKMAN Series C++ Solutions Chapter No.2 (Conditional Statements)


Question 6
Question Number.6
Write a program to input a single letter in char variable. if "m" is input print "You are a male"
otherwise print "You are a female"

#include<iostream>
using namespace std;

int main()
{

char ch;

cout<<"Enter a single character ? ";


cin>>ch;
if (!(ch=='m'))
cout<<"you are a female"<<endl;
if (ch=='m')
cout<<"You are male"<<endl;

return 0;
}

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

Page |8

AIKMAN Series C++ Solutions Chapter No.2 (Conditional Statements)


Question 5

Define the following:

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.

The basic syntax of if statement is as following.

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()
{

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

Page |9

int number;

cout << "Input the number: "; cin >> number;

/* check whether the number is negative number */


if(number < 0)
{
/* If it is a negative then convert it into positive. */
number = -number;
cout<<"The absolute value is: "<< number<<endl;
system("PAUSE");
}
}

ii) "switch statement"


a switch statement is a type of selection control mechanism used to allow the value of a variable or
expression to change the control flow of program execution via a multiway branch.

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"); }

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

P a g e | 10

iii) "nested if" statement

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:

Value of a is 100 and b is 200


Exact value of a is : 100

Exact value of b is : 200

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

P a g e | 11

AIKMAN Series C++ Solutions Chapter No.2 (Conditional Statements)


Question 4
Question No. 4

write the output of the following programs.

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";

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

P a g e | 12

Output

Number is not odd


OK

------------------------------------------------

c)

#include<iostream.h>
main()
{

int ac, b = 6, c=7;


ac = 15;
if(ac%2==0)
if(b>c)
cout<<"b is greater than c and ac is even"<<endl;
else
cout<<"b is less than c and ac is even"<<endl;
else
cout<<"Number is odd"<<endl;
cout<<"OK";

Output

Number is odd
OK

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

P a g e | 13

AIKMAN Series C++ Solutions Chapter No.2 (Conditional Statements)


Question 3
Find the errors if any into the following programs.

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;

else// else is not used in switch statement


cout<<"value is greater than 2";
}
}

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";

-------------------------------------------------------------------------------------------------------------------------------------

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

P a g e | 14

AIKMAN Series C++ Solutions Chapter No.2 Question 1 and Question


2 Conditional Statements
Question No.1
Fill in the blanks
i. A relational expression returns only one value. It may be true or false.

ii. In C++ == sign is used to compare if the two value are equal.

iii. The statements in curly brackets are also known as Block .

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.

v. If the “ if “ statement comes under another “ if “ that is called Nested-if .

vi. The else-if statement is used for multiple selections.

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.

ix. The Switch is used to alternative of simple if-else.

x. The && is used as AND logical operator.

xi. The ! is used as NOT logical operator.

xii. The Break statement is an unconditional control statement.

----------------------------------------------------------------------------------

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

P a g e | 15

Question No.2
Mark True or False

i. In sequential execution, the statement of a program is executed one after the


other in the same order in which they are written. TRUE

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

vi. The “?” symbol is used as a conditional operator. TRUE

vii. The logical not operator is represented as != . it is also known as unary


operator. TRUE

viii. In C++ a semicolon ends the label name. FALSE

ix. The use of keyword default is optional in the switch statement. FALSE

x. The “if” statement is conditional statement. TRUE

xi. The logical operator && is used to combine two expressions if both the expressions
are true then the result will be true. TRUE

xii. The “goto” statement is a conditional statement. FALSE

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

P a g e | 16

AIKMAN Series C++ Solutions Chapter No.1 Introduction Part 1

Question Number.1
Select a suitable choice.

i) C++ is an Object Oriented language.

ii) The extension of source program file of C++ is cpp

iii) The header files contain the definition of built in functions.

iv) The quantity whose value may be changed during the execution of the program is
called variable.

v) A quantity whose value is not changed during the execution of a program is


called constant .

vi) The define directives is used to add definition of a function used in the program .

vii) The % operator is used as modulus arithmetic operator


(Answer: non of these ).

viii) The cout object is an output stream used to print the output on the screen.

ix) The \n escape sequence is used to insert a new line.

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

------------------------------------------------------------------------------------------------

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

P a g e | 17

Question No.2

Mark TRUE or FALSE

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

v) The increment or decrement operator cannot be used in any arithmetic


expression. 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.

viii) The first letter of a variable may be a digit. FALSE

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

xii) The preprocessor directive statement is terminated with a semicolon. FALSE

-----------------------------------------------------------------------------------------------

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

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

c) include INVALID (Reserved Word )

d) DEFINE VALID

e) token VALID

f) h rent INVALID ( blank space is not allowed in variable name )

g) 9a INVALID ( variable name cannot be started with a number )

h) cpp VALID

i) main INVALID ( Reserved Word )

j) US$ INVALID ( Special Characters cannot be used in variable name $ is a special


character )

k) a-b INVALID (Arithmetic Operators cannot be used in variable name - is an


Arithmetic Operator special character )

l) C++ INVALID ( Arithmetic Operators cannot be used in variable name + is an


Arithmetic Operator special character )

-----------------------------------------------------------------------------------------

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

P a g e | 19

Question No.4

Fill in the blanks

i) The expression 15%6 will return

ii) Each statement in C++ is terminated with ; (Semicolon)

iii) The /* */ or // is used to give comments in C++ program

iv) The increment operator that is used before the variable is called prefix increment
operator

v) The -- operator is used to subtract 1 from the value of an integer variable.

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.

xi) A float type variable occupies 4 bytes in the memory.

xii) A long double type of variable occupies 8 bytes in the memory.

xiii) The main () function indicates the start of C++ program.

xiv) The extension of the source program file in C++ is CPP.

xv) C++ was developed by Bjarne Stroustrup

xvi) The int type variable for MSDOS takes 2 bytes in memory

--------------------------------------------------------------------------

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

P a g e | 20

Question No.5

Write a Program to calculate the radius of circle.

(radius= π r π = 3.14)
2,

Solution:

#include<iostream>

using namespace std;

int main(){

float r;

float radius;

float pi = 3.14;

cout<<"Please insert the value of r = ";

cin>>r;

radius = pi*r*r;

cout <<"\n\nAnswer is "<<radius;

return 0;

(Note: This is the solution of the program asked in this book.)

Otherwise the r is radius itself and the formula given is the formula of Area of the circle )

-------------------------------------------------------------------------------------------------------

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

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>

using namespace std;

int main(){

string name;

string address;

int age;

cout<<"Please type your name : ";

cin>>name;

cout<<"\n\nPlease type your address : ";

cin>>address;

cout<<"\n\nPlease type your age in years : ";

cin>>age;

cout<<"\n\n Name :: "<<name;

cout<<"\n Address ::"<<address;

cout<<"\n Age ::"<<age<<" Years";

/* Note:

 This program can take only one word as input.


 if you type spaces in the name or address fields , then it will not
take further inputs and will terminate the program.
 Also you are allowed to input only integer value in age section.*/

-------------------------------------------------------------------------------------------------------------------------------------

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

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>

using namespace std;

int main(){

float pkr;

float dollars;

cout<<"Insert the amount in Rupees : ";

cin>>pkr;

dollars = pkr/99;

cout<<"Rs."<<pkr<<" = "<<dollars<<"$";

return 0;

-------------------------------------------------------------------------------------------------------------------------------------

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

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>

using namespace std;

int main(){

int age;

cout<<"Please input your age in years : ";

cin>>age;

cout<<"Your Age in Months :: "<<age*12 <<" months";

cout<<"\nYour Age in Days :: "<<age*365<<" days";

cout<<"\nYour Age in Hours :: "<<age*365*24<<" hours";

cout<<"\nYour Age in Minuts :: "<<age*365*24*60<<" minutes";

cout<<"\nYour Age in Seconds :: "<<age*365*24*60*60<<" seconds";

-------------------------------------------------------------------------------------------------------------------------------------

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

P a g e | 24

Question No.9

What are preprocessor directives? Explain with suit able example.

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 (;).

This # is used to include a header file into the source file.

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

#define SQR(X) ((X)* (X))


#define CUBE(y) SQR(y)* (y)

int main(int argc,char *argv[])


{
int radius;

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);
}

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

P a g e | 25

The above example illustrates the following:

We do not terminate the macros using (;)


Macros can be parameterized.
Macros can be dependent on other macros (must be defined before they are used.)
Macros are customarily written in capital letters to avoid confusion between macro and functions.
(This is not a rule)
Parameters are enclosed in () to avoid the ambiguity arising due to operator precedence. For
instance in the example above we call SQR (4+3). This statement will be expanded as (4+3) * (4+3).
Directives are helpful whenever things are prone to changes and are used at many places in one
program. Changing the directive value at the top will cause the corresponding changes at all the
places in the program.
-------------------------------------------------------------------------------------------------------------------------------------

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

Bool Stores either value true or false.

Char Typically a single octet(one byte). This is an integer type.

Int The most natural size of integer for the machine.

Float A single-precision floating point value.

Double A double-precision floating point value.

Void Represents the absence of type.

wchar_t A wide character type.

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

P a g e | 26

-------------------------------------------------------------------------------------------------------------------------------------
Question No.10
What is variable ? write down the rules to define a variable name in C++

Answer:

Every variable name should start with alphabets or underscore (_).


No spaces are allowed in variable declaration.
Except underscore (_) no special symbol are allowed in the middle of the variable declaration.
Maximum length of variable is 8 characters depend on compiler and operation system.
Every variable name always should exist in the left hand side of assignment operator.
No keyword should access variable name.
Note: In a c++ program variable name always can be used to identify the input or output data.

-------------------------------------------------------------------------------------------------------------------------------------

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.

Output is normally directed to the screen (<<)


while input is directed from the keyboard (>>).

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.

cout<<"This Piece of code will Print something on the screen";


cin>>var_name; will get some input from the keyboard in the predefined variable "var_name"

cout is for output


cin is for input

cout uses <<


cin uses >>

Downloaded by Dr Doom (moltrogaming684@gmail.com)


lOMoARcPSD|52522856

P a g e | 27

________________________________________________________________________________

Downloaded by Dr Doom (moltrogaming684@gmail.com)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy