Subscribe Us

C ++ Sample program

 




A complete program in C ++ must have a function called main which contains statements between braces {and }. Each statement can extend to more than one line but must end with a semicolon . Comments must be enclosed by /* and*/ or preceded by  // extending to the rest of the line.  The first can be used for comments that stand on many lines while the second for a whole line or an end part of a line. One of them can be used to comment out the other. Comments and blank lines are ignored by the compiler and contribute only to the readability of a program .  Below is a simple program that calculates the sum of all integers between two given integers.  The user is prompted to enter these two integers from the standard input stream (usually the terminal screen) represented by cin . The result is output to the standard output stream (usually the terminal screen) represented by cout. The streams cin and cout are defined in a C ++ standard library called<iostream>.  The program reads:

/*A sample program to illustrate some basic features of C++

It adds all integers between two given integers and

outputs the sum to the screen*/

 

cout <<"Enter two integers: \n"; // output to //screen

cin >> n>> m; //input will be assigned to n , m

if (n > m )

{// if n is bigger than m, swap them

int temp= n;// declare temp and initialize it

n = m; //assign value of m to n

m = temp;

} // assign value of temp to m


double sum = 0.00; //sum has double precision

// a loop ,  i changes from n to m with increment 1 each time

for(int i= n; i<= m; i++) 

{

sum+=i;//sum += i: sum = sum + I;

}

cout <<"Sum of integers from "<<  n<<" to"<< m

<<" is"<< sum<<"\n";// output sum to screen

return 0;

}


 

 

The first three lines in the program are comments enclosed by /* and

*/ which are usually used for multiline comments. Other comments in the

program are short ones and preceded by two slashes //; they can start from

the beginning or middle of a line extending to the rest of it.

 

Input and output are not part of C++ and their declarations are provided

in a header file called iostream that must be included at the beginning of

the program using

#include <iostream>

with the sign # standing in the first column. The statement using namespace std

 

lets the program gain access to declarations in the namespace std. All standard libraries are defined in a namespace called std. The mechanism namespace enables one to group related declarations and definitions together and supports modular programming.

 

The compiler will not recognize the identifiers cin and cout if the standard library <iostream> is not included. Similarly, the math

library <math.h> must be included when mathematical functions like

sin (sine), cos (cosine), atan(arctangent),exp (exponential), sqrt(square root), and log (logarithm) are used. The header files for standard libraries reside somewhere in the C++ system, and a programmer normally does not

have to care where. When they are included in a program, the system will find them automatically.

 

The symbol << is called the output operator and >>the input operator.

The statement

cout <<"Enter two integers: \n";

 

outputs the string “Enter two integers” followed by a new line to the screen, telling the user to type in, on the keyboard, two integers separated by a whitespace. Then the statement

cin >> n >> m;

 

causes the computer to pause while characters are entered from the key

board and stored in memory at locations identified by n and m. It is equivalent to the following two statements

cin >> n; // first input from screen is stored in n

cin >> m;// second input from screen is stored //in m

 

Notice that a variable must be declared before it can be used. The integer

variables n and m are declared by the statement,

int n, m; // declare n and m to be integers

The character ‘\n’ represents a newline. A character string like “Sum of

integers from “must appear between double quotation marks, while a single

character appear between single quotation marks such as ‘A’,’5’ and ‘\n’.

Note that ‘\n’ is a single character.

 

The words int, double, if, and for are reserved words in C++ and can not be used for the name of a variable. The reserved word int stands for

integers while double stands for double precision floating point numbers.

 

The variable sum is declared to have double precision by

double sum =0.00// sum is initialized to 0

 

This statement not only declares sum to have type double, but also assigns

it an initial value 0.00. A variable may not have to be initialized when it is declared.

 

In the if conditional statement, 

if    (n >m )// if n is bigger than m, swap them

{

int temp = n;// declare temp and initialize it

n = m;// assign value of m to n

m = temp;// assign value of temp to m

}

 

the statements inside the braces will be executed if the condition n > m

(n is strictly larger than m) is true and will be skipped otherwise. This if  statement instructs the computer to swap interchange the values of n and m if n is larger than m. Thus, after this if statement, n stores the smaller of the two input integers and m stores the larger one. Notice that a temporary  variable temp is used to swap n and m. The braces in an if statement can be omitted when there is only one statement inside them.

 

For example, the  following two statements are equivalent,

if    (n > m) m = n+100;// one statement

if    (n > m){ // an equivalent statement

m = n+100;

}

Share:

0 Comments:

Post a Comment

If you have any doubts . Please let me know.

Powered by Blogger.

Ad Code

Responsive Advertisement

Ad Code

Responsive Advertisement

Featured post

Search This Blog

Recently added book names

THE HTML AND CSS WORKSHOP   | MICROSOFT POWER BI COOKBOOK   | MongoDB in Action, 2nd Edition  | ADVANCED DEEP LEARNING WITH PYTHON   | Cracking Codes with Python An Introduction to Building and Breaking  | Moris Mano Degital Design 3rd Edition  | Beginning App Development with Flutter by Rap Payne  |react hooks in Action - John Larsen   | Artificial Intelligence A Modern Approach Third Edition Stuart Russel  | Data Structures and Algorithms - Narasimha Karumanchi   | Thomas S.M. - PostgreSQL High Availability Cookbook - 2017  | Gunnard Engebreth PHP 8 Revealed Use Attributes the JIT Compiler   | ICSE Class X Computer Application Notes   | INTERNET OF THINGS PROJECTS WITH ESP32   | 100 aptitude trick(102pgs)s   | OBJECT_ORIENTED_PROGRAMMING Question & Answer   | C questions and answer   | Full_Book_Python_Data_Structures_And_Algorithm   | Jira 8 Administration Cookbook Third Edition  | KALI LINUX WIRELESS PENETRATION TESTING BEGINNERS GUIDE THIRD EDITION - Cameron Buchanan, Vivek Ramachandran  HTML5 & javascript By :- Jeanine Meyer   | Python For Beginners Ride The Wave Of Artificial Intelligence   | HackingTheXbox   | Introduction to Algorithms 3rd.Edition - (CLRS)   | The C++ Programming Language - Bjarne Stroustrup   | Modern C++ Programming Cookbook - Marius Bancila   | Java The Complete Reference Eleventh Edition   Data_Communications and Networking 4th Ed Behrouz A Forouzan   | DevOps with Kubernetes - Hideto Saito   | The-Linux-Command-Line-A-Complete-Introduction   | Assembly Language for X86 Processors KIP R. Irvine   | Effective_Modern_C++ - Scott Meyer

Contact Form

Name

Email *

Message *

Followers

Mobile Logo Settings

Mobile Logo Settings
image

Computer Training School Regd. under Govt. of West Bengal Society Act 1961

Header Ads Widget

Responsive Advertisement

Hot Widget

random/hot-posts

Recent in Sports

Popular Posts

Labels

Blog Archive

Blogger templates