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

Convert a Given Number of Days in terms of Years, Weeks & Days

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
/* Convert a Given Number of Days in terms of Years, Weeks & Days This program takes the number of days as input and converts in terms of years, weeks & days. 1. Take the number of days as input. 2. For the number of years, divide the input by 365 and obtain its quotient. 3. For the number of weeks, divide the input by 365 and obtain its remainder. Further divide the remainder by 7(no of days in a week) and obtain its quotient. 4. For the number of days, divide the input by 365 and obtain its remainder. Further divide the remainder by 7(no of days in a week) and obtain its remainder. */ /* - C program to convert given number of days to a measure of time given - in years, weeks and days. For example 375 days is equal to 1 year - 1 week and 3 days (ignore leap year) */ #include <stdio.h> #define days-in-week 7 void main() { int ndays, year, week, days; printf("Enter the number of days\n"); scanf("%d", &ndays); year = ndays / 365; week =(ndays % 365) / days-in-week; days =(ndays % 365) % days-in-week; printf ("%d is equivalent to %d years, %d weeks and %d daysn", ndays, year, week, days); } /* Program Explanation - Take the number of days as input and store it in variable ndays. - For the number of years, divide the input by 365(no of days in a year) and obtain its quotient. Store this in the variable year. - For the number of weeks, divide the input by 365 and obtain its remainder. Further divide the remainder by 7(no of days in a week) and obtain its quotient. Store this in the variable week. - For the number of days, divide the input by 365 and obtain its remainder. Further divide the remainder by 7(no of days in a week) and obtain its remainder. Store this in the variable days. - Print the output and exit. */

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.

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

In the C Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions.

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.

This Code is very simple and a good example of using conditional statement (if-else) in an iteration (i.e. for loop). We shall initiate a for loop having some finite iterations and check


Filename, device, inode, protection, number of hard links, user ID of owner, group ID of owner, device type, total size, blocksize for filesystem I/O, number of blocks allocated,


Input temperature in Centigrade and convert to Fahrenheit. How to convert temperature from degree centigrade to degree Fahrenheit in C programming. The logic of temperature



Declare recursive function to find reverse of a number. Lets give a meaningful name to our function, say reverse(). C function computes reverse of number, hence it must accept an...

C reverse array without using another array relies on the above logic. What we need to do is maintain two array indexes. arrIndex that moves from size - 1 to 0. revIndex that moves

Your time has started. Start typing. Hit Esc when done. Erase previous character instead of cursoring. Eliminate a special character that is printed for Esc. A Backspace followed