10 Simple C++ Programs for Beginners (2024)

Last Updated on June 14, 2023 by Abhishek Sharma

10 Simple C++ Programs for Beginners (1)

C++ is one the most popular language in the programming world. In this article we will be looking towards 10 basic C++ programs for beginners in CPP. C++ is a powerful general-purpose programming language that was developed in the early 1980s as an extension of the C programming language. It is widely used for developing a wide range of applications, including system software, game development, embedded systems, high-performance applications, and more.

C++ combines both high-level and low-level programming features, offering a balance between performance and abstraction. It supports procedural, object-oriented, and generic programming paradigms, giving developers flexibility in designing and implementing their solutions.

10 Simple C++ Programs for Beginners

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++.
    #include <iostream>using namespace std;int main() {int a ;int b ;cin>>a>>b;cout<<a+b;return 0;}Input: 2 5Output: 7
  2. 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.
    #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: 8Output: even
  3. 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.
    #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 2020 10
  4. 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.
    #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 3Largest number: 3
  5. 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 –
    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: 5Output: 15
  6. 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.
    #include <iostream>using namespace std;int main() {int a ;cin>>a;int b = 2;//start from b as 1 can divide any numberbool 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
  7. 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.
    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 3Output: 27
  8. 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.
    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: 31 4 5Output: 3.33333
  9. Write a program to find the GCD of two numbers in C++.
    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 25Output: 5
  10. 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.
    #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: abcdeOutput: 5

Summary

  • Building Basic C++ programs is a great way for beginners to learn the basics of the language and gain confidence in programming.
  • By working on these programs, beginners can practice fundamental concepts like variables, data types, control structures, and functions.
  • The 10 simple C++ programs cover a range of topics, including arithmetic operations, input/output, conditionals, loops, and arrays.
  • Working through these programs allows beginners to apply their knowledge and develop problem-solving skills.
  • It is important to understand and analyze the code of these programs to grasp the underlying concepts fully.
  • As beginners progress, they can modify and expand upon these programs to create more complex applications.

FAQs related to 10 simple C++ programs for beginners:

Some FAQs related to Basic C++ programs for Beginners are listed below:
Q1. What are some recommended programs for beginners in C++?
Some recommended programs for beginners include calculating the area of a triangle, finding the factorial of a number, generating Fibonacci series, converting temperature units, and implementing a simple calculator.

Q2. How do I compile and run these programs?
To compile and run C++ programs, you need a C++ compiler installed on your system. Popular compilers include GCC (GNU Compiler Collection) and Microsoft Visual C++. You can use a command-line interface or an Integrated Development Environment (IDE) like Code::Blocks, Dev-C++, or Visual Studio Code to write, compile, and run your programs.

Q3. What are some common errors beginners may encounter?
Beginners may encounter errors such as syntax errors (e.g., missing semicolons or parentheses), logical errors (e.g., incorrect conditions or loop structures), or runtime errors (e.g., dividing by zero or accessing invalid memory locations). These errors can be resolved by carefully analyzing the code and using debugging techniques.

Q4. How can I improve my programming skills beyond these simple programs?
To improve your programming skills, you can challenge yourself by tackling more complex programs and projects. Explore advanced topics like object-oriented programming, file handling, data structures, and algorithms. Additionally, participate in coding competitions, join programming communities, and practice regularly to enhance your skills.

Q5. Are there any additional resources to learn C++ for beginners?
Yes, there are numerous resources available for beginners to learn C++. Some recommended resources include online tutorials, video courses, interactive coding platforms and C++ programming books. These resources often provide step-by-step guidance and exercises to reinforce learning.

10 Simple C++ Programs for Beginners (2024)
Top Articles
The Freehold Transcript and The Monmouth Inquirer from Freehold, New Jersey
SolarEdge Home Opslag & noodstroomvoorziening
AMC Theatre - Rent A Private Theatre (Up to 20 Guests) From $99+ (Select Theaters)
Skyward Houston County
Palm Coast Permits Online
1970 Chevelle Ss For Sale Craigslist
Affidea ExpressCare - Affidea Ireland
Nfr Daysheet
Miss Carramello
Noaa Weather Philadelphia
อพาร์ทเมนต์ 2 ห้องนอนในเกาะโคเปนเฮเกน
2016 Hyundai Sonata Price, Value, Depreciation & Reviews | Kelley Blue Book
Explore Top Free Tattoo Fonts: Style Your Ink Perfectly! 🖌️
Zürich Stadion Letzigrund detailed interactive seating plan with seat & row numbers | Sitzplan Saalplan with Sitzplatz & Reihen Nummerierung
Pay Boot Barn Credit Card
We Discovered the Best Snow Cone Makers for Carnival-Worthy Desserts
Samantha Aufderheide
Fsga Golf
Graphic Look Inside Jeffrey Dahmer
Euro Style Scrub Caps
Pioneer Library Overdrive
Stockton (California) – Travel guide at Wikivoyage
130Nm In Ft Lbs
Worthington Industries Red Jacket
Christmas Days Away
Craigs List Tallahassee
Broken Gphone X Tarkov
Exploring TrippleThePotatoes: A Popular Game - Unblocked Hub
Selfservice Bright Lending
Polk County Released Inmates
Directions To 401 East Chestnut Street Louisville Kentucky
Merge Dragons Totem Grid
Elgin Il Building Department
Maxpreps Field Hockey
Alpha Asher Chapter 130
Myql Loan Login
Michael Jordan: A timeline of the NBA legend
Mvnt Merchant Services
D-Day: Learn about the D-Day Invasion
Cheetah Pitbull For Sale
Skyward Marshfield
Sun Tracker Pontoon Wiring Diagram
Dinar Detectives Cracking the Code of the Iraqi Dinar Market
Lucyave Boutique Reviews
Poe Self Chill
Makes A Successful Catch Maybe Crossword Clue
Phmc.myloancare.com
Menu Forest Lake – The Grillium Restaurant
Dancing Bear - House Party! ID ? Brunette in hardcore action
Meet Robert Oppenheimer, the destroyer of worlds
Lebron James Name Soundalikes
Pronósticos Gulfstream Park Nicoletti
Latest Posts
Article information

Author: Edmund Hettinger DC

Last Updated:

Views: 6141

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Edmund Hettinger DC

Birthday: 1994-08-17

Address: 2033 Gerhold Pine, Port Jocelyn, VA 12101-5654

Phone: +8524399971620

Job: Central Manufacturing Supervisor

Hobby: Jogging, Metalworking, Tai chi, Shopping, Puzzles, Rock climbing, Crocheting

Introduction: My name is Edmund Hettinger DC, I am a adventurous, colorful, gifted, determined, precious, open, colorful person who loves writing and wants to share my knowledge and understanding with you.