## C++ program to find area of a triangle.

Q22. Write a program to find area of a triangle.

Ans.

#include<iostream.h>

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

//Main() function

void main()

{

float height,base,area;

clrscr();  //for clear screen

cout<<“\n Enter the height and base of the triangle: “; Read More …

## C++ program to read number n and print n2, n3, n4 and n5.

Q21. Write a program to read a number n and print n2, n3, n4 and n5.

Ans.

#include<iostream.h>

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

//Main function

void main()

{

int n,n2,n3,n4,n5;

clrscr();  //for clear screen

cout<<“\n Enter a number: “;

cin>>n;

## C++ program for electricity board charges.

Q20. An electricity board charges according to following rates:
For the first 100 units – 40 P per unit  (P-Paise)

For the next 200 units – 50 P per unit

Beyond 300 units – 60 P per unit.

All users are charged meter charge also which is R50/-.

Write a program to read the names of users and number of units consumed, and print out the charges with names.

Ans.

#include<iostream.h>

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

//A class called customer to hold all the information related to customer

class customer

{

char name[20];

int units;

float charge;

//Member functions of class customer

public:

//A function of class customer to read name and number of units of customer

{

cout<<“\n Enter the name of customer: “;

cin>>name;

cout<<“\n Enter the number of units consumed: “;

cin>>units;

}

//A function of class customer to calculate the charge of customer

void calculate()

{

float cal;

if(units<=100)

{

cal=0.40;

}

else

{

if(units<=300)

{

cal=0.50;

}

else

{

cal=0.60;

}

}

charge= 50+cal;

}

//A function of class customer to print the details of cutomers

void print()

{

cout<<“\n Customer name: “<<name;

cout<<“\n Number of units consumed: “<<units;

cout<<“\n Charge: Rs.”<<charge;

}

};

//End of class customer

//Main function

void main()

{

clrscr();  //for clear screen

//Class Variable

customer x[31];

//for loop to read the details of 3 customers

for(int i=0;i<3;i++)

{

}

//for loop to print the details of 3 customers

for (i=0;i<3;i++)

{

x[i].calculate();

x[i].print();

}

//To hold the screen

getch();

}

//End of main() function

Output:

Enter the name of customer: Anita

Enter the number of units consumed: 200

Enter the name of customer: Babita

Enter the number of units consumed: 777

Enter the name of customer: Ganesha

Enter the number of units consumed: 300

Customer name: Anita

Number of units consumed: 200

Charge: Rs.50.5

Customer name: Babita

Number of units consumed: 777

Charge: Rs.50.599998

Customer name: Ganesha

Number of units consumed: 300

Charge: Rs.50.5

## C++ program that takes height in centimeters and then converts heights to feet and inches.

Q19. Write a short program that asks for your height in centimeters and then converts your heights to feet and inches. (1 foot = 12 inches, 1 inch = 2.54 cm).

Ans.

#include<iostream.h>

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

void main()

{

float cm, foot, inch;

clrscr();

cout<<“\n Enter your height in cm: “;

## Impact of access modifier const over variables.

Q17. Explain the impact of access modifier const over variables. Support your answer with examples.

Ans. If the access modifier const is used before variable’s definition, it modifies its access types, that is, the access of the constant variable is readable only, it can no more be written on to. For instance,

int val = 10;

declares a variable val with initial value 10. However, the value of val can be changed in the program at any time. But, if we modify the above definition of variable as follows:

const int val = 10;

the variable val becomes constant and its value remains 10 throughout the program, it can never be changed during program run.

## Variable Initialization.

Q16. How many ways can a variable be initialized into? Give examples for each types of initialization.

Ans. A first value (initial value) may be specified in the definition of a variable. A variable with a declared first value is said to be an initialized variable. C++ supports two forms of variable initialization at the time of variable definition:

int val = 1001;

int val (1001);

In both cases, val is initialized with s first value of 1001. Following are some more examples of initialized variables:

double price = 214.70, discount = 0.12; Read More …

## Explain the function and usage of variables.

Q15. Explain the function and usage of variables with example.

Ans. 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;

## Why are so many data types provided in C++.

Q14. Why are so many data types provided in C++?

Ans. The reason for providing so many data types is to allow programmer to take advantage of hardware characteristics. Machines are significantly different in their memory requirements, memory access times (time taken to read memory), and computation speeds. Suppose, a program that earlier was working with 2-byte int efficiently, on shifting to a machine that provides 4-byte int and 2-byte short can easily work with short of the new machine thereby not increasing the memory requirements of the program.

## Explain the function and usage of a union.

Q13. Explain the function and usage of a union giving an example.

Ans. A union is a memory location that is shared by two or more different variables, generally of different types at different times. Defining a union is similar to defining a structure. Following declaration declares a union share having two variables (integer and character type) and creates a union object cnvt of union type share:

union share

{

int i;

char ch;

};

union share cnvt;

the keyword union is used for declaring and creating a union cnvt, both integer I and character ch share the same memory location. (of course, i (being integer) occupies 2 bytes and ch (being character) uses only 1 byte.

i

 Byte 0 Byte 1

ch

Figure: Union elements I and ch utilizing union cnvt

At any point, you can refer to the data stored in a cnvt as either an integer or a character. To assign the integer 20 to element i of cnvt, write

cnvt.i = 20;

To print the value of element ch of cnvt, write

cout<<cnvt.ch;

## Declaration for a student class.

Last Questions 11.

Q12. Write declaration for a class that holds the information of Q11. For associated operations, make suitable assumptions.

Ans.

class stud

{

int roll_no;

char name[20];

int class;

float marks;