Sei sulla pagina 1di 17

Class 9 (C++) .

.
Q-1) What do you mean by literals? .
Constants refer to fixed values that the program may else
not alter and they are called literals. Constants can be of { //statement(s)/else block; }
any of the basic data types and can be divided into
Integer Numerals, Floating-Point Numerals, Characters, Q-8) What are logical operators?
Strings and Boolean Values. Logical operators provide us with the capability to test
multiple conditions. C++ has 3 logical operators:
Q-2) Why do we use clrscr() function? Operator Symbol Form Operation
clrscr() is a predefined function in "conio.h" (console
input output header file) used to clear the console Logical true if x is false, or false
! !x
screen. It is a predefined function, by using this function NOT if x is true
we can clear the data from console (Monitor). Logical x && true if both x and y are
&&
AND y true, false otherwise
Q-3) What do you understand by header files?
In C and C++ Language, a header file is a file with Logical true if either x or y are
|| x || y
extension .h which contains C/C++ function declarations OR true, false otherwise
and macro definitions to be shared between several Logical NOT – It reverses the input
source files. In simple terms, it's a library file that Logical NOT (operator !)
contains various functions, macros and classes that you
can use during the development of your program. Operand Result
true false
Q-4) What is #include directive?
#include directive is used to include the header file or false true
standard file. These header files contain definition of
functions which are used in C++. Like iostream.h Logical OR – It tests whether either of two conditions is
contains declaration of cin and cout. true. If the left operand evaluates to true, or the right
operand evaluates to true, or both are true, then
Q-5) What is the difference between variable and the logical OR operator returns true. Otherwise it will
constant? return false.
The difference between variables and constants is that
Logical OR (operator ||)
variables can change their value at any time
but constants can never change their value. Left operand Right operand Result
(The constants value is locked for the duration of the
false False false
program). Constants can be very useful, Pi for instance
is a good example to declare as a constant. false True true
true False true
Q-6) What is the difference between unary and binary
operators? true True True
Binary operator requires two operands and Unary
operator requires one operand. The minus Logical AND – It tests whether both operands are true.
sign between 5 and 6 is binary operator which requires If both operands are true, logical AND returns true.
two operands (5 and 6 here). The minus sign in -11 is a Otherwise, it returns false.
unary operator. The compiler differentiates unary and
Logical AND (operator &&)
binary signs using operator precedence and
associativity. Left operand Right operand Result
false false false
Q-7) write the syntax of if else if ladder?
if(test_condition1) false true false
{ //statement(s)/block1; }
true false false
else if(test_condition2)
{ //statement(s)/block2; } true true true
Q-9) What is the difference between division (/) and grater thatn (>) , less than or equal to (<=), greater than
modulus (%) operator? equal to (>=), equivalent (==) and not equivalent (!=).
'%' is known as the modulus operator or the
remainder operator; it is used to find the remainder Logical Operators - AND (&&) and OR (||). They are
of division of two numbers. '/' is known as the division used to combine two different expressions together. If
operator; it is used to find the quotient in the division of two statement are connected using AND operator, the
two numbers. validity of both statements will be considered, but if
they are connected using OR operator, then either one
Q-10) What do you mean by escape sequence? of them must be valid.
An escape sequence is a sequence of characters
that does not represent itself when used inside a Bitwise Operators - Used to change individual bits into
character or string literal, but is translated into another a number. They work with only integral data types
character or a sequence of characters that may be like char, int and long and not with floating point values.
difficult or impossible to represent directly. E.g., ‘\n’, ‘\t’ • Bitwise AND operators &
• Bitwise OR operator |
Q-11) What do you mean by operators, write the • And bitwise XOR operator ^
names of operators supported by C++. • And, bitwise NOT operator ~
Operators are special type of functions that take one or They can be used as shorthand notation too, &
more arguments and produce a new value. For = , |= , ^= , ~= etc.
example: addition (+), substraction (-), multiplication (*)
etc, are all operators. Operators are used to perform Shift Operators - Used to shift Bits of any variable. It is
various operations on variables and constants. of three types,
1. Left Shift Operator <<
2. Right Shift Operator >>

Unary Operators - Work on only one operand. There


