C++ Important Programs | Programming Practice with C++

1. Write a C++ program to check two numbers are equal or not.

Solution:

Output – 1:

Enter 2 numbers: 12 12

Numbers are equal

Output – 2:

Enter 2 numbers: 23 45

Numbers are not equal

2. Write a program to find the greater number among two numbers.

Solution:

OR

Output:

Enter 2 numbers: 23 56

Greater number is: 56

3. Write a program to check whether a number is even or odd.

Solution:

Output – 1:

Enter a number: 4

4 is even number

Output – 2:

Enter a number: 7

7 is odd number

4. Write a program to check whether a year is leap year or not.

Solution:

Output – 1:

Enter a year: 2020

2020 is leap year

Output – 1:

Enter a year: 2025

2025 is not leap year

5. Write a C++ program to find the largest number among three numbers.

Solution:

Output:

Enter 3 numbers: 10 20 30

Largest number is: 30

6. Write a C++ program to check whether a character is vowel or not using switch case.

Solution:

Output – 1:

Enter any character: A

A is a vowel

Output – 2:

Enter any character: r

r is not a vowel

7. Write a program in C++ to display the name of the day in a week.

Solution:

Output – 1:

Enter a number between 1 to 7: 5

Thursday

Output – 2:

Enter a number between 1 to 7: 10

Invalid Number

8. Write a C++ program to make a simple calculator to add, subtract, multiply and divide using switch case.

Solution:

Output – 1:

Enter 2 number: 10 20

Enter an operator (+, -, *, /): *

Result: 200

Output – 2:

Enter 2 number: 10 20

Enter an operator (+, -, *, /): ?

Error! Invalid Operator

OR

Output – 1:

Enter two numbers: 10 20

Enter an operator (+, -, *, /) : *

10*20=200

Output – 2:

Enter two numbers: 12 23

Enter an operator (+, -, *, /) : ?

Error! operator is not correct

9. Write a C++ program to print 1st 100 natural numbers.

                             OR

Write a program in C++ to print the number from 1 to 100.

Solution:

Output:

First 100 Natural Number:

1       2       3       4       5       6       7       8       9       10      11      12      13      14      15     16       17      18      19      20      21      22      23      24      25      26      27      28      29      30     31       32      33      34      35      36      37      38      39      40      41      42      43      44      45     46       47      48      49      50      51      52      53      54      55      56      57      58      59      60     61       62      63      64      65      66      67      68      69      70      71      72      73      74      75     76       77      78      79      80      81      82      83      84      85      86      87      88      89      90     91       92      93      94      95      96      97      98      99      100

10. Write a C++ program to generate Multiplication Table.

OR

Write a C++ program to find the multiplication table of a number using for loop.

Solution:

Output:

Enter a number: 5

Multiplication Table of 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

11. Write a C++ program to print multiplication table from 1 to 5.

Solution:

Output:

Multiplication Table from 1 to 5

1 x 1 = 1

1 x 2 = 2

1 x 3 = 3

1 x 4 = 4

1 x 5 = 5

1 x 6 = 6

1 x 7 = 7

1 x 8 = 8

1 x 9 = 9

1 x 10 = 10

————-

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

2 x 8 = 16

2 x 9 = 18

2 x 10 = 20

————-

3 x 1 = 3

3 x 2 = 6

3 x 3 = 9

3 x 4 = 12

3 x 5 = 15

————–

————-

12. Write a program to calculate sum of first n natural numbers.

Solution:

Output:

Enter a number: 6

Sum of first 6 natural number:21

13. Write a program to calculate the factorial of a number.

Solution:

Output:

Enter a number: 5

Factorial of 5 = 120

14. Write a program in C++ to print the following series:

1+1/2+1/3+1/4…….+1/n

Solution:

Note: In C++, std::setprecision is an I/O stream manipulator used to control the number of digits displayed for floating-point values.It is defined within the <iomanip> header file and changes the visual presentation without altering the actual value stored in memory.

