## C calculating Integration using Trapezoidal Rule

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

## C Program for string encryption using Caesar cipher (shift encoding).

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.

## C Program for Rational Approximations for Real Numbers

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.

## C Program Calculation of Perfect Numbers

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.

## C Program for GCD of Two Integers using Euclid's algorithm

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

## C program to sum the series

by Dinesh Thakur

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

by Dinesh Thakur

by Dinesh Thakur

## C program to calculate tax, given the following conditions

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.

## Nested FOR Loops in C

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.

## Compound Conditions In For loop in C

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.

## Endless for loop in C

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.

## Compound Conditions in a While in C

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.

## Nested While Statements in C

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.

## continue Statement in C Language

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:

## C Program Prints Prime Numbers in a given range m to n

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.

## break Statement in C Language

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.

## C Program print a given integer number in words

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.

## C Program Print Pythagorean triplets

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:

## C Program Prime factors of a given number

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.

## C Program Sum of digits of number repeatedly to obtain a single-digit number

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.

## Nested Loops in C

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

## C Program Data entry Validation

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.

## C Program Count Number of odd and even digits in a given integer number

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

## C Program Four digit special perfect square numbers

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.

## C Program Date is Valid or Not

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.

## C Program Print Odd Numbers in a given range m to n

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.

## C Program Print a comma-separated list of numbers from 1 to 10

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:

## What is the difference between IF-ELSE and SWITCH?

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:

## C Program Examination result in a single subject with data validation

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

## C Program Character test letter, vowel, consonant

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.

## Nested if Statements in C Language

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.

## C Program Reverse digits in an integer number

by Dinesh Thakur

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

## C Program Determine sum and average of several numbers entered from the keyboard.

by Dinesh Thakur

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

## C Program Accept data from the keyboard until correct data is entered

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.

## C Program Sum of Digits of a Given Integer Number with do-while loop

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.

## do ... while Loop in C

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.

## C Program for GCD using Euclid’s algorithm

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.

## C Program Sum of Digits of a Given Integer Number

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.

## Add numbers until a negative or zero is encountered

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.

## Money payment: add numbers until desired sum is obtained

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

## while loop in C

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:

## C Programming for Loop

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.

## C Program for Print all uppercase letters followed by all lowercase letters on the next line

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

## C Program for Print integer number from 100 to 0 in steps of -10

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.

## C Program for Print integer number in a given range

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.

## C program for Print a line of dashes

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.

## C Program find the absolute value of a given Number

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.

## C Program division without causing a division by zero error

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.

## C Program Find the Validity of marks in a single subject

by Dinesh Thakur

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

printf("Valid marks");

else printf("Invalid marks");

## C Program Find the Result of a student in SSC Examination

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.

## C program Test for whitespace

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.

## C Program for print a name of color that start with a given character

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.

## C Program for print a given digit as a word

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;

## C Program for Simple Calculator

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.

## C Program Find The Larger between Two Numbers

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

## C Program Find The Days in February

by Dinesh Thakur

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

## switch Statement in C

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.

## Block Statements in the if Statement

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

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

## Write a C program to print out all armstrong numbers between 1 to 1000.

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

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

## Write a C program to find the roots of quadratic equation.

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.

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

by Dinesh Thakur

Page 1 of 3