10 simple c++ programs for beginners

10 simple c++ programs for beginners

C++ is one the most popular languages in the programming world. In this article we will be looking towards 10 simple programs for beginners in CPP.

1. Adding to numbers in C++.
Ans. Take two variables and take user input and add them.

``````#include <iostream>
using namespace std;
int main() {
int a ;
int b ;
cin>>a>>b;
cout<<a+b;
return 0;
}
Input: 2 5
Output: 7``````
2. Check if a number is even or odd.
Ans. If the given number is divisible by 2 then it is even otherwise odd.

``````#include <iostream>
using namespace std;
int main() {
int a ;
cin>>a;
if(a%2 == 0)  // if remainder is zero then even number
cout<<â€ťevenâ€ť;
else       cout<<â€ťoddâ€ť;
return 0;
}
Input: 8
Output: even``````
3. Write a program to swap two numbers.
Ans. Use a temporary variable to store one of the numbers.

``````#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 20;
cout<<a<<" "<<b<<endl;
int temp = a;
a = b;
b = temp;
cout<<a<<" "<<b<<endl;
return 0;
}
Output: 10 20
20 10``````
4. Write a program to find the largest number among three numbers.
Ans. A number will be largest if number is greater than both the other numbers.

``````#include <iostream>
using namespace std;
int main() {
float a, b, c;
cin >> a >> b >> c;
if(a >= b && a >= c)
cout << "Largest number: " << a;
if(b >= a && b >= c)
cout << "Largest number: " << b;
if(c >= a && c >= b)
cout << "Largest number: " << c;
return 0;
}
Input: 1 2 3
Largest number: 3``````
5. Find the sum of all the natural numbers from 1 to n.
Ans. We can do this using two ways, one is by iterating from 1 to n and adding them up while the other way is using the summation formula –
``````summation of i from 1 to n=n(n+1)/2=1+2+3+4+..+(n-1)+n

#include <iostream>
using namespace std;
int main()
{
int n, sum = 0;
cin >> n;
for (int i = 1; i <= n; ++i) {
sum += i;
}
// or sum = n*(n+1)/2;
cout << sum;
return 0;
}
Input: 5
Output: 15``````
1. Write a program to check whether a number is prime or not.
Ans. A prime number is not divisible by any number smaller than it except 1. Basically, it has only two factors 1 and itself.

``````#include <iostream>
using namespace std;
int main() {
int a ;
cin>>a;
int b = 2;
//start from b as 1 can divide any number
bool prime = true;
while(b!=a){
if(a%b == 0)
{
prime = false;
break;
}
b++;
}
if(prime)
cout<<"prime";
else cout<<"not prime";
return 0;
}
Output: prime``````
2. Compute the power a given number to a given power.
Ans. We need to keep multiplying the same number power times.

``````3^3=3*3*3=27
#include <iostream>
using namespace std;
int main()
{
int power;
float base, result = 1;
cin >> base >>power;
while (power != 0) {
result *= base;
power--;
}
cout << result;
return 0;
}
Input: 3 3
Output: 27``````
3. Calculate the average of all the elements present in an array.
Ans.

``````average= summationă€–arr[i]ă€—/n
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
float sum = 0.0;
for(int i = 0;i<n;i++)
cin>>arr[i];
for(int i = 0;i<n;i++)
sum += arr[i];
cout<<(float)(sum/(float)n);
return 0;
}
Input: 3
1 4 5
Output: 3.33333``````
4. Write a program to find the GCD of two numbers.
Ans. GCD is the greatest common divisor of the two numbers.

``````#include <iostream>
using namespace std;
int gcd(int a,int b){
if(b == 0)
return a;
return gcd(b,a%b);
}
int main() {
int a ,b ;
cin>>a>>b;
cout<<gcd(a,b);
return 0;
}
Input: 35 25
Output: 5``````
5. Write a function to find the length of a string in CPP.
Ans. Iterate through the string and increase count by 1 till we reach the end of the string.

``````#include <iostream>
using namespace std;
int main()
{
string str;
cin>>str;
int count = 0;
for(int i = 0;str[i];i++) // till the string character is null
count++;
cout<<count;
}
Input: abcde
Output: 5``````