What is the function of operators? What are arithmetic operators?

Q1. What is the function of operators? What are arithmetic operators? Give their examples.

Ans. The operations being carried out on data, are represented by operators.

C++ provides operators for five basic arithmetic calculations: addition, subtraction, multiplication, division and remainder which are +,-,*,and %.

Difference between a variable, reference variable, a constant variable and a pointer variable.

Q3. Explain the difference between a variable, reference variable, a constant variable and a pointer variable. Support your answer with examples.

Ans.

Variable

Variables represent named storage locations, whose values can be manipulated during program run. For instance, to store name of a student and marks of a student during a program run, we require storage locations that too named so that these can be distinguished easily. Variables, called as symbolic variables, serve the purpose. The variables are called symbolic variables because these are named locations. For instance, the following statement declares a variable i of the data type int:

int i;

 

Reference Variable

A reference is an alternative name for an object. A reference variable provides an alias for a previously defined variable. A reference declaration consists of a base type, an & (ampersand), a reference variable name equated to a variable name. The general form of declaring a reference variable is Read More …

User-defined data types in C++ with examples.

Q2. Explain user-defined data types in C++ with appropriate examples.

Ans. There are some derived data types that are defined by the user. These are: Class, Structure, Union and Enumeration.

  1. Class

A class represents a group of similar objects. To represent classes in C++, it offers a user-defined data type called class. Once a class has been defined in C++, objects belonging to that class can easily be created. A class bears the same relationship to an object that a type does to a variable.

To define a class you describe what sort of information it can represent and what sort of actions you can perform with that data. For instance, if you want to create a class department. It can represent information like name of the department, number of its employee, name of its head and actions that can be performed with this information are : addition, deletion, modification of employee data, printing of department report etc.. now this class will be declared in C++ as follows:

 

class department

{

char name[20];

int num_emp;

char h_o_d[20];

public:

add();

delete(); Read More …

C++ program to print ASCII code for given digit and backspace.

Q28. Write a program to

  • print ASCII code for a given digit.
  • print ASCII code for backspace.

(Hint: Store escape sequence for backspace in an integer variable).

Ans(i).

#include<iostream.h>

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

 

//Main Function

void main()

{

int num;

clrscr();  //for clear screen

Read More …

C++ program to compute simple interest and compound interest.

 Q27. Write a program to compute simple interest and compound interest.

Ans.

#include<iostream.h>

#include<math.h>   //for pow()

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

 

//Main Function

void main()

{

float principle,rate,time,n,SI,CI,f;

clrscr();   //for clear screen

 

cout<<“\n Enter Principle Amount: “;

cin>>principle;

cout<<“\n Enter Interest Rate: “; Read More …

C++ program to read two numbers and print quotient and remainder.

Q26. Write a program to read two numbers and print their quotient and remainder.

Ans.

#include<iostream.h>

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

 

//Main Function

void main()

{

int dividend,divisor,remainder,quotient;

clrscr();  //for clear screen

 

cout<<“\n Enter the dividend: “;

cin>>dividend; Read More …

C++ program to convert inches into yards, feet and inches.

Q25. Write a program to convert given inches into its equivalent yards, feet and inches.

(1 yard = 36 inches, 1 foot = 12 inches)

Ans.

#include<iostream.h>

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

 

//Main Function

void main()

{

float inches,feet,yard;

clrscr();  //for clear screen

 

cout<<“\n Given input in inches: “;

cin>>inches;

 

feet=inches/12;

yard=feet/36;

 

cout<<“\n Output in yards: “<<yard<<” yards”;

cout<<“\n Output in feet: “<<feet<<” feet”;

cout<<“\n Output in inches: “<<inches<<” inches”;

 

getch();  //To hold the output screen

}

//End of main()

 

Output:

Given input in inches: 3600

 

Output in yards: 8.333333 yards

Output in feet: 300 feet

Output in inches: 3600 inches

C++ program to accept three digits (that is, 0-9) and print all possible combinations.

Q24. Write a program to accept three digits (that is, 0-9) and print all possible combinations from these digits. (For example, if the three digits are 1, 2 and 3 then all possible combinations are 123, 132, 231, 213, 312 and 321).

Ans.

#include<iostream.h>

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

 

//Main Function

void main()

{

int a[3],i,n=0,swap;

clrscr();  //for clear screen

cout<<“\n Enter three numbers: “; Read More …

C++ program to find even or odd number.

Q23. Write a program to find whether a given number is even or odd.

Ans.

#include<iostream.h>

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

 

//Main function

void main()

{

int n;

clrscr();  //for clear screen

cout<<“\n Enter a number: “; Read More …