Output:

Enter the value of n: 4

Sum =2.08333

15. Write a C++ program to display Fibonacci Series.

Solution:

Output:

Enter the number of terms: 10

Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

16. Write a C++ program to print the following pattern:

Solution:

17. Write a C++ program to display the following pattern:

Solution:

18. Write a program in C++ to print the following pattern:

Solution:

19. Write a program in C++ to print the following pattern:

Solution:

20. Write a program in C++ to display the following pattern:

Solution:

21. Write a C++ program to display the following pattern:

Solution:

22. Write a C++ program to print the reverse of a given number.

OR

Write a program in C++ to reverse digits of an integer.

Solution:

Output:

Enter any number to reverse: 123

Reverse = 321

23. Write a C program to calculate the sum of a 3-digit number.

                    OR

Write a C program to find the sum of a given integer.

OR

Write a C program to find the sum of digits in a given number

Solution:

Output:

Enter a number: 456

Sum of digits = 15

24. Write a program to print elements of a one-dimensional (1D) array.

Solution:

OR

Output:

Array elements are: 3 4 2 6

25. Write a program to print elements of a one-dimensional (1D) array taking input from user.

Solution:

Output:

Enter 5 array elements: 2 5 8 1 4

Array elements are: 2 5 8 1 4

26. Write a program to print sum and average of array elements. 

Solution:

Output:

Enter 5 elements in the array: 1 2 3 4 5

The array elements are:  1  2  3  4  5

Sum = 15

Average = 3

27. Write a program to calculate total and average marks of 5 subjects.

Solution:

Output:

Enter Marks of subject 1= 50

Enter Marks of subject 2= 70

Enter Marks of subject 3= 78

Enter Marks of subject 4= 89

Enter Marks of subject 5= 95

Total Marks: 382

Average Mark: 76.4

28. Write a program to find the biggest number in an array.

OR

Write a program in C++ to find the largest element of an array.

Solution:

Output:

Enter size of the array:5

Enter 5 elements: 2 8 5 7 3

Biggest no. of array: 8

29. Write a program in C++ to display the largest and smallest element in an integer array.

OR

Write a C++ program to find maximum and minimum element in array.

Solution:

Output:

Enter size of the array: 6

Enter 6 elements: 8 6 7 9 4 2

Largest element: 9

Smallest element: 2

30. Write a C++ program that counts the number of times each vowel has been entered in a line of text. Plot in the form of a horizontal histogram.

Solution:

Output-1:

Enter a string: EDUCATION

A       #

E       #

I       #

O       #

U       #

Output-2:

Enter a string: My Name is Eliza

A       ##

E       ##

I       ##

O

U

31. Write a program to print elements of a two-dimensional (2D) array.

Solution:

Output:

The array elements are:

1 2 5

3 4 1

32. Write a program to read and print elements of a two-dimensional (2D) array.

Solution:

Output:

Enter 12 elements in the array:

2 3 4 1 5 6 7 9 8 2 4 6

The array elements are:

2 3 4

1 5 6

7 9 8

2 4 6

33. Write a C++ program to find maximum and minimum element in a two-dimensional (2D) array.

Solution:

Output:

Enter number of rows and columns:2 3

Enter elements in the array:2 1 5 3 4 6

Maximum element:6

Minimum element:1

34. Write a C++ program to add two matrices.

Solution:

Output:

Enter elements of 1st matrix:

1 2 3

4 5 6

Enter elements of 2nd matrix:

7 8 9

1 2 3

Sum of 2 matrix is:

8  10  12

5  7  9

OR

Output:

Enter number of rows and columns:2 2

Enter values of 1st matrix:

2 6

4 7

Enter values of 2nd matrix:

1 3

2 5

Sum of two matrix is:

3 9

6 12

35. Write a C++ program to find the difference between two matrices.

OR

Write a C++ program to subtract two matrices.

Solution:

Output:

Enter elements of 1st matrix:

7 8 9

