Introduction
In this article, I am going to write 5 basic c++ programs for beginners. Here, beginners mean those who have just started to learn c++. These programs will definitely help you.
Now, here are some important basic c++ programs.
Also Read: Simple Interest Program in C++ using Class
Average of Two Numbers in C++
Here, we will read two numbers from the keyboard and display the average of these two numbers.
#include <iostream>
using namespace std;
int main()
{
float a,b,sum,avg;
cout<<"Enter any two numbers"<<endl;
cin>>a>>b;
sum=a+b;
avg=sum/2;
cout<<"Average of "<<a<<" and "<<b<<" is "<<avg;
return 0;
}
Also Read: A Simple C++ Program with Explanation
Output of the above program is
Enter any two numbers
5 10
Average of 5 and 10 is 7.5
I am assuming that you are completely new to c++ programming. So, this explanation is only for you.
Explanation
Now, I am going to explain this program in detail.
Variable Declaration
In the c++ programming, you can declare variable anywhere in the program before using it. In c programming, we have to declare variables at the beginning. So, this is the advantage of c++. But, in this program, I have declared the variables at the beginning.
Here, I have declared four variables of float type.
cout<<“Enter any two numbers”<<endl;
Here, cout is the object of the class ostream i.e. output stream. With this cout, we are using insertion (<<) operator. Using this operator, we can display the output on the console. You can observe here that we have used insertion operator two times. Because we have to display two different output to the console. The string in the double quotation will be displayed as it is. The endl is used to terminate the line i.e. whatever we will write after this endl, it will be printed on the next line.
cin>>a>>b;
This is input statement in the c++ program. The cin is the object of the class istream and >> is known as the extraction operator. Whatever we are reading from the console, it will be stored in the variable a and b. But, keep in mind the use of cin. The number of extraction operator and the number of variables must be equal.
sum=a+b;
For calculating average, we need the addition. So, using this statement we are performing the addition operation. Here, the addition of a and b will be stored in the variable sum.
avg=sum/2;
Here, we are finding the average of the two numbers.
So, this is the simple explanation of the program. Now, I am going to write four more basic c++ programs.
C++ Program to Display Following Output Using Single out Statement.
Maths = 90
Physics = 77
Chemistry = 69
Again, this is a very basic c++ program. Here, we have to use only a single cout statement. Now, see the following program.
#include <iostream>
using namespace std;
int main()
{
int maths=90, physics = 77, chemistry = 69;
cout<<"Maths = "<<maths
<<"\nPhysics = "<<physics
<<"\nChemistry = "<<chemistry;
return 0;
}
As you can see in the above program, I have used only one cout statement for displaying the marks of different subjects. For this, I have used ‘\n’ which is new line character. Here is the output of this program.
Output
Maths = 90
Physics = 77
Chemistry = 69
Also Read: C++ Program to Read and Display Student Details using Class
Now, let us move to the next program.
C++ Program to Find the Larger Value Between Two Numbers
In this program, we will read two numbers from the keyboard and find the larger number.
#include <iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter any two numbers\n";
cin>>a>>b;
if(a>b)
{
cout<<a<<" is larger than "<<b;
}
else if(b>a)
{
cout<<b<<" is larger than "<<a;
}
else
{
cout<<"Both numbers are equal";
}
return 0;
}
Here, we are using if-else statement for comparing two numbers.
Ouput
Enter any two numbers
5
10
10 is larger than 5
Display Corresponding ASCII Value for the Given Character
In this program, we will read a single character from the keyboard and then display its equivalent ASCII value. ASCII means American Standard Code for Information Interchange. For every character, there is an equivalent ASCII value like ASCII value for ‘A’ is 65 and ‘a’ is 97.
Logic of this program is so simple. Here, we are converting character value to the integer value. So, it will automatically convert character to ASCII value. See the following program.
#include <iostream>
using namespace std;
int main()
{
char ch;
int a;
cout<<"Enter any character\n";
cin.get(ch);
a=ch; // character to integer
cout<<"ASCII value for "<<ch<<" is "<<a;
return 0;
}
Output of this program is
Enter any character
a
ASCII value for a is 97
We have already seen 4 basic c++ programs. I hope you are enjoying it. Now, we will see the last program.
Convert Temperature in Fahrenheit to Celsius
Here, we are going to write c++ program that will ask for a temperature in Fahrenheit and display it in Celsius. For doing this program, we must know the conversion formula and that formula is
C = (5/9)*(F-32)
Here, C means Celsius and F means Fahrenheit. Now, using this formula, I am going to write this c++ program.
#include <iostream>
using namespace std;
int main()
{
float f,c;
cout<<"Enter Temperature in Fahrenheit\n";
cin>>f;
c=(5/9.0)*(f-32);
cout<<"Temperature in Celsius is "<<c;
return 0;
}
Here, our input is temperature in Fahrenheit and output is temperature in Celsius. See the following output.
Enter Temperature in Fahrenheit
100
Temperature in Celsius is 37.7778
So guys, these are very five basic c++ programs. I hope you have understood this. If not, then please feel free to contact me.
Thank you.