by Dinesh Thakur
Category: Control Structures

Perfect numbers are positive integers which have the special property that the sum of all their factors equals the number itself, e.g., 6 = 1+2+3. The C program finds and prints out all perfect numbers less than 1000.

by Dinesh Thakur
Category: Control Structures

The Greatest Common Divisor of two positive integers can be calculated iteratively by the following formula known as Euclid's algorithm. You can see that this is a recursive definition with GCD(m,n) defined in terms of GCD(n,m%n).

by Dinesh Thakur
Category: Control Structures

C program to sum the series 1/1^{2}+1/2^{2}+1/3^{2}

by Dinesh Thakur
Category: Control Structures

1. if income is less thn, 1,50,000 then no tax

2. if taxable income is in the range 1,50,001-300,000 then charge 10% tax

3. if taxable income is in the range 3,00,001-500,000 then charge 20% tax

4. if taxable income is above 5,00,001 then charge 30% tax.

by Dinesh Thakur
Category: Control Structures

Nested *for *loops have many applications, particularly, in programs dealing with sorting of lists, input/output of multi-dimensional arrays, etc. and also in the evaluation of expressions involving more than one parameter. The code for nested/or loops is given below.

by Dinesh Thakur
Category: Control Structures

So far, we have used only one variable in a *for *loop; however, more than one variable with different end values and with different modes of increments/decrements may also be used. In a compound *for *expression, the variables may be separated by a comma as illustrated for *i *and *j *below.

by Dinesh Thakur
Category: Control Structures

In the expression for *for *loop the inclusion of the expressions are optional. However, two semicolons must be included. An endless *for *loop may be written as shown below.

by Dinesh Thakur
Category: Control Structures

The *while *expression may consist of a single expression (as it is generally done in most of the programs); however, we may also use compound conditions or expressions. Multiple expressions may be connected by a comma operator or by Boolean operators. If the expressions are simply connected by comma, it is the last expression that is evaluated. The expressions preceding the last are ignored. In the following *while *expression the first expression, i.e., j <4 is neglected.

by Dinesh Thakur
Category: Control Structures

There are several situations where more than one parameter need to be varied over a range of values to obtain the desired results. For example, there may be a function having two variables, say *x *and *y *and it is desired to evaluate the function for different values of *x *and y. In such cases, for every value of *x *the values *of y *are varied over the range of values *of y. *This calls for *nested while *expressions as illustrated below.

by Dinesh Thakur
Category: Control Structures

The continue **statement **is another loop interruption statement provided in the C language. It interrupts only the current iteration of the loop as opposed to the break statement which interrupts the execution of the entire loop. The format of the continue statement is as follows:

by Dinesh Thakur
Category: Control Structures

The outer for loop is set up to process each number in the given range. Each number is tested within this loop using the simplified code. The execution of the break statement causes inner for loop to terminate as it is the nearest loop enclosing the break statement. If the inner for loop is completely executed, i. e., if condition d == num is true, num is printed as a prime number.

by Dinesh Thakur
Category: Control Structures

We have seen that a break **statement **is usually used in a switch statement after the statements in each case. The execution of such a break statement causes the execution of the switch statement to be terminated and the control to be transferred to the statement following the switch statement.

by Dinesh Thakur
Category: Control Structures

In this example, the initial value of loop variable j (of type int) is 100 and the update expression (j -= 10) reduces it by 10 after each iteration of the for loop.

by Dinesh Thakur
Category: Control Structures

Consider that we wish to print a given positive integer number in words, as a sequence of digit strings. For example, number 123 should be printed as *One Two Three. *This might be required in financial applications, for example, to print the cheque amount in words.

by Dinesh Thakur
Category: Control Structures

Three positive integer numbers *a, *band *c, *such that *a<b<c *form a Pythagorean triplet if c^{2}= *a ^{2} *+

by Dinesh Thakur
Category: Control Structures

As the name indicates, the prime factors of a given number are its factors that are also prime numbers, e.g., prime factors of 30 are 2, 3 and 5 but not 1, 6, 10, 15 and 30. One or more prime factors of a given number may repeat, e. g., prime factors of 120 are 2, 2, 2, 3 and 5.

by Dinesh Thakur
Category: Control Structures

The program segment given below determines the sum of digits of a given number repeatedly until a single digit number is obtained. For example, 5985 => 27 => 9, where symbol => indicates a digit sum operation. Thus, if digit sum exceeds 9, it is used as a number for subsequent digit sum operations.

by Dinesh Thakur
Category: Control Structures

The C language provides three loops (for,while and do ...while).As contained *statement *in the body of the loop can be any valid C statement, we can obtain several nested-loop structures by replacing this *statement *with another loop statement. Thus, if we replace the *statement *in a for loop with another for loop, we will get a two-level nested for loop as

About Dinesh Thakur

Dinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Basic Courses

Advance Courses