0% found this document useful (0 votes)
21 views14 pages

CS Notes C++

The document provides a comprehensive overview of C++ programming concepts, including error identification in code snippets, variable declaration rules, differences between control structures, and explanations of various operators and keywords. It also covers topics such as type casting, comments, logical expressions, and the syntax of switch statements. Additionally, it includes practical examples and exercises to illustrate the concepts discussed.
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)
21 views14 pages

CS Notes C++

The document provides a comprehensive overview of C++ programming concepts, including error identification in code snippets, variable declaration rules, differences between control structures, and explanations of various operators and keywords. It also covers topics such as type casting, comments, logical expressions, and the syntax of switch statements. Additionally, it includes practical examples and exercises to illustrate the concepts discussed.
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

Computer Science – I

C++ Programming
Q1 State the error(s) in the following C++ statements, if any. Also, write the output for the statement that
doesn’t show any error.
Consider the initial values: int a =50, c, b; float d = 3.0; Use the changed values of these variables, if any, for
the subsequent statements.
1 c = a % d;
Ans: Error: % (modulus operator) cannot work on float value and variable d is of type float.
2 cout<<a++;
Ans: 50 [Execution: ++ is post-fixed so first value of a is printed as 50 then a will be incremented to 51]
3 d/d * -a = b;
Ans: Error: The left-operand to the assignment operator cannot be an expression; it must always be a
variable.
4 c = !(a != a);
Ans: c = 1 [Execution: !(a!=a) means ! (51 !=51 ) = !(0) = 1, hence 1 is assigned to c]
5 b = 30000 + d;
Ans: b = 30003 [Execution: 30000.0 + 3.0 is 30003.0 but left operand is b of type int hence only
integral part will be stored]
6 cout<<++50;
Ans: Error: ++ cannot work on constant value.

Q2 Define a variable. Mention the rules for naming a variable.


Ans: A variable is a name given to a single or multiple memory location(s) that store value(s).
Rules for naming a variable:
a) A variable name can have alphabets, digits or/and an underscore.
b) The length of the name should not be more than 32 characters.
c) It must not start with a digit. Although, the starting character can be an alphabet or an underscore.
d) Existing keywords’ names must not be used for variables.

Q3 How is switch-case different from if-else?


Ans: 1) A float value cannot be checked in switch-case.
2) A break statement is needed to end the switch block unlike if-else. After the execution of, if block the
control automatically skips the else block; no exclusive statement is required to end an if or else block.
3) Cases can never have a variable expression but if-else can! [Example: case a + 2: is an invalid statement.]
4) Cases can never have a relational or logical expression [Example case a>2:] unlike if-else.

Q4 Mention any two keywords in C++. Also, explain them.


Ans: 1) int: It’s a data-type in C++, used to declare a variable. It allows a variable to store an integer value
within range of -32678 to +32767 with a memory space of 2 bytes.
2) break: It is a keyword that forcefully takes the control out of the block, skipping all the following
statements within the block, where it appears; without checking any condition unlike if statement. Hence,
it is called an unconditional branching statement. But, there are some limitations to it, which is it can be
used only within switch-case, and loop blocks.

Q5 Differentiate between ‘=’ and ‘==’ operators. [Minimum 2 distinct points]


Ans: = ==
This operator assigns (stores) the value of This operator compares the left operand with
right hand side operand/expression to the left right operand for equality and returns true if
hand side operand. they are equal
The left operand must be variable. Both the operands can be any form
Although, the right-operand can be any (variable/constant/expression) evaluating to
expression evaluating to a value. a value.
Example: A = 8 * r Example: 8 == A

Q6 In the statement #include<math.h>, what does ‘h’ stand for? Why is this statement used in a C++ program?
Ans: The ‘h’ here denotes the extension of a header file. The Pre-defined functions to perform
mathematical tasks are collectively defined in “math.h” file. To make use of such functions in any C++
program, we need to import this file to make the compiler aware about its location (where it has been
defined).
Computer Science – I
C++ Programming
Q7 Write a single print statement in C++ to give the following output.
CLASS: XI EXAM SEAT NO.: A123456
DIV : Y NO. OF EXPERIMENTS PERFORMED: 16
Ans: cout<<”CLASS: XI \t EXAM SEAT NO.: A123456 \n DIV : Y \t NO. OF EXPERIMENTS PERFORMED: 16”;