are many unary operators, but increment ++ and
decrement -- operators are most used. Other Unary
Operators : address of &,
dereference *, new and delete, bitwise not ~, logical
not !, unary minus - and unary plus +.
Types of operators
1. Assignment Operator Ternary Operator - The ternary if-else ? : is an operator
2. Mathematical Operators which has three operands.
3. Relational Operators
4. Logical Operators Comma Operator - This is used to separate variable
5. Bitwise Operators names and to separate expressions. In case of
6. Shift Operators expressions, the value of last expression is produced
7. Unary Operators and used.
8. Ternary Operator
9. Comma Operator Q-12) What is ternary operator explain with an
example.
Assignment Operator (=) - used for assignment, it takes A ternary operator has the following form:
the right-hand side (called rvalue) and copy it into the exp1 ? exp 2 : exp 3
left-hand side (called lvalue). The expression exp 1 will be evaluated always. Execution
of exp2 and exp3 depends on the outcome of exp 1. If the
Mathematical Operators - Used to perform basic outcome of exp 1 is non zero exp2 will be evaluated,
mathematical operations. Addition (+) , subtraction (-) , otherwise exp3will be evaluated. E.g.,
diversion (/) multiplication (*) and modulus (%) are the
basic mathematical operators. #include<iostream.h>
#include<conio.h>
Relational Operators - Establish a relationship between void main()
operands. The relational operators are : less than (<) , { int a, b, c, large;
clrscr(); The control statements are:-
cout<<"Enter any three number: "; • switch
cin>>a>>b>>c; • if
large=a>b ? (a>c?a:c) : (b>c?b:c); • if else
cout<<"Largest Number is: "<<large; • while
getch(); } • do while
• for
Q-13) Explain pre-increment and post-increment with
an example. Selection structures are implemented using if, if
Pre-increment operator: A pre-increment operator is else and switch statements. Looping structures are
used to increment the value of a variable before using it implemented using while, do while and for statements.
in a expression. In the Pre-Increment, value is first
incremented and then used inside the expression. E.g.,
a = ++x; Here, if the value of ‘x’ is 10 then value of ‘a’
will be 11 because the value of ‘x’ gets modified before
using it in the expression.

Post-increment operator: A post-increment operator is


used to increment the value of variable after executing
expression completely in which post increment is used.
In the Post-Increment, value is first used in a expression
and then incremented. E.g., a = x++; Here, suppose the
value of ‘x’ is 10 then value of variable ‘b’ will be 10
because old value of ‘x’ is used.

Q-14) What do you mean by control structure


statements?
Control structures form the basic entities of a
“structured programming language“. Control structures
are used to alter the flow of execution of the program.
We use control structures to make decisions and alter
the direction of program flow in one or the other
Selection structure - Used to perform ‘decision making‘
path(s) available. There are three types of control
and then branch the program flow based on the
structures available in C and C++:
outcome of decision making. if and if else statements
are 2 way branching statements where as switch is a
1) Sequence structure (straight line paths)
multi branching statement.
2) Selection structure (one or many branches)
3) Loop structure (repetition of a set of activities)
The simple If statement -
if (expression)
All the 3 control structures and its flow of execution is
// This expression is evaluated. If expression is TRUE
represented in the flow charts given below.
statements inside the braces will be executed
{ statement 1;
statement 2; }
statement 1;
// Program control is transferred directly to this line, if
the expression is FALSE
statement 2;

The expression given inside the brackets after if is


evaluated first. If the expression is true, then
statements inside the curly braces that follow
if(expression) will be executed. If the expression is false,
the statements inside curly braces will not be executed
and program control goes directly to statements after along with the statements 1 and 2 inside the curly
curly braces. braces of last else statement.

