C++ Basic - Exercises, Practice, Solution - w3resource
C++ Basic - Exercises, Practice, Solution - w3resource
Write a program in C++ to print the sum of two numbers using variables.
Print the sum of two numbers :
-----------------------------------
The sum of 29 and 30 is : 59
Click me to see the sample solution
Write a in C++ program to check the upper and lower limits of integers.
Expected Output:
Check the upper and lower limits of integer :
--------------------------------------------------
The maximum limit of int data type : 2147483647
The minimum limit of int data type : -2147483648
The maximum limit of unsigned int data type : 4294967295
The maximum limit of long long data type : 9223372036854775807
The minimum limit of long long data type : -9223372036854775808
The maximum limit of unsigned long long data type : 18446744073709551615
The Bits contain in char data type : 8
The maximum limit of char data type : 127
The minimum limit of char data type : -128
The maximum limit of signed char data type : 127
The minimum limit of signed char data type : -128
The maximum limit of unsigned char data type : 255
The minimum limit of short data type : -32768
The maximum limit of short data type : 32767
The maximum limit of unsigned short data type : 65535
Click me to see the sample solution
Write a C++ program that checks whether primitive values cross the limit.
Check whether the primitive values crossing the limits or not :
--------------------------------------------------------------------
The Gender is : F
Is she married? : 1
Number of sons she has : 2
Year of her appointment : 2009
Salary for a year : 1500000
Height is : 79.48
GPA is 4.69
Salary drawn upto : 12047235
Balance till : 995324987
Click me to see the sample solution
Write a C++ program that displays mixed data types and arithmetic operations.
Sample output:
Display arithmetic operations with mixed data type :
---------------------------------------------------------
5 + 7 = 12
3.7 + 8.0 = 11.7
5 + 8.0 = 13.0
5 - 7 = -2
3.7 - 8.0 = -4.3
5 - 8.0 = -3.0
5 * 7 = 35
3.7 * 8.0 = 29.6
5 * 8.0 = 40.0
5/7=0
3.7 / 8.0 = 0.5
5 / 8.0 = 0.6
Click me to see the sample solution
Write a C++ program to display the operation of pre and post increment and decrement.
Sample Output:
Display the operation of pre and post increment and decrement :
--------------------------------------------------------------------
The number is : 57
After post increment by 1 the number is : 58
After pre increment by 1 the number is : 59
After increasing by 1 the number is : 60
After post decrement by 1 the number is : 59
After pre decrement by 1 the number is : 58
After decreasing by 1 the number is : 57
Click me to see the sample solution
Write a C++ program to add two numbers and accept them from the keyboard.
Sample Output:
Sum of two numbers :
-------------------------
Input 1st number : 25
Input 2nd number : 39
The sum of the numbers is : 64
Click me to see the sample solution
Write a C++ program to find the area of any triangle using Heron's formula.
Sample Output:
Find the area of any triangle using Heron's Formula :
----------------------------------------------------------
Input the length of 1st side of the triangle : 5
Input the length of 2nd side of the triangle : 5
Input the length of 3rd side of the triangle : 5
The area of the triangle is : 10.8253
Click me to see the sample solution
Write a program in C++ that converts kilometers per hour to miles per hour.
Sample Output:
Convert kilometers per hour to miles per hour :
----------------------------------------------------
Input the distance in kilometer : 25
The 25 Km./hr. means 15.5343 Miles/hr.
Click me to see the sample solution
Write a C++ program to compute the total and average of four numbers.
Sample Output:
Compute the total and average of four numbers :
----------------------------------------------------
Input 1st two numbers (separated by space) : 25 20
Input last two numbers (separated by space) : 15 25
The total of four numbers is : 85
The average of four numbers is : 21.25
Click me to see the sample solution
Write a C++ program to input a single-digit number and print it in a rectangular form of 4 columns and 6 rows.
Sample Output:
Make a rectangular shape by a single digit number :
--------------------------------------------------------
Input the number : 5
5555
55
55
55
55
5555
Click me to see the sample solution
Write a program in C++ to divide two numbers and print them on the screen.
Sample Output:
Divide two numbers and print:
----------------------------------
The quotient of 30 and 10 is : 3
Click me to see the sample solution
Write a program in C++ to compute the specified expressions and print the output.
Sample Output:
Compute the specified expressions and print the output:
------------------------------------------------------------
Result of the expression (25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5) is : 2.13889
Click me to see the sample solution
Write a C++ program that takes a number as input and prints its multiplication table up to 10.
Sample Output:
Print the multiplication table of a number upto 10:
--------------------------------------------------------
Input a number: 5
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Click me to see the sample solution
Write a C++ program that accepts the user's first and last name and prints them in reverse order with a space between them.
Sample Output:
Print the name in reverse where last name comes first:
-----------------------------------------------------------
Input First Name: Alexandra
Input Last Name: Abramov
Name in reverse is: Abramov Alexandra
Click me to see the sample solution
Write a C++ program that accepts the radius of a circle from the user and computes the area and circumference.
Sample Output:
Find the area and circumference of any circle :
----------------------------------------------------
Input the radius(1/2 of diameter) of a circle : 5
The area of the circle is : 78.5397
The circumference of the circle is : 31.4159
Click me to see the sample solution
Write a C++ program to calculate the sum of all even and odd numbers in an array.
Sample Output:
Original array: 1 2 3 4 5 6 7 8
Sum of all even and odd numbers: 20,16
Click me to see the sample solution
Write a C++ program that swaps two variables without using a third variable.
Sample Output:
Swap two numbers without using third variable:
---------------------------------------------------
Input 1st number : 25
Input 2nd number : 20
After swapping the 1st number is : 20
After swapping the 2nd number is : 25
Click me to see the sample solution
Write a C++ program to print the code (ASCII code / Unicode code etc.) of a given character.
Sample Output:
Print code (ASCII code / Unicode code etc.) of a given character:
-----------------------------------------------------------------------
Input a character: a
The ASCII value of a is: 97
The character for the ASCII value 97 is: a
Click me to see the sample solution
Write a C++ program to enter length in centimeters and convert it into meters and kilometers.
Sample Output:
Convert centimeter into meter and kilometer :
--------------------------------------------------
Input the distance in centimeter : 250000
The distance in meter is: 2500
The distance in kilometer is: 2.5
Click me to see the sample solution
Write a C++ program that converts kilometers per hour to miles per hour.
Sample Output:
Convert kilometers per hour to miles per hour :
----------------------------------------------------
Input the distance in kilometer : 5
The 5 Km./hr. means 3.10686 Miles/hr.
Click me to see the sample solution
Write a C++ program to enter two angles of a triangle and find the third angle.
Sample Output:
Find the third angle of a triangle :
-----------------------------------------
Input the 1st angle of the triangle : 35
Input the 2nd angle of the triangle : 35
The 3rd of the triangle is : 110
Click me to see the sample solution
53. Equilateral Triangle Area
Write a C++ program to compute the distance between two points on the surface of the earth.
Sample Output:
Print the the distance between two points on the surface of earth:
-----------------------------------------------------------------------
Input the latitude of coordinate 1: 25
Input the longitude of coordinate 1: 35
Input the latitude of coordinate 2: 35.5
Input the longitude of coordinate 2: 25.5
The distance between those points is: 1480.08
Click me to see the sample solution
Write a C++ program to swap the first and last digits of any number.
Sample Output:
Input any number: 12345
The number after swapping the first and last digits are: 52341
Click me to see the sample solution
Write a C++ program that reads the integer n and prints a twin prime that has the maximum size among twin primes less than or equal to n.
According to wikipedia "A twin prime is a prime number that is either 2 less or 2 more than another prime number" for example, either
member of the twin prime pair (41, 43). In other words, a twin prime is a prime that has a prime gap of two".
Click me to see the sample solution
Write a C++ program that prints the three highest numbers from a list of numbers in descending order.
Click me to see the sample solution
Write a C++ program to compute the sum of the two given integers and count the number of digits in the sum value.
Click me to see the sample solution
Write a C++ program to check whether a given length of three sides forms a right triangle.
Click me to see the sample solution
Write a C++ program that prints the central coordinate and the radius of a circumscribed circle of a triangle. This circle is created from three
points on the plane surface.
Click me to see the sample solution
Write a C++ program that reads seven numbers and sorts them in descending order.
Click me to see the sample solution
69. Factorial of n
For n = 10, write a C++ program that reads the integer n and prints its factorial.
Click me to see the sample solution
Write a C++ program to replace all the lower-case letters in a given string with the corresponding capital letters.
Click me to see the sample solution
Write a C++ program that reads a sequence of integers and prints the mode values of the sequence. The number of integers is greater than or
equal to 1 and less than or equal to 100.
Note: The mode of a set of data values is the value that appears most often.
Click me to see the sample solution
72. Digit Combinations with Given Sum
Write a C++ program that reads n digits chosen from 0 to 9 and counts the number of combinations where the sum of the digits equals the
given number. Do not use the same digits in a combination.
For example, the combinations where n = 2 and s = 5 are as follows:
0+5=5
1+4=5
3+2=5
Click me to see the sample solution
Write a C++ program that accepts the sales unit price and sales quantity of various items and computes the total sales amount and the average
sales quantity. All input values must be greater than or equal to 0 and less than or equal to 1,000. In addition, the number of pairs of sales unit
and sales quantity does not exceed 100. If a fraction occurs in the average of the sales quantity, round to the first decimal place.
Click me to see the sample solution
Write a C++ program that accepts various numbers and computes the difference between the highest number and the lowest number. All input
numbers should be real numbers between 0 and 1,000,000. The output (real numbers) may include an error of 0.01 or less.
Click me to see the sample solution
Write a C++ program to compute the sum of the specified number of prime numbers.
For example when n = 7,
s = 2 + 3 + 5 + 7 + 11 + 13 + 17 = 58.
Click me to see the sample solution
An even number of 4 or more can be represented by the sum of two prime numbers. This is called the Goldbach expectation, and it is
confirmed that it is correct up to a considerable number by computer calculation. For example, 10 can be expressed as the sum of two prime
numbers 7 + 3, 5 + 5. Write a C++ program that accepts an integer (n) from the user and outputs the number of combinations that express n as
the sum of two prime numbers.
Note: n should be greater than or equal to 4 and less than or equal to 50,000.
Click me to see the sample solution
There are four different points on a plane: A(x1, y1), B(x2, y2), C(x3, y3) and D(x4, y4).
C language courses
Write a C++ program to check whether two straight lines AB and CD are orthogonal or not.
Input:
06
56
38
32
Output:
yes
Click me to see the sample solution
Write a C++ program to display all the leap years between two given years. If there is no leap year in the given period,display a suitable
message.
Note: Range of the two given years: ( 0 < year1 = year2 < 3,000).
Click me to see the sample solution
Write a C++ program that accepts n different numbers (0 to 100) as well as an integer s which is equal to the sum of the n different numbers.
Your job is to find the number of combinations of n numbers and the same number cannot be used for one combination.
Click me to see the sample solution
Write a C++ program that replaces all the words "dog" with "cat".
Sample Text: The quick brown fox jumps over the lazy dog. You can assume that the number of characters in a text is less than or equal to
1000.
Click me to see the sample solution
Write a C++ program that reads a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers.
Click me to see the sample solution
Write a C++ program to convert a given number into hours and minutes. Separate the number of hours and minutes with a colon.
For example if a given number is 67 the output should be 1:7
Click me to see the sample solution
Write a C++ program to check whether the sequence of the numbers in a given array is an "Arithmetic" or "Geometric" sequence. Return -1 if
the sequence is not "Arithmetic" or "Geometric".
From Wikipedia
In mathematics, an arithmetic progression (AP) or arithmetic sequence is a sequence of numbers such that the difference between the
consecutive terms is constant. Difference here means the second minus the first. For instance, the sequence 5, 7, 9, 11, 13, 15, . . . is an
arithmetic progression with common difference of 2.
In mathematics, a geometric progression, also known as a geometric sequence, is a sequence of numbers where each term after the first is
found by multiplying the previous one by a fixed, non-zero number called the common ratio. For example, the sequence 2, 6, 18, 54, ... is a
geometric progression with common ratio 3. Similarly 10, 5, 2.5, 1.25, ... is a geometric sequence with common ratio 1/2.
Example:
Sample Input: int nums1[] = { 1, 3, 5, 7 }
Sample Output: Arithmetic sequence
Click me to see the sample solution
Write a C++ program to find the total number of minutes between two given times (formatted with a colon and am or pm).
Example:
Sample Input: Minutes between 12:01AM to 12:00PM:
Sample Output: Minutes between 12:01AM to 12:00PM: 1439
Click me to see the sample solution
Write a C++ program to add up all the digits between two given integers. Add all the digits between 11 and 16.
Example:
Add up all the digits between 39 and 41 is: 21
Click me to see the sample solution
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.