## C++ program single cout statement.

Q5. Write a program to display the following output using a single cout statement.

Program = 20

Documentation = 23

Logic = 21

Flow chart = 18

Ans.

#include<iostream.h>

#include<conio.h>   //for clrscr()

void main()

{

clrscr(); //for clear screen

cout<<“\n Program = 20 \n Documentation = 23 \n Logic = 21 \n Flow chart = 18”;

}

Output:

Program = 20

Documentation = 23

Logic = 21

Flow chart = 18

## C++ Program for Calculating Percentage.

Q4. Write a C++ program that inputs a student’s marks in three subjects (out of 100) and prints the percentage marks.

Ans.

#include<iostream.h>

#include<conio.h>  //for clrscr()

int main()

{

clrscr();  //for clear screen

float sub1,sub2,sub3,marks,perc;

cout<<“Enter marks obtained in 3 subjects:”;

cin>>sub1>>sub2>>sub3;

marks=sub1+sub2+sub3;

perc=(marks/300)*100;

cout<<“\n”<<“The percentage marks are:”<<perc<<“%”;

return 0;

}

Output

Enter marks obtained in 3 subjects:69 89 78

The percentage marks are:78.666664%

## C++ program to calculate square of number.

Q3. Write a program to calculate the square of a number.

Ans.

#include<iostream.h>

#include<conio.h>    //for clrscr()

int main()

{

clrscr(); //for clear screen

int var,sqrs;

cout<<“Enter variables:”;

cin>>var;

sqrs=var*var;

cout<<“\n”<<“The square is: “<<sqrs;

return 0;

}

Output

Enter variables:5

The square is: 25

## C++ program to print sum of two values.

Q2. Write a program to print sum of two values.

Ans.

#include<iostream.h>

#include<conio.h> //for clrscr()

int main()

{

clrscr();  //for clear screen

int value1, value2, sum;

cout<<“Enter First Value: “;

cin>>value1;

cout<<“Enter Second Value: “;

cin>>value2;

sum=value1 + value2;

cout<<“The sum of given values is: “;

cout<<sum;

return 0;

}

Output

Enter First Value: 6

Enter Second Value: 7

The sum of given values is: 13

## C++ program to print string.

Q1. Write a program to print a string on the screen.

Ans.

//My first C++ PROGRAM

#include<iostream.h>

int main()

{

cout<<“WELCOME TO C++ PROGRAMMING”;

return 0;

}

Output

WELCOME TO C++ PROGRAMMING

## Role of Compiler.

Q4. Explain the role of a compiler in a programming language. What type of errors it generally reports?

Ans. A part of the compiler’s job is to analyze the program code for “correctness”. If the meaning of a program is correct, then a compiler can not detect error (for example, if different statements are used etc.). but a compiler can certainly detect error in the form of a program. Some common forms of program errors are as given below:

1. Syntax Errors

Syntax error occur when rules of programming language are misused, that is, when a grammatical rule of C++ is violated.

Syntax refers to formal rules governing the construction of valid statements in a language. Read More …

## iostream.h in C++.

Q3. Explain the role and importance of file iostream.h in C++.

Ans. The header file iostream.h is included in every C++ program to implement input/output facilities. Input/Output facilities are not defined with C++ language, but rather are implemented through a component of C++ standard library, iostream.h which, is I/O library.

In C++, all devices are treated as files. Thus, the standard input device (the keyboard) (where from the input is received), the standard output device (the screen/monitor) (where the output is displayed) and the standard error device (the screen/monitor) (where the errors, if any, are displayed) are all treated as files. At its lowest level, a file is interpreted simply as a sequence, or stream of bytes. At this level, the notion of a data type is absent, that is, data is treated simply as sequence of bytes without considering its data type. However, at the user level, a file consists of a sequence of possibly intermixed data types-characters, arithmetic values, class objects etc.

The Function of I/O Library – iostream.h Read More …

## types of literals in C++

Q2. Explain different types of literals in C++ by giving appropriate examples.

Ans. Literals (often referred to as constants) are data items that never change their value during a program run.

C++ allows three types of literals:

1. Integer Constant
2. Character Constant
3. Floating Constant
4. String Literal

For example:

1234, ‘t’, 2.0, “rty”, etc.

Integer Constant

Integer constants are whole numbers without any fractional part.

The method of writing integer constants has been specified in the following rule:

An integer constant must have at least one digit and must not contain any decimal point. It may contain either + or – sign. A number with no sign is assumed to be positive. Commas cannot appear in an integer constant.

For example, 1234, 41, +97, -17.

Character Constant

A character constant is one character enclosed in single quote, as in ‘z’.

The rule for writing character constant is given below:

A character constant in C++ must contain one character and must be enclosed in single quotation marks. Read More …

## Tokens in C++.

Q1. Explain tokens in C++. Also discuss their role and importance.

Ans. The smallest individual unit in a program is known as a token.

C++ has the following tokens:

1. Keywords
2. Identifiers
3. Literals
4. Punctuators
5. Operators

1. Keywords

Keywords are the words that convey a special meaning to the language compiler. These are reserved for special purpose and must not be used as normal identifier names.

For example:

break, delete, if, goto, int, char, etc.

1. Identifiers

Identifiers are fundamental building blocks of a program and are used as the general terminology for the names given to different parts of the program through variables, objects, classes, functions, arrays etc.

For example:

Myfile, MYFILE, _CHGK, DATE_7_73,etc.

Identifier forming rule of C++ states the following:

An identifier is an arbitrarily long sequence of letters and digits. The first character must be a letter; the underscore (_) counts as a letter. Upper and lower-case letters are different. All characters are significant.

1. Literals

Literals (often referred to as constants) are data items that never change their value during a program run.

C++ allows three types of literals:

• Integer Constant
• Character Constant
• Floating Constant
• String Literal

For example:

1234, ‘t’, 2.0, “rty”, etc. Read More …

## C++ program that print their sum.

Q27. Write a C++ program to accept two integers and print their sum.

Ans.

#include<iostream.h>

#include<conio.h>    //for clrscr()

void main()

{

int value1,value2,sum;

clrscr();   //for clear screen

cout<<“\n Enter the first value: “;

cin>>value1;

cout<<“\n Enter the second value: “;

cin>>value2;

sum=value1+value2;

cout<<“\n The sum of these values is “<<sum;

}

Output

Enter the first value: 34

Enter the second value: 45

The sum of these values is 79