Q8 Justify the statement: The operator ‘-’ in C++, is a unary and binary operator as well.
Ans: In C++, there are many symbols/operators used for various purposes. One of them is, the ‘-’ operator. It is
termed as unary operator when used with single operand otherwise it acts as binary operator when used
with two operands. Well, in both cases it performs different tasks. In case of one operand, it changes the
sign of the operand and in later one it performs subtraction. Example: cout<<-a; cout<<ab;

Q9 If two operators of same precedence are used in an expression then which operator’s task will be performed
first and why? Give an example.
Ans: If the operators with same precedence are used in an expression then the compiler resolves the ambiguity
with the operations using ‘associativity’. Each set of operators have been given associativity as left to right
or right to left. If the associativity is left to right then whichever operator’s expression appears first will be
evaluated followed by the next.
Example, a* b * c. a * b will be solved first and the result will be multiplied with c.

Q10 Explain the following C++ terms, with an example.


1 main( )
Ans: The execution of every C++ program starts from main ( ). So, main ( ) is a must function in any
C++ program. The instructions for whatever task(s) is/are to be performed must be enclosed
within a pair of braces of main ( ) block.
Example: void main ( )
{
cout<<”\n hello world”;
}
2 variable declaration statement
Ans: Every variable must be declared just once before it is used. Declaring a
variable means specifying the data-type of the corresponding variable. Example: char c; In the
given example, c is a variable of type character; capable of storing any single character value which
needs one byte of memory space for storage.
3 Decrement operator
Ans: The decrement operator is represented as -- in C++. It decrements the value of the
operand by 1 and stores the decremented value in the same operand. Example: int a =6; --a; In this
example, the value of a will be decremented to 5 and this value will be stored in variable a itself. As
given in the syntax/example, it has just one operand hence; it is termed as unary operator. But, the
operand must always be a variable.

Q11 Explain insertion and extraction operators, in C++, with an example of each.
Ans: a) The operators used for input and output are >> and << called as extraction operator and insertion
operator.
b) Insertion Operator: The Insertion operator ‘<<’ inserts the data that follows it into the stream (cout
i.e. console output screen) that precedes it.
Eg : cout<<” Welcome to C++ programming”;
c) Extraction Operator: The Extraction operator ‘>>’ is used to extract the data given as an input. The
input given will be stored in a variable that follows it (written after the extraction operator)
Eg : cin>>a;

Q12 Analyze the following set of C++ codes and explain the difference in the output generated with the given
codes.
a) cout<<”\n Hi”; b) clrscr( );
clrscr( ); cout<<”\nHi”;
Ans: a) the code print the statement “Hi” on the output screen and immediately the clear screen function is
called because of which the output generated on output screen is cleared off.
b) the code first clears the output screen which had the output of previous execution and immediately
displays the statement “Hi” represented by the following cout statement.
Computer Science – I
C++ Programming
Q13 Explain compile time and run time variable initialization in C++, with an example.
Ans: In compile initialization the variable’s initial value is preset in the program itself and known to the
compiler at the time of compilation. Example: sum = 0;
In run time initialization the variable’s initial value is not predetermined. It can be assigned by the user at
the time of execution of program or it could be set by an expression which is not known to the compiler
beforehand. Example: area = 3.14 * r * r;

Q14 State and explain any two backslash sequence character in C++, with an example.
Ans: ‘\n’ , ‘\t’ and ‘\a’ are the backslash sequences in C++.
\n is a new line character sequence. It is used to break the current line and go to the first column in the
next line.
\t is a horizontal tab character. It is used to give multiple spaces ( generally 8 character spaces) from the
current position.
\a is a sound character. It is used to generate an alert bell.
Example: cout<<”\n Name:\tSachin Tendulkar”;