#include<iostream.h> #include<iostream.h>
void main() void main()
{ int num; { int num;
cout<<"Hello user, Enter a number"; cout<<"Hello user, Enter a number";
cin>>num; // Collects the number from user cin>>num; // Collects the number from user
if(num==1) if(num==1)
{ {
cout<<"UNITED STATES"; cout<<"UNITED STATES";
} }
if(num==2) else if(num==2)
{ {
cout<<"SPAIN"; cout<<"SPAIN";
} }
if(num==3) else if(num==3)
{ {
cout<<”INDIA"; cout<<"INDIA";
}} }
else
The If else statement - {
if(expression 1) cout<<"WRONG ENTRY";
// Expression 1 is evaluated. If TRUE, statements inside // See how else is used to output "WRONG ENTRY"
the curly braces are executed. }}
{ //If FALSE program control is transferred to
immediate else if statement. switch statement - switch is a multi branching control
statement 1; statement.
statement 2; } switch(expression)
else if(expression 2) // Expression is evaluated. The outcome of the
// If expression 1 is FALSE, expression 2 is evaluated. expression should be an integer or a character constant
{ statement 1; {
statement 2; } case value1: // case is the keyword used to match the
else if(expression 3) integer/character constant from expression.
// If expression 2 is FALSE, expression 3 is evaluated //value1, value2 ... are different possible values that can
{ statement 1; come in expression
statement 2; } statement 1;
else // If all expressions (1, 2 and 3) are FALSE, the statement 2;
statements that follow this else (inside curly braces) is break; // break is a keyword used to break the program
executed. control from switch block.
{ statement 1; case value2:
statement 2; } statement 1;
other statements; statement 2;
break;
The execution begins by evaluation expression 1. If it default: // default is a keyword used to execute a set of
is TRUE, then statements inside the immediate curly statements inside switch, if no case values match the
braces is evaluated. If it is FALSE, program control is expression value.
transferred directly to immediate else if statement. statement 1;
Here expression 2 is evaluated for TRUE or FALSE. The statement 2;
process continues. If all expressions inside the different break;
if and else if statements are FALSE, then the }
last else statement (without any expression) is executed
Execution of switch statement begins by evaluating the Loop structures
expression inside the switch keyword brackets. The
expression should be an integer (1, 2, 100, 57 etc ) or a
character constant like ‘a’, ‘b’ etc. This expression’s
value is then matched with each case values. There can
be any number of case values inside a switch
statements block. If first case value is not matched with
the expression value, program control moves to next
case value and so on. When a case value matches with
expression value, the statements that belong to a
particular case value are executed.

Notice that last set of lines that begins with default. The
word default is a keyword in C/C++. When used inside
switch block, it is intended to execute a set of
statements, if no case values matches with expression
value. So if no case values are matched with expression
value, the set of statements that follow default: will get
executed.

Note: Notice the break statement used at the end of


