0% found this document useful (0 votes)
37 views

10 Simple C++ Programs

The document provides 10 simple C++ programs for beginners including programs to add two numbers, check if a number is even or odd, swap two numbers, find the largest of three numbers, find the sum of natural numbers from 1 to n, check if a number is prime, compute a number to a given power, calculate the average of an array, find the greatest common divisor of two numbers, and find the length of a string.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

10 Simple C++ Programs

The document provides 10 simple C++ programs for beginners including programs to add two numbers, check if a number is even or odd, swap two numbers, find the largest of three numbers, find the sum of natural numbers from 1 to n, check if a number is prime, compute a number to a given power, calculate the average of an array, find the greatest common divisor of two numbers, and find the length of a string.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

10 Simple C++ Programs

Below are the top 10 simple C++ programs for beginners:

1. Wrtie a program for Adding two numbers in C++.


To Add two numbers in C++ we will read two numbers a and b from the user then perform add operation to add a and b together to print the addition of
two numbers in C++.

2. #include <iostream>

3. using namespace std;

4. int main() {

5. int a ;

6. int b ;

7. cin>>a>>b;

8. cout<<a+b;

9. return 0;

10. }

11. Input: 2 5

Output: 7

12. C++ program to Check if a number is even or odd.


TO Check if a given number is even or odd in C++, we simply divide the given number by 2, if the remainder is 0 then it is even otherwise odd.

13. #include <iostream>

14. using namespace std;

15. int main() {

16. int a ;

17. cin>>a;

18. if(a%2 == 0) // if remainder is zero then even number


19. cout<<”even”;

20. else

21. cout<<”odd”;

22. return 0;

23. }

24. Input: 8

Output: even

25. Write a program to swap two numbers in C++.


We will use a temporary variable to store one of the numbers to perform the swap operation.

26. #include <iostream>

27. using namespace std;

28. int main() {

29. int a = 10;

30. int b = 20;

31. cout<<a<<" "<<b<<endl;

32. int temp = a;

33. a = b;

34. b = temp;

35. cout<<a<<" "<<b<<endl;

36. return 0;

37. }

38. Output: 10 20
20 10

39. Write a C++ program to find the largest number among three numbers.
A number will be largest if number is greater than both the other numbers.

40. #include <iostream>

41. using namespace std;

42. int main() {

43. float a, b, c;

44. cin >> a >> b >> c;

45. if(a >= b && a >= c)

46. cout << "Largest number: " << a;

47. if(b >= a && b >= c)

48. cout << "Largest number: " << b;

49. if(c >= a && c >= b)

50. cout << "Largest number: " << c;

51. return 0;

52. }

53. Input: 1 2 3

Largest number: 3

54. Write a C++ Program to Find the sum of all the natural numbers from 1 to n.
To find the sum of all the natural number from 1 to n in C++, We have two methods, one is by iterating from 1 to n and adding them up while the other
way is using the summation formula –

55. summation of i from 1 to n=n(n+1)/2=1+2+3+4+..+(n-1)+n

56. #include <iostream>

57. using namespace std;


58. int main()

59. {

60. int n, sum = 0;

61. cin >> n;

62. for (int i = 1; i <= n; ++i) {

63. sum += i;

64. }

65. // or sum = n*(n+1)/2;

66. cout << sum;

67. return 0;

68. }

69. Input: 5

Output: 15

70. Write a program in C++ to check whether a number is prime or not.


A prime number is not divisible by any number smaller than it except 1. Basically, it has only two factors 1 and itself.

71. #include <iostream>

72. using namespace std;

73. int main() {

74. int a ;

75. cin>>a;

76. int b = 2;

77. //start from b as 1 can divide any number


78. bool prime = true;

79. while(b!=a){

80. if(a%b == 0)

81. {

82. prime = false;

83. break;

84. }

85. b++;

86. }

87. if(prime)

88. cout<<"prime";

89. else

90. cout<<"not prime";

91. return 0;

92. }

Output: prime

93. Write a C++ program to Compute the power a given number to a given power.
To compute the power of a given number in C++, We initialize a variable result to 1. Then, we’ll use a while loop to multiply the result by base for power
number of times.

94. 3^3=3*3*3=27

95. #include <iostream>

96. using namespace std;

97. int main()


98. {

99. int power;

100. float base, result = 1;

101. cin >> base >>power;

102. while (power != 0) {

103. result *= base;

104. power--;

105. }

106. cout << result;

107. return 0;

108. }

109. Input: 3 3

Output: 27

110. Write a C++ program to Calculate the average of all the elements present in an array.
We iterate over each element of the array and calculate the sum of all the elements. Then, we divide the sum by the size of the array to get the
average. The average is stored in a variable of type float and returned.

111. average= summation〖arr[i]〗/n

112. #include <iostream>

113. using namespace std;

114. int main()

115. {

116. int n;

117. cin>>n;
118. int arr[n];

119. float sum = 0.0;

120. for(int i = 0;i<n;i++)

121. cin>>arr[i];

122. for(int i = 0;i<n;i++)

123. sum += arr[i];

124. cout<<(float)(sum/(float)n);

125. return 0;

126. }

127. Input: 3

128. 1 4 5

Output: 3.33333

129. Write a program to find the GCD of two numbers in C++.


GCD is the greatest common divisor of the two numbers.

130. #include <iostream>

131. using namespace std;

132. int gcd(int a,int b){

133. if(b == 0)

134. return a;

135. return gcd(b,a%b);

136. }

137. int main() {


138. int a ,b ;

139. cin>>a>>b;

140. cout<<gcd(a,b);

141. return 0;

142. }

143. Input: 35 25

Output: 5

144. Write a function to find the length of a string in C++.


To find the length of a string in C++, we will Iterate through the string and increase count by 1 till we reach the end of the string.

145. #include <iostream>

146. using namespace std;

147. int main()

148. {

149. string str;

150. cin>>str;

151. int count = 0;

152. for(int i = 0;str[i];i++) // till the string character is null

153. count++;

154. cout<<count;

155. }

156. Input: abcde

Output: 5

You might also like