Q15 Write and explain the syntax of a conditional operator in C++.


Ans: the syntax of conditional operator:
Expression 1 ? Expression 2 : Expression 3
If the condition is Expression 1 evaluates to true value then Expression 2 is executed else Expression 3 will
be executed.

Q16 What is the use of ‘default’ and ‘break’ keywords in switch block, in C++?
Ans: default: if the all the mentioned cases’ value doesn’t match with switch value then default case statements
will be executed. It is like the else block in if-else structure.
break: it is used to stop the further execution of switch-case block and transfer the control of execution to
the statement following the switch block.

Q17 Explain cascading in C++, with an example.


Ans: Combining multiple input/output (cin/cout) statements into single input/output (cin/cout) statement is
called as cascading.
Example: cout<<”My roll no. is”;
cout<<roll;
The above 2 cout statements can be combined as
cout<<”My roll no. is “<<roll;

Q18 What is the difference between ‘signed int’ and ‘unsigned int’ in C++, with an example?
Ans: A variable of type signed int can store a –ve , 0 or a +ve integer value within the range of -32768 to +
32767. A variable of type unsigned int can store a 0 or +ve integer value within the range of 0 to 65536.

Q19 If a = 10, b = 12, c = 0, find the values of the expressions in the following table:
Expression Value
a != 6 && b > 5
a == 9 || b < 3
! (a < 10)
!a > 5 && c
Ans: Expression Value
a != 6 && b > 5 1
a == 9 || b < 3 0
! (a < 10) 1
!a > 5 && c 0

Q20 What is Type Casting ? Explain how a ‘float’ value be type casted into an ‘int’ value
Ans: Type Casting is a process in which the datatype of a particular variable is changed to some other datatype
temporarily i.e just for a single operation .Eg : An 'int' datatype variable can be converted to 'float'.
There are two different syntax for type conversion
float x =10.3;
cout<<int(x);
OR
float x = 10.3;
int a=x;
cout<<x;
Computer Science – I
C++ Programming

Q21 Give two differences between unary and binary operators. [Note: Include an example]
Ans: Unary operators Binary operators
1) These operators need only one operand to 1) These operators need two operands to
perform an operation perform an operation.
2) Associativity of unary operator is from 2) Associativity of binary operator can be
Right to Left. from Right to Left or Left to Right.
3) Eg: ++ (increment operator), - (unary 3) Eg: + (Addition), ==(Equal to) etc..
minus operator) etc.

Q22 Write a C++ statement that


a) Prints “ You are an adult” if age is from 18 to 60
b) Prints whether the value of a variable 'c' is a vowel.
Ans: a) if(age>=18 && age<=60)
cout<<”You are an adult”;

b) if(c==’a’|| c==’e’|| c==’i’|| c==’o’|| c==’u’||c==’A’|| c==’E’|| c==’I’|| c==’O’|| c==’U’)


cout<<”It is a vowel”;

Q23 Describe the two ways to include comments in a C++ program


Ans: The two ways to include comments in C++ program are:
1) Single line comments: Any sentence after the double forward slash(//) is called single line comment. It
is only for that particular line.
Eg: //Welcome to C++ programming
2) Multiple line comments: To give multiple line comments, the comment should start with a delimiter /*
and end with the delimiter */
Eg: /* Welcome to C++ programming
This is my first program */

Q24 Construct a logical expression to represent each of the following conditions :


a) n is divisible by 3 but not by 30
b) answer is either ‘n’ or ‘N’
Ans: a) n % 3==0 && n % 30 !=0
b) ans==’n’ || ans==’N’

Q25 Define the term Variable. Are the following C++ statements same or different? Explain.
float a;
float a=1.5;
Ans: A variable is an identifier that is used to represent data item /items i.e a numerical quantity, character or a
string value.
The given C++ statements are different
float a;
This statement is just a variable declaration statement in which the variable ‘a’ will hold a garbage
(unknown) value while the next C++ statement i.e
float a=1.5;
is variable declaration as well as variable initialization in which the variable ‘a’ will hold a float value 1.5.