each case values set of statements. The word break is
a keyword in C/C++ used to break from a block of curly
braces. The switch block has two curly braces { }. The Loop Structure - Used to execute a certain set of actions
keyword break causes program control to exit from for a predefined number of times or until a particular
switch block. condition is satisfied. There are 3 control statements
available in C/C++ to implement loop structures: while,
#include<iostream.h> do while and for statements.
void main()
{ int num; The while statement -
cout<<"Hello user, Enter a number"; while(condition)// This condition is tested for TRUE or
cin>>num; // Collects the number from user FALSE. Statements inside curly braces are executed as
switch(num) long as condition is TRUE
{ { statement 1;
case 1: statement 2;
cout<<"UNITED STATES"; statement 3; }
break;
case 2: The condition is checked for TRUE first. If it is TRUE then
cout<<"SPAIN"; all statements inside curly braces are executed. Then
break; program control comes back to check the condition has
case 3: changed or to check if it is still TRUE. The statements
cout<<"INDIA"; inside braces are executed repeatedly, as long as the
default: condition is TRUE. When the condition turns FALSE,
cout<<"WRONG ENTRY"; program control exits from while loop.
}} Note:- while is an entry controlled loop. Statement
inside braces are allowed to execute only if condition
Note:- Switch statement is used for multiple branching. inside while is TRUE.
The same can be implemented using nested “If
Else” statements. But use of nested if else statements #include<iostream.h>
make program writing tedious and complex. Switch void main()
makes it much easier. { int num;
int count=0; // count is initialized as zero to start
printing from zero.
cout<<"Hello user, Enter a number"; Working of for loop:
cin>>num; // Collects the number from user The program control enters the for loop. At first it
while(count<=num) // Checks the condition - if value of execute the statements given as initialization
count has reached value of num or not. statements. Then the condition statement is evaluated.
{ If conditions are TRUE, then the block of statements
cout<<count; inside curly braces is executed. After executing curly
count=count+1; // value of count is incremented by 1 to brace statements fully, the control moves to the
print next number. "iteration" statements. After executing iteration
} } statements, control comes back to condition
statements. Condition statements are evaluated again
The do while statement - for TRUE or FALSE. If TRUE the curly brace statements
do are executed. This process continues until the condition
{ statement 1; turns FALSE.
statement 2; Note 1:- The statements given as "initialization
statement 3; } while(condition); statements" are executed only once, at the beginning of
a for loop.
Unlike while, do while is an exit controlled loop. Here Note 2: There are 3 statements given to a for loop as
the set of statements inside braces are executed first. shown. One for initialization purpose, other for
The condition inside while is checked only after finishing condition testing and last one for iterating the loop.
the first time execution of statements inside braces. If Each of these 3 statements are separated by
the condition is TRUE, then statements are executed semicolons.
again. This process continues as long as condition is
TRUE. Program control exits the loop once the condition #include<iostream.h>
turns FALSE. void main()
{ int num,count;
#include<iostream.h> cout<<”Hello user, Enter a number";
void main() cin>>num; // Collects the number from user
{ int num; for(count=0;count<=num;count++)// count is initialized
int count=0; // count is initialized as zero to start to zero inside for statement. The condition is checked
printing from zero. and statements are executed.
cout<<"Hello user, Enter a number"; { cout<<count; // Values from 0 are printed. } }
cin>>num; // Collects the number from user
do Q-15) What is the difference between while and do
{ while loop?
cout<<count; // Here value of count is printed for one The most important difference between while and do-
time intially and then only condition is checked. while is that in do-while, the block of code is executed
count=count+1; // value of count is incremented by 1 to at least once. ie, the do-while loop runs at least once,
print next number. even though the condition given is false. In while loop,
}while(count<=num); } the condition is first tested and then the block of code is
executed if the test result is true. In do-while, the code
The for statement - is first executed and then the condition is checked. The
for(initialization statements;test condition;iteration next iteration is executed if the test result is true. while
statements) is entry controlled and do-while is exit controlled.
{ statement 1;
statement 2; Syntax:
statement 3; } while(condition is true)
{ Something }
The for statement is an entry controlled loop. The
difference between while and for is in the number of do
repetitions. The for loop is used when an action is to be { Something }while(condition is true)
executed for a predefined number of times.
The while loop is used when the number of repetitions
is not predefined.
Q-16) Why do we use getch() and clrscr()? (The constants value is locked for the duration of the
clrscr()- It can be expanded as CLear SCReen and so is its program). Constants can be very useful, Pi for instance
purpose- to empty the buffer(memory) containing the is a good example to declare as a constant.
output of previously executed programs.
getch()- It is used to make program wait for the user's Q-23) What is the syntax of nested for loop?
input before it returns to the editing window after The placing of one loop inside the body of another loop
successful compilation and execution. Otherwise use is called nesting. When you "nest" two loops, the outer
will not be able to see the output of the program. loop takes control of the number of complete
repetitions of the inner loop. While all types of loops
Q-17) Explain cin and cout in c++. may be nested, the most commonly nested loops
cin is an object of the input stream and is used to take are for loops.
input from input streams like files, console, etc. cout is
an object of the output stream that is used to show for(num2 = 0; num2 <= 3; num2++)
output. Basically, cin is an input statement while cout is {
an output statement. They also use different operators. for(num1 = 0; num1 <= 2; num1++)
cin uses the insertion operator( >> ) while cout uses the {
extraction operator( << ). cout<< num2<< " " << num1<< endl;
}
Q-18) What are identifiers in C++? }
Identifier is a name used to identify a variable, function,
class, module, or any other user-defined item. An Q-24) What's the difference between break and
identifier starts with a letter A to Z or a to z or an continue statements?
underscore (_) followed by zero or more letters, The major difference between break and continue
underscores, and digits (0 to 9). statements is that a break causes the innermost
enclosing loop or switch to be exited immediately.
Q-19) What is the difference between / and %? The continue statement is used when we want to skip
% is a modulo operator, It give remainder of two one or more statements in loop's body and to transfer
numbers on division as result and discard quotient and the control to the next iteration.
gives an integer
eg. 11%4=3 Q-25) What do you mean by character set?
Character set is the combination of English language
/ is a division operator , it give quotient of two numbers (Alphabets and White spaces) and math's symbols
on division as result and gives a floating point number. (Digits and Special symbols). The characters and
eg. 11/4=2.75 symbols that a C++ Program can understand and accept.
These are grouped to form the commands, expressions,
Q-20) Write the role of default in switch. words, statements and other tokens for C++ Language.
Default executes when none of the other cases match In addition to these characters, C++ also uses a
the control expression. If no case matches and combination of characters to represent special
the default is omitted the switch statement simply exits. conditions. For example. character combinations such
as '\n, '\b' and '\t' are used to represent newline,
Q-21) Explain the difference between c and c++ backspace and horizontal tab respectively.
The major difference between C and C++ is that C is a
procedural programming language and does not Q-26) Can we use keywords as an identifier? Write any
support classes and objects, while C++ is a combination two naming conventions for an identifier.
of both procedural and object oriented programming The identifier can not be a keyword. Keywords
language; therefore C++ can be called a hybrid are reserved. The identifier can only be composed of
language. letters (lower or upper case), numbers,
and the underscore character. That means the name
Q-22) what is the difference between variables and can not contain symbols, (except the underscore) nor
constants? whitespace (spaces or tabs).
The difference between variables and constants is that
variables can change their value at any time
but constants can never change their value.
Q-27) Suppose you want to find the area of a circle, Q-33) Complete and draw the following table for
which data type will you prefer to use for declaring the Logical AND in your answer sheet:
variable? Why? Conditionl Condition2 Output
Area of a circle is given by pi*r*r, where pi takes the True (l) True (1) True (1)
value 3.14, we can use int for radius, r, but since area is False (0) True (l) False (0)
going to contain float values, therefore all the variables True (1) False (0) False (0)
should be taken into float. False (0) False (0) False (0)

