Close
Close

Practice questions on Variables and literals

Level 1

1.
Store two integers in two variables x and y. Print the sum of the two.
#include <iostream>
int main()
{
	using namespace std;
	int x = 3;
	int y =5;
	cout << x+y << endl;
	return 0;
}

2.
Store two integers in two variables x and y. Print the product of the two.
#include <iostream>
int main()
{
	using namespace std;
	int x = 3;
	int y =5;
	cout << x*y << endl;
	return 0;
}

3.
Write a C++ program to take two integer inputs from user and print sum and product of them.
#include <iostream>
int main()
{
	using namespace std;
	int x;
	int y;
	cin >> x; //taking input for x
	cin >> y; //taking input for y
	cout << x+y << endl;
	cout << x*y << endl;
	return 0;
}

4.
Take two integer inputs from user. First calculate the sum of two then product of two. Finally, print the sum and product of both obtained results.
#include <iostream>
int main()
{
	using namespace std;
	int x;
	int y;
	cin >> x; //taking input for x
	cin >> y; //taking input for y
	int sum = x+y;
	int prod = x*y;
	cout << sum+prod << endl;
	cout << sum*prod << endl;
	return 0;
}

5.
Write a program to take input of length and breadth of a rectangle from the user and print its area.
#include <iostream>
int main()
{
	using namespace std;
	int length;
	int breadth;
	cout << "Enter value of length" << endl;
	cin >> length;
	cout << "Enter value of breadth" << endl;
	cin >> breadth; //taking input for y

	int area = length*breadth;
	cout << "Area is " << area << endl;
	return 0;
}

6.
Write a C++ program to print an int, a double and a char on screen.
#include <iostream>
int main()
{
	using namespace std;
	int x;
	double y;
	char z;
	cout << "Enter value of a int, double and a char" << endl;
	cin >> x;
	cin >> y;
	cin >> z;

	cout << "int is " << x << " double is " << y << " char is " << z << endl;
	return 0;
}

7.
Print the ASCII value of the character 'h'.
#include <iostream>
int main()
{
	using namespace std;
	cout << "ASCII value of h is " << (int)'h' << endl;
	return 0;
}

8.
Write a program to assign a value of 100.235 to a double variable and then convert it to int.
#include <iostream>
int main()
{
	using namespace std;
	double x = 100.235;
	cout << (int)x << endl;
	return 0;
}

9.
Write a program to add 3 to the ASCII value of the character 'd' and print the equivalent character.
#include <iostream>
int main()
{
	using namespace std;
	char x = 'd'+3;
	cout << x << endl;
	return 0;
}

10.
Write a program to add an integer variable having value 5 and a double variable having value 6.2.

11.
Write a program to find the square of the number 3.9.

12.
Take value of length and breath of a rectangle from user as float. Find its area and print it on screen after type casting it to int.

13.
Take a char input from user and print it's ASCII value.

Level 2

Level 3

Ask Yours
Post Yours