Q26 Enlist 3 basic datatypes used in C++ with range of values and bytes they occupy in memory.
Ans: Datatype Range Bytes
char -128 to 127 1
int -32768 to 32767 2
float -3.4e38 to +3.4e38 4
double -1.7e308 to +1.7e308 8
Computer Science – I
C++ Programming
Q27 Write and explain the syntax of switch statement. Also state 2 disadvantages of switch- case .
Ans: The syntax of switch- case statement is as follows:
switch(expression or variable)
{
case value_1:
statements;
break; //optional
case value_2:
statements;
break;
case value_n:
statements;
break;
default:
statements;
}

Explanation:
The switch statement is a selection statement. Followed by the keyword 'switch', a variable or an
expression is placed in the parenthesis. The body of the switch statement is enclosed in braces .It consists
of 'Case Labels', which take the keyword 'case' followed by a constant value. The case labels are followed
by a colon. When a switch variable or expression is evaluated, the control is transferred to the case label
whose constant is equal to the derived value and then all the statements followed by the case label are
executed. The break statement is used to terminate the execution of switch block. Special case label
'default' is used if the expression does not match any of the case label

Disadvantages:
1) A float expression cannot be tested using switch.
2) Multiple cases cannot use same expressions.
3) Cases can never have variable expressions.( Eg: case x+4: is invalid)

Q28 Define the following terms with respect to C++


1 Keywords
Ans: Keywords are the special words, which are designated to do a specific task and whose meaning is
already known to the compiler.
2 Compiler
Ans: Compiler is a computer program that accepts a program written in high level language as input
and generates a corresponding machine language program as output. It translates the entire
program into machine language before executing any of the instruction.
3 Identifiers
Ans: Identifiers are names that are given to various program elements, such as variables, functions etc.
Identifiers consists of letters, digits and underscore, except that the first character must not be a
digit.
4 Cascading
Ans: Combining multiple cout/cin statements into a single cout /cin, in c++, is called as Cascading
5 Literals
Ans: Literals refer to fixed values that the program cannot alter. Literals can be of any of the basic data
types and can be divided into Integer Numerals, Floating-Point Numerals, Characters and string.
6 Escape sequence
Ans: Certain non-printing characters can be expressed in terms of escape sequences. An escape
sequence always begins with a backward slash and is followed by one or more characters eg. ‘\n’
as a newline character

Q29 Justify the following: Every C++ program should have a main function.
Ans: 1) When a C++ program is under execution, the control of the computer is passed over to that program.
The key point is that the computer needs to know where inside your program the control needs to be
passed. In the case of a C++ program, it's the main() function that the computer is looking for.
2) At a minimum, the main() function looks like this:
main() {}
Like all C++ language functions, first comes the function's name, main, then comes a set of parentheses,
and finally comes a set of braces, also called curly braces.
If a C++ program contains only this line of code, it can be executed. It won't do anything, but that's perfect
because the program doesn't tell the computer to do anything. Even so, the computer found the main()
Computer Science – I
C++ Programming
function and was able to pass control to that function — which did nothing but immediately return control
right back to the operating system.
[Note: Only 1st point is required for as the solution to the justification. The 2nd point is just a supporting
explanation to it.]

Q30 Give 2 points of difference between ‘!’ and ‘!=’ operators.


Ans: 1) ‘!’ is a Logical negation operator while ‘!=’ is a Relational Operator
2) '!' is an unary operator while '!=' is a binary operator
3) ‘!’ operator causes an expression that is originally true to become false or vice versa while ‘!=’ checks
whether its first operand is not equal to the second operand.

Q31 Write four different expressions which increments the value of integer variable x
Ans: The 4 different expressions are :
1) x=x+1;
2) x++;
3) ++x;
4) x+=1;

Q32 What will be the output of the following program:


void main()
{
int a=5, b;
b=++a+a+++a;
cout<<a<<”\t”;
cout<<b;
}
Ans: The output of the above program will be:
7 18

