## C Program Prime factors of a given number

by Dinesh Thakur Category: Control Structures

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

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

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

Page 2 of 4

Dinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles