C/C++

Write a Program and Implementation in C++

write a program

Table of Contents

C++ write a program and implementation

Write a program in c++:- I have already seen some programs written in C++. However, writing the program does not 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. A program is written to run the result has to go through the following steps.



Steps of Program Writing:

Write a program in C++

The so-called program is a set of instructions that a computer system can recognize and execute. Each instruction makes the computer execute special Set operation. Programs written in high-level languages ​​are called “source programs”. The source program of C++ is With .cpp as the suffix (cpp is the abbreviation of cplus plus).

Compile the source program

Fundamentally speaking, computers can only recognize and execute binary instructions composed of 0 and 1, but cannot recognize and execute instructions. Run instructions written in high-level languages. In order to enable the computer to execute high-level language source programs, it must first be Interpreter (complier)” software (also called compiler or compilation system), the source program is translated into binary form “target “Object program”. Compilation is based on the source program file as the unit of compilation, each program unit composes a source program file, if there are multiple program units, the system compiles them into multiple target programs. The target program is generally .obj or .0 It is a suffix (short for 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, misspell 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, but in the front The variable a is not defined. Check all contents in the file when compiling, and finally display all Compilation error message. Generally, the error messages given by the compilation system are divided into two types, one is error; the other is Warning, refers to some minor errors that do not affect the operation (such as A variable is defined, but it has not been used Over). If the error of the error class is checked, the target program will not be generated and must be corrected and recompiled. )

write a program
figure 1.6

Connect the target

file f.cpp After correcting all errors and compiling them, 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 Finally, an executable binary file is formed, whose suffix is edit .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 above process is shown in Figure 1.6. The solid line represents the operation flow correct? The dashed line indicates the input and output of the file.

For example, after editing, 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”.

C++ Programm Writing Examples:

C++ Programming Example: Write a program that inputs a character and displays its ASCII code using cin cout stream objects:

#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 Example: Write a program that inputs time in seconds and converts it into standard format (such as hh:mm:ss format) using cin cout stream objects:

#include <iostream>
using namespace std;

main()
{
int seconds, ss,mm,hh;
cout<<"Enter time in second: ";
cin>>seconds;
hh=seconds/3600;
seconds=seconds%3600;
mm=seconds/60;
ss=seconds%60;
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 Example: 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:

#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 petorl in liter: 45                                                         
car will cover distance 238.5 miles

C++ Programming Example:  write a program that inputs the name, age, height and gender of a student using ‘cin C++ stream  object’:

Flowchart:

write a program

#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;
    
}

write a program



C++ Programming Example:  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:

#include <iostream>
using namespace std;

int main()
{
    float km, miles;
    cout<<"Enter miles: ";
    cin>>miles;
    km=1.609 * miles;
    cout<<miles<<" Miles = "<<km<<" Kilometers";
    
    
}

write a program

C++ Programming Example:  write a program in C++ to converts pound into kilograms using cin cout stream objects:

1 pound = 0.453592

#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";
}

write a program

C++ Programming Example:  write a program in C++ which convert Inches to centimeters using cin cout stream objects:

1 inch = 2.54 centimeters

#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";
}

write a program


Example 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:

#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;
}

write a program

Example 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.

#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;
}

write a program


Related Article:

String Function: strtok, strcmp, strcpy, strlen, strstr, strcat, strcat in c++

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button