Write a Program in C++ and Implementation, with example codes
Table of Contents
Write a program in C++ and implementation:
Write a program in C++, I have already observed some programs written in C++. Notwithstanding, writing the program doesn’t mean that the problem has been it was resolved, because it has not been run on the computer yet, and the final result has not been obtained. Let’s say a program has been written and now to run the program and display the result it will have to go through the following steps.
Amazon Purchase Links:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Write a program in C++
The so-called program is a set of instructions that a computer system can perceive and execute. Each instruction makes the computer execute a special set of operations. All the Programs written in high-level languages like the C and C++ itself are called “source programs”. The source program of C++ is with .cpp as the suffix (cpp is the abbreviation of c plus plus).
Compile the source program
Fundamentally speaking, computers can just recognize and execute binary instructions made out of 0 and 1, yet cannot recognize and execute instructions. Run instructions written in high-level languages. To enable the computer to execute high-level language source programs, it should initially be Interpreter (compiler)” software (also called compiler or compilation system), the source program is translated into binary structure “target “Object program”. The compilation is based on the source program file as the unit of compilation, each program unit forms a source program file, if there are different program units, and the system compiles them into numerous target programs. The target program is generally .obj or .0 it is a suffix (short for an object). The function of the compiler is to perform lexical and grammatical checks on the source program. Law check is
Check the spelling of words in the source program, for example, incorrectly spell main as mian. Grammar check is based on the source program to check the program syntax for errors, for example, output the value of variable a in the cout statement, yet in the front The variable a isn’t defined. Check all contents in the file when incorporating, and finally display all Compilation mistake message. Generally, the mistake messages given by the compilation system are isolated into two sorts, one is error; the other is Warning, alludes to some minor errors that don’t affect the operation, (for example, A variable is defined, yet it has not been utilized Over). In the event that the mistake of the error class is checked, the target program won’t be generated and should be adjusted and recompiled. )
Connect the target
Let’s say we just created a new program with the name f.cpp after correcting all the errors and compiling the code, one or more object files are obtained. At this time, use the system to mention The “linker” provided by Start The program is connected with the library files of the system and other information provided by the system. At last, an executable binary file is formed, whose suffix is edited .exe, can be executed directly. Orphan program Compile.
Run the program
Have Run the final executable binary file (.exe file) Wrong? target program f.obj Files), get the running result. no connection
Analyze the running results Executable
Target program and other]) f.exe Execution of bidding procedures If the running result is not correct, check whether the program or algorithm is has a problem. Incorrect result the process is shown in Figure 1.6 given above. The solid line represents the operation flow correct? The dashed line indicates the input and output of the file.
For instance, after editing the code, you get a Source program file f.cpp, and then compile the source program End File f.cpp input, after compilation, the target program file f.obj, Figure 1.6 Then enter the target program file f.obj into the memory, and the library provided by the system Files, etc. are connected to obtain the executable file f.exe, and finally f.exe is transferred into the memory and executed. Library file “correct”.
Examples, how to write a C++ program:
C++ Programming Example1: Write a program that inputs a character and displays its ASCII code using cin cout stream objects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> using namespace std; main() { char ch; int asc; cout<<"Enter the character: "; cin>>ch; asc=ch; cout<<"ASCII code of "<<ch<<"is: " <<asc; } Output: Enter the character: B ASCII code of B is 66 |
C++ Programming Example2: Write a program that inputs time in seconds and converts it into a standard format (such as hh:mm:ss format) using cin cout stream objects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> using namespace std; main() { int seconds, ss,mm,hh; cout<<"Enter time in second: "; cin>>seconds; hh=seconds/3600; seconds=seconds600; mm=seconds/60; ss=seconds`; cout<<"time in hh:mm:ss format is: "; cout<<hh<<":"<<mm<<":"<<ss; } Output: Enter time in second: 5000 time in hh:mm:ss format is: 1:23:20 |
C++ Programming Example3: A car can travel 5.3 miles in one liter. Write a program that inputs petrol in liters and displays how much distance the car can cover using the available petrol using cin cout stream objects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> using namespace std; main() { float petrol, distance; cout<<"Enter petorl in liter: "; cin>>petrol; distance=5.3*petrol; cout<<"car will cover distance "<<distance<<" miles"; } Output: Enter petrol in liter: 45 the car will cover a distance 238.5 miles |
C++ Programming Example4: write a program that inputs the name, age, height, and gender of a student using ‘cin C++ stream object’:
Flow chart:
Programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> using namespace std; int main() { char name[16], gender[5]; float height; int age; cout<<"Enter Name of the Student: "; cin>>name; cout<<"Enter Age of the Student: "; cin>>age; cout<<"Enter Height of the Student: "; cin>>height; cout<<"Enter Sex of the Student: "; cin>>gender; cout<<"\n-------- Student Detail -----------"; cout<<"\nName of the student is: "<<name<<endl; cout<<"Age of the student is: "<<age<<endl; cout<<"Height of the student is: "<<height<<endl; cout<<"Sex of the student is: "<<gender<<endl; } |
C++ Programming Example5: write a program that inputs distance in miles from user and converts miles into kilometers. One mile =1.609 kilometer using cin cout stream objects:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> using namespace std; int main() { float km, miles; cout<<"Enter miles: "; cin>>miles; km=1.609 * miles; cout<<miles<<" Miles = "<<km<<" Kilometers"; } |
C++ Programming Example6: write a program in C++ to converts pound into kilograms using cin cout stream objects:
1 pound = 0.453592
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> using namespace std; int main() { double kg, pounds; cout<<"Enter in pounds: "; cin>>pounds; kg=0.453592 * pounds; cout<<pounds<<" Pounds = "<<kg<<" Kilograms"; } |
C++ Programming Example7: write a program in C++ which convert Inches to centimeters using cin cout stream objects:
1 inch = 2.54 centimeters
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> using namespace std; int main() { float inches, cm; cout<<"Enter in inches: "; cin>>inches; cm=2.54 * inches; cout<<inches<<" Inches = "<<cm<<" Centimeters"; } |
Example8: write a program that computes the volume of a cylinder using const qualifier. The formula to find the volume to cylinder is: volume. The value of is 3.1417:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> using namespace std; int main() { float R,H, volume; const float PI=3.1417; cout<<"Enter the value of R: "; cin>>R; cout<<"Enter the value of H: "; cin>>H; volume=PI*R*R*H; cout<<"Volume of a Cylinder is = "<<volume; } |
Example9: write program that computes the area of a circle using constant define directive. The formula to find the area of a circle is: area. The value of is 3.1417.
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #define PI 3.1417 using namespace std; int main() { float R, area; cout<<"Enter the value of R: "; cin>>R; area=PI*R*R; cout<<"Area of a circle is = "<<area; } |