## C Program Data entry Validation

by Dinesh Thakur Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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 Category: Control Structures

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:

Page 5 of 16