4 5 6

Enter elements of 2nd matrix:

5 4 6

1 3 2

Difference of 2 matrix is:

2  4  3

3  2  4

OR

Output:

Enter number of rows and columns:3 3

Enter elements of 1st matrix:

5 4 8

3 6 2

9 5 7

Enter elements of 2nd matrix:

3 2 5

1 4 2

4 3 5

Difference of 2 matrix is:

2  2  3

2  2  0

5  2  2

36. Write a C++ program to multiply two matrices.

Solution:

Output:

Enter elements of 1st matrix:

1 3 2

2 1 4

Enter elements of 2nd matrix:

2 2

1 3

4 3

Product of 2 matrix is:

13  17

21  19

OR

Output:

Enter number of rows and columns:2 2

Enter elements of 1st matrix:

3 4

2 5

Enter elements of 2nd matrix:

1 4

2 3

Product of 2 matrix is:

11  24

12  23

OR

Output -1:

Enter number of rows and columns of 1st matrix:2 2

Enter number of rows and columns of 2nd matrix:2 2

Enter elements of 1st matrix:

1 2

3 4

Enter elements of 2nd matrix:

4 3

2 1

Product of 2 matrix is:

8  5

20  13

Output -2:

Enter number of rows and columns of 1st matrix:1 3

Enter number of rows and columns of 2nd matrix:2 3

Matices cannot be multiplied.

37. Write a C++ program to store information of Student using structure.

Solution:

Output:

Student Information:

———————-

Name:Ankit Das

Roll Number:1

GPA:9.7

Grade:A

OR

Output:

Enter information of student:

——————————

Enter name: Ankit Das

Enter roll number: 1

Enter GPA: 9.7

Enter grade: A

Student Information:

—————————-

Name:Ankit Das

Roll Number:1

GPA:9.7

Grade:A

38. Write a program in C++ to display information of employees using structure.

Solution:

Note: In C++, std::cin.ignore() used to discard or skip characters currently sitting inside the input buffer. It is most frequently used to fix input bugs where the program skips a std::getline() call right after a standard std::cin>> operation.

Output:

Enter the details of 3 employees:

Employee:1

Enter name: Raja Gogoi

Enter age: 25

Enter salary: 20000

Employee:2

Enter name: Nilom sharma

Enter age: 35

Enter salary: 30000

Employee:3

Enter name: Richa Das

Enter age: 25

Enter salary: 20000

Displaying Employee Information

Employee: 1

Name: Raja Gogoi

Age: 25

Salary: 20000

Employee: 2

Name: Nilom sharma

Age: 35

Salary: 30000

Employee: 3

Name: Richa Das

Age: 25

Salary: 20000

39. Write a C++ program to input any two numbers(integer) from the user and find the sum of the given numbers using function.

Solution:

Output:

Enter 2 numbers: 100 200

Sum of numbers is 300

40. Write a C++ program to input any number from the user and find the cube of the number using function.

Solution:

Output:

Enter a number: 5

Cube of the number is 125

41. Write a C++ program to input the radius of a circle from the user and find the diameter, circumference and area of the circle using function. Use Pi=3.142 in calculation.

Solution:

Output:

Enter radius of circle: 8

Diameter of the circle is 16

Circumference of the circle is 50.272

Area of the circle is 201.088

42. Write a program in C++ to calculate and return the area of a rectangle and area of a square using Function Overloading.

Solution:

Output:

Area of rectangle: 72

Area of square: 64

43. Write a program in C++ to calculate factorial using recursion.

Solution:

Output:

Enter a number: 5

Factorial of 5 = 120

44. Write a C++ program for print address of variable using Pointer.

Solution:

Output:

Value of num: 10

Address of num: 0x70fe34

Address of num: 0x70fe34

New value of num: 100

45. Write a program in C++ to add two numbers using Pointer.

Solution:

Output:

Enter 2 numbers: 5 8

Sum of 2 numbers is: 13

Leave a Comment