Turn Desktop View Off
by Dinesh Thakur

The trapezoidal rule is the simplest method to approximate the definite integral of a function f(x) over the interval [a, b]. Given N equally space points (with a spacing of h) X0, X1, ..., XN such that X0 = a and XN = b, the integral of f(x) can be approximated as the sum

 
by Dinesh Thakur

C program to encrypt text using one of the simplest ciphers known as the "Caesar cipher." In this encryption scheme, we shift all characters by a given offset. For example, if we use an offset of 4, every occurrence of 'A' will be replaced by 'E', every occurrence of 'B' will be replaced by 'F', and so forth. The encrypted text can be decrypted by using the reverse process if know the offset used for the encryption.

 
by Dinesh Thakur

The C program computes the rational approximation to a given real number, i.e., given a real number val, the program computes a pair of integers N and D such that the fraction N/D is a good approximation to val. To do so, we use the following series of steps.

 
by Dinesh Thakur

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

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

C program to sum the series 1/12+1/22+1/32

 
by Dinesh Thakur

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

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

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

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

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

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

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

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

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

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

Three positive integer numbers a, band c, such that a<b<c form a Pythagorean triplet if c2= a2 +b2 , i. e,, a, b and c form the sides of a right-angled triangle. To select the values of a and b such that a < b and a, b < max, we can use nested for loops as shown below:

 
by Dinesh Thakur

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

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

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

 
by Dinesh Thakur

The program segment given below reads a number from the keyboard (in variable num of type float) and prints its square root. However, as the sqrt function requires a non-negative argument, the program uses a do ..while loop to read the data until valid data is entered.

 
by Dinesh Thakur

This program segment given below uses a straight-forward approach to count the number of odd and even digits in a given integer number(num).

 
by Dinesh Thakur

The program segment given below prints four-digit special perfect square numbers in which the upper and lower two-digit numbers are perfect squares as well.

 
by Dinesh Thakur

Let us use variables dd,mm and yy (all of type int)to represent the day, month and year in a given date. The given date is valid only if year (yy)is non-zero, month (mm)is in the range 1 to 12 and day of month (dd) is in the range 1 to mdays, the number of days in a given month mm. An algorithm to determine the validity of a given date is given below.

 
by Dinesh Thakur

In this example, a for loop is set up with values of loop variable num from m to n. The if statement is executed for each value of num and if nums an odd number (num%2 equals 1), it is printed using the printf statement. The output of this code is given below for m= 20 and n=40.

 
by Dinesh Thakur

The if statement within the body of the for loop is used to print a comma after each value of the loop variable except the last one.

Consider the code given below to print a comma-separated list of numbers from 1 to 10:

 
by Dinesh Thakur

Both the switch and if-else-if statements enable us to select one of several alternative statements for execution. However, they differ in several aspects:

 
by Dinesh Thakur

The program segment given below accepts marks in a single subject and uses a nested if statement to determine the validity of marks and the result if the value of marks is valid. This code can be written in a more readable form using an if-else-if statement as

 
by Dinesh Thakur

The program segment given below uses a nested if statement to determine whether a given character is a letter or not. In addition, if the given character is a letter, it tests whether it is a vowel or consonant.

 
by Dinesh Thakur

The C language allows nested if statements in which the if block and/or else block of an if statement contains if or if-else statements. The inner if statement(s) may in turn contain other if statements and so on.

 
by Dinesh Thakur

The program segment given below obtains a number by reversing the digits in the given number.

 
by Dinesh Thakur

The program segment given below using a do ...while loop and then determines the average of the given numbers.

 
by Dinesh Thakur

As a good programming practice, we display a message to prompt the user before accepting data from the keyboard. This enables the user to enter the required data correctly. However, the user may still enter incorrect data. Such data may cause the programs to print incorrect results.

 
by Dinesh Thakur

A program to determine the sum of digits of a given non-negative integer number using a while loop is presented in Program. The program segment given below does the same thing using a do...while loop. 

 
by Dinesh Thakur

The while loop is particularly suitable when the number of iterations is not known or can not be determined in advanced. In this section, another loop that is useful in similar situations, the do ... while loop is discussed.

 
by Dinesh Thakur

Let us use variables m and n to represent two integer numbers and variable r to represent the remainder of their division, i. e., r = m % n. Euclid's algorithm to determine the GCD of two numbers m and n is given below and its action is illustrated form= 50 and n = 35.

 
by Dinesh Thakur

we determined the sum of digits of a non-negative integer number by determining the least significant digit and then removing it from given number.

 
by Dinesh Thakur

The program segment given below accepts numbers from the keyboard until we enter a zero or a negative number and calculates their sum excluding the last number.

 
by Dinesh Thakur

Consider that we have to add the given numbers until the desired sum is obtained (i. e., as long as the sum is less than a specified value).

 
by Dinesh Thakur

The while loop is particularly useful when the number of iterations is not known or cannot be determined in advance. The general syntax of the while loop is as follows:

 
by Dinesh Thakur

The for loop is used for repetitive execution of a statement or a group of statements. It is a very powerful and flexible statement of the C language. It is generally used in situations where the number of iterations of the loop statement is known or can be determined in advance.

 
by Dinesh Thakur

In this example, we use two for loops in conjunction with the putchar library function to print all uppercase letters followed by all lowercase letters. The loop variable ch is assumed to be of type char or int. The first for loop prints the uppercase letters as loop variable ch assumes values as ‘A’, 'B’, ... , ‘z’.

 
by Dinesh Thakur

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

This code segment first accepts a range of values in variables m and n, both of type int. The variable num, also of type int, is used as the loop variable. It assumes values from m to n. For each value of loop variable num, the printf statement within the for loop is executed.

 
by Dinesh Thakur

The for loop given above prints a line of fifty dashes followed by a newline. The for loop uses i as the loop variable whose initial and final values are 0 and 49, respectively (note the < operator in i < 50). The update expression increments the value of i by 1.

 
by Dinesh Thakur

These statements determine and print the absolute value of variable n. If n is negative, then n < 0 evaluates as true and n is assigned the value of -n, which is then printed in the next statement assuming variable n to be of some integer type. However, if the value of n is positive, it remains unchanged and is printed.

 
by Dinesh Thakur

While performing a division operation, if the divisor is zero, the division by zero error occurs and program execution halts abruptly. The program segment given below calculates and prints the value of a/b without causing a division by zero error.

 
by Dinesh Thakur

if (marks >= 0 && marks <= 100)

    printf("Valid marks");

    else printf("Invalid marks");

 
by Dinesh Thakur

Consider that we wish to determine the result of a student in the SSC (Secondary School Certificate) examination, which has six subjects: four of 100 marks each and two of 150 marks each.

 
by Dinesh Thakur

A character is a whitespace if it is one of the following characters: blank space (' '), newline ('\n'), horizontal tab ('\t'), carriage return ('\r'), form feed ('\f') or vertical tab ('\v'). The if statement given below tests whether the given character ch is equal to one of these characters using logical or (||) operator and prints an appropriate message.

 
by Dinesh Thakur

The program segment given below accepts a character from the keyboard and prints the name of the corresponding color, e. g., if the user enters character R, it prints Red. However, it handles only three colors, namely, red, green and blue.

 
by Dinesh Thakur

The program segment given below accepts an integer number in variable digit of type int from the keyboard and if it is a single digit number (0-9), it displays the word corresponding to it;

 
by Dinesh Thakur

Let us use variables a and b of type float to represent two operands, variable c of type float to represent the result and variable op of type char to represent the operator. In the program given below, an expression of the form a op b is first accepted from the keyboard and a switch statement is used to evaluate the result.

 
by Dinesh Thakur

large= a> b? a : b;

This example illustrates how a conditional expression can be used to assign one of two alternative values to a variable depending on the outcome of some condition. As the conditional expression operator has higher precedence than the assignment operator, the above assignment statement is equivalent to

 
by Dinesh Thakur

The if-else statement given below uses the leap year test to determine the number of days in February.

 
by Dinesh Thakur

The switch statement allows us to test an expression and depending on its value, execute a statement (or group of statements) amongst several alternatives. It is the most involved statement provided in C language. It uses four keywords, namely, switch, case, default and break. The last two keywords are optional and can be omitted. The general form of this statement is given below and its flowchart is shown in.

 
by Dinesh Thakur

Often we wish to perform more than one operation when the condition in an if statement evaluates as true and/or false as shown in Fig. In such situations, we use block statements (also called compound statements) within the if statement.

 
by Dinesh Thakur

An Armstrong number has an interesting property that the sum of cubes of its digits is equal to the number itself (e. g., 153 = 13 + 53 + 33). The program segment given below prints all the three-digit Armstrong numbers.

 
by Dinesh Thakur

The program given below uses these equations directly to evaluate the roots of a quadratic equation. The main function declares variables a, b and c of type float to represent the coefficients of the quadratic equation, and variables xl and x2 also of type float to represent the roots.

 

Page 1 of 3