Q-28) Where do we use insertion (<<) and extraction Q-34) Underline the error (if any) in the following
(>>) operators? Give an example of each. codes and rewrite correct code (consider header files
The insertion operator << is the one we usually use for and getch() in each codes).
output, as in, cout << "This is output" << endl; a) void main()
It gets its name from the idea of inserting data into the { int a;
output stream. cout<<enter the value”;
The extraction operator >> is the one we usually use for cin>>”a”;
input, as in, cin >> X; It gets its name from the idea of cout<<the value is<<a; }
extracting data from the input stream.
Correction:
Q-29) While using loops, why is it important to put void main()
break statement inside if condition? { int a;
Using break statement we can come out of the loop cout<<”enter the value”;
instantly. Whenever a break statement is encountered cin>>a;
inside a loop in if condition, the control directly comes cout<<”the value is”<<a; }
out of loop and the loop gets terminated for rest of the
iterations. It is used along with if statement, whenever b) void main()
used inside loop so that the loop gets terminated for a { inta=1,b=2,c;
particular condition. c=a+b+,
cout<<c }
Q-30) Convert the following expressions into short
hand expression: Correction:
1. a=a+(c/b) a+=(c/b) void main()
2. c=c-(a+b/d) c-=(a+b/d) {
int a=1,b=2,c=0;
Q-31) Write the difference between entry control loop c=a+b;
and exit control loop? cout<<c; }
For loop and while loop are the examples of entry
controlled loop. Here if test condition is true then c) void main()
loop body will be executed. Do while loop is the { float a=6.0,b=3.0,c;
example of exit controlled loop. Exit controlled loop is c=a\b;
used when condition can be checked at the last after cout<<c; }
executing the loop body at least once.
Correction:
Q-32) Explain the use of assignment operator (=) and void main()
relational operator == with an example of each. { float a=6.0,b=3.0,c;
= operator - The “=” is an assignment operator and is c=a/b;
used to assign the value on the right to the variable on cout<<c; }
the left. For example: a = 10; b = 20; ch = 'y';
The ‘==’ operator checks whether the two given d) void main
operands are equal or not. If so, it returns true. { int x=4,y=5z;
Otherwise it returns false. For example: 5==5, This will z=((y>x)(x<y))&&(x==y));
return true. cout<<z; }
Correction: Correction:
void main void main()
{ int x=4,y=5,z; { int a=20;
z=((y>x)||(x<y)&&(x==y)); while(a>=11)
cout<<z; } {
cout<<a;
e) void main() a--;
{ int a,b; }}
cout<<”enter the value of a &b”;
cin>>a>>b; i) void main()
c=a/4+b+1, { int a= 10;
cout<<c; } do while(a<=10)
{
Correction: cout<<a;
void main() a--;
{ int a,b; }}
cout<<”enter the value of a &b”;
cin>>a>>b; Correction:
c=a/4+b+1; void main()
cout<<c; } { int a= 10;
do
f) void main() {
{ int a=1,b=2; cout<<a;
a=+b; a--;
cout<<a; } } while(a<=10); }

Correction: j) void main()


void main() { inta=0;
{ int a=1,b=2; for(a=1,a<=5,++a)
a+=b; {cout<<a;
cout<<a; } }}

g) void main() Correction:


{ int a=10; void main()
if(a==0) { int a=0;
cout<<”a is zero”; for(a=1;a<=5;++a)
elseif {
cout<<”a is not zero”; } cout<<a;
}}
Correction:
void main() k) void main()
{ int a=10; { int a=10,b=12;
if(a==0) int c==a+b;
cout<<”a is zero”; cout<<c; }
else
cout<<”a is not zero”; } Correction:
void main()
h) void main() { int a=10,b=12;
{ while(a>=11) int c=a+b;
{ cout<<c; }
cout<<a;
a+ l) void main()
}} { int p=1;
switch(p){ int y=t++ + t**;
case p=1: cout<<”hello”;break; cout<<y; }
case p=2: cout<<”world”;break;
default: cout<<”none; } Correction:
void main()
Correction: { int t=2,y=0;
void main() int y=t++ + t++;
{ int p=1; cout<<y; }
switch(p)
{ q) void main()
case 1: cout<<”hello”;break; { int x=10, y=20;
case 2: cout<<”world”;break; if(x==10 and y==20)
default: cout<<”none”; cout<<x<<y; }
}}
Correction:
m) void main() void main()
{ { int x=10, y=20;
int i=1,j=10; if(x==10 && y==20)
for(i=1;i<=j;i==1;) cout<<x<<y; }
cout<<i*i;
} r) void main()
{ int a=10,b=100;
Correction: int x=(a>b) : a ? b;
void main() cout<<x; }
{ int i=1,j=10;
for(i=1;i<=j;i++) Correction:
cout<<i*i; } void main()
{ int a=10,b=100;
n) void main() int x=(a>b) ? a: b;
{ cout<<x; }
int k=10,m=2;
p=k/m; s) void main()
cout<<p; } { int i=1;
for(i=1;<=10;i++)
Correction: if(i==4)
void main() break; }
{ int k=10,m=2,p=0;
p=k/m; Correction:
cout<<p; } void main()
{ int i=1;
o) void main() for(i=1;i<=10;i++)
{ int $w=110; {
int u=w*10; if(i==4)
cout<<u; } { break;}
cout<<i;}
Correction:
void main() Q-35) Write the output for the following codes
{ int w=110,u=0; (consider header files and getch() in each code).
int u=w*10; a) void main()
cout<<u; } { int a=1,b=2,c;
c=a+++,b++;
p) void main() cout<<a<<b<<c; }
{ int t=2; 233
b) void main() }}
{ int a=2,b=4,c; 18
c=(b*8)/4+(b/4)+4-b+(5/8);
cout<<c; } i) void main()
9 { int x=4,y=5,z;
z=(y>=x)&&(!(x==y)||(z>y));
c) void main() cout<<z; }
{ int a=16,b=4; 1
cout<<a/b<<”\n”;
cout<<a%b; } j) void main()
4 { for(int i=0;i<=5;i+=2)
0 {
cout<<i<<”\t”;
d) void main() }}
{ int j=11,i=3,k; 024
k=i%j;
if(k==1) k) void main()
cout<<”k is 1”; { int x,int y; intz;
else if(k==2) z=x%y;
cout<<”k is 2”; cout<<c; }
else Divide error
cout<<”k is not 2”; }
k is not 2 l) void main()
{ int a,b,c;
e) void main() c=(a==b):a?b;
{ int a==4,b=0; cout<<c; }
b=a++*3; 0
cout<<b; }
12 m) void main()
{ int a=9,b=3;
f) void main() int c=++a + b--;
{ int a=1,b=2; cout<<”thevalue of c is”<<c; }
a*4; 13
b+=1;
cout<<a/b; }
n) void main()
0 { int a=16,b=4,c=1;
d=a/b*c+15
g) void main() cout<<d; }
{ for(int i=0;i<=11;i++) 19
{
cout<<i<<”\t”;
o) void main()
}}
{ int a,b;
0 1 2 3 4 5 6 7 8 9 10 11 cout<<”enter the value of a & b”;
cin>>a>>b;
h) void main() if(a>b)
{ int i=2, sum=0; cout<<”a is greater than b”;
while(i<=5) else
{ cout<<”b is greater than a”; }
i=i+1; Enter the value of a and b
sum=sum+i; 20 10
} a is greater than b
cout<<sum;
p) void main() v) void main()
{ int a=7; { int a=20 , b=5,c=10,d;
while(a>=0); d=((a* c/b)-c)%4;
{ cout<<d; }
cout<<a; 2
a--;
}} w) void main()
No output { Int x=5,y=8,z;
(x>y)?cout<<x:cout<<y; }
q) void main() 8
{ int a=7;
do x) void main()
{ { int a=500,b=100,c;
cout<<a; if(!a>=400)
a++; b=300;
while (a<=11); c=200;
}} cout<<b<<”\t”<<c; }
7891011 100 200

r) void main() y) void main()


