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 > Miscellaneous Code Examples

C Program to Print Diamond Pattern

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
/* C Program to Print Diamond Pattern - Take the number of rows as input. - According to the number of rows, print the " " and "*" using for loops. - Exit. */ #include <stdio.h> int main() { int number, j, k, count = 1; printf("Enter number of rows\n"); scanf("%d", &number); count = number - 1; for (k = 1; k <= number; k++) { for (j = 1; j <= count; j++) printf(" "); count--; for (j = 1; j <= 2 * k - 1; j++) printf("*"); printf("\n"); } count = 1; for (k = 1; k <= number - 1; k++) { for (j = 1; j <= count; j++) printf(" "); count++; for (j = 1 ; j <= 2 *(number - k)- 1; j++) printf("*"); printf("\n"); } return 0; } /* - Take the number of rows as input and store in the variable number. - Firstly decrement the variable number by 1 and assign this value to the variable count. - Use this variable count as terminator in the for loop to print " ". - Decrement count by 1. - Use another for loop starting from 1 to (2*k-1) to print "*". - Do steps 3, 4, and 5 inside the for loop starting from 1 to variable number. - Steps 2-6 are used to print half of the diamond pattern. - For the next half, assign the variable count by 1. - Use this variable count as terminator in the for loop to print " ". - Increment count by 1. - Use another for loop starting from 1 to (2*(number-k)-1) to print "*". - Do steps 8-11 inside the for loop starting from 1 to value (number-1). */

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

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop.

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.

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,

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 for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance. The for-loop statement is a very specialized while loop, which increases the readability of a program. It is frequently used to traverse the data structures like the array and linked list.


How to print Odd Numbers from 1 to N using loop in C programming. Input upper limit to print odd number from user. Store it in some variable N. Run a loop from 1 to N, increment






Program to generate Fibonacci Sequence up to a certain number. The Fibonacci sequence is a series where the Next term is the Sum of Pervious 2 terms. The First Two terms of the



Check a files attributes. Check if the file can be written to. File cannot be written. File can be written to. Check if file can be read. File cannot be read. File can be read. Check if...