C++ Basic Exercise Part – 1

0 0
Read Time:19 Minute, 54 Second
  1. Write a program in C++ to print welcome text on a separate line.
    Expected Output:
    Welcome to
    CodeDixa
    Solution
  2. Write a program in C++ to print the sum of two numbers.
    Expected Output:
    Print the sum of two numbers :
    ———————————–
    The sum of 29 and 30 is : 59
    Solution
  3. Write a in C++ program to find the size of fundamental data types.
    Expected Output:
    Find Size of fundamental data types :
    ——————————————
    The sizeof(char) is : 1 bytes
    The sizeof(short) is : 2 bytes
    The sizeof(int) is : 4 bytes
    The sizeof(long) is : 8 bytes
    The sizeof(long long) is : 8 bytes
    The sizeof(float) is : 4 bytes
    The sizeof(double) is : 8 bytes
    The sizeof(long double) is : 16 bytes
    The sizeof(bool) is : 1 bytes
    Solution
  4. Write a program in C++ to print the sum of two numbers using variables.
    Expected Output:
    Print the sum of two numbers :
    ———————————–
    The sum of 29 and 30 is : 59
    Solution
  5. 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
  6. Write a C++ program that checks whether primitive values cross the limit.
    Check whether the primitive values crossing the limits or not :

    Expected Output:
    ——————————————————————–
    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
  7. Write a C++ program that displays mixed data types and arithmetic operations.
    Sample output:
    Display arithmetic operations with mixed data type :
    Expected Output:
    ———————————————————
    8 + 7 = 15
    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
  8. Write a C++ program to check overflow/underflow during various arithmetic operations.
    Sample Output:
    Check overflow/underflow during various arithmetical operation :
    Range of int is [-2147483648, 2147483647]
    ———————————————————————
    Overflow the integer range and set in minimum range : -2147483648
    Increasing from its minimum range : -2147483647
    Product is :1
    Underflow the range and set in maximum range : 2147483647
    Decreasing from its maximum range : 2147483646
    Product is : 0
  9. Write a C++ program to display the operation of pre and post increment and decrement.
    Expected 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
  10. Write a C++ program to format the output.
    Expected Output:
    Formatting the output :
    —————————-
    The value of pi : 3.1416
    The value of pi 4 decimal place of total width 8 : | 3.1416|
    The value of pi 4 decimal place of total width 10 : | 3.1416|
    The value of pi 4 decimal place of total width 8 : |–3.1416|
    The value of pi 4 decimal place of total width 10 : |—-3.1416|
    The value of pi in scientific format is : 3.1416e+00
    Status in number : 0
    Status in alphabet : false
  11. Write a C++ program to print the results of the specified operations.
    (-1+4*6)
    ((35+5)%7)
    (14+-4*6/11)
    (2+15/6*1-7%2
    Expected Output:
    Print the result of some specific operation :
    ————————————————–
    Result of 1st expression is : 23
    Result of 2nd expression is : 5
    Result of 3rd expression is : 12
    Result of 4th expression is : 3
  12. Write a C++ program to add two numbers and accept them from the keyboard.
    Expected Output:
    Sum of two numbers :
    ————————-
    Input 1st number : 25
    Input 2nd number : 39
    The sum of the numbers is : 64
  13. Write a C++ program that swaps two numbers using third variable.
    Expected Output:
    Swap two numbers :
    ———————–
    Input 1st number : 25
    Input 2nd number : 39
    After swapping the 1st number is : 39
    After swapping the 2nd number is : 25
  14. Write a C++ program that swaps two numbers without using third variable.
    Expected Output:
    Swap two numbers :
    ———————–
    Input 1st number : 80
    Input 2nd number : 90
    After swapping the 1st number is : 90
    After swapping the 2nd number is : 80
  15. Write a C++ program that calculates the volume of a sphere.
    Example Output:
    Calculate the volume of a sphere :
    —————————————
    Input the radius of a sphere : 6
    The volume of a sphere is : 904.32
  16. Write a C++ program that calculates the volume of a cube.
    Expected Output:
    Calculate the volume of a cube :
    —————————————
    Input the side of a cube : 5
    The volume of a cube is : 125
  17. Write a C++ program that calculates the volume of a cylinder.
    Expected Output:
    Calculate the volume of a cylinder :
    —————————————–
    Input the radius of the cylinder : 6
    Input the height of the cylinder : 8
    The volume of a cylinder is : 904.32
  18. Write a C++ program to find the Area and Perimeter of a Rectangle.
    Expected Output:
    Find the Area and Perimeter of a Rectangle :
    ————————————————-
    Input the length of the rectangle : 10
    Input the width of the rectangle : 15
    The area of the rectangle is : 150
    The perimeter of the rectangle is : 50
  19. Write a C++ program to find the area of any triangle using Heron’s formula.
    Expected 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
  20. Write a C++ program to find the area and circumference of a circle.
    Expected 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
  21. Write a C++ program to convert temperature in Celsius to Fahrenheit.
    Expected Output:
    Convert temperature in Celsius to Fahrenheit :
    —————————————————
    Input the temperature in Celsius : 35
    The temperature in Celsius : 35° C
    The temperature in Fahrenheit : 95 F
  22. Write a C++ program to convert temperature in Fahrenheit to Celsius.
    Expected Output:
    Convert temperature in Fahrenheit to Celsius :
    —————————————————
    Input the temperature in Fahrenheit : 95
    The temperature in Fahrenheit : 95 F
    The temperature in Celsius : 35° C
  23. Write a C++ program to find the third angle of a triangle.
    Expected Output:
    Find the third angle of a triangle :
    —————————————–
    Input the 1st angle of the triangle : 30
    Input the 2nd angle of the triangle : 60
    The 3rd of the triangle is : 90
  24. Write a program in C++ that converts kilometers per hour to miles per hour.
    Expected Output:

    Convert kilometers per hour to miles per hour :
    —————————————————-
    Input the distance in kilometer : 25
    The 25 Km./Hrs. means 15.5343 Miles/Hrs.
  25. Write a program in C++ to convert temperature in Kelvin to Fahrenheit.
    Expected Output:
    Convert temperature in Kelvin to Fahrenheit :
    —————————————————
    Input the temperature in Kelvin : 300
    The temperature in Kelvin : 300 K
    The temperature in Fahrenheit : 80.33 F
  26. Write a program in C++ to convert temperature in Kelvin to Celsius.
    Expected Output:
    Convert temperature in Kelvin to Celsius :
    ————————————————
    Input the temperature in Kelvin : 300
    The temperature in Kelvin : 300 K
    The temperature in Celsius : 26.85° C
  27. Write a program in C++ to convert temperature in Fahrenheit to Kelvin.
    Expected Output:
    Convert temperature in Fahrenheit to Kelvin :
    —————————————————
    Input the temperature in Fahrenheit : 80.33
    The temperature in Fahrenheit : 80.33 F
    The temperature in Kelvin : 300 K
  28. Write a program in C++ to convert temperature in Celsius to Kelvin.
    Sample Output:
    Convert temperature in Celsius to Kelvin :
    —————————————————
    Input the temperature in Celsius : 26.85
    The temperature in Celsius : 26.85 ° C
    The temperature in Kelvin : 300 K
  29. Write a C++ program to find the area of the Scalene Triangle.
    Expected Output:
    Find the area of Scalene Triangle :
    —————————————-
    Input the length of a side of the triangle : 5
    Input the length of another side of the triangle : 6
    Input the angle between these sides of the triangle : 6
    The area of the Scalene Triangle is : 1.56793
  30. Write a C++ program to compute the quotient and remainder.
    Expected Output:
    Compute quotient and remainder :
    ————————————-
    Input the dividend : 25
    Input the divisor : 3
    The quotient of the division is : 8
    The remainder of the division is : 1
  31. Write a C++ program to compute the total and average of four numbers.
    Expected 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
  32. Write a C++ program to input a single-digit number and print it in a rectangular form of 4 columns and 6 rows.
    Expected Output:
    Make a rectangular shape by a single digit number :
    ——————————————————–
    Input the number : 8
    8 8 8 8
    8 8
    8 8
    8 8
    8 8
    8 8 8 8
  33. Write a program in C++ to check whether a number is positive, negative or zero.
    Expected Output:
    Check whether a number is positive, negative or zero :
    ———————————————————–
    Input a number : 8
    The entered number is positive.
  34. Write a program in C++ to divide two numbers and print them on the screen.
    Expected Output:
    Divide two numbers and print:
    ———————————-
    The quotient of 30 and 10 is : 3
  35. Write a C++ program to display the current date and time.
    Expected Output:
    Display the Current Date and Time :
    —————————————-
    seconds = 57
    minutes = 33
    hours = 12
    day of month = 6
    month of year = 7
    year = 2017
    weekday = 4
    day of year = 186
    daylight savings = 0
    Current Date: 6/7/2017
    Current Time: 12:33:57
  36. Write a program in C++ to compute the specified expressions and print the output.
    Expected 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
  37. Write a program in C++ to test Type Casting.
    Expected Output:
    Formatting the output using type casting:
    ———————————————-
    Print floating-point number in fixed format with 1 decimal place:
    Test explicit type casting :
    0
    0.5
    0.5
    0.0
    Test implicit type casting :
    0
    0
    int implicitly casts to double:
    4.0
    double truncates to int!:
    6
  38. Write a C++ program to print a mystery series from 1 to 50.
    Example Output:
    Print a mystery series:
    ————————-
    The series are:
    5 4 2 7 11 10 8 13 17 16 14 19 23 22 20 25 29 28 26 31 35 34 32 37 41 4 0 38 43 47 46 44 49
  39. Write a C++ program that takes a number as input and prints its multiplication table up to 10.
    Example Output:
    Print the multiplication table of a number upto 10:
    ——————————————————–
    Input a number: 5
    5 x 1 = 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
  40. Write a C++ program to print the area and perimeter of a rectangle.
    Example Output:
    Print the area and perimeter of a rectangle:
    ———————————————-
    Input the width of the rectangle: 8.5
    Input the height of the rectangle: 5.6
    The area of the rectangle is: 47.6
    The perimeter of the rectangle is: 28.2
  41. Write a C++ program that accepts the user’s first and last name and prints them in reverse order with a space between them.
    Expected 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
  42. Write a C++ program that accepts the radius of a circle from the user and computes the area and circumference.
    Example 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
  43. Write a C++ program to get the volume of a sphere with radius 6.
    Expected Output:
    Calculate the volume of a sphere :
    —————————————
    Input the radius of a sphere : 5
    The volume of a sphere is : 523.333
  44. Write a C++ program to calculate the volume of a cone.
    Input: 5 3
    Expected Output:
    Input Cone’s radius: Input Cone’s height: The volume of the cone is: 78.5397
  45. Write a C++ program to calculate the volume of a cylinder.
    Expected Output:
    Calculate the volume of a cylinder :
    —————————————–
    Input the radius of the cylinder : 4
    Input the height of the cylinder : 8
    The volume of a cylinder is : 401.92
  46. Write a C++ program to calculate the sum of all even and odd numbers in an array.
    Expected Output:
    Original array: 1 2 3 4 5 6 7 8
    Sum of all even and odd numbers: 20,16
  47. Write a C++ program that swaps two variables without using a third variable.
    Expected 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
  48. 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
  49. 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
  50. 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.
  51. 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
  52. Write a C++ program to calculate the area of an equilateral triangle.
    Sample Output:
    Calculate the area of the Equilateral Triangle :
    —————————————————-
    Input the value of the side of the equilateral triangle: 5
    The area of equilateral triangle is: 10.8253
  53. Write a C++ program to enter P, T, R and calculate Simple Interest.
    Sample Output:
    Calculate the Simple Interest :
    ———————————–
    Input the Principle: 20000
    Input the Rate of Interest: 10
    Input the Time: 1.5
    The Simple interest for the amount 20000 for 1 years @ 10 % is: 2000
  54. Write a C++ program to enter P, T, R and calculate compound interest.
    Sample Output:
    Calculate the Compound Interest :
    ————————————- Input the Principle: 20000
    Input the Rate of Interest: 10
    Input the Time: 1.5
    The Interest after compounded for the amount 20000 for 1.5 years @ 10
    % is: 3073.8 The total amount after compounded for the amount 20000 for 1.5 years @
    10 % is: 23073.8
  55. Write a C++ program to show the manipulation of a string.
    Sample Output:
    Show the manipulation of a string:
    The string:: welcome, CodeDixa
    The length of the string:: 19
    The char at index 1 of the string:: e
    The char at index 1 of the string [using array ]:: e
    Is the string empty:: 0
    Retrieve the sub-string from 3rd position for 4 characters:: come
    The sub-string replace by ‘went’:: welwent, CodeDixa
    Append a string ‘end’ at last of the string:: welwent, CodeDixa end
    Append a string ‘end’ at last of the string using operator:: welwent, CodeDixa end end
    The string ‘insert’ inserting at 3rd position of the string:: wel insert went, CodeDixa end
    The new string is:: wel insert went, CodeDixa end
    Input a sentence:: The quick brown fox jumps over the lazy dog.
    The quick brown fox jumps over the lazy dog.
  56. Write a C++ program to print the area of a hexagon.
    Expected Output:
    Print the area of a hexagon:
    ———————————
    Input the side of the hexagon: 6
    The area of the hexagon is: 93.5307
  57. Write a C++ program to print the area of a polygon.
    Expected Output:
    Print the area of a polygon:
    ———————————
    Input the number of sides of the polygon: 7
    Input the length of each side of the polygon: 6
    The area of the polygon is: 130.821
  58. Write a C++ program to compute the distance between two points on the surface of the earth.
    Expected 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
  59. Write a C++ program to add two binary numbers.
    Expected Output:
    Addition of two binary numbers:
    ———————————–
    Input the 1st binary number: 1010
    Input the 2nd binary number: 0011
    The sum of two binary numbers is: 1101
  60. Write a C++ program to swap the first and last digits of any number.
    Expected Output:
    Input any number: 12345
    The number after swapping the first and last digits are: 52341
  61. 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”.
  62. Write a C++ program that prints the three highest numbers from a list of numbers in descending order.
  63. Write a C++ program to compute the sum of the two given integers and count the number of digits in the sum value.
  64. Write a C++ program to check whether a given length of three sides forms a right triangle.
  65. Write a C++ program to add all the numbers from 1 to a given number.
    Add 1 to 4: 10
    Add 1 to 100: 5050
  66. 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.
  67. Write a C++ program that reads seven numbers and sorts them in descending order.
  68. For n = 10, write a C++ program that reads the integer n and prints its factorial.
  69. Write a C++ program to replace all the lower-case letters in a given string with the corresponding capital letters.
  70. 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.
  71. 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
  72. 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.
  73. 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.
  74. 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.
  75. 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.
  76. There are four different points on a plane: A(x1, y1), B(x2, y2), C(x3, y3) and D(x4, y4).
    Write a C++ program to check whether two straight lines AB and CD are orthogonal or not.
    Input:
    0 6
    5 6
    3 8
    3 2
    Output:
    yes
  77. Write a C++ program to sum all positive integers in a sentence.
    Sample string: There are 12 chairs, 15 desks, 1 blackboard and 2 fans.
    Output: 30
  78. 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).
  79. 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.
  80. 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.
  81. 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.
  82. 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
  83. 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
  84. 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
  85. 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
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %