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;

n2=n*n; Read More …

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: “;

cin>>cm; Read More …

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.

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;

char grade;

 

public:

percent();  //to calculate the percentage of student

calgrade();  //calculate grade

prntreport();  //prints the report of the student

};

Explain floating point types offered by C++.

Q5. Explain floating point types offered by C++ along with the minimal ranges and digit of precision they support.

Ans. C++ has three floating-point types: float, double and long double. These types are described in terms of the number of significant figures they can represent and the minimum allowable range of exponents.

Type float occupies 4 bytes of memory. Type double occupies 8 bytes, twice as much memory as type float and stores floating-point numbers with much larger range and precision. Floating-point type Long double occupies 10 bytes and has only slightly greater range and precision than type double.

 

Read More …

When are unsigned integers preferred over signed integers?

Q4. When are unsigned integers preferred over signed integers? What is the advantage of unsigned integers over signed integers?

Ans. The prefix unsigned makes the integer types not to hold negative values. For instance, if short (2-byte long) represents the range -32768 to +32767, then the unsigned version can represent the range 0 to 65535. This has the advantage of increasing the largest value the variables can hold. Unsigned types are used for quantities that are never negative such as populations, sports scores, inventory counts etc.

Integer types are offered by C++.

Q3. What main integer types are offered by C++?

Ans. By using different number of bytes to store values, C++ offers three types of integers: short, int and long that can represent up to three different sizes. Each comes in both signed and unsigned versions. That gives you a choice of six different integer types. Read More …