Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C Programming Code Examples

C > If Else and Switch Case Code Examples

program to print number of days in a month using switch case

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/* program to print number of days in a month using switch case Write a C program to input month number and print total number of days in month using switch...case. C program to find total number of days in a month using switch...case. Total days in a month is given by below table. Month Total days January, March, May, July, August, October, December 31 days February 28/29 days April, June, September, November 30 days Step by step descriptive logic to print number of days in a month using switch...case. Input month number from user. Store it in some variable say month. Switch the value of month i.e. switch(month) and match with cases. There can be 12 possible values (choices) of month i.e. from 1 to 12. Hence, write 12 cases inside switch and one default case as else block. Print 31 for case 1, 3, 5, 7, 8, 10, 12. Print 30 for case 4, 6, 9, 11. Print 28/29 for case 2. Print invalid input for default case. */ #include <stdio.h> int main() { int month; /* Input month number from user */ printf("Enter month number(1-12): "); scanf("%d", &month); switch(month) { case 1: printf("31 days"); break; case 2: printf("28/29 days"); break; case 3: printf("31 days"); break; case 4: printf("30 days"); break; case 5: printf("31 days"); break; case 6: printf("30 days"); break; case 7: printf("31 days"); break; case 8: printf("31 days"); break; case 9: printf("30 days"); break; case 10: printf("31 days"); break; case 11: printf("30 days"); break; case 12: printf("31 days"); break; default: printf("Invalid input! Please enter month number between 1-12"); } return 0; }

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block.

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

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.

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 break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops.





C Program code takes the decimal number (entered by user) as input and converts it into an octal number using the function. In this c program, you should be familiar with the...

If condition returns true then the statements inside the body of "if" are executed and the statements inside body of "else" are skipped. If condition returns false then the statements





C Program code implement prims algorithm using greedy method. For first smallest edge. We have fist smallest edge in graph. We have to find min dis of each vertex from either...