Difference between perfect square and a number that can be expressed as product of consecutive integers:

Image
A perfect square is a number that can be expressed as the product of an integer by itself or as the second exponent of an integer¹. For example, 1, 4, 9, and 16 are perfect squares because they are the squares of 1, 2, 3, and 4 respectively. A perfect square can also be written as x^2, where x is an integer. A number that can be expressed as a product of consecutive integers is a number that can be obtained by multiplying two or more integers that follow each other in order. For example, 6, 24, and 120 are numbers that can be expressed as products of consecutive integers because they are equal to 2 x 3, 2 x 3 x 4, and 2 x 3 x 4 x 5 respectively. A number that can be expressed as a product of consecutive integers can also be written as x(x + 1)(x + 2)...(x + n), where x and n are integers. The difference between a perfect square and a number that can be expressed as a product of consecutive integers is that a perfect square has only one factor pair that consists of the same integer, whi

C programing Lesson 2:

 


The First C Program : 

Once we learn  about variables, constants & keywords, the next logical step would be to combine them to form instructions. However, instead of this, we would write our first C program now. Once we have done that we would see in detail the instructions that it made use of. The first program is very simple. It calculates simple interest for a set of values representing principal, number of years and rate of interest.


Let us now understand this program in detail:

Form of a C Program :

Form of a C program indicates how it has to be written/typed. There are certain rules about the form of a C program that are applicable to all C programs. These are as under: 

(a) Each instruction in a C program is written as a separate statement.

(b) The statements in a program must appear in the same order in which we wish them to be executed.

(c) Blank spaces may be inserted between two words to improve the readability of the statement.

(d) All statements should be in lower case letters.

(e) C has no specific rules for the position at which a statement is to be written in a given line. That’s why it is often called a free-form language.

(f) Every C statement must end with a semicolon ( ; ). Thus ; acts as a statement terminator. 


Comments in a C Program:

Comments are used in a C program to clarify either the purpose of the program or the purpose of some statement in the program. It is a good practice to begin a program with a comment indicating the purpose of the program, its author and the date on which the program was written. Here are a few things that you must remember while writing.

comments in a C program:

 (a) Comment about the program should be enclosed within /* */. Thus, the first two statements in our program are comments. 
(b) Sometimes it is not very obvious as to what a particular statement in a program accomplishes. At such times it is worthwhile mentioning the purpose of the statement (or a set of statements) using a comment. For example:

/* formula for simple interest */ 
si = p * n * r / 100 ;
(c) Any number of comments can be written at any place in the program. For example, a comment can be written before the statement, after the statement or within the statement as shown below. 

/* formula */ si = p * n * r / 100 ;
 si = p * n * r / 100 ; /* formula */
 si = p * n * r / /* formula */ 100 ;  

(d) The normal language rules do not apply to text written within /* .. */. Thus we can type this text in small case, capital or a combination. This is because the comments are solely given for the understanding of the programmer or the fellow programmers and are completely ignored by the compiler. 
 (e) Comments cannot be nested. This means one comment cannot be written inside another comment. For example,

/* Cal of SI /* Author: gekay date: 25/06/2016 */ */   

 is invalid.

(f) A comment can be split over more than one line, as in,

 /* This comment has
 three lines
 in it */ 
Such a comment is often called a multi-line comment. 

(g) ANSI C permits comments to be written in the following way: 

 // Calculation of simple interest 

 // Formula 

Variables and their Usage:

We have learnt constants and variables in the first lesson. Let us understand their significance with reference to our first C program.

(a) Any variable used in the program must be declared before using it. For example,

int p, n ; /* declaration */

 float r, si ; /* declaration */ 

si = p * n * r / 100 ; /* usage */

(b) In the statement,

si = p * n * r / 100 ;  

* and / are the arithmetic operators. The arithmetic operators available in C are +, -, * and /. C is very rich in operators. There are as many as 45 operators available in C. Surprisingly there is no operator for exponentiation... a slip, which can be forgiven considering the fact that C has been developed by an individual, not by a committee.

printf( ) and its Purpose:

C does not contain any instruction to display output on the screen. All output to screen is achieved using readymade library functions. One such function is printf( ). Let us understand this function with respect to our program.

(a) Once the value of si is calculated it needs to be displayed on the screen. We have used printf( ) to do so. 

(b) For us to be able to use the printf( ) function, it is necessary to use #include at the beginning of the program. #include is a preprocessor directive. Its purpose will be clarified in Chapter 8. For now, use it whenever you use printf( ).

 (c) The general form of printf( ) function is,

printf ( "<Format string/format specifier>", list of variables) ; 

<format string/format specifier> can contain %d, %f, or  %c.

%f for printing real values 

%d for printing integer values 

%c for printing character values

In addition to format specifiers like %f, %d and %c, the format string may also contain any other characters. These characters are printed as they are when printf( ) is executed.

(d) Given below are some more examples of usage of printf( ) function:

printf ( "%f", si ) ;

printf ( "%d %d %f %f", p, n, r, si ) ;

printf ( "Simple interest = Rs. %f", si ) ; 

printf ( "Principal = %d \nRate = %f", p, r ) ;

The output of the last statement would look like this...

Principal = 1000 

Rate = 8.500000 

What is ‘\n’ doing in this statement? It is called newline and it takes the cursor to the next line. Therefore, you get the output split over two lines. ‘\n’ is one of the several Escape Sequences available in C. These are discussed in detail in Chapter 18. Right now, all that we can say is ‘\n’ comes in handy when we want to format the output properly on separate lines.

(e) printf( ) can not only print values of variables, it can also print the result of an expression. An expression is nothing but a valid combination of constants, variables and operators. Thus, 3, 3 + 2, c and a + b * c – d all are valid expressions. The results of these expressions can be printed as shown below.

printf ( "%d %d %d %d", 3, 3 + 2, c, a + b * c – d ) ; 

Note that 3 and c also represent valid expressions.

Compilation and Execution: 

Once you have written the program you need to type it and instruct the machine to execute it. To type your C program you need another program called Editor. Once the program has been typed it needs to be converted to machine language instructions before the machine can execute it. To carry out this conversion we need another program called Compiler(discussed in first lesson). Compiler vendors provide an Integrated Development Environment (IDE) which consists of an Editor as well as the Compiler.


Comments

Popular posts from this blog

CP Open ended Lab:

C language program to find prime number

What is a computer language