Q33 Find and rectify the errors in the following C++ code
1 void main
{
float a=12.23 ; b=12.52;
IF(a==b)
{
cout<<” a and b are equal”;
}
Ans: void main()
{
float a=12.23 , b=12.52;
if(a==b)
{
cout<<” a and b are equal”;
}
}
2 void main()
{
Int a,b,c=20;
a=b=c;
if(a!20)
cout<<” Hello”
elseif(b==20)
cout<<” Hii”;
}
Ans: void main()
{
int a,b,c=20;
a=b=c;
if(a!=20)
cout<<” Hello”;
else if(b==20)
cout<<” Hii”;
}
Computer Science – I
C++ Programming
3 #include<iostream.h
#include<conio.h;
void main(
{
clrscr{ };
cout<<“\nhello world”;
}
Ans: #include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
cout<<“\nhello world”;
}
4 #include<iostream.h>
#include<conio.h>
VOID main()
{
Int a;
cout“\nenter value of a ”;
cin>>a
Ans: #include<iostream.h>
#include<conio.h>
void main( )
{
int a;
cout << “\nenter value of a ”;
cin>>a;
5 #include<iostream.h>
#include<conio.h>
Void main()
{
float a=1.2, b
a + 2.2 = b
Cout<<a
}
Ans: #include<iostream.h>
#include<conio.h>
void main()
{
float a=1.2, b ;
b = a + 2.2 ;
cout<<a ;
}
6 void main()
{
cout“\n enter an integer value ”;
cin>>a
}
Ans: void main()
{
cout << “\n enter an integer value ”;
cin>>a ;
}

Q34 Write a C++ arithmetic statement for the mathematical expression


2 + 6.22 ( + )
=
+
Ans: R = (2 * v + 6.22 * (c +d)) / (g + v);
Computer Science – I
C++ Programming
Q35 Write a C++ statement for the following:
a) d= b2-4ac
Ans: d = b * b – 4 * a * c
b) p=2(l+w)
Ans: p=2*(l+w)
c) a=1/2(b1+b2)h
Ans: A = 1 / 2 * (b1 + b2) * h
d) d=a2+2ab+b2
Ans: d=a*a+2*a*b+b*b
e) e=b2-4ac/2a
Ans: e=b*b–4*a*c/2*a
f) c=(a+b)2
Ans: c = (a + b) ^ 2

Q36 Assume the declarations: int a=8, b=3, c=-5, d, e, r;


1 What will be the value of variables d and e after the execution of the following statements?
d = (a*c)%b;
e = a*(c%b);
Ans: d = -1
e = -16
2 What will be the value of variable r after the execution of the following statement?
r = 2*b+3*(a-c);
Ans: r = 45
3 Consider the following code and find the value of variable r?
int a=5, b = 2;
float c=1.0;
r = b / (c * a);
Ans: r=0
4 What will be the value of variables r, m1 and m2, if int a=5,b=6;
1) r=++a + b--;
2) m1=++a * 7;
3) m2=a++ * 7;
Ans: r = 12
m1 = 49
m2 = 49
5 void main()
{
int x=8;
cout<<x++;
}
Ans: 8
6 void main()
{
int b =2, c= 3, d;
d = b + c – 5 * b % c;
cout<<d;
}
Ans: 4
7 Assume int n = 4, a = 1, t = 3, d; what will be the value of d after evaluating the following
expression?
d = n * a / 2 + 3 / 2 * a + 2 + t;
Ans: d=8
8 Assume int n = 4, a = 1, t = 3, d; what will be the value of d after evaluating the following
expression?
d = n * a / ((2 + 3) / 2 * a) + 2 + t;
Ans: d=7
Computer Science – I
C++ Programming
9 What will be the output of following code?
int n1=50, n2=4, q, r;.
q = n1/n2;
r=n1-(q*n2)
cout<<“The quotient and remainder are : ”<<q<<” ”<r;
Ans: The quotient and remainder are : 12 2

Q.1. Write a C++ program to find minimum of three numbers (using nested if).
Ans: #include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int a,b,c;
cout<<"Enter 3 numbers : ";
cin>>a>>b>>c;
if(a<b)
{
if(a<c)
cout<<"Minimum is "<<a<<endl;
else
cout<<"Minimum is "<<c<<endl;
}
else
{
if(b<c)
cout<<"Minimum is "<<b<<endl;
else
cout<<"Minimum is "<<c<<endl;
}
getch();
}

Q.2. Write a C++ program to print the day of a week using switch-case construct.
Ans: #include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int day;
cout << "Enter a number : ";
cin >> day;
switch(day)
{
case 1 : cout << "Sunday";
break;
case 2 : cout << "Monday";
break;
case 3 : cout << "Tuesday";
break;
case 4 : cout << "Wednesday";
break;
case 5 : cout << "Thursday";
break;
case 6 : cout << "Friday";
break;
case 7 : cout << "Saturday";
break;
}
getch();
}
Computer Science – I
C++ Programming
Q.3. Write a C++ program which accepts two numbers and operator. According to operator, the
program should display expected results.
Ans: #include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int a,b,c,d,e;
char oper;
float f;

cout << "Enter first number : ";


cin >> a;
cout << "Enter the operator : ";
cin >> oper;
cout << "Enter second number : ";
cin >> b;

if(oper == '+')
{
c = a + b;
cout << "The result : " << c;
}
else if(oper == '-')
{
d = a - b;
cout << "The result : " << d;
}
else if(oper == '*')
{
e = a * b;
cout << "The result : " << e;
}
else if(oper == '/')
{
if(b == 0)
{
cout << "Denominator should not be 0";
}
else
{
f = (float) a/b;
cout << "The result : " << f;
}
}
else
cout << "Invalid operator";
getch();
}

Q.4. Write a C++ program to display odd numbers between 1 – 100 using for loop.
Ans: #include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int i;
for (i=1;i<=100;i=i+2)
{
cout << i << "\t";
}
getch();
}
Computer Science – I
C++ Programming

Q.5. Write a menu-driven program in C++ that performs one of the following tasks as per user’s choice.
1. To calculate area of circle
2. To calculate area of square
3. To calculate area of rectangle
4. To calculate area of sphere
Ans: #include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int s;
float r,L;

cout << ”Enter….. 1. To calculate area of circle”


<< ”\n2. To calculate area of square”
<< ”\n3. To calculate area of rectangle”
<< ”\n4. To calculate area of sphere”;
cin>>s;

switch(s)
{
case 1: cout<<”\n Enter the radius of a circle”;
cin>>r;
cout<<”\n the area of circle is “<<3.14*r*r;
break;
case 2: cout<<”\n Enter the side of a square”;
cin>>r;
cout<<”\n the area of square is “<<r*r;
break;
case 3: cout<<”\n Enter the length and breadth of a rectangle”;
cin>>L>>r;
cout<<”\n the area of rectangle is “<<L*r;
break;
case 4: cout<<”\n Enter the radius of a sphere”;
cin>>r;
cout<<”\n the area of sphere is “<<4*3.14*r*r;
break;
default: cout<<”\n Sorry.. Invalid input”;
}
getch();
}

Q.6. Write a C++ program to read marks obtained in 6 subjects by a student from the user. Calculate
and print total marks, percentage and Class (as per the given criteria) obtained by the student with
a suitable message.
[NOTE: Distinction with first class for 75% or above, First class for 60% or above but less than 75%,
Second class for 45% or above but less than 60%, Pass class for 35% or above but less than 45%;
otherwise failed for percentage below 35 or students scoring less than 35 in any one of the
subjects.]
Ans: #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int p, c, m, e, cs1, cs2, total;
float per;

cout<<”\n enter the marks obtained in 6 subjects”;


cin>>p>>c>>m>>e>>cs1>>cs2;

total = p + c + m + cs1 + cs2 + e;


per = total / 6.0;
Computer Science – I
C++ Programming

if (per<35 || p<35 || c<35 || m<35 || e<35 || cs1<35 || cs2<35)


