C Programming Decision Making and Loops Examples
You will find necessary examples to create programs containing loops and decision making statements in this article.
To understand examples on this page, you should have the knowledge of following topics:
C Control Flow Examples
Check Whether a Number is Even or Odd
An even number is an integer that is exactly divisible by 2. Example: 0, 8, -24
An odd number is an integer that is not exactly divisible by 2. Example: 1, 7, -11, 15
Example #1: Program to Check Even or Odd
Output
Enter an integer: -7 -7 is odd.
In the program, integer entered by the user is stored in variable number.
|
Check Whether a Character is Vowel or consonant
The five alphabets A, E, I, O and U are called vowels. All other alphabets except these 5 vowel letters are called consonants.
This program assumes that the user will always enter an alphabet character.
Example #1: Program to Check Vowel or consonant
Output
Enter an alphabet: G
G is a consonant.
The character entered by the user is stored in variable c.
The isLowerCaseVowel evaluates to 1 (true) if c is a lowercase vowel and 0 (false) for any other character.
Similarly, isUpperCaseVowel evaluates to 1(true) if c is an uppercase vowel and 0 (false) for any other character.
If both isLowercaseVowel and isUppercaseVowel is equal to 0, the test expression evaluates to 0 (false) and the entered character is a consonant.
However, if either isLowercaseVowel or isUppercaseVowel is 1 (true), the test expression evaluates to 1 (true) and the entered character is a vowel.
The program above assumes that the user always enters an alphabet. If the user enters any other character other than an alphabet, the program will not work as intended.
Example #2: Program to Check Vowel or consonant
The program below asks the user to enter a character until the user enters an alphabet. Then, the program checks whether it is a vowel or a consonant.
|
Find the Largest Number Among Three Numbers Entered by User |
This program uses only if statement to find the largest number.
Example #1
This program uses if...else statement to find the largest number.
Example #2
This program uses nested if...else statement to find the largest number.
Example #3
Though, the largest number among three numbers is found using multiple ways, the output of all these program will be same.
Enter three numbers: -4.5 3.9 5.6 5.60 is the largest number. |
The standard form of a quadratic equation is:
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
The term
b2-4ac is known as the discriminant of a quadratic equation. The discriminant tells the nature of the roots.
Example: Program to Find Roots of a Quadratic Equation
Output
Enter coefficients a, b and c: 2.3 4 5.6 Roots are: -0.87+1.30i and -0.87-1.30i
In this program, library function
sqrt() is used to find the square root of a number. To learn more, visit: sqrt() function.
A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.
Example: Program to Check Leap Year
Output 1
Enter a year: 1900 1900 is not a leap year. |
Check Whether a Number is Positive or Negative or Zero
This program takes a number from the user and checks whether that number is either positive or negative or zero.
Example #1: Check if a Number is Positive or Negative Using if...else
Example #2: Check if a Number is Positive or Negative Using Nested if...else
Output 1
Enter a number: 12.3 You entered a positive number.
Output 2
Enter a number: 0 You entered 0. |
Checker Whether a Character is an Alphabet or notExample: Program to Check Alphabet
Output
Enter a character: * * is not an alphabet
In C programming, a character variable holds ASCII value (an integer number between 0 and 127) rather than that character itself.
The ASCII value of lowercase alphabets are from 97 to 122. And, the ASCII value of uppercase alphabets are from 65 to 90.
If the ASCII value of the character entered by the user lies in the range from 97 to 122 or from 65 to 90, that number is an alphabet. In the program,'a' is used instead of 97 and 'z' is used instead of 122. Similarly, 'A' is used instead of 65 and 'Z' is used instead of 90.
You can also check whether a character is an alphabet or not using isalpha() function
|
Find Sum of Natural Numbers
The positive numbers 1, 2, 3... are known as natural numbers. The programs below takes a positive integer (let say n) as an input from the user and calculates the sum up to n.
Example #1: Sum of Natural Numbers Using for Loop
The above program takes the input from the user and stores in variable n. Then, for loop is used to calculate the sum upto the given number.
Example #2: Sum of Natural Numbers Using while Loop
Output
Enter a positive integer: 100 Sum = 5050
In both programs, the loop is iterated n number of times. And, in each iteration, the value of i is added to sum and i is incremented by 1.
Though both programs are technically correct, it is better to use for loop in this case. It's because the number of iteration is known.
The above programs doesn't work properly if the user enters a negative integer. Here's a little modification of the above program to take input from the user until positive integer is entered.
Example #3: Program to Read Input Until User Enters a Positive Integer
|
Comments
Post a Comment