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 …

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 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

        void read()

{

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++)

{

x[i].read();

}

 

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

cin>>cm; Read More …