cout<<”\n total marks =”<<marks<<” percentage = “<<per
<<”\n the student has failed”;
else if (per>=75)
cout<<”\n total marks =”<<marks<<” percentage = “<<per
<<”\n the student has passed with distinction”;
else if (per>=60)
cout<<”\n total marks =”<<marks<<” percentage = “<<per
<<”\n the student has passed with first class”;
else if (per>=45)
cout<<”\n total marks =”<<marks<<” percentage = “<<per
<<”\n the student has passed with second class”;
else
cout<<”\n total marks =”<<marks<<” percentage = “<<per
<<”\n the student has passed with pass class”;
getch();
}

Q.7. Write a program in C++ to read a character from the user. Check and print whether the given
character is an alphabet, digit or a special symbol.
Ans: #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char n;

cout<<”\n enter a character value”;


cin>>n;

if ((n>=’A’ && n<=’Z’) || (n>=’a’ && n<=’z’))


cout<<”\n the given character is an alphabet”;
else if (n>=’0’ && n<=’9’)
cout<<”\n the given character is a digit”;
else
cout<<”\n the given character is a special symbol”;
getch();
}

Q.8. Write a C++ program to read four distinct integer values from the user. Find and print the greatest
integer from the given ones. It must give an appropriate message if the values are not distinct.
Ans: #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, c, d;
cout<<”\n enter four distinct integer values”;
cin>>a>>b>>c>>d;

if (a ==b || a==c || a==d || b==c || b ==d || c==d)


cout<<”\n the given integers must be distinct”;
else if (a>b && a>c && a>d)
cout<<”\n the greatest integer is “<<a;
else if(b>c && b>a && b>d)
cout<<”\n the greatest integer is “<<b;
else if(c>a && c>b && c>d)
cout<<”\n the greatest integer is “<<c;
else
cout<<”\n the greatest integer is “<<d;
getch();
}
Computer Science – I
C++ Programming

Q.9. Write a Menu-driven program in C++, to read a measurement from the user in Feet (F) or in Inches
(I), as per the user’s choice. The program converts the given measurement from one unit into the
other. Display the converted value along with the given value, with a suitable message.

Sample Output:
Convert 1. Feet to Inches 2. Inches to Feet
Enter your choice 1
Enter the measurement in Feet 8
The given measurement 8 feet is equivalent to 96 inches.
Ans: #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int f,i,ch;

cout<<"\n Convert : \t 1.Feet to Inches \t 2.Inches to Feet";


cout<<"\n Enter your choice : ";
cin>>ch;

switch(ch)
{
case 1:
cout<<"\n Enter the measurement in Feet : ";
cin>>f;
cout<<"\n The given measurement "<<f<<" Feet is equivalent to "
<<f*12<<" inches";
break;
case 2:
cout<<"\n Enter the measurement in Inches : ";
cin>>i;
cout<<"\n The given measurement "<<i<<" inches is equivalent to "
<<(i/12)<<" feet";
break;
default:
cout<<” Enter a valid option”;
}
getch();
}

Q.10. Write a program in C++ to display the output in following format:


*
* *
* * *
* * * *
* * * * *
Ans: #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for (i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout << "*\t";
}
cout << "\n";
}
getch();
}
Computer Science – I
C++ Programming

Q.11. Write a program in C++ to display the output in following format:


* * * * *
* * * *
* * *
* *
*
Ans: #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for (i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
cout << "*\t";
}
cout << "\n";
}
getch();
}

Q.12. Write a program in C++ to generate n terms of Fibonacci Series.


Ans: #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, ft = 0, st = 1, tt;
cout << "Enter number of terms : ";
cin >> n;

cout << ft << " " << st << " ";


for(i=3; i<=n; i++)
{
tt = ft + st;
cout << tt << " ";
ft = st;
st = tt;
}
getch();
}

Q.13. Write a program in C++ to calculate sum of all elements in given array.
Ans: #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[] = {1,2,3,4,5};
int i, sum = 0;

for(i=0;i<5;i++)
{
sum = sum + a[i];
}
cout << "SUM = " << sum;
getch();
}

You might also like