C Programming Code Examples
C > If Else and Switch Case Code Examples
C program to check vowel or consonant
/* C program to check vowel or consonant
Write a C program to check whether an alphabet is vowel or consonant using if else. How to check vowels and consonants using if else in C programming. C Program to input a character from user and check whether it is vowel or consonant.
English alphabets a, e, i, o and u both lowercase and uppercase are known as vowels. Alphabets other than vowels are known as consonants.
Input a character from user. Store it in some variable say ch.
Check conditions for vowel i.e. if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'), then it is vowel.
If character is alphabet but not vowel then it is consonant. Means check ch >= 'a' && ch <= 'z' then, it is consonant.
If it is neither vowel nor consonant, then it is not alphabet.
Character in C is represented inside single quote. Do not forget to add single quote whenever checking for character constant.
Let us implement the logic with both lower and upper case alphabets. */
#include <stdio.h>
int main()
{
char ch;
/* Input character from user */
printf("Enter any character: ");
scanf("%c", &ch);
/* Condition for vowel */
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' ||
ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
printf("'%c' is Vowel.", ch);
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
/* Condition for consonant */
printf("'%c' is Consonant.", ch);
}
else
{
/*
* If it is neither vowel nor consonant
* then it is not an alphabet.
*/
printf("'%c' is not an alphabet.", ch);
}
return 0;
}
Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.
Relational Operators are the operators used to create a relationship and compare the values of two operands. For example, there are two numbers, 5 and 15, and we can get the greatest number using the greater than operator (>) that returns 15 as the greatest or larger number to the 5. Following are the various types of relational operators in C. Equal To Operator (==) is used to compare both operands and returns 1 if both are equal or the same, and 0 represents the operands that are not equal. Not Equal To Operator (!=) is the opposite of the Equal To Operator and is represented as the (!=) operator. The Not Equal To Operator compares two operands and returns 1 if both operands are not the same; otherwise, it returns 0.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:
Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming. These operators are used to perform logical operations and used with conditional statements like C if-else statements.
Program performs a Pattern Searching using "KMP" 'Pattern Searching Algorithm'. Pattern searching is used for Finding the Existence of a Substring in a String. The pattern searching
In this c program code, a structure Distance is defined. The structure has two members inch (a float) and feet (an integer). Two variables (distance1 and distance2) are created which...
Code compare function for qsort. Add ONE element to the array. Allocate memory for ONE 'struct node'. Copy the data into the new element ( structure ). Qsort array of...
C Program code to Check Whether a number is even or odd using if else. A number exactly divisible by 2 leaving no remainder, is known as even number. Any number modulo divided
Go back to normal graphics mode or deleting viewport. Checking the egg position. Display mouse pointer. Define the upper left boundry of mouse. Define the bottom right boundry
Adding two numbers using inline assembly language. Assembly language can be written in c. C supports assembly as well as higher language features so called "middle level...
Prints 3 characters forward and backward to demonstrate character printing in C program code example. Define the first character, define the second character, define the third
C Program to check Whether a given string is palindrome or not 'using recursion'. Program, with recursion, determines the entered string is a 'palindrome' or not. Palindrome is a word,