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. [Read more…] about C calculating Integration using Trapezoidal Rule
C Program for string encryption using Caesar cipher (shift encoding).
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. [Read more…] about C Program for string encryption using Caesar cipher (shift encoding).
C Program for Rational Approximations for Real Numbers
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. [Read more…] about C Program for Rational Approximations for Real Numbers
C Program Calculation of Perfect Numbers
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. [Read more…] about C Program Calculation of Perfect Numbers
C Program for GCD of Two Integers using Euclid’s algorithm
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).
[Read more…] about C Program for GCD of Two Integers using Euclid’s algorithm
C program to sum the series
C program to sum the series 1+1/2 + 1/3…+ 1/n
In this tutorial, we can learn C program to sum the series 1+1/2 + 1/3…+ 1/n. In this c program, we enter a number and and generate the sum of series. [Read more…] about C program to sum the series 1+1/2 + 1/3…+ 1/n
C Program square and cube of first n natural numbers
C program to calculate tax, given the following conditions
In this tutorial, we can learn C program to calculate tax. In this c program, we enter a number and calculate tax with the given following conditions. [Read more…] about C program to calculate tax, given the following conditions
Nested FOR Loops in C
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. [Read more…] about Nested FOR Loops in C
Compound Conditions In For loop in C
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. [Read more…] about Compound Conditions In For loop in C
Endless for loop in C
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. [Read more…] about Endless for loop in C
Compound Conditions in a While in C
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. [Read more…] about Compound Conditions in a While in C
Nested While Statements in C
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. [Read more…] about Nested While Statements in C
C Program Prints Prime Numbers in a given range m to n
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 break statement’s execution causes the inner for loop to terminate as it is the nearest loop enclosing the break statement. If the inner for loop is entirely executed, i. e., if condition d == num is true, num is printed as a prime number. [Read more…] about C Program Prints Prime Numbers in a given range m to n
break Statement in C Language
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. [Read more…] about break Statement in C Language
C Program print a given integer number in words
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. [Read more…] about C Program print a given integer number in words
C Program Print Pythagorean triplets
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: [Read more…] about C Program Print Pythagorean triplets
C Program Prime factors of a given number
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. [Read more…] about C Program Prime factors of a given number
C Program Sum of digits of number repeatedly to obtain a single-digit number
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. [Read more…] about C Program Sum of digits of number repeatedly to obtain a single-digit number
Nested Loops in C
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 [Read more…] about Nested Loops in C
C Program Data entry Validation
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. [Read more…] about C Program Data entry Validation
C Program Count Number of odd and even digits in a given integer number
This program segment below uses a straight-forward approach to count the number of odd and even digits in a given integer number(num).
[Read more…] about C Program Count Number of odd and even digits in a given integer number
C Program Four digit special perfect square numbers
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. [Read more…] about C Program Four digit special perfect square numbers
C Program Date is Valid or Not
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. [Read more…] about C Program Date is Valid or Not
C Program Print Odd Numbers in a given range m to n
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. [Read more…] about C Program Print Odd Numbers in a given range m to n
C Program Print a comma-separated list of numbers from 1 to 10
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. [Read more…] about C Program Print a comma-separated list of numbers from 1 to 10
What is the difference between IF-ELSE and SWITCH?
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: [Read more…] about What is the difference between IF-ELSE and SWITCH?
C Program Examination result in a single subject with data validation
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 [Read more…] about C Program Examination result in a single subject with data validation
C Program Character test letter, vowel, consonant
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. [Read more…] about C Program Character test letter, vowel, consonant