{ for (a=1,a<=5,a++) { int i=1;
{ switch(i)
cout<<”a”<<”\n”; {
}} case1:
a cout<<”this is case1\n”;
a case 2:
a cout<<”this is case 2\n”;
a default:
a cout<<”this is default\n”;
}}
s) void main() This is case1
{ int a=7,b=2,c,d; This is case2
c=a/b; This is default
d=a%b)
cout<<c<<”\n”<<d; } z) void main()
3 { int s=0;
1 for(int i=1; i<4;i++)
{
t) void main() s=s+1;
{ int a =11,b; }
b=++a; cout<<s; }
cout<<b++<<”\n”<<a”\t”<<b; } 3
12
12 12 aa) void main()
{ int a=4;
u) void main() while(a>1)
{ int a =8,b=13 c; {
c=a++ +++b; cout<<a<<”\t”;
cout<<c; } a--;
22 if(a==3)
{
continue;
} break;
}} }
432 cout <<i<<end1;
}}
bb) void main() 1
{ int i=1; 3
do, 5
{ 7
i=i+1; 9
i++;
cout<<i<<”\t; hh) void main()
} { int z=5,x=10,v=7,b=0;
while(i<=3); } b=(z*v + z)/x;
3 5 switch(b)
{
cc) void main() case: 2
{ int a=1; cout<<”excellent!\n”; break;
for(a=1;a<=10;a+=2) case: 4
cout<<a<<endl; } cout<<”well done!\n”; break;
1 case:6
3 cout<<”good\n”; break;
5 default:
7 cout<<”not a valid entry\n”;
9 }}
Well Done
dd) void main()
{ int a=3,b=4,c=5,d=6,e=0; ii) void main()
e=(a*b + b*c +b)/d; { int p=100,q=10,r=1;
cout<<e; } r=(p>q)?(p+q):(p-q);
6 cout<<r; }
110
ee) void main()
{ int m=10,n=20; jj) void main()
n= ++m; { int k=3;
cout<<n; } if (k==1)
11 cout<<”one”;
else if (k==2)
cout<<”two”;
ff) void main()
else
{ int a=13;
court<<”check the number”; }
if(a%2==0)
cout<<”the number is even”; Check the number
else
cout<<”the number is odd”; } kk) void main()
The number is odd { int i=10;
do
{
gg) void main()
cout<<i<<end1;
{ int i=1;
i--;
clrscr();
} while(i>=5); }
for (i=1;i<=10;i+=2)
{ 10
if (i%2==) 9
{ 8
7 cout<<”Enter any number”;
6 cin>>num;
5 for (reverse = 0; num > 0; num = num / 10)
{
Q-36) rewrite the following program using while loop: reverse = reverse * 10;
#incude<iostream.h> reverse = reverse + num % 10;
#include<conio.h> }
void main() cout<<”Reversed number is”<<reverse; }
{
int x=0,sum=0; Q-38) Convert the following code to while loop.
for(x=1;x<=10;x++) #include <iostream.h>
{ void main( )
sum+=x;) { int number, i = 1, factorial = 1;
cout<<sum; cout<< "Enter a positive integer: ";
getch(); cin>> number;
} for (int i = 1; i<= number; ++i)
{
#incude<iostream.h> factorial=factorial*i;
#include<conio.h> }
void main() cout<<factorial;
{ }}
int x=1,sum=0;
while(x<=10) #include <iostream.h>
{ void main( )
sum+=x; { int number, i = 1, factorial = 1;
x++; cout<< "Enter a positive int.eger: ";
} cin>> number;
cout<<sum; while(i<= number)
getch(); {
} factorial=factorial*i;
i++;
Q-37) Convert the following code from while loop to }
for loop. cout<<factorial; }
#include<iosream.h>
#include<conio.h> Q-39) Write a program to calculate area of a circle
void main() where radius is input by user.
{ clrscr(); #include <iostream.h>
int num,r,reverse=0; void main()
cout<<”Enter any number”; { float radius, area;
cin>>num; // take radius as input
while(num>0) cout << "Enter the radius of circle: ";
{ cin >> radius;
r=num%10; area = 3.14 * radius * radius;
reverse=reverse*10+r; cout << "Area of circle: " << area << endl; }
num=num/10;
} Q-40) Write a program to find to check that entered
cout<<”Reversed number is”<<reverse; } number is divisible by 11 or not
#include <iostream.h>
#include<iosream.h> void main()
#include<conio.h> { int a;
void main() cout << "Enter the number: ";
{ clrscr(); cin >> a;
int num,reverse=0; if (a % 11 == 0)
{ { clrscr();
cout << "The given number is divisible by 11" << endl; int age;
} cout<<"enter your age:";
else cin>>age;
{ if(age>=18)
cout << "The given number is not divisible by 11" << {
endl; cout<<"you are eligible for voting:";
}} }
else
Q-41) Program to find smaller of two numbers entered {
by user using if else. cout<<"you are noneligible for voting:";
#include<iostream.h> cout<<"wait for"<<18-age<<"year(s):";
#include<conio.h> }
void main() getch(); }
{ clrscr();
int a, b, big; Q-44) Write a program to print a series of natural
cout<<"Enter two number : "; numbers upto nth term in c++.
cin>>a>>b; #include <iostream.h>
if(a>b) void main()
{ { int n,i,sum=0;
big=a; cout << " Input a number of terms: ";
} cin>> n;
else cout << "The natural numbers upto "<<n<<"th terms
{ are: \n";
big=b; for (i = 1; i <= n; i++)
} {
cout<<"Biggest of the two number is "<<big; cout << i << " ";
getch(); } }}

Q-42) Write a program to find largest among three Q-45) Write a program to print table of a number.
numbers using nested if else #include<iostream.h>
#include<iostream.h> void main()
void main() { int i,n;
{ int num1, num2, num3; cout<<"Enter any number:";
cout << "Enter three numbers: "<<endl; cin>>n;
cin >> num1 >> num2 >> num3; for(i=1;i<=10;++i)
if(num1 >= num2) cout<<"\n"<<n<<" * "<<i<<" = "<<n*i; }
{
if(num1 >= num3) Q-46) Write a program to print the following pattern.
cout <<num1 << " is the largest number"; *
else **
cout <<num3 << " is the largest number"; ***
} ****
else if(num2 >= num3) #include<iostream.h>
cout <<num2 << " is the largest number"; void main()
else { int i,j;
cout <<num3 << " is the largest number"; } for(i=1;i<=4;++i)
{
Q-43) Write a program to check whether a candidate is for(j=1;i<=i;++j)
eligible to vote or not? {
#include<iostream.h> cout<<" * ";
#include<string.h> }
void main() cout<<”\n”; } }
Q-47) Write a program in C++ to find the electricity bill default: cout<<"Invalid input! Please enter week no.
of a person who consumes 200 units of energy and between 1-7.";
each unit is of Rs.0.75. If the total cost is more than }}
100 then 10% surcharge is added
#include <iostream.h> Q-50) Write a program to print series of odd numbers
#include <conio.h> upto nth term.
void main() #include<iostream.h>
{ clrscr(); void main()
float unit=200; { int i,n;
tc=unit*0.75; cout<<"Enter any number:";
float surcharge=0; cin>>n;
if(tc>100) for(i=1;i<=n;++i)
surcharge=tc*0.10; {
float total_cost; if(n%2!=0)
total_cost = surcharge + tc; {
cout<<"\n\nYOUR BILL AMOUNT IS "<<total_cost; cout<<n<<’\t’;
getch(); } }}}

Q-48) Write a program to swap two numbers without Q-51) Write a program to print the following pattern.
using third variable. 1
#include <iostream.h> 12
void main() 123
{ int a=5, b=10; 1234
cout<<"Before swap a= "<<a<<" b= "<<b<<endl; #include<iostream.h>
a=a*b; //a=50 (5*10) void main()
b=a/b; //b=5 (50/10) { int i,j;
a=a/b; //a=10 (50/5) for(i=1;i<=4;++i)
cout<<"After swap a= "<<a<<" b= "<<b<<endl; } {
for(j=1;i<=i;++j)
Q-49) Write a program to enter a number and print {
name of day using switch statement. cout<<j;
#include <iostream.h> }
void main() cout<<”\n”;
{ int daynumber; }}
cout<<"Enter day number(1-7): ";
cin>>daynumber; Q-52) Write a program to print the following series 1
switch(daynumber) 27 125 343 729.
{ # include<iostream.h>
case 1: cout<<"Monday"; #include<conio.h>
break; void main()
case 2: cout<<"Tuesday"; { int i=0;
break; for( i=1; i<=9;i+=2)
case 3: cout<<"Wednesday"; {
break; cout<<i*i*i<<"\t";
case 4: cout<<"Thursday"; }
break; getch(); }
case 5: cout<<"Friday";
break; Q-53) Write a program to print even numbers between
case 6: cout<<"Saturday"; I and 50 using continue statement.
break; #include<iostream.h>
case 7: cout<<"Sunday"; void main()
break; { int i;
for(i=1;i<=50;i++)
{
if(i%2==0)
{
cout<<i;
}
continue;
}
getch(); }

Q-54) Look at the following program carefully and


answer the questions that follows:
# include<iostream.h>
#include<conio.h>
void main()
{ int i=0,j=9;
for( i=1; i<=j;i+=2)
{
cout<<i<<"\n";
}
getch(); }

a) How many times the above loop will execute?


5 times
b) What will be the value of i after execution of loop?
11

Potrebbero piacerti anche