## 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:

## C Programming for Loop

by Dinesh Thakur Category: Control Structures

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

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 in a given range

by Dinesh Thakur Category: Control Structures

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

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

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

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

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

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

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

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.